avoid saving {this:null} for permissions, as it's equivalent to default
Massimo Melina committed
Oct 23, 2023 at 12:59 UTC
b925c22f7cfde875d5e0f4cbab880b7b5a03e837
2 files changed
+21
-11
admin/src/FileForm.ts
+20
-10
@@ -14,8 +14,10 @@ import {
14
StringField
15
} from '@hfs/mui-grid-form'
16
import { apiCall, useApiEx } from './api'
17
-import { basename, Btn, defaultPerms, formatBytes, formatTimestamp, IconBtn, isEqualLax, LinkBtn, modifiedSx,
18
- newDialog, objSameKeys, onlyTruthy, prefix, useBreakpoint, VfsPerms, Who, wikiLink } from './misc'
17
+import {
18
+ basename, Btn, defaultPerms, formatBytes, formatTimestamp, IconBtn, isEqualLax, isWhoObject, LinkBtn, modifiedSx,
19
+ newDialog, objSameKeys, onlyTruthy, prefix, useBreakpoint, VfsPerms, Who, WhoObject, wikiLink
20
+} from './misc'
21
import { reloadVfs, VfsNode } from './VfsPage'
22
import md from './md'
23
import _ from 'lodash'
@@ -165,8 +167,10 @@ function WhoField({ value, onChange, parent, inherit, accounts, helperText, show
167
isChildren, isDir, contentText="folder content", setApi, ...rest }: WhoFieldProps): ReactElement {
168
const defaultLabel = (byMasks !== undefined ? "As per mask: " : parent !== undefined ? "As parent: " : "Default: " )
169
+ who2desc(byMasks ?? inherit)
168
- const objectMode = value != null && typeof value === 'object' && !Array.isArray(value)
169
- const childrenValue = objectMode && value.children
170
+ const objectMode = isWhoObject(value)
171
+ const [forceObject, setForceObject] = useState(objectMode)
172
+ const showObjectMode = objectMode || forceObject
173
+ const childrenValue = objectMode ? value.children : undefined
174
const thisValue = objectMode ? value.this : value
175
176
const options = useMemo(() =>
@@ -185,12 +189,12 @@ function WhoField({ value, onChange, parent, inherit, accounts, helperText, show
189
const timeout = 500
190
const arrayMode = Array.isArray(thisValue)
191
// a large side band will convey union across the fields
188
- return h(Box, { sx: { borderRight: objectMode ? '8px solid #8884' : undefined, transition: `all ${timeout}ms` } },
192
+ return h(Box, { sx: { borderRight: showObjectMode ? '8px solid #8884' : undefined, transition: `all ${timeout}ms` } },
193
h(SelectField as typeof SelectField<typeof thisValue | null>, {
194
...rest,
195
value: arrayMode ? [] : thisValue ?? null,
196
onChange(v, { event }) {
193
- onChange(objectMode ? { this: v ?? undefined, children: childrenValue } : v ?? undefined, { was: value, event })
197
+ onChange(showObjectMode ? simplify({ this: v ?? undefined, children: childrenValue }) : v ?? undefined, { was: value, event })
198
},
199
options,
200
}),
@@ -207,22 +211,28 @@ function WhoField({ value, onChange, parent, inherit, accounts, helperText, show
211
sx: { display: 'block', mt: -.5 },
212
onClick(event) {
213
if (thisValue === undefined) return
210
- onChange(objectMode ? thisValue : { this: value }, { was: value, event })
214
+ setForceObject(!showObjectMode)
215
+ if (objectMode)
216
+ onChange(thisValue, { was: value, event })
217
}
212
- }, objectMode ? "Different permission for " : "Same permission for ", contentText)
218
+ }, showObjectMode ? "Different permission for " : "Same permission for ", contentText)
219
),
214
- !isChildren && h(Collapse, { in: objectMode, timeout },
220
+ !isChildren && h(Collapse, { in: showObjectMode, timeout },
221
h(WhoField, {
222
label: "Permission for " + contentText,
223
parent, inherit, accounts, showInherited, otherPerms, isDir,
224
isChildren: true,
225
value: childrenValue ?? undefined,
226
onChange(v, { event }) {
221
- onChange({ this: thisValue ?? undefined, children: v }, { was: value, event })
227
+ onChange(simplify({ this: thisValue ?? undefined, children: v }), { was: value, event })
228
}
229
})
230
),
231
)
232
+
233
+ function simplify(v: WhoObject) {
234
+ return v.this === v.children ? v.this : v
235
+ }
236
}
237
238
function who2desc(who: any) {
src/cross.ts
+1
-1
@@ -56,7 +56,7 @@ export type Who = typeof WHO_ANYONE
56
| keyof VfsPerms
57
| WhoObject
58
| AccountList // use false instead of empty array to keep the type boolean-able
59
-interface WhoObject { this?: Who, children?: Who }
59
+export interface WhoObject { this?: Who, children?: Who }
60
61
export const defaultPerms: Required<VfsPerms> = {
62
can_see: 'can_read',