diff --git a/studio/components/SoMeInputs.tsx b/studio/components/SoMeInputs.tsx index b5970906c..8c627a59f 100644 --- a/studio/components/SoMeInputs.tsx +++ b/studio/components/SoMeInputs.tsx @@ -1,5 +1,5 @@ -import React, { useEffect, useState } from "react"; -import { ObjectInputProps, PatchEvent, set } from "sanity"; +import React from "react"; +import { ObjectInputProps, set } from "sanity"; import { Box, TextInput, Select, Stack, Label } from "@sanity/ui"; export const SoMePlatforms: { [key: string]: string } = { @@ -14,53 +14,51 @@ export const SoMePlatforms: { [key: string]: string } = { tikTok: "Tiktok", }; +const detectPlatformFromUrl = (url: string): string | null => { + for (const key in SoMePlatforms) { + if (url.includes(key)) { + return SoMePlatforms[key]; + } + } + return null; +}; + const SoMeInputs: React.FC>> = ({ value = {}, onChange, }) => { - const [url, setUrl] = useState(value.url || ""); - const [platform, setPlatform] = useState(value.platform || ""); - - useEffect(() => { - let detectedPlatform = ""; - for (const key in SoMePlatforms) { - if (url.includes(key)) { - detectedPlatform = SoMePlatforms[key]; - break; - } - } - setPlatform(detectedPlatform); - if (onChange) { - onChange( - PatchEvent.from([set({ ...value, url, platform: detectedPlatform })]), - ); - } - }, [url, onChange]); - const handleUrlChange = (event: React.ChangeEvent) => { + if (!onChange) return; const newUrl = event.target.value; - setUrl(newUrl); + onChange(set(newUrl, ["url"])); + const detectedPlatform = detectPlatformFromUrl(newUrl); + if (detectedPlatform !== null) { + onChange(set(detectedPlatform, ["platform"])); + } }; const handlePlatformChange = ( event: React.ChangeEvent, ) => { + if (!onChange) return; const newPlatform = event.target.value; - setPlatform(newPlatform); - if (onChange) { - onChange( - PatchEvent.from([set({ ...value, url, platform: newPlatform })]), - ); - } + onChange(set(newPlatform, ["platform"])); }; + const urlInputName = "social-url-input"; + const platformInputName = "social-platform-input"; + return ( - + @@ -68,8 +66,15 @@ const SoMeInputs: React.FC>> = ({ - - {Object.values(SoMePlatforms).map((platform) => (