From 74c201d3a865cafc57eba9ede4442f7f5a81a725 Mon Sep 17 00:00:00 2001 From: Kyle Corbitt Date: Fri, 14 Jul 2023 17:35:57 -0700 Subject: [PATCH] Make save button disappear on save Fixes a bug where often the "Save" button wouldn't disappear as expected the first time you clicked it. --- src/components/OutputsTable/VariantEditor.tsx | 13 ++++++++----- src/components/OutputsTable/index.tsx | 4 ++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/components/OutputsTable/VariantEditor.tsx b/src/components/OutputsTable/VariantEditor.tsx index 4452932..eb5f16f 100644 --- a/src/components/OutputsTable/VariantEditor.tsx +++ b/src/components/OutputsTable/VariantEditor.tsx @@ -5,7 +5,8 @@ import { type PromptVariant } from "./types"; import { api } from "~/utils/api"; import { useAppStore } from "~/state/store"; import { editorBackground } from "~/state/sharedVariantEditor.slice"; -export default function VariantConfigEditor(props: { variant: PromptVariant }) { + +export default function VariantEditor(props: { variant: PromptVariant }) { const monaco = useAppStore.use.sharedVariantEditor.monaco(); const editorRef = useRef["editor"]["create"]> | null>(null); const [editorId] = useState(() => `editor_${Math.random().toString(36).substring(7)}`); @@ -17,10 +18,12 @@ export default function VariantConfigEditor(props: { variant: PromptVariant }) { const checkForChanges = useCallback(() => { if (!editorRef.current) return; - const currentConfig = editorRef.current.getValue(); - setIsChanged(currentConfig !== lastSavedFn); + const currentFn = editorRef.current.getValue(); + setIsChanged(currentFn.length > 0 && currentFn !== lastSavedFn); }, [lastSavedFn]); + useEffect(checkForChanges, [checkForChanges, lastSavedFn]); + const replaceVariant = api.promptVariants.replaceVariant.useMutation(); const utils = api.useContext(); const toast = useToast(); @@ -75,9 +78,9 @@ export default function VariantConfigEditor(props: { variant: PromptVariant }) { }); } - await utils.promptVariants.list.invalidate(); + setIsChanged(false); - checkForChanges(); + await utils.promptVariants.list.invalidate(); }, [checkForChanges]); useEffect(() => { diff --git a/src/components/OutputsTable/index.tsx b/src/components/OutputsTable/index.tsx index 2206ec7..db38e88 100644 --- a/src/components/OutputsTable/index.tsx +++ b/src/components/OutputsTable/index.tsx @@ -3,7 +3,7 @@ import { api } from "~/utils/api"; import NewScenarioButton from "./NewScenarioButton"; import NewVariantButton from "./NewVariantButton"; import ScenarioRow from "./ScenarioRow"; -import VariantConfigEditor from "./VariantEditor"; +import VariantEditor from "./VariantEditor"; import VariantHeader from "./VariantHeader"; import { cellPadding } from "../constants"; import { BsPencil } from "react-icons/bs"; @@ -94,7 +94,7 @@ export default function OutputsTable({ experimentId }: { experimentId: string | {variants.data.map((variant) => ( - + ))} {variants.data.map((variant) => (