diff --git a/src/common/stores/workspace/WorkspaceLiveFilePicker.tsx b/src/common/stores/workspace/WorkspaceLiveFilePicker.tsx index 4fbd3460f..10cbaa2fe 100644 --- a/src/common/stores/workspace/WorkspaceLiveFilePicker.tsx +++ b/src/common/stores/workspace/WorkspaceLiveFilePicker.tsx @@ -29,7 +29,7 @@ export function WorkspaceLiveFilePicker(props: { labelButton: string; labelTooltip?: string; liveFileId: LiveFileId | null; - onSelectLiveFile: (id: LiveFileId | null) => void; + onSelectLiveFile: (id: LiveFileId | null) => Promise; onSelectFileOpen: (workspaceId: DWorkspaceId | null) => Promise; onSelectFileSystemFileHandle?: (workspaceId: DWorkspaceId | null, fsHandle: FileSystemFileHandle) => Promise; }) { diff --git a/src/modules/blocks/enhanced-code/livefile-patch/useLiveFilePatch.tsx b/src/modules/blocks/enhanced-code/livefile-patch/useLiveFilePatch.tsx index 4c828dbea..a81904d45 100644 --- a/src/modules/blocks/enhanced-code/livefile-patch/useLiveFilePatch.tsx +++ b/src/modules/blocks/enhanced-code/livefile-patch/useLiveFilePatch.tsx @@ -27,33 +27,41 @@ export function useLiveFilePatch(title: string, code: string, isPartial: boolean const isEnabled = useUXLabsStore((state) => state.labsEnhanceCodeLiveFile && isLiveFileSupported()); - // handlers - - const handleSelectLiveFile = React.useCallback((id: LiveFileId | null) => { - setLiveFileId(id); + // [effect] apply the text to the LiveFile + const processLiveFile = React.useCallback(async (liveFileId: LiveFileId) => { + console.log('Processing LiveFile:', liveFileId); + // const success = await liveFileWriteAndReload(id, code); + // if (!success) + // setStatus({ message: 'Error writing to the file.', mtype: 'error' }); }, []); + + // handlers + + const handleSelectLiveFile = React.useCallback(async (id: LiveFileId | null) => { + setLiveFileId(id); + if (id) + await processLiveFile(id); + }, [processLiveFile]); + const handleSelectFileSystemFileHandle = React.useCallback(async (workspaceId: DWorkspaceId | null, fsfHandle: FileSystemFileHandle) => { - // Create a new LiveFile and attach it to the workspace try { - const newLiveFileId = await liveFileCreateOrThrow(fsfHandle); - setLiveFileId(newLiveFileId); + const newId = await liveFileCreateOrThrow(fsfHandle); + setLiveFileId(newId); - // Pair the file with the workspace - if (!workspaceId) - console.warn('[DEV] No workspaceId to pair the file with.'); + // Attach it to the workspace + if (workspaceId) + workspaceActions().liveFileAssign(workspaceId, newId); else - workspaceActions().liveFileAssign(workspaceId, newLiveFileId); - - // TODO: Implement file content writing logic here - // For example: - // await liveFileContentWriteAndReload(code); + console.warn('[DEV] No workspaceId to pair the file with.'); + // proceed + await processLiveFile(newId); } catch (error) { console.error('Error creating new file:', error); // setStatus({ message: `Error pairing the file: ${error?.message || typeof error === 'string' ? error : 'Unknown error'}`, mtype: 'error' }); } - }, []); + }, [processLiveFile]); const handleSelectFilePicker = React.useCallback(async (workspaceId: DWorkspaceId | null) => { // pick a file @@ -73,26 +81,7 @@ export function useLiveFilePatch(title: string, code: string, isPartial: boolean // components const button = React.useMemo(() => !isEnabled ? null : ( - - - {/*/!* Patch LiveFile *!/*/} - {/*{!!liveFileId && (*/} - {/* setLiveFileId(null)}*/} - {/* >*/} - {/* TODO - TEST*/} - {/* */} - {/*)}*/} - - {/* Pick LiveFile */} + - ), [handleSelectLiveFile, handleSelectFilePicker, handleSelectFileSystemFileHandle, isEnabled, liveFileId, title]); - const actionBar = React.useMemo(() => (!isEnabled || !liveFileId || true) ? null : ( {JSON.stringify(liveFileId)}