|
| 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 | + * `record:attachments` — schema-addressable wrapper around |
| 11 | + * `RecordAttachmentsPanel`, mirroring how `record:discussion` wraps |
| 12 | + * `RecordChatterPanel` (plugin-detail/renderers/record-chatter.tsx). |
| 13 | + * |
| 14 | + * Registered here (app-shell) rather than in plugin-detail because the panel |
| 15 | + * depends on `@object-ui/providers` (presigned upload adapter) and |
| 16 | + * `@object-ui/auth` (Bearer fetch), which plugin-detail deliberately does not |
| 17 | + * pull in. The side-effect registration is imported from the app-shell barrel |
| 18 | + * (`src/index.ts`), so any host that mounts the shell — the console runtime |
| 19 | + * AND Studio's PagePreview — resolves the type before a synthesized page |
| 20 | + * references it (objectstack#4358). |
| 21 | + * |
| 22 | + * Renders nothing without a record identity (e.g. a page composed outside a |
| 23 | + * RecordContext). A missing dataSource is tolerated: the panel just shows its |
| 24 | + * empty state — that is what Studio's PagePreview binds. |
| 25 | + */ |
| 26 | + |
| 27 | +import * as React from 'react'; |
| 28 | +import { ComponentRegistry } from '@object-ui/core'; |
| 29 | +import { useRecordContext } from '@object-ui/react'; |
| 30 | +import { useAuth } from '@object-ui/auth'; |
| 31 | +import { RecordAttachmentsPanel } from './RecordAttachmentsPanel'; |
| 32 | + |
| 33 | +const splitDesigner = (props: Record<string, any>) => { |
| 34 | + const { 'data-obj-id': id, 'data-obj-type': type, style, ...rest } = props || {}; |
| 35 | + return { designer: { 'data-obj-id': id, 'data-obj-type': type, style }, rest }; |
| 36 | +}; |
| 37 | + |
| 38 | +export interface RecordAttachmentsRendererProps { |
| 39 | + schema?: Record<string, any>; |
| 40 | + className?: string; |
| 41 | + [k: string]: any; |
| 42 | +} |
| 43 | + |
| 44 | +export const RecordAttachmentsRenderer: React.FC<RecordAttachmentsRendererProps> = ({ |
| 45 | + schema: _schema, |
| 46 | + className, |
| 47 | + ...props |
| 48 | +}) => { |
| 49 | + const ctx = useRecordContext(); |
| 50 | + const { user } = useAuth(); |
| 51 | + const { designer } = splitDesigner(props); |
| 52 | + |
| 53 | + const objectName = ctx?.objectName; |
| 54 | + const recordId = ctx?.recordId != null ? String(ctx.recordId) : ''; |
| 55 | + if (!objectName || !recordId) return null; |
| 56 | + |
| 57 | + return ( |
| 58 | + <div className={className} {...designer}> |
| 59 | + <RecordAttachmentsPanel |
| 60 | + objectName={objectName} |
| 61 | + recordId={recordId} |
| 62 | + dataSource={ctx?.dataSource as any} |
| 63 | + currentUserId={user?.id} |
| 64 | + /> |
| 65 | + </div> |
| 66 | + ); |
| 67 | +}; |
| 68 | + |
| 69 | +ComponentRegistry.register('attachments', RecordAttachmentsRenderer, { |
| 70 | + namespace: 'record', |
| 71 | + skipFallback: true, |
| 72 | + category: 'record', |
| 73 | + label: 'Attachments', |
| 74 | + icon: 'Paperclip', |
| 75 | + inputs: [{ name: 'className', type: 'string', label: 'CSS Class' }], |
| 76 | +}); |
| 77 | + |
| 78 | +export default RecordAttachmentsRenderer; |
0 commit comments