-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug creating a file with the same name of an existing folder. When
is created the folder with that name dissapears from the UI. Looks a client bug
- Loading branch information
1 parent
4e50293
commit 5fe7f31
Showing
7 changed files
with
276 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/web-ui/src/sections/Document/Sidebar/Files/DocumentHeader/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { useCallback, useMemo } from 'react' | ||
|
||
import { Icons } from '$ui/ds/atoms' | ||
import { MenuOption } from '$ui/ds/atoms/DropdownMenu' | ||
import { cn } from '$ui/lib/utils' | ||
import { useFileTreeContext } from '$ui/sections/Document/Sidebar/Files/FilesProvider' | ||
import NodeHeaderWrapper, { | ||
ICON_CLASS, | ||
IndentType, | ||
} from '$ui/sections/Document/Sidebar/Files/NodeHeaderWrapper' | ||
import { useTempNodes } from '$ui/sections/Document/Sidebar/Files/useTempNodes' | ||
|
||
import { Node } from '../useTree' | ||
|
||
export default function DocumentHeader({ | ||
open, | ||
selected, | ||
node, | ||
indentation, | ||
}: { | ||
open: boolean | ||
selected: boolean | ||
node: Node | ||
indentation: IndentType[] | ||
}) { | ||
const { onNavigateToDocument, onDeleteFile, onCreateFile } = | ||
useFileTreeContext() | ||
const { deleteTmpFolder, reset } = useTempNodes((state) => ({ | ||
reset: state.reset, | ||
deleteTmpFolder: state.deleteTmpFolder, | ||
})) | ||
const onSaveValue = useCallback( | ||
async ({ path }: { path: string }) => { | ||
const parentPath = node.path.split('/').slice(0, -1).join('/') | ||
await onCreateFile(`${parentPath}/${path}`) | ||
reset() | ||
}, | ||
[reset, onCreateFile, node.path], | ||
) | ||
const handleClick = useCallback(() => { | ||
if (selected) return | ||
if (!node.isPersisted) return | ||
|
||
onNavigateToDocument(node.doc!.documentUuid) | ||
}, [node.doc!.documentUuid, selected, node.isPersisted]) | ||
const actions = useMemo<MenuOption[]>( | ||
() => [ | ||
{ | ||
label: 'Delete file', | ||
type: 'destructive', | ||
onClick: () => { | ||
onDeleteFile({ node, documentUuid: node.doc!.documentUuid }) | ||
}, | ||
}, | ||
], | ||
[node.doc!.documentUuid, onDeleteFile], | ||
) | ||
return ( | ||
<NodeHeaderWrapper | ||
open={open} | ||
node={node} | ||
actions={actions} | ||
selected={selected} | ||
indentation={indentation} | ||
onClick={handleClick} | ||
onSaveValue={onSaveValue} | ||
onLeaveWithoutSave={deleteTmpFolder} | ||
icons={ | ||
<Icons.file | ||
className={cn(ICON_CLASS, { | ||
'text-accent-foreground': selected, | ||
})} | ||
/> | ||
} | ||
/> | ||
) | ||
} |
110 changes: 110 additions & 0 deletions
110
packages/web-ui/src/sections/Document/Sidebar/Files/FolderHeader/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { useCallback, useMemo } from 'react' | ||
|
||
import { Icons } from '$ui/ds/atoms' | ||
import { MenuOption } from '$ui/ds/atoms/DropdownMenu' | ||
import { cn } from '$ui/lib/utils' | ||
import { useFileTreeContext } from '$ui/sections/Document/Sidebar/Files/FilesProvider' | ||
import NodeHeaderWrapper, { | ||
ICON_CLASS, | ||
IndentType, | ||
} from '$ui/sections/Document/Sidebar/Files/NodeHeaderWrapper' | ||
import { useOpenPaths } from '$ui/sections/Document/Sidebar/Files/useOpenPaths' | ||
import { useTempNodes } from '$ui/sections/Document/Sidebar/Files/useTempNodes' | ||
|
||
import { Node } from '../useTree' | ||
|
||
export default function FolderHeader({ | ||
node, | ||
open, | ||
indentation, | ||
onToggleOpen, | ||
}: { | ||
node: Node | ||
open: boolean | ||
indentation: IndentType[] | ||
onToggleOpen: () => void | ||
}) { | ||
const { onDeleteFolder } = useFileTreeContext() | ||
const { openPaths, togglePath } = useOpenPaths((state) => ({ | ||
togglePath: state.togglePath, | ||
openPaths: state.openPaths, | ||
})) | ||
const { addFolder, updateFolder, updateFolderAndAddOther, deleteTmpFolder } = | ||
useTempNodes((state) => ({ | ||
addFolder: state.addFolder, | ||
updateFolder: state.updateFolder, | ||
updateFolderAndAddOther: state.updateFolderAndAddOther, | ||
deleteTmpFolder: state.deleteTmpFolder, | ||
})) | ||
const onUpdateFolderAndAddOther = useCallback( | ||
({ path, id }: { path: string; id: string }) => { | ||
updateFolderAndAddOther({ | ||
id, | ||
path, | ||
onNodeUpdated: (updatedPath) => { | ||
togglePath(updatedPath) | ||
}, | ||
}) | ||
}, | ||
[updateFolderAndAddOther, togglePath], | ||
) | ||
|
||
const onAddNode = useCallback( | ||
({ isFile }: { isFile: boolean }) => | ||
() => { | ||
if (!open) { | ||
togglePath(node.path) | ||
} | ||
addFolder({ parentPath: node.path, parentId: node.id, isFile }) | ||
}, | ||
[node.path, togglePath, open], | ||
) | ||
const FolderIcon = open ? Icons.folderOpen : Icons.folderClose | ||
const ChevronIcon = open ? Icons.chevronDown : Icons.chevronRight | ||
const actions = useMemo<MenuOption[]>( | ||
() => [ | ||
{ label: 'New folder', onClick: onAddNode({ isFile: false }) }, | ||
{ label: 'New Prompt', onClick: onAddNode({ isFile: true }) }, | ||
{ | ||
label: 'Delete folder', | ||
type: 'destructive', | ||
onClick: () => { | ||
if (node.isPersisted) { | ||
onDeleteFolder({ node, path: node.path }) | ||
} else { | ||
deleteTmpFolder({ id: node.id }) | ||
} | ||
}, | ||
}, | ||
], | ||
[ | ||
addFolder, | ||
onDeleteFolder, | ||
deleteTmpFolder, | ||
node.path, | ||
node.isPersisted, | ||
openPaths, | ||
togglePath, | ||
], | ||
) | ||
return ( | ||
<NodeHeaderWrapper | ||
onClick={onToggleOpen} | ||
onSaveValue={updateFolder} | ||
onSaveValueAndTab={onUpdateFolderAndAddOther} | ||
onLeaveWithoutSave={deleteTmpFolder} | ||
node={node} | ||
open={open} | ||
actions={actions} | ||
indentation={indentation} | ||
icons={ | ||
<> | ||
<div className='min-w-6 h-6 flex items-center justify-center'> | ||
<ChevronIcon className={cn(ICON_CLASS, 'h-4 w-4')} /> | ||
</div> | ||
<FolderIcon className={ICON_CLASS} /> | ||
</> | ||
} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.