admin/fs: changing the source will possibly update name too

Massimo Melina committed Apr 17, 2023 at 12:53 UTC 9c32ef206f5d6b9bbc81dd1e4ad7bc22141de3f3
2 files changed +11 -8
admin/src/FileForm.ts
+7 -8
@@ -14,7 +14,7 @@ import {
14 StringField
15 } from '@hfs/mui-grid-form'
16 import { apiCall, useApiEx } from './api'
17 -import { formatBytes, IconBtn, isEqualLax, modifiedSx, newDialog, objSameKeys, onlyTruthy, prefix } from './misc'
17 +import { basename, formatBytes, IconBtn, isEqualLax, modifiedSx, newDialog, objSameKeys, onlyTruthy, prefix } from './misc'
18 import { reloadVfs, VfsNode, VfsPerms, Who } from './VfsPage'
19 import md from './md'
20 import _ from 'lodash'
@@ -69,7 +69,9 @@ export default function FileForm({ file, anyMask, defaultPerms, addToBar, urls }
69 values,
70 set(v, k) {
71 if (k === 'link') return
72 - setValues({ ...values, [k]: v })
72 + const nameIsVirtual = k === 'source' && values.source?.endsWith(values.name)
73 + const name = nameIsVirtual ? basename(v) : values.name // update name if virtual
74 + setValues({ ...values, name, [k]: v })
75 },
76 barSx: { gap: 2, width: '100%', ...barColors },
77 stickyBar: true,
@@ -84,15 +86,12 @@ export default function FileForm({ file, anyMask, defaultPerms, addToBar, urls }
86 ],
87 onError: alertDialog,
88 save: {
87 - sx: modifiedSx(!isEqualLax(values, file)),
89 + sx: modifiedSx(!isEqualLax(values, rest)),
90 async onClick() {
91 const props = _.omit(values, ['ctime','mtime','size','id'])
92 if (!props.masks)
93 props.masks = null // undefined cannot be serialized
92 - await apiCall('set_vfs', {
93 - uri: values.id,
94 - props,
95 - })
94 + await apiCall('set_vfs', { uri: values.id, props })
95 if (props.name !== file.name) // when the name changes, the id of the selected file is changing too, and we have to update it in the state if we want it to be correctly re-selected after reload
96 state.selectedFiles[0].id = file.parent!.id + props.name + (isDir ? '/' : '')
97 reloadVfs()
@@ -100,7 +99,7 @@ export default function FileForm({ file, anyMask, defaultPerms, addToBar, urls }
99 },
100 fields: [
101 isRoot ? h(Alert,{ severity: 'info' }, "This is Home, the root of your shared files. Options set here will be applied to all files.")
103 - : { k: 'name', required: true, helperText: source && "You can decide a name that's different from the one on your disk" },
102 + : { k: 'name', required: true, helperText: hasSource && "You can decide a name that's different from the one on your disk" },
103 { k: 'id', comp: LinkField, urls },
104 { k: 'source', label: "Source on disk", comp: FileField, files: !isDir, folders: isDir, multiline: true,
105 placeholder: "Not on disk, this is a virtual folder",
shared/index.ts
+4
@@ -164,3 +164,7 @@ export function getHFS() {
164 export function getPrefixUrl() {
165 return getHFS().prefixUrl
166 }
167 +
168 +export function basename(path: string) {
169 + return path.slice(path.lastIndexOf('/') + 1 || path.lastIndexOf('\\') + 1)
170 +}
\ No newline at end of file