Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
useRef,
useState,
} from 'react'
import { createLogger } from '@sim/logger'
import { Paperclip } from 'lucide-react'
import { useParams } from 'next/navigation'
import { Button, Tooltip } from '@/components/emcn'
Expand Down Expand Up @@ -58,6 +59,8 @@ import type { ChatContext } from '@/stores/panel'

export type { FileAttachmentForApi } from '@/app/workspace/[workspaceId]/home/types'

const logger = createLogger('UserInput')

function getCaretAnchor(
textarea: HTMLTextAreaElement,
caretPos: number
Expand Down Expand Up @@ -148,7 +151,8 @@ export const UserInput = forwardRef<UserInputHandle, UserInputProps>(function Us
const [value, setValue] = useState(() => {
if (defaultValue) return defaultValue
if (!draftScopeKey) return ''
return useMothershipDraftsStore.getState().drafts[draftScopeKey]?.text ?? ''
const text = useMothershipDraftsStore.getState().drafts[draftScopeKey]?.text
return typeof text === 'string' ? text : ''
})
const overlayRef = useRef<HTMLDivElement>(null)
const plusMenuRef = useRef<PlusMenuHandle>(null)
Expand Down Expand Up @@ -189,14 +193,17 @@ export const UserInput = forwardRef<UserInputHandle, UserInputProps>(function Us
useEffect(() => {
if (hasRestoredDraftRef.current || !draftScopeKey) return
hasRestoredDraftRef.current = true
const draft = useMothershipDraftsStore.getState().drafts[draftScopeKey]
if (!draft) return
if (draft.contexts?.length) {
contextManagement.setSelectedContexts(draft.contexts)
}
if (draft.fileAttachments?.length) {
files.restoreAttachedFiles(
draft.fileAttachments.map((a) => ({
let restoredContexts: ChatContext[] | null = null
let restoredFiles: AttachedFile[] | null = null
let caretText: string | null = null
try {
const draft = useMothershipDraftsStore.getState().drafts[draftScopeKey]
if (!draft) return
if (draft.contexts?.length) {
restoredContexts = draft.contexts
}
if (draft.fileAttachments?.length) {
restoredFiles = draft.fileAttachments.map((a) => ({
id: a.id,
name: a.filename,
size: a.size,
Expand All @@ -205,13 +212,22 @@ export const UserInput = forwardRef<UserInputHandle, UserInputProps>(function Us
key: a.key,
uploading: false,
}))
)
}
if (typeof draft.text === 'string' && draft.text.length > 0) {
caretText = draft.text
}
} catch (err) {
logger.error('Failed to read draft, clearing', { err })
useMothershipDraftsStore.getState().clearDraft(draftScopeKey)
return
}
if (draft.text) {
if (restoredContexts) contextManagement.setSelectedContexts(restoredContexts)
if (restoredFiles) files.restoreAttachedFiles(restoredFiles)
if (caretText !== null) {
const textarea = textareaRef.current
if (textarea) {
textarea.focus()
textarea.setSelectionRange(draft.text.length, draft.text.length)
textarea.setSelectionRange(caretText.length, caretText.length)
}
}
Comment thread
waleedlatif1 marked this conversation as resolved.
}, []) // eslint-disable-line react-hooks/exhaustive-deps -- intentional mount-only restore
Expand Down
Loading