|
| 1 | +import { useEffect } from 'react'; |
| 2 | +import { Meta, StoryObj } from '@storybook/react'; |
| 3 | +import { useSetRecoilState } from 'recoil'; |
| 4 | +import { ComponentDecorator } from 'twenty-ui'; |
| 5 | + |
| 6 | +import { FieldContext } from '@/object-record/record-field/contexts/FieldContext'; |
| 7 | +import { RelationFromManyFieldDisplay } from '@/object-record/record-field/meta-types/display/components/RelationFromManyFieldDisplay'; |
| 8 | +import { FieldDefinition } from '@/object-record/record-field/types/FieldDefinition'; |
| 9 | +import { FieldMetadata } from '@/object-record/record-field/types/FieldMetadata'; |
| 10 | +import { |
| 11 | + RecordFieldValueSelectorContextProvider, |
| 12 | + useSetRecordValue, |
| 13 | +} from '@/object-record/record-store/contexts/RecordFieldValueSelectorContext'; |
| 14 | +import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState'; |
| 15 | +import { MemoryRouterDecorator } from '~/testing/decorators/MemoryRouterDecorator'; |
| 16 | +import { getProfilingStory } from '~/testing/profiling/utils/getProfilingStory'; |
| 17 | + |
| 18 | +import { |
| 19 | + fieldValue, |
| 20 | + relationFromManyFieldDisplayMock, |
| 21 | +} from './relationFromManyFieldDisplayMock'; |
| 22 | + |
| 23 | +const RelationFieldValueSetterEffect = () => { |
| 24 | + const setEntity = useSetRecoilState( |
| 25 | + recordStoreFamilyState(relationFromManyFieldDisplayMock.entityId), |
| 26 | + ); |
| 27 | + |
| 28 | + const setRelationEntity = useSetRecoilState( |
| 29 | + recordStoreFamilyState(relationFromManyFieldDisplayMock.relationEntityId), |
| 30 | + ); |
| 31 | + |
| 32 | + const setRecordValue = useSetRecordValue(); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + setEntity(relationFromManyFieldDisplayMock.entityValue); |
| 36 | + setRelationEntity(relationFromManyFieldDisplayMock.relationFieldValue); |
| 37 | + |
| 38 | + setRecordValue( |
| 39 | + relationFromManyFieldDisplayMock.entityValue.id, |
| 40 | + relationFromManyFieldDisplayMock.entityValue, |
| 41 | + ); |
| 42 | + setRecordValue( |
| 43 | + relationFromManyFieldDisplayMock.relationFieldValue.id, |
| 44 | + relationFromManyFieldDisplayMock.relationFieldValue, |
| 45 | + ); |
| 46 | + }, [setEntity, setRelationEntity, setRecordValue]); |
| 47 | + |
| 48 | + return null; |
| 49 | +}; |
| 50 | + |
| 51 | +const meta: Meta = { |
| 52 | + title: 'UI/Data/Field/Display/RelationFromManyFieldDisplay', |
| 53 | + decorators: [ |
| 54 | + MemoryRouterDecorator, |
| 55 | + (Story) => ( |
| 56 | + <RecordFieldValueSelectorContextProvider> |
| 57 | + <FieldContext.Provider |
| 58 | + value={{ |
| 59 | + entityId: relationFromManyFieldDisplayMock.entityId, |
| 60 | + basePathToShowPage: '/object-record/', |
| 61 | + isLabelIdentifier: false, |
| 62 | + fieldDefinition: { |
| 63 | + ...relationFromManyFieldDisplayMock.fieldDefinition, |
| 64 | + } as unknown as FieldDefinition<FieldMetadata>, |
| 65 | + hotkeyScope: 'hotkey-scope', |
| 66 | + }} |
| 67 | + > |
| 68 | + <RelationFieldValueSetterEffect /> |
| 69 | + <Story /> |
| 70 | + </FieldContext.Provider> |
| 71 | + </RecordFieldValueSelectorContextProvider> |
| 72 | + ), |
| 73 | + ComponentDecorator, |
| 74 | + ], |
| 75 | + component: RelationFromManyFieldDisplay, |
| 76 | + argTypes: { value: { control: 'date' } }, |
| 77 | + args: { fieldValue: fieldValue }, |
| 78 | + parameters: { |
| 79 | + chromatic: { disableSnapshot: true }, |
| 80 | + }, |
| 81 | +}; |
| 82 | + |
| 83 | +export default meta; |
| 84 | + |
| 85 | +type Story = StoryObj<typeof RelationFromManyFieldDisplay>; |
| 86 | + |
| 87 | +export const Default: Story = {}; |
| 88 | + |
| 89 | +export const Performance = getProfilingStory({ |
| 90 | + componentName: 'RelationFromManyFieldDisplay', |
| 91 | + averageThresholdInMs: 0.5, |
| 92 | + numberOfRuns: 20, |
| 93 | + numberOfTestsPerRun: 100, |
| 94 | +}); |
0 commit comments