Skip to content

Commit

Permalink
refactor: split state
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs committed Sep 9, 2024
1 parent 86ae0c9 commit cdfa028
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 186 deletions.
2 changes: 1 addition & 1 deletion src/components/config-panel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Redo, Undo } from "lucide-react";
import { useConfig, useUndoManager, useYDoc } from "../state";
import { useConfig, useUndoManager, useYDoc } from "../state/index";
import { fileToYDoc } from "../utils";
import { ConnectButton } from "./connect-button";
import { ExportButton } from "./export-button";
Expand Down
2 changes: 1 addition & 1 deletion src/components/connect-button.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Cable, RotateCw } from "lucide-react";
import { useCallback, useEffect, useState } from "react";
import { ConnectProvider } from "../providers/types";
import { useYDoc } from "../state";
import { useYDoc } from "../state/index";
import { ConnectDialog } from "./connect-dialog";
import { StatusIndicator } from "./status-indicator";
import { Button } from "./ui/button";
Expand Down
2 changes: 1 addition & 1 deletion src/components/connect-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RocketIcon, TriangleAlert } from "lucide-react";
import { useState } from "react";
import * as Y from "yjs";
import { ConnectProvider } from "../providers/types";
import { useYDoc } from "../state";
import { useYDoc } from "../state/index";
import { Alert, AlertDescription, AlertTitle } from "./ui/alert";
import { Button } from "./ui/button";
import {
Expand Down
2 changes: 1 addition & 1 deletion src/components/delete-dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Path } from "@textea/json-viewer";
import { useYDoc } from "../state";
import { useYDoc } from "../state/index";
import { getHumanReadablePath } from "../utils";
import {
getYTypeFromPath,
Expand Down
2 changes: 1 addition & 1 deletion src/components/export-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "@/components/ui/dropdown-menu";
import { Download } from "lucide-react";
import * as Y from "yjs";
import { useYDoc } from "../state";
import { useYDoc } from "../state/index";
import { yShapeToJSON } from "../y-shape";

function downloadFile(blob: Blob, filename: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/filter-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
useIsFilterEnabled,
useSetHasValidFilterRule,
useUpdateFilterPredicate,
} from "../state";
} from "../state/index";
import {
createFlattenFilterGroup,
filterFnList,
Expand Down
2 changes: 1 addition & 1 deletion src/components/load-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from "lucide-react";
import { useState } from "react";
import * as Y from "yjs";
import { useYDoc } from "../state";
import { useYDoc } from "../state/index";
import { fileToYDoc } from "../utils";
import { toast } from "./ui/use-toast";

Expand Down
7 changes: 6 additions & 1 deletion src/components/preview-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { JsonViewer, Path } from "@textea/json-viewer";
import { Bug } from "lucide-react";
import { useEffect, useState } from "react";
import { yDataType } from "../data-types";
import { useConfig, useFilterMap, useIsFilterEnabled, useYDoc } from "../state";
import {
useConfig,
useFilterMap,
useIsFilterEnabled,
useYDoc,
} from "../state/index";
import { getYTypeFromPath, isYArray, isYDoc, isYMap } from "../y-shape";
import { AddDataDialog } from "./add-data-dialog";
import { DeleteDialog } from "./delete-dialog";
Expand Down
2 changes: 1 addition & 1 deletion src/components/status-indicator.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CloudDownload, CloudUpload, Unplug } from "lucide-react";
import { useCallback, useEffect, useRef, useState } from "react";
import { useDownloadListener, useUploadListener } from "../state";
import { useDownloadListener, useUploadListener } from "../state/index";

export function StatusIndicator({ className }: { className?: string }) {
const [status, setStatus] = useState<"download" | "upload" | "none">("none");
Expand Down
2 changes: 1 addition & 1 deletion src/data-types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ComponentType } from "react";
import * as Y from "yjs";
import { Badge } from "./components/ui/badge";
import { toast } from "./components/ui/use-toast";
import { useConfig } from "./state";
import { useConfig } from "./state/index";
import { getYTypeName, isYShape, parseYShape } from "./y-shape";

const TypeLabel = ({ value }: { value: unknown }) => {
Expand Down
176 changes: 0 additions & 176 deletions src/state.ts

This file was deleted.

File renamed without changes.
24 changes: 24 additions & 0 deletions src/state/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useAtom } from "jotai";
import { atomWithStorage } from "jotai/utils";

export type Config = {
parseYDoc: boolean;
showDelta: boolean;
showSize: boolean;
editable: boolean;
};
const defaultConfig = {
parseYDoc: true,
showDelta: true,
showSize: true,
editable: false,
} satisfies Config;

export const configAtom = atomWithStorage<Config>(
"yjs-playground-config",
defaultConfig,
);

export const useConfig = () => {
return useAtom(configAtom);
};
49 changes: 49 additions & 0 deletions src/state/filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { atom, useAtomValue, useSetAtom } from "jotai";
import { YShapeItem } from "../components/filter-sphere";
import { filterYDoc } from "../filter-map";
import { configAtom } from "./config";
import { yDocAtom } from "./ydoc";

const falseFn = () => false;

const filterPredicateAtom = atom<{ fn: (data: YShapeItem) => boolean }>({
fn: falseFn,
});

export const useUpdateFilterPredicate = () => {
const set = useSetAtom(filterPredicateAtom);
return set;
};
const hasValidFilterRuleAtom = atom(false);
const filteredYDocAtom = atom((get) => {
const hasValidFilterRule = get(hasValidFilterRuleAtom);
if (!hasValidFilterRule) {
return {};
}
const yDoc = get(yDocAtom);
const predicate = get(filterPredicateAtom).fn;
const filterMap = filterYDoc(yDoc, predicate);
return filterMap;
});
const filterCountAtom = atom((get) => {
const data = get(filteredYDocAtom);
return Object.keys(data).length;
});

export const useSetHasValidFilterRule = () => {
return useSetAtom(hasValidFilterRuleAtom);
};

export const useFilterMap = () => {
return useAtomValue(filteredYDocAtom);
};

export const useFilterDataCount = () => {
return useAtomValue(filterCountAtom);
};

export const useIsFilterEnabled = () => {
const hasValidFilterRule = useAtomValue(hasValidFilterRuleAtom);
const config = useAtomValue(configAtom);
return config.parseYDoc && hasValidFilterRule;
};
4 changes: 4 additions & 0 deletions src/state/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./config";
export * from "./filter";
export * from "./undo";
export * from "./ydoc";
Loading

0 comments on commit cdfa028

Please sign in to comment.