Skip to content

Commit 3ad3710

Browse files
committed
fix(user-input): restore draft text synchronously to preserve contexts on nav
The SSR-safe approach (empty useState + effect restore) created a timing window where the sync effect in useContextManagement fired with message='' before the value was set, clearing any restored contexts. Folder and workflow contexts (not re-added by applyAutoMentions) were lost on every nav-back. Revert to the staging approach: initialize value synchronously from the draft store so message is already populated when effects run, matching the behavior on staging.
1 parent b06f757 commit 3ad3710

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

  • apps/sim/app/workspace/[workspaceId]/home/components/user-input

apps/sim/app/workspace/[workspaceId]/home/components/user-input/user-input.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,12 @@ export const UserInput = forwardRef<UserInputHandle, UserInputProps>(function Us
161161
const { navigateToSettings } = useSettingsNavigation()
162162
const { data: workflowsById = {} } = useWorkflowMap(workspaceId)
163163
const { data: skills = [] } = useSkills(workspaceId)
164-
// SSR-safe initial value. The server has no localStorage, so reading a
165-
// persisted draft here would diverge from the client's synchronous
166-
// zustand-persist rehydration and trigger a hydration mismatch (flash).
167-
// Draft text is restored after mount in the effect below instead.
168-
const [value, setValue] = useState(defaultValue)
164+
const [value, setValue] = useState(() => {
165+
if (defaultValue) return defaultValue
166+
if (!draftScopeKey) return ''
167+
const text = useMothershipDraftsStore.getState().drafts[draftScopeKey]?.text
168+
return typeof text === 'string' ? text : ''
169+
})
169170
const valueRef = useRef(value)
170171
valueRef.current = value
171172
const overlayRef = useRef<HTMLDivElement>(null)
@@ -234,8 +235,6 @@ export const UserInput = forwardRef<UserInputHandle, UserInputProps>(function Us
234235
if (restoredContexts) contextManagement.setSelectedContexts(restoredContexts)
235236
if (restoredFiles) files.restoreAttachedFiles(restoredFiles)
236237
if (caretText !== null) {
237-
setValue(caretText)
238-
valueRef.current = caretText
239238
const textarea = textareaRef.current
240239
if (textarea) {
241240
textarea.focus()

0 commit comments

Comments
 (0)