Table page: fix tableSettings not updating

This commit is contained in:
Sidney Alcantara
2022-10-11 13:28:11 +11:00
parent c2d7bb6527
commit 63d5bc0d22
2 changed files with 36 additions and 2 deletions

View File

@@ -1,4 +1,8 @@
import { useEffect } from "react";
import { useSetAtom } from "jotai";
import { useAtomsDebugValue } from "jotai/devtools";
import useMemoValue from "use-memo-value";
import { isEqual } from "lodash-es";
export function DebugAtoms(
options: NonNullable<Parameters<typeof useAtomsDebugValue>[0]>
@@ -6,3 +10,26 @@ export function DebugAtoms(
useAtomsDebugValue(options);
return null;
}
/**
* Sets an atoms value when the `value` prop changes.
* Useful when setting an atoms initialValue and you want to keep it in sync.
*/
export function SyncAtomValue<T>({
atom,
scope,
value,
}: {
atom: Parameters<typeof useSetAtom>[0];
scope: Parameters<typeof useSetAtom>[1];
value: T;
}) {
const memoized = useMemoValue(value, isEqual);
const setAtom = useSetAtom(atom, scope);
useEffect(() => {
setAtom(memoized);
}, [setAtom, memoized]);
return null;
}

View File

@@ -1,4 +1,4 @@
import { Suspense, useMemo } from "react";
import { Suspense } from "react";
import { useAtom, Provider } from "jotai";
import { DebugAtoms } from "@src/atoms/utils";
import { useParams, useOutlet } from "react-router-dom";
@@ -25,6 +25,7 @@ import {
tableIdAtom,
tableSettingsAtom,
} from "@src/atoms/tableScope";
import { SyncAtomValue } from "@src/atoms/utils";
import useDocumentTitle from "@src/hooks/useDocumentTitle";
/**
@@ -39,7 +40,7 @@ export default function ProvidedTablePage() {
const [projectSettings] = useAtom(projectSettingsAtom, projectScope);
const [tables] = useAtom(tablesAtom, projectScope);
const tableSettings = useMemo(() => find(tables, ["id", id]), [tables, id]);
const tableSettings = find(tables, ["id", id]);
useDocumentTitle(projectId, tableSettings ? tableSettings.name : "Not found");
if (!tableSettings) {
@@ -75,6 +76,12 @@ export default function ProvidedTablePage() {
]}
>
<DebugAtoms scope={tableScope} />
<SyncAtomValue
atom={tableSettingsAtom}
scope={tableScope}
value={tableSettings}
/>
<TableSourceFirestore />
<Suspense
fallback={