Skip to content

Commit

Permalink
Fix bug creating a file with the same name of an existing folder. When
Browse files Browse the repository at this point in the history
is created the folder with that name dissapears from the UI. Looks a
client bug
  • Loading branch information
andresgutgon committed Jul 30, 2024
1 parent 4e50293 commit 5fe7f31
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 193 deletions.
24 changes: 18 additions & 6 deletions packages/web-ui/src/ds/atoms/ResizablePane/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,33 @@ const ResizableHandle = ({
}: ComponentProps<typeof ResizablePrimitive.PanelResizeHandle>) => (
<ResizablePrimitive.PanelResizeHandle
className={cn(
'relative flex w-px items-center justify-center',
'bg-border after:absolute after:inset-y-0 after:left-1/2 after:w-1 after:-translate-x-1/2',
'w-px relative flex items-center justify-center',
'data-[panel-group-direction=vertical]:h-px data-[panel-group-direction=vertical]:w-full',
'data-[panel-group-direction=vertical]:after:left-0 data-[panel-group-direction=vertical]:after:h-1',
'data-[panel-group-direction=vertical]:after:w-full data-[panel-group-direction=vertical]:after:-translate-y-1/2',
'data-[panel-group-direction=vertical]:after:translate-x-0 [&[data-panel-group-direction=vertical]>div]:rotate-90',
'ring-offset-0',
// NOTE: Force the right cursor for the handle
'data-[panel-group-direction=horizontal]:!cursor-ew-resize data-[panel-group-direction=vertical]:!cursor-ns-resize',
'hover:ring-2 hover:ring-accent-foreground hover:bg-accent-foreground',
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent-foreground focus-visible:bg-accent-foreground',
className,
)}
{...props}
/>
>
<div
className={cn(
'group/handler',
'absolute inset-0 w-2 -left-1',
'duration-200 transition-all flex items-center justify-center',
)}
>
<div
className={cn(
'duration-75 transition-all',
'h-full w-px bg-border',
'group-hover/handler:w-0.5 group-hover/handler:bg-accent-foreground',
)}
/>
</div>
</ResizablePrimitive.PanelResizeHandle>
)

export { ResizablePanelGroup, ResizablePanel, ResizableHandle }
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,
})}
/>
}
/>
)
}
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} />
</>
}
/>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const NodeHeaderWrapper = forwardRef<HTMLDivElement, Props>(function Foo(
>
<div
onClick={onClick}
className='min-w-0 flex-grow flex flex-row items-center justify-between py-0.5'
className='min-w-0 flex-grow flex flex-row items-center py-0.5'
>
<IndentationBar
indentation={indentation}
Expand Down
Loading

0 comments on commit 5fe7f31

Please sign in to comment.