|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import React from 'react'; |
| 10 | +import { Combobox, cn } from '@object-ui/components'; |
| 11 | +import { SchemaRendererContext, type ApiFetch } from '@object-ui/react'; |
| 12 | +import type { FieldWidgetComponentProps } from './types.js'; |
| 13 | +import { toDomProps } from './toDomProps.js'; |
| 14 | +import { useFieldTranslation } from './useFieldTranslation.js'; |
| 15 | +import { objectRefOptionLabel } from './objectRefLabel.js'; |
| 16 | +import { ObjectRefField } from './ObjectRefField.js'; |
| 17 | + |
| 18 | +/** |
| 19 | + * ShareableObjectRefField — the `shareable-object-ref` widget: the object |
| 20 | + * picker of a sharing rule, offering only the objects a rule can take effect on |
| 21 | + * (`sys_sharing_rule.object_name`). |
| 22 | + * |
| 23 | + * The list is the server's, read from the sharing plugin's shareable-objects |
| 24 | + * door through the host's authenticated fetch (`SchemaRendererContext.apiFetch`). |
| 25 | + * The door lists exactly the objects a rule save admits (it asks the save |
| 26 | + * doors' own `SHARING_NOT_ENABLED` verdict), business objects first, then |
| 27 | + * platform objects, each by label. The picker shows that order as given and |
| 28 | + * never re-sorts it. |
| 29 | + * |
| 30 | + * When the door does not answer the list, the field is the `object-ref` |
| 31 | + * picker, unchanged: every registered object, as before this widget existed. |
| 32 | + * That covers a caller the door refuses (`403`: the list is the sharing-rule |
| 33 | + * administrators'), a deployment where sharing rules do not run (`501`), a |
| 34 | + * server without the door, an answer that is not the door's shape, and a host |
| 35 | + * with no authenticated fetch. The save doors stay the authority on which |
| 36 | + * object a rule may name, so the wider list costs a refusal at save, never a |
| 37 | + * wrong rule; an empty picker would leave the field unfillable. |
| 38 | + * |
| 39 | + * The stored value is always the object's machine name. A stored name the list |
| 40 | + * does not hold (a rule on an object that no longer takes one) stays on screen |
| 41 | + * as its own option, so editing the rule never drops it. The read-only face is |
| 42 | + * `object-ref`'s, which labels the stored name without asking the door. |
| 43 | + */ |
| 44 | + |
| 45 | +/** The door the list is read from (plugin-sharing `shareable-objects-routes.ts`). */ |
| 46 | +export const SHAREABLE_OBJECTS_URL = '/api/v1/sharing/shareable-objects'; |
| 47 | + |
| 48 | +/** One listed object, as the door answers it. */ |
| 49 | +interface ShareableObjectHeader { |
| 50 | + name: string; |
| 51 | + label: string; |
| 52 | +} |
| 53 | + |
| 54 | +type ShareableCatalog = |
| 55 | + | { status: 'loading' } |
| 56 | + | { status: 'listed'; objects: ShareableObjectHeader[] } |
| 57 | + | { status: 'fallback' }; |
| 58 | + |
| 59 | +const LOADING: ShareableCatalog = { status: 'loading' }; |
| 60 | +const FALLBACK: ShareableCatalog = { status: 'fallback' }; |
| 61 | + |
| 62 | +/** The door's list, or `null` when the answer is not the door's success shape. */ |
| 63 | +function listOf(body: unknown): ShareableObjectHeader[] | null { |
| 64 | + const raw = (body as { data?: { objects?: unknown } } | null)?.data?.objects; |
| 65 | + if (!Array.isArray(raw)) return null; |
| 66 | + const objects: ShareableObjectHeader[] = []; |
| 67 | + for (const entry of raw) { |
| 68 | + const name = (entry as { name?: unknown } | null)?.name; |
| 69 | + if (typeof name !== 'string' || name === '') continue; |
| 70 | + const label = (entry as { label?: unknown }).label; |
| 71 | + objects.push({ name, label: typeof label === 'string' ? label : '' }); |
| 72 | + } |
| 73 | + return objects; |
| 74 | +} |
| 75 | + |
| 76 | +async function readShareableObjects(doFetch: ApiFetch): Promise<ShareableObjectHeader[] | null> { |
| 77 | + const res = await doFetch(SHAREABLE_OBJECTS_URL, { |
| 78 | + headers: { accept: 'application/json' }, |
| 79 | + credentials: 'include', |
| 80 | + }); |
| 81 | + if (!res.ok) return null; |
| 82 | + return listOf(await res.json()); |
| 83 | +} |
| 84 | + |
| 85 | +function useShareableObjects(doFetch: ApiFetch | undefined, enabled: boolean): ShareableCatalog { |
| 86 | + const [catalog, setCatalog] = React.useState<ShareableCatalog>(doFetch && enabled ? LOADING : FALLBACK); |
| 87 | + React.useEffect(() => { |
| 88 | + if (!doFetch || !enabled) { |
| 89 | + setCatalog(FALLBACK); |
| 90 | + return; |
| 91 | + } |
| 92 | + let cancelled = false; |
| 93 | + setCatalog(LOADING); |
| 94 | + readShareableObjects(doFetch).then( |
| 95 | + (objects) => { |
| 96 | + if (!cancelled) setCatalog(objects ? { status: 'listed', objects } : FALLBACK); |
| 97 | + }, |
| 98 | + () => { |
| 99 | + if (!cancelled) setCatalog(FALLBACK); |
| 100 | + }, |
| 101 | + ); |
| 102 | + return () => { |
| 103 | + cancelled = true; |
| 104 | + }; |
| 105 | + }, [doFetch, enabled]); |
| 106 | + return catalog; |
| 107 | +} |
| 108 | + |
| 109 | +export function ShareableObjectRefField(props: FieldWidgetComponentProps<string>) { |
| 110 | + const { value, onChange, readonly, className, error, ...rest } = props; |
| 111 | + const ctx = React.useContext(SchemaRendererContext); |
| 112 | + const { t } = useFieldTranslation(); |
| 113 | + const catalog = useShareableObjects(ctx?.apiFetch, !readonly); |
| 114 | + |
| 115 | + const options = React.useMemo(() => { |
| 116 | + if (catalog.status !== 'listed') return []; |
| 117 | + const opts = catalog.objects.map((o) => ({ value: o.name, label: objectRefOptionLabel(o.name, o.label) })); |
| 118 | + if (value && !opts.some((o) => o.value === value)) opts.unshift({ value, label: value }); |
| 119 | + return opts; |
| 120 | + }, [catalog, value]); |
| 121 | + |
| 122 | + if (readonly || catalog.status === 'fallback') return <ObjectRefField {...props} />; |
| 123 | + |
| 124 | + // DOM pass-through onto the combobox trigger, as `object-ref` does: `name` |
| 125 | + // is withheld because the trigger is a button, not a submission control. |
| 126 | + const { name: _domName, ...triggerDomProps } = toDomProps(rest); |
| 127 | + const loading = catalog.status === 'loading'; |
| 128 | + |
| 129 | + return ( |
| 130 | + <Combobox |
| 131 | + {...triggerDomProps} |
| 132 | + options={options} |
| 133 | + value={value ?? ''} |
| 134 | + onValueChange={(v) => onChange(v)} |
| 135 | + placeholder={loading ? t('fields.objectRef.loading') : t('fields.objectRef.placeholder')} |
| 136 | + searchPlaceholder={t('fields.objectRef.search')} |
| 137 | + emptyText={loading ? t('common.loading') : t('fields.objectRef.empty')} |
| 138 | + disabled={rest.disabled} |
| 139 | + className={cn('w-full', className)} |
| 140 | + aria-invalid={!!error} |
| 141 | + /> |
| 142 | + ); |
| 143 | +} |
| 144 | + |
| 145 | +export default ShareableObjectRefField; |
0 commit comments