|
| 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 | +/** |
| 10 | + * The record a detail body edits in place answers its own field locks |
| 11 | + * (objectstack#27146, the residual of objectstack#26594 ruling B). |
| 12 | + * |
| 13 | + * `DetailSection` and `HeaderHighlight` ask a field's in-place edit of the |
| 14 | + * `recordEdit` row (`resolveFieldAffordance`), which reads `usePermissions()`. |
| 15 | + * Under the page's provider alone that is `/auth/me/permissions`' answer, which |
| 16 | + * holds no row: a field permission whose `when` names only some rows is served |
| 17 | + * there as its lock, and the field was disabled on every row, the draft the |
| 18 | + * server lets the caller write included. Below `RecordFieldAccessScope` |
| 19 | + * (`@object-ui/permissions`) the same call answers from the server's |
| 20 | + * projection for THIS stored row (`DataSource.getRecordFieldAccess`, the |
| 21 | + * `GET /data/{object}/{id}/field-access` route) — the one source the record |
| 22 | + * form's edit dialog asks. Nothing here evaluates a `when`. |
| 23 | + * |
| 24 | + * Mounted by the two record blocks that edit the page's record in place, |
| 25 | + * `record:details` and `record:highlights`, around their bodies only. They are |
| 26 | + * sibling blocks of the page schema with no ancestor in this package, and a |
| 27 | + * mount above them (the page host) would also cover the page's action dialogs |
| 28 | + * and related lists, where a form editing ANOTHER row of the same object would |
| 29 | + * read this row's answer until its own arrives. So each block asks for itself: |
| 30 | + * a page drawing both asks twice. |
| 31 | + * |
| 32 | + * It asks only when the block's in-place edit is on: the page's inline-edit |
| 33 | + * session may edit (`canEdit`), the block offers it (`enabled`), the record is |
| 34 | + * stored (an id) and the data source carries the method. Otherwise the |
| 35 | + * row-blind answer stands, as it does for a non-ObjectStack backend. |
| 36 | + * |
| 37 | + * The answer belongs to the stored row, so it is asked again whenever the row |
| 38 | + * may have moved: a write the data source reports for this record (the inline |
| 39 | + * save bar's included) and a change on the data-invalidation bus for it (the |
| 40 | + * signal the page re-reads its record on). These are the triggers |
| 41 | + * `RecordFieldAccessForForm` (`@object-ui/plugin-form`) holds; that wrapper is |
| 42 | + * not exported and this package does not depend on that one, so the few lines |
| 43 | + * are repeated here rather than published as a new entry of either package. |
| 44 | + */ |
| 45 | + |
| 46 | +import React, { useEffect, useState } from 'react'; |
| 47 | +import { RecordFieldAccessScope } from '@object-ui/permissions'; |
| 48 | +import { useDataInvalidation, useInlineEdit } from '@object-ui/react'; |
| 49 | +import type { DataSource } from '@object-ui/types'; |
| 50 | + |
| 51 | +export interface RecordFieldAccessForRecordProps { |
| 52 | + objectName?: string | null; |
| 53 | + recordId?: string | number | null; |
| 54 | + dataSource?: DataSource | null; |
| 55 | + /** Whether the block draws in-place editors at all. Defaults to `true`. */ |
| 56 | + enabled?: boolean; |
| 57 | + children: React.ReactNode; |
| 58 | +} |
| 59 | + |
| 60 | +export function RecordFieldAccessForRecord({ |
| 61 | + objectName, |
| 62 | + recordId, |
| 63 | + dataSource, |
| 64 | + enabled = true, |
| 65 | + children, |
| 66 | +}: RecordFieldAccessForRecordProps) { |
| 67 | + const inline = useInlineEdit(); |
| 68 | + const load = dataSource?.getRecordFieldAccess; |
| 69 | + const asks = enabled |
| 70 | + && !!inline?.canEdit |
| 71 | + && !!objectName |
| 72 | + && recordId !== undefined && recordId !== null && recordId !== '' |
| 73 | + && typeof load === 'function'; |
| 74 | + const id = asks ? String(recordId) : undefined; |
| 75 | + const busRevision = useDataInvalidation(asks ? (objectName as string) : undefined, id); |
| 76 | + const [writeRevision, setWriteRevision] = useState(0); |
| 77 | + useEffect(() => { |
| 78 | + if (!asks || typeof dataSource?.onMutation !== 'function') return; |
| 79 | + const object = (objectName as string).toLowerCase(); |
| 80 | + return dataSource.onMutation((event) => { |
| 81 | + if ((event?.resource ?? '').toLowerCase() !== object) return; |
| 82 | + if (event.id != null && String(event.id) !== id) return; |
| 83 | + setWriteRevision((n) => n + 1); |
| 84 | + }); |
| 85 | + }, [asks, dataSource, objectName, id]); |
| 86 | + if (!asks) { |
| 87 | + return <>{children}</>; |
| 88 | + } |
| 89 | + return ( |
| 90 | + <RecordFieldAccessScope |
| 91 | + object={objectName as string} |
| 92 | + recordId={id as string} |
| 93 | + load={(object, recordKey) => (load as NonNullable<typeof load>).call(dataSource, object, recordKey)} |
| 94 | + revision={busRevision + writeRevision} |
| 95 | + > |
| 96 | + {children} |
| 97 | + </RecordFieldAccessScope> |
| 98 | + ); |
| 99 | +} |
0 commit comments