ERC x LFS: workspace supports d/d

This commit is contained in:
Enrico Ros
2024-08-14 00:44:46 -07:00
parent c745aae281
commit 0bb4fd4517
4 changed files with 117 additions and 72 deletions
@@ -28,23 +28,15 @@ export function useLiveFilePatch(title: string, code: string, isPartial: boolean
// handlers
const handleLiveFileSelected = React.useCallback((id: LiveFileId | null) => {
const handleSelectLiveFile = React.useCallback((id: LiveFileId | null) => {
setLiveFileId(id);
}, []);
const handleSelectNewFile = React.useCallback(async (workspaceId: DWorkspaceId | null) => {
// pick a file
const fileWithHandle = await fileOpen({ description: 'Insert into file...' }).catch(() => null /* The User closed the files picker */);
if (!fileWithHandle)
return;
const fileSystemFileHandle = fileWithHandle.handle;
if (!fileSystemFileHandle) {
// setStatus({ message: `Browser does not support LiveFile operations. ${isLiveFileSupported() ? 'No filesystem handles.' : ''}`, mtype: 'error' });
return;
}
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(fileSystemFileHandle);
const newLiveFileId = await liveFileCreateOrThrow(fsfHandle);
setLiveFileId(newLiveFileId);
// Pair the file with the workspace
@@ -59,9 +51,24 @@ export function useLiveFilePatch(title: string, code: string, isPartial: boolean
} 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' });
}
}, []);
const handleSelectFilePicker = React.useCallback(async (workspaceId: DWorkspaceId | null) => {
// pick a file
const fileWithHandle = await fileOpen({ description: 'Insert into file...' }).catch(() => null /* The User closed the files picker */);
if (!fileWithHandle)
return;
const fileSystemFileHandle = fileWithHandle.handle;
if (!fileSystemFileHandle) {
// setStatus({ message: `Browser does not support LiveFile operations. ${isLiveFileSupported() ? 'No filesystem handles.' : ''}`, mtype: 'error' });
return;
}
// proceed
await handleSelectFileSystemFileHandle(workspaceId, fileSystemFileHandle);
}, [handleSelectFileSystemFileHandle]);
// components
@@ -88,14 +95,16 @@ export function useLiveFilePatch(title: string, code: string, isPartial: boolean
{/* Pick LiveFile */}
<WorkspaceLiveFilePicker
autoSelectName={title}
buttonLabel='Insert...'
labelButton='Insert...'
labelTooltip='Insert this code into a file'
liveFileId={liveFileId}
onSelectLiveFile={handleLiveFileSelected}
onSelectNewFile={handleSelectNewFile}
onSelectFileOpen={handleSelectFilePicker}
onSelectFileSystemFileHandle={handleSelectFileSystemFileHandle}
onSelectLiveFile={handleSelectLiveFile}
/>
</Box>
), [handleLiveFileSelected, handleSelectNewFile, isEnabled, liveFileId, title]);
), [handleSelectLiveFile, handleSelectFilePicker, handleSelectFileSystemFileHandle, isEnabled, liveFileId, title]);
const actionBar = React.useMemo(() => (!isEnabled || !liveFileId || true) ? null : (