Refactor/context and scopes (#1602)
* Put onImport in a context * Refactored RecoilScopeContexts * Refactored naming * Fix tests --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -1,23 +1,25 @@
|
||||
import { Context, useRef } from 'react';
|
||||
import { useRef } from 'react';
|
||||
import { v4 } from 'uuid';
|
||||
|
||||
import { type RecoilScopeContext as RecoilScopeContextType } from '@/types/RecoilScopeContext';
|
||||
|
||||
import { RecoilScopeContext } from '../states/RecoilScopeContext';
|
||||
|
||||
export function RecoilScope({
|
||||
children,
|
||||
scopeId,
|
||||
SpecificContext,
|
||||
CustomRecoilScopeContext,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
scopeId?: string;
|
||||
SpecificContext?: Context<string | null>;
|
||||
CustomRecoilScopeContext?: RecoilScopeContextType;
|
||||
}) {
|
||||
const currentScopeId = useRef(scopeId ?? v4());
|
||||
|
||||
return SpecificContext ? (
|
||||
<SpecificContext.Provider value={currentScopeId.current}>
|
||||
return CustomRecoilScopeContext ? (
|
||||
<CustomRecoilScopeContext.Provider value={currentScopeId.current}>
|
||||
{children}
|
||||
</SpecificContext.Provider>
|
||||
</CustomRecoilScopeContext.Provider>
|
||||
) : (
|
||||
<RecoilScopeContext.Provider value={currentScopeId.current}>
|
||||
{children}
|
||||
|
||||
@ -1,12 +0,0 @@
|
||||
import { Context, useContext } from 'react';
|
||||
|
||||
export function useContextScopeId(SpecificContext: Context<string | null>) {
|
||||
const recoilScopeId = useContext(SpecificContext);
|
||||
|
||||
if (!recoilScopeId)
|
||||
throw new Error(
|
||||
`Using useContextScopedId outside of the specified context : ${SpecificContext.displayName}, verify that you are using a RecoilScope with the specific context you want to use.`,
|
||||
);
|
||||
|
||||
return recoilScopeId;
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
import { useContext } from 'react';
|
||||
|
||||
import { RecoilScopeContext } from '@/types/RecoilScopeContext';
|
||||
|
||||
export function useRecoilScopeId(RecoilScopeContext: RecoilScopeContext) {
|
||||
const recoilScopeId = useContext(RecoilScopeContext);
|
||||
|
||||
if (!recoilScopeId)
|
||||
throw new Error(
|
||||
`Using useRecoilScopeId outside of the specified context : ${RecoilScopeContext.displayName}, verify that you are using a RecoilScope with the specific context you want to use.`,
|
||||
);
|
||||
|
||||
return recoilScopeId;
|
||||
}
|
||||
@ -6,9 +6,11 @@ import { RecoilScopeContext } from '../states/RecoilScopeContext';
|
||||
export function useRecoilScopedFamilyState<StateType>(
|
||||
recoilState: (familyUniqueId: string) => RecoilState<StateType>,
|
||||
uniqueIdInRecoilScope: string,
|
||||
SpecificContext?: Context<string | null>,
|
||||
CustomRecoilScopeContext?: Context<string | null>,
|
||||
) {
|
||||
const recoilScopeId = useContext(SpecificContext ?? RecoilScopeContext);
|
||||
const recoilScopeId = useContext(
|
||||
CustomRecoilScopeContext ?? RecoilScopeContext,
|
||||
);
|
||||
|
||||
if (!recoilScopeId)
|
||||
throw new Error(
|
||||
|
||||
@ -5,9 +5,11 @@ import { RecoilScopeContext } from '../states/RecoilScopeContext';
|
||||
|
||||
export function useRecoilScopedState<StateType>(
|
||||
recoilState: (param: string) => RecoilState<StateType>,
|
||||
SpecificContext?: Context<string | null>,
|
||||
CustomRecoilScopeContext?: Context<string | null>,
|
||||
) {
|
||||
const recoilScopeId = useContext(SpecificContext ?? RecoilScopeContext);
|
||||
const recoilScopeId = useContext(
|
||||
CustomRecoilScopeContext ?? RecoilScopeContext,
|
||||
);
|
||||
|
||||
if (!recoilScopeId)
|
||||
throw new Error(
|
||||
|
||||
@ -5,9 +5,11 @@ import { RecoilScopeContext } from '../states/RecoilScopeContext';
|
||||
|
||||
export function useRecoilScopedValue<T>(
|
||||
recoilState: (param: string) => RecoilState<T> | RecoilValueReadOnly<T>,
|
||||
SpecificContext?: Context<string | null>,
|
||||
CustomRecoilScopeContext?: Context<string | null>,
|
||||
) {
|
||||
const recoilScopeId = useContext(SpecificContext ?? RecoilScopeContext);
|
||||
const recoilScopeId = useContext(
|
||||
CustomRecoilScopeContext ?? RecoilScopeContext,
|
||||
);
|
||||
|
||||
if (!recoilScopeId)
|
||||
throw new Error(
|
||||
|
||||
Reference in New Issue
Block a user