fix(ui): re-sync settings/thread forms on loaded record

This commit is contained in:
Gabriel Brown
2026-07-11 17:14:06 -04:00
parent bca86196fb
commit a3ec04ad99
3 changed files with 216 additions and 2 deletions
@@ -1,6 +1,6 @@
'use client';
import { useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import { useRouter } from 'next/navigation';
import { useMutation, useQuery } from 'convex/react';
import { MessageSquarePlus } from 'lucide-react';
@@ -34,6 +34,7 @@ const runtimeLabels: Record<RuntimeName, string> = {
};
type AgentSettings = {
_id?: Id<'spoonAgentSettings'>;
defaultBaseBranch?: string;
runtime?: RuntimeName | 'openai_direct';
agentModel: string;
@@ -82,6 +83,33 @@ export const ThreadWorkspaceForm = ({
);
const [runtime, setRuntime] = useState<RuntimeName | undefined>();
const [submitting, setSubmitting] = useState(false);
// The parent gates rendering on a different query than `agentSettings`, so
// this form can mount before the agent settings record resolves. Re-seed the
// settings-derived fields once the real record arrives (and again if the
// parent swaps to a different spoon) so the form reflects saved config rather
// than the initial defaults.
const hydratedSpoonId = useRef<Id<'spoons'> | null>(null);
useEffect(() => {
if (!agentSettings || hydratedSpoonId.current === spoon._id) return;
const timeout = window.setTimeout(() => {
setBaseBranch(
agentSettings.defaultBaseBranch ??
spoon.forkDefaultBranch ??
spoon.upstreamDefaultBranch,
);
setMaterializeEnvFile(agentSettings.materializeEnvFileByDefault ?? false);
setEnvFilePath(
agentSettings.envFilePath === 'custom'
? (agentSettings.customEnvFilePath ?? '.env.local')
: (agentSettings.envFilePath ?? '.env.local'),
);
setAiProviderProfileId(agentSettings.aiProviderProfileId ?? '__settings');
hydratedSpoonId.current = spoon._id;
}, 0);
return () => window.clearTimeout(timeout);
}, [agentSettings, spoon]);
const effectiveProviderProfileId =
aiProviderProfileId === '__settings'
? (agentSettings?.aiProviderProfileId ?? defaultProfile?._id)