Skip to content

Commit

Permalink
Adjust store to handle a paths array instead of path as string
Browse files Browse the repository at this point in the history
  • Loading branch information
guergana committed Jun 2, 2024
1 parent f2c30d7 commit af2f975
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 32 deletions.
2 changes: 1 addition & 1 deletion client/components/Application/Browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function Browser() {
}

function DefaultBrowser() {
const path = useStore((state) => state.path)
const path = useStore((state) => state.paths)
const files = useStore((state) => state.files)
const fileEvent = useStore((state) => state.fileEvent)
const selectFile = useStore((state) => state.selectFile)
Expand Down
6 changes: 3 additions & 3 deletions client/components/Application/Buttons/Delete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import { selectors, useStore } from '../store'
import { useKeyPress } from 'ahooks'

export default function DeleteButton() {
const path = useStore((state) => state.path)
const paths = useStore((state) => state.paths)
const isFolder = useStore(selectors.isFolder)
const updateState = useStore((state) => state.updateState)
const type = isFolder ? 'Folder' : 'File'
useKeyPress(['ctrl.i'], (event) => {
event.preventDefault()
if (path) updateState({ dialog: `delete${type}` })
if (paths) updateState({ dialog: `delete${type}` })
})
return (
<LightTooltip title="Delete file [Ctrl+I]">
<Box>
<IconButton
label="Delete"
Icon={DeleteIcon}
disabled={!path}
disabled={!paths}
variant="text"
color="warning"
onClick={() => updateState({ dialog: `delete${type}` })}
Expand Down
6 changes: 3 additions & 3 deletions client/components/Application/Dialogs/DeleteFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import ConfirmDialog from '../../Parts/Dialogs/Confirm'
import { useStore } from '../store'

export default function DeleteFileDialog() {
const path = useStore((state) => state.path)
const paths = useStore((state) => state.paths)
const deleteFile = useStore((state) => state.deleteFile)
const updateState = useStore((state) => state.updateState)
if (!path) return null
if (!paths) return null
return (
<ConfirmDialog
open={true}
Expand All @@ -15,7 +15,7 @@ export default function DeleteFileDialog() {
cancelLabel="No"
onCancel={() => updateState({ dialog: undefined })}
onConfirm={async () => {
await deleteFile(path)
await deleteFile(paths)
updateState({ dialog: undefined })
}}
/>
Expand Down
53 changes: 29 additions & 24 deletions client/components/Application/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as helpers from '../../helpers'
import * as types from '../../types'

export interface State {
path?: string[]
paths?: string[]
client: Client
config?: types.IConfig
record?: types.IRecord
Expand Down Expand Up @@ -46,10 +46,10 @@ export interface State {
createFile: (path: string, prompt?: string) => Promise<void>
adjustFile: (name?: string, type?: string) => Promise<void>
copyFile: (path: string, toPath: string) => Promise<void>
deleteFile: (path: string) => Promise<void>
deleteFile: (paths: string[]) => Promise<void>
moveFile: (path: string, toPath: string) => Promise<void>
locateFile: (path: string) => Promise<void>
selectFile: (path?: string) => Promise<void>
selectFile: (paths?: string[]) => Promise<void>
openFile: (path: string) => Promise<void>
closeFile: () => void

Expand Down Expand Up @@ -111,7 +111,7 @@ export function makeStore(props: ApplicationProps) {
const { loadFiles, selectFile } = get()
await loadFiles()
set({ fileEvent: { type: 'create', paths } })
if (paths.length === 1) selectFile(paths[0])
if (paths.length === 1) selectFile(paths)
await delay(500)
set({ fileEvent: undefined })
},
Expand All @@ -126,7 +126,7 @@ export function makeStore(props: ApplicationProps) {
onFilePatch: async (path) => {
const { selectFile } = get()
set({ fileEvent: { type: 'update', paths: [path] } })
selectFile(path)
selectFile([path])
await delay(500)
set({ fileEvent: undefined })
},
Expand Down Expand Up @@ -194,13 +194,14 @@ export function makeStore(props: ApplicationProps) {
}
},
adjustFile: async (name, type) => {
const { path, client, closeFile, loadFiles, selectFile } = get()
if (!path) return
await client.filePatch({ path, name, type })
set({ path: undefined })
const { paths, client, closeFile, loadFiles, selectFile } = get()
// only adjust file if one file is selected from menu
if (!paths || paths.length !== 1) return
await client.filePatch({ path: paths[0], name, type })
set({ paths: undefined })
closeFile()
await loadFiles()
await selectFile(path)
await selectFile(paths)
},
copyFile: async (path, toPath) => {
const { client, onFileCreate } = get()
Expand All @@ -220,21 +221,26 @@ export function makeStore(props: ApplicationProps) {
onFileCreate([result.path])
},
locateFile: async (path) => {
set({ path })
set({ paths: [path] })
set({ fileEvent: { type: 'locate', paths: [path] } })
await delay(500)
set({ fileEvent: undefined })
},
selectFile: async (newPath) => {
const { path, record, openFile } = get()
if (path === newPath) return
set({ path: newPath })
if (!newPath) return
if (record?.path === newPath) return
selectFile: async (newPaths) => {
console.log('selectFile called with', newPaths)
const isSingleFile = newPaths && newPaths.length === 1
const { paths, record, openFile } = get()
if (paths === newPaths) return
set({ paths: newPaths })
if (!newPaths) return
if (record?.path === newPaths[0]) return
if (selectors.isFolder(get())) return
await openFile(newPath)
if (isSingleFile) {
await openFile(newPaths[0])
} else return
},
openFile: async (path) => {
console.log('openFile', path)
const { client, loadFiles, fileEvent } = get()
set({ record: undefined, measure: undefined })
set({ indexing: true })
Expand Down Expand Up @@ -358,15 +364,14 @@ export function makeStore(props: ApplicationProps) {

export const selectors = {
isFolder: (state: State) => {
return !!state.files.find(
(file) => file.path === state.path && file.type === 'folder'
)
const path = state.paths ? state.paths[0] : null
return !!state.files.find((file) => file.path === path && file.type === 'folder')
},
folderPath: (state: State) => {
if (!state.path) return undefined
if (!state.paths) return undefined
const isFolder = selectors.isFolder(state)
if (isFolder) return state.path
return helpers.getFolderPath(state.path)
if (isFolder) return state.paths[0]
return helpers.getFolderPath(state.paths[0])
},
notIndexedFiles: (state: State) => {
return state.files.filter((file) => !file.name)
Expand Down
2 changes: 1 addition & 1 deletion client/components/Parts/Trees/File.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface FileTreeProps {
files: types.IFile[]
event?: types.IFileEvent
selected?: string[]
onSelect: (path?: string[]) => void
onSelect: (paths?: string[]) => void
defaultExpanded?: string[]
}

Expand Down

0 comments on commit af2f975

Please sign in to comment.