Clean and re-organize post table refactoring (#1000)

* Clean and re-organize post table refactoring

* Fix tests
This commit is contained in:
Charles Bochet
2023-07-30 18:26:32 -07:00
committed by GitHub
parent 86a2d67efd
commit ade5e52e55
336 changed files with 638 additions and 2757 deletions
@@ -0,0 +1,24 @@
import { Context, useRef } from 'react';
import { v4 } from 'uuid';
import { RecoilScopeContext } from '../states/RecoilScopeContext';
export function RecoilScope({
SpecificContext,
children,
}: {
SpecificContext?: Context<string | null>;
children: React.ReactNode;
}) {
const currentScopeId = useRef(v4());
return SpecificContext ? (
<SpecificContext.Provider value={currentScopeId.current}>
{children}
</SpecificContext.Provider>
) : (
<RecoilScopeContext.Provider value={currentScopeId.current}>
{children}
</RecoilScopeContext.Provider>
);
}
@@ -0,0 +1,12 @@
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,20 @@
import { Context, useContext } from 'react';
import { RecoilState, useRecoilState } from 'recoil';
import { RecoilScopeContext } from '../states/RecoilScopeContext';
export function useRecoilScopedState<StateType>(
recoilState: (param: string) => RecoilState<StateType>,
SpecificContext?: Context<string | null>,
) {
const recoilScopeId = useContext(SpecificContext ?? RecoilScopeContext);
if (!recoilScopeId)
throw new Error(
`Using a scoped atom without a RecoilScope : ${
recoilState('').key
}, verify that you are using a RecoilScope with a specific context if you intended to do so.`,
);
return useRecoilState<StateType>(recoilState(recoilScopeId));
}
@@ -0,0 +1,20 @@
import { Context, useContext } from 'react';
import { RecoilState, useRecoilValue } from 'recoil';
import { RecoilScopeContext } from '../states/RecoilScopeContext';
export function useRecoilScopedValue<T>(
recoilState: (param: string) => RecoilState<T>,
SpecificContext?: Context<string | null>,
) {
const recoilScopeId = useContext(SpecificContext ?? RecoilScopeContext);
if (!recoilScopeId)
throw new Error(
`Using a scoped atom without a RecoilScope : ${
recoilState('').key
}, verify that you are using a RecoilScope with a specific context if you intended to do so.`,
);
return useRecoilValue<T>(recoilState(recoilScopeId));
}
@@ -0,0 +1,3 @@
import { createContext } from 'react';
export const RecoilScopeContext = createContext<string | null>(null);
@@ -0,0 +1,13 @@
import { RecoilState, Snapshot } from 'recoil';
export function getSnapshotScopedState<T>({
snapshot,
state,
contextScopeId,
}: {
snapshot: Snapshot;
state: (scopeId: string) => RecoilState<T>;
contextScopeId: string;
}) {
return snapshot.getLoadable(state(contextScopeId)).valueOrThrow();
}
@@ -0,0 +1,11 @@
import { RecoilState, Snapshot } from 'recoil';
export function getSnapshotState<T>({
snapshot,
state,
}: {
snapshot: Snapshot;
state: RecoilState<T>;
}) {
return snapshot.getLoadable(state).valueOrThrow();
}