feat: wip record table view group

This commit is contained in:
Jérémy Magrin 2024-10-24 17:57:14 +02:00
parent c7d460324e
commit 5b9a6a1648
8 changed files with 71 additions and 24 deletions

View File

@ -1,25 +1,26 @@
import { RecordGroupTableInstanceContext } from '@/object-record/record-group/states/contexts/RecordGroupTableInstanceContext';
import { recordGroupDefinitionsComponentState } from '@/object-record/record-group/states/recordGroupDefinitionsComponentState';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { useMemo } from 'react';
import { useContext, useMemo } from 'react';
import { isDefined } from '~/utils/isDefined';
export const useRecordGroupDefintion = () => {
const recordGroupDefinitions = useRecoilComponentValueV2(
recordGroupDefinitionsComponentState,
);
// const currentViewContext = useContext(RecordGroupTableInstanceContext);
const currentViewContext = useContext(RecordGroupTableInstanceContext);
// if (!isDefined(currentViewContext)) {
// throw new Error('Current view context is not defined');
// }
const recordGroupDefinition = useMemo(() => {
if (!isDefined(currentViewContext)) {
return null;
}
const recordGroupDefinition = useMemo(
() =>
recordGroupDefinitions.find(
(recordGroupDefinition) => recordGroupDefinition.id === 'TODO',
),
[recordGroupDefinitions],
);
return recordGroupDefinitions.find(
(recordGroupDefinition) =>
recordGroupDefinition.id === currentViewContext.instanceId,
);
}, [recordGroupDefinitions, currentViewContext]);
return recordGroupDefinition;
};

View File

@ -1,8 +1,12 @@
import { RecordGroupTableInstanceContext } from '@/object-record/record-group/states/contexts/RecordGroupTableInstanceContext';
import { RecordTableScopeInternalContext } from '@/object-record/record-table/scopes/scope-internal-context/RecordTableScopeInternalContext';
import { createComponentStateV2 } from '@/ui/utilities/state/component-state/utils/createComponentStateV2';
export const tableRowIdsComponentState = createComponentStateV2<string[]>({
key: 'tableRowIdsComponentState',
defaultValue: [],
componentInstanceContext: RecordTableScopeInternalContext,
componentInstanceContext: [
RecordTableScopeInternalContext,
RecordGroupTableInstanceContext,
],
});

View File

@ -1,16 +1,26 @@
import { useComponentInstanceStateContext } from '@/ui/utilities/state/component-state/hooks/useComponentInstanceStateContext';
import { ComponentInstanceStateContext } from '@/ui/utilities/state/component-state/types/ComponentInstanceStateContext';
import { isNonEmptyString } from '@sniptt/guards';
import { FixedLengthArray } from 'type-fest';
export const useAvailableComponentInstanceIdOrThrow = <
T extends { instanceId: string },
>(
Context: ComponentInstanceStateContext<T>,
Context:
| FixedLengthArray<ComponentInstanceStateContext<any>, 2>
| ComponentInstanceStateContext<T>,
instanceIdFromProps?: string,
): string => {
const instanceStateContext = useComponentInstanceStateContext(Context);
const firstInstanceStateContext = useComponentInstanceStateContext(
Array.isArray(Context) ? Context[0] : Context,
);
const secondInstanceStateContext = useComponentInstanceStateContext(
Array.isArray(Context) ? Context[1] : Context,
);
const instanceIdFromContext = instanceStateContext?.instanceId;
const instanceIdFromContext =
secondInstanceStateContext?.instanceId ??
firstInstanceStateContext?.instanceId;
if (isNonEmptyString(instanceIdFromProps)) {
return instanceIdFromProps;

View File

@ -8,6 +8,7 @@ import { globalComponentInstanceContextMap } from '@/ui/utilities/state/componen
import { SelectorGetter } from '@/ui/utilities/state/types/SelectorGetter';
import { SelectorSetter } from '@/ui/utilities/state/types/SelectorSetter';
import { isDefined } from 'twenty-ui';
import { FixedLengthArray } from 'type-fest';
export const createComponentFamilySelectorV2 = <
ValueType,
@ -21,7 +22,10 @@ export const createComponentFamilySelectorV2 = <
key: string;
get: SelectorGetter<ValueType, ComponentFamilyStateKeyV2<FamilyKey>>;
set?: SelectorSetter<ValueType, ComponentFamilyStateKeyV2<FamilyKey>>;
componentInstanceContext: ComponentInstanceStateContext<any> | null;
componentInstanceContext:
| FixedLengthArray<ComponentInstanceStateContext<any>, 2>
| ComponentInstanceStateContext<any>
| null;
}):
| ComponentFamilySelectorV2<ValueType, FamilyKey>
| ComponentFamilyReadOnlySelectorV2<ValueType, FamilyKey> => {

View File

@ -5,11 +5,15 @@ import { globalComponentInstanceContextMap } from '@/ui/utilities/state/componen
import { AtomEffect, atomFamily, SerializableParam } from 'recoil';
import { isDefined } from 'twenty-ui';
import { FixedLengthArray } from 'type-fest';
type CreateComponentFamilyStateArgs<ValueType> = {
key: string;
defaultValue: ValueType;
componentInstanceContext: ComponentInstanceStateContext<any> | null;
componentInstanceContext:
| FixedLengthArray<ComponentInstanceStateContext<any>, 2>
| ComponentInstanceStateContext<any>
| null;
effects?: AtomEffect<ValueType>[];
};

View File

@ -10,18 +10,25 @@ import { globalComponentInstanceContextMap } from '@/ui/utilities/state/componen
import { SelectorGetter } from '@/ui/utilities/state/types/SelectorGetter';
import { SelectorSetter } from '@/ui/utilities/state/types/SelectorSetter';
import { isDefined } from 'twenty-ui';
import { FixedLengthArray } from 'type-fest';
export function createComponentSelectorV2<ValueType>(options: {
key: string;
get: SelectorGetter<ValueType, ComponentStateKeyV2>;
componentInstanceContext: ComponentInstanceStateContext<any> | null;
componentInstanceContext:
| FixedLengthArray<ComponentInstanceStateContext<any>, 2>
| ComponentInstanceStateContext<any>
| null;
}): ComponentReadOnlySelectorV2<ValueType>;
export function createComponentSelectorV2<ValueType>(options: {
key: string;
get: SelectorGetter<ValueType, ComponentStateKeyV2>;
set: SelectorSetter<ValueType, ComponentStateKeyV2>;
componentInstanceContext: ComponentInstanceStateContext<any> | null;
componentInstanceContext:
| FixedLengthArray<ComponentInstanceStateContext<any>, 2>
| ComponentInstanceStateContext<any>
| null;
}): ComponentSelectorV2<ValueType>;
export function createComponentSelectorV2<ValueType>({
@ -33,7 +40,10 @@ export function createComponentSelectorV2<ValueType>({
key: string;
get: SelectorGetter<ValueType, ComponentStateKeyV2>;
set?: SelectorSetter<ValueType, ComponentStateKeyV2>;
componentInstanceContext: ComponentInstanceStateContext<any> | null;
componentInstanceContext:
| FixedLengthArray<ComponentInstanceStateContext<any>, 2>
| ComponentInstanceStateContext<any>
| null;
}): ComponentSelectorV2<ValueType> | ComponentReadOnlySelectorV2<ValueType> {
if (isDefined(componentInstanceContext)) {
globalComponentInstanceContextMap.set(key, componentInstanceContext);

View File

@ -3,13 +3,17 @@ import { ComponentStateKeyV2 } from '@/ui/utilities/state/component-state/types/
import { ComponentStateV2 } from '@/ui/utilities/state/component-state/types/ComponentStateV2';
import { globalComponentInstanceContextMap } from '@/ui/utilities/state/component-state/utils/globalComponentInstanceContextMap';
import { AtomEffect, atomFamily } from 'recoil';
import { FixedLengthArray } from 'type-fest';
import { isDefined } from '~/utils/isDefined';
type CreateComponentInstanceStateArgs<ValueType> = {
key: string;
defaultValue: ValueType;
componentInstanceContext: ComponentInstanceStateContext<any> | null;
componentInstanceContext:
| FixedLengthArray<ComponentInstanceStateContext<any>, 2>
| ComponentInstanceStateContext<any>
| null;
effects?: AtomEffect<ValueType>[];
};

View File

@ -1,5 +1,6 @@
import { ComponentInstanceStateContext } from '@/ui/utilities/state/component-state/types/ComponentInstanceStateContext';
import { isDefined } from 'twenty-ui';
import { FixedLengthArray } from 'type-fest';
class ComponentInstanceContextMap {
constructor() {
@ -8,11 +9,20 @@ class ComponentInstanceContextMap {
}
}
public get(key: string): ComponentInstanceStateContext<any> {
public get(
key: string,
):
| FixedLengthArray<ComponentInstanceStateContext<any>, 2>
| ComponentInstanceStateContext<any> {
return (window as any).componentComponentStateContextMap.get(key);
}
public set(key: string, context: ComponentInstanceStateContext<any>) {
public set(
key: string,
context:
| FixedLengthArray<ComponentInstanceStateContext<any>, 2>
| ComponentInstanceStateContext<any>,
) {
(window as any).componentComponentStateContextMap.set(key, context);
}
}