-
-
-
-
-
- {isEditing ? (
-
-
-
- ) : (
-
-
- {node.name}
-
+ error={error}
+ onChangeInput={onInputChange}
+ onKeyDownInput={onInputKeyDown}
+ icons={
+ <>
+
+
- )}
-
-
- {!isEditing ? (
-
-
-
- ) : null}
-
+
+ >
+ }
+ />
)
}
@@ -229,13 +126,34 @@ function FileHeader({
node: Node
indentation: IndentType[]
}) {
- const { onDeleteFile, onNavigateToDocument } = useFileTreeContext()
+ const { onNavigateToDocument, onDeleteFile, onCreateFile } =
+ useFileTreeContext()
+ const { deleteTmpFolder, reset } = useTempNodes((state) => ({
+ reset: state.reset,
+ deleteTmpFolder: state.deleteTmpFolder,
+ }))
+ const nodeRef = useRef
(null)
+ const inputRef = useRef(null)
+ const { isEditing, error, onInputChange, onInputKeyDown } = useNodeValidator({
+ node,
+ nodeRef,
+ inputRef,
+ saveValue: async ({ path }: { path: string }) => {
+ const parentPath = node.path.split('/').slice(0, -1).join('/')
+ await onCreateFile(`${parentPath}/${path}`)
+ reset()
+ },
+ leaveWithoutSave: () => {
+ deleteTmpFolder({ id: node.id })
+ },
+ })
const handleClick = useCallback(() => {
if (selected) return
+ if (!node.isPersisted) return
onNavigateToDocument(node.doc!.documentUuid)
- }, [node.doc!.documentUuid, selected])
- const options = useMemo(
+ }, [node.doc!.documentUuid, selected, node.isPersisted])
+ const actions = useMemo(
() => [
{
label: 'Delete file',
@@ -249,33 +167,26 @@ function FileHeader({
)
return (
-
+ onKeyDownInput={onInputKeyDown}
+ onChangeInput={onInputChange}
+ isEditing={isEditing}
+ error={error}
+ icons={
-
-
- {node.name}
-
-
-
-
-
-
-
+ }
+ />
)
}
@@ -384,7 +295,11 @@ export function FilesTree({
currentPath,
documents,
navigateToDocument,
+ createFile,
+ destroyFile,
}: {
+ createFile: (args: { path: string }) => Promise
+ destroyFile: (documentUuid: string) => Promise
documents: SidebarDocument[]
currentPath: string | undefined
navigateToDocument: (documentUuid: string) => void
@@ -402,13 +317,11 @@ export function FilesTree({
{
- console.log('onCreateFile', path)
- }}
- onDeleteFile={(documentUuid) => {
- console.log('onDeleteFile', documentUuid)
+ onCreateFile={async (path) => {
+ createFile({ path })
}}
- onDeleteFolder={(path) => {
+ onDeleteFile={destroyFile}
+ onDeleteFolder={async (path) => {
console.log('onDeleteFolder', path)
}}
>
diff --git a/packages/web-ui/src/sections/Document/Sidebar/Files/useNodeValidator/index.ts b/packages/web-ui/src/sections/Document/Sidebar/Files/useNodeValidator/index.ts
index ff4a5684e..44f9e9bbb 100644
--- a/packages/web-ui/src/sections/Document/Sidebar/Files/useNodeValidator/index.ts
+++ b/packages/web-ui/src/sections/Document/Sidebar/Files/useNodeValidator/index.ts
@@ -56,7 +56,6 @@ export function useNodeValidator({
saveValue: (args: { path: string }) => Promise
leaveWithoutSave?: () => void
}) {
- const [value, setValue] = useState(node.name?.trim() ?? '')
const [isEditing, setIsEditing] = useState(node.name === ' ')
const [validationError, setError] = useState()
const onInputChange: ChangeEventHandler = useCallback(
@@ -77,7 +76,6 @@ export function useNodeValidator({
const onClickOutside = useCallback(async () => {
const val = inputRef.current?.value ?? ''
const value = val.trim()
- setValue(value)
if (!value) {
leaveWithoutSave?.()
@@ -118,6 +116,5 @@ export function useNodeValidator({
onInputChange,
onInputKeyDown,
error: validationError,
- keepFocused: !value,
}
}
diff --git a/packages/web-ui/src/sections/Document/Sidebar/Files/useTempNodes/index.test.ts b/packages/web-ui/src/sections/Document/Sidebar/Files/useTempNodes/index.test.ts
index 5a3008d37..9f728e17f 100644
--- a/packages/web-ui/src/sections/Document/Sidebar/Files/useTempNodes/index.test.ts
+++ b/packages/web-ui/src/sections/Document/Sidebar/Files/useTempNodes/index.test.ts
@@ -15,6 +15,7 @@ describe('useTempNodes', () => {
result.current.addFolder({
parentPath: 'some-folder',
parentId: 'fake-id',
+ isFile: false,
}),
)
@@ -24,6 +25,7 @@ describe('useTempNodes', () => {
id: expect.any(String),
path: 'some-folder/ ',
name: ' ',
+ isFile: false,
isPersisted: false,
}),
],
@@ -35,6 +37,7 @@ describe('useTempNodes', () => {
act(() =>
result.current.addFolder({
parentPath: 'some-folder',
+ isFile: false,
parentId: 'fake-id',
}),
)
@@ -47,6 +50,7 @@ describe('useTempNodes', () => {
id: expect.any(String),
path: 'some-folder/new-name',
name: 'new-name',
+ isFile: false,
isPersisted: false,
}),
],
@@ -59,6 +63,7 @@ describe('useTempNodes', () => {
result.current.addFolder({
parentPath: 'some-folder',
parentId: 'fake-id',
+ isFile: false,
}),
)
const id = result?.current?.tmpFolders?.['some-folder']?.[0]?.id
@@ -73,6 +78,7 @@ describe('useTempNodes', () => {
result.current.addFolder({
parentPath: 'some-folder',
parentId: 'fake-id',
+ isFile: false,
}),
)
const id = result?.current?.tmpFolders?.['some-folder']?.[0]?.id
@@ -84,6 +90,7 @@ describe('useTempNodes', () => {
result.current.addFolder({
parentPath: 'some-folder/parent-tmp-folder',
parentId: id!,
+ isFile: false,
}),
)
@@ -101,6 +108,7 @@ describe('useTempNodes', () => {
path: 'some-folder/parent-tmp-folder',
name: 'parent-tmp-folder',
isPersisted: false,
+ isFile: false,
children: [],
}),
],
@@ -113,6 +121,7 @@ describe('useTempNodes', () => {
result.current.addFolder({
parentPath: 'some-folder',
parentId: 'fake-id',
+ isFile: false,
}),
)
const id = result?.current?.tmpFolders?.['some-folder']?.[0]?.id
@@ -124,6 +133,7 @@ describe('useTempNodes', () => {
result.current.addFolder({
parentPath: 'some-folder/parent-tmp-folder',
parentId: id!,
+ isFile: false,
}),
)
@@ -139,6 +149,7 @@ describe('useTempNodes', () => {
path: 'some-folder/parent-tmp-folder/child-tmp-folder',
name: 'child-tmp-folder',
isPersisted: false,
+ isFile: false,
isRoot: false,
})
const rootTmpNode = new Node({
@@ -146,6 +157,7 @@ describe('useTempNodes', () => {
path: 'some-folder/parent-tmp-folder',
name: 'parent-tmp-folder',
isPersisted: false,
+ isFile: false,
children: [child],
})
child.parent = rootTmpNode
diff --git a/packages/web-ui/src/sections/Document/Sidebar/Files/useTempNodes/index.ts b/packages/web-ui/src/sections/Document/Sidebar/Files/useTempNodes/index.ts
index a0a7a7c65..9fd37d811 100644
--- a/packages/web-ui/src/sections/Document/Sidebar/Files/useTempNodes/index.ts
+++ b/packages/web-ui/src/sections/Document/Sidebar/Files/useTempNodes/index.ts
@@ -4,7 +4,11 @@ import { defaultGenerateNodeUuid, Node } from '../useTree'
type TmpFoldersState = {
tmpFolders: Record
- addFolder: (args: { parentPath: string; parentId: string }) => void
+ addFolder: (args: {
+ parentPath: string
+ parentId: string
+ isFile: boolean
+ }) => void
updateFolder: (args: { id: string; path: string }) => void
deleteTmpFolder: (args: { id: string }) => void
reset: () => void
@@ -23,16 +27,21 @@ function allTmpNodes(tmpFolders: TmpFoldersState['tmpFolders']) {
function createEmptyNode({
parentPath,
parent,
+ isFile,
}: {
parentPath: string
+ isFile: boolean
parent: Node | undefined
}) {
const emptyName = ' '
+ const path = `${parentPath}/${emptyName}`
return new Node({
id: defaultGenerateNodeUuid(),
name: emptyName,
- path: `${parentPath}/${emptyName}`,
+ path,
isPersisted: false,
+ doc: isFile ? { path, documentUuid: defaultGenerateNodeUuid() } : undefined,
+ isFile,
parent,
})
}
@@ -57,6 +66,7 @@ function cloneNode(node: Node) {
name: node.name,
path: node.path,
parent: node.parent,
+ isFile: node.isFile,
isPersisted: node.isPersisted,
})
return clonedNode
@@ -84,7 +94,7 @@ export const useTempNodes = create((set) => ({
tmpFolders: {},
})
},
- addFolder: ({ parentPath, parentId }) => {
+ addFolder: ({ parentPath, parentId, isFile }) => {
set((state) => {
const allNodes = allTmpNodes(state.tmpFolders)
const parentNode = allNodes.find((node) => node.id === parentId)
@@ -92,6 +102,7 @@ export const useTempNodes = create((set) => ({
const node = createEmptyNode({
parentPath: parentNode ? parentNode.path : parentPath,
parent: parentNode,
+ isFile,
})
// When adding to an existing tmp node we
diff --git a/packages/web-ui/src/sections/Document/Sidebar/Files/useTree/index.ts b/packages/web-ui/src/sections/Document/Sidebar/Files/useTree/index.ts
index dce789361..74317c324 100644
--- a/packages/web-ui/src/sections/Document/Sidebar/Files/useTree/index.ts
+++ b/packages/web-ui/src/sections/Document/Sidebar/Files/useTree/index.ts
@@ -24,11 +24,13 @@ export class Node {
isPersisted,
children = [],
isRoot = false,
+ isFile,
path,
name = '',
}: {
id: string
path: string
+ isFile: boolean
parent?: Node
isPersisted: boolean
doc?: SidebarDocument
@@ -42,7 +44,7 @@ export class Node {
this.isPersisted = isPersisted
this.name = isRoot ? 'root' : name
this.isRoot = isRoot
- this.isFile = !!doc
+ this.isFile = isFile
this.children = children
this.doc = doc
}
@@ -102,6 +104,7 @@ function buildTree({
const node = new Node({
id: generateNodeId({ uuid }),
isPersisted: true,
+ isFile,
name: segment,
path,
doc: file,
@@ -146,6 +149,7 @@ export function useTree({
path: '',
children: [],
isRoot: true,
+ isFile: false,
})
const nodeMap = new Map()
nodeMap.set('', root)