Skip to content

Commit

Permalink
refactor: tidy up implementation after performance changes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Feb 27, 2025
1 parent 751a5f6 commit d65e62c
Show file tree
Hide file tree
Showing 45 changed files with 1,284 additions and 1,133 deletions.
2 changes: 1 addition & 1 deletion apps/docs/components/Preview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const ConfigPreviewInner = ({
{componentConfig.render && (
<div className={getClassNameConfigPreview("preview")}>
{componentConfig.render({
...appState.data["content"][0].props,
...appState.data["content"][0]?.props,
puck: { renderDropZone: () => <div />, isEditing: false },
})}
</div>
Expand Down
15 changes: 8 additions & 7 deletions packages/core/components/AutoField/fields/ArrayField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import { reorder, replace } from "../../../../lib";
import { useCallback, useEffect, useState } from "react";
import { DragIcon } from "../../../DragIcon";
import { ArrayState, ItemWithId } from "../../../../types";
import { getAppStore, useAppStore } from "../../../../stores/app-store";
import { useAppStore, useAppStoreApi } from "../../../../store";
import { Sortable, SortableProvider } from "../../../Sortable";
import { NestedFieldProvider, useNestedFieldContext } from "../../context";
import { usePermissionsStore } from "../../../../stores/permissions-store";

const getClassName = getClassNameFactory("ArrayField", styles);
const getClassNameItem = getClassNameFactory("ArrayFieldItem", styles);
Expand Down Expand Up @@ -47,17 +46,19 @@ export const ArrayField = ({
setLocalState({ arrayState, value });
}, [value, thisArrayState]);

const appStore = useAppStoreApi();

const mapArrayStateToUi = useCallback(
(partialArrayState: Partial<ArrayState>) => {
const state = getAppStore().state;
const state = appStore.getState().state;
return {
arrayState: {
...state.ui.arrayState,
[id]: { ...arrayState, ...partialArrayState },
},
};
},
[arrayState]
[arrayState, appStore]
);

const getHighestIndex = useCallback(() => {
Expand Down Expand Up @@ -105,8 +106,8 @@ export const ArrayField = ({

const [isDragging, setIsDragging] = useState(false);

const canEdit = usePermissionsStore(
(s) => s.getPermissions({ item: getAppStore().selectedItem }).edit
const canEdit = useAppStore(
(s) => s.permissions.getPermissions({ item: s.selectedItem }).edit
);

const forceReadOnly = !canEdit;
Expand Down Expand Up @@ -138,7 +139,7 @@ export const ArrayField = ({
move.target
);

const state = getAppStore().state;
const state = appStore.getState().state;

const newUi = {
arrayState: {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/components/AutoField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from "./fields";
import { Lock } from "lucide-react";
import { ObjectField } from "./fields/ObjectField";
import { useAppStore } from "../../stores/app-store";
import { useAppStore } from "../../store";
import { useSafeId } from "../../lib/use-safe-id";
import { NestedFieldContext } from "./context";

Expand Down
7 changes: 3 additions & 4 deletions packages/core/components/ComponentList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import styles from "./styles.module.css";
import getClassNameFactory from "../../lib/get-class-name-factory";
import { ReactNode } from "react";
import { useAppStore } from "../../stores/app-store";
import { useAppStore } from "../../store";
import { ChevronDown, ChevronUp } from "lucide-react";
import { Drawer } from "../Drawer";
import { usePermissionsStore } from "../../stores/permissions-store";

const getClassName = getClassNameFactory("ComponentList", styles);

Expand All @@ -17,9 +16,9 @@ const ComponentListItem = ({
index?: number; // TODO deprecate
}) => {
const overrides = useAppStore((s) => s.overrides);
const canInsert = usePermissionsStore(
const canInsert = useAppStore(
(s) =>
s.getPermissions({
s.permissions.getPermissions({
type: name,
}).insert
);
Expand Down
13 changes: 7 additions & 6 deletions packages/core/components/DragDropContext/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DragDropProvider } from "@dnd-kit/react";
import { getAppStore, useAppStore } from "../../stores/app-store";
import { useAppStore, useAppStoreApi } from "../../store";
import {
createContext,
Dispatch,
Expand Down Expand Up @@ -29,7 +29,6 @@ import { generateId } from "../../lib/generate-id";
import { createStore } from "zustand";
import { getDeepDir } from "../../lib/get-deep-dir";
import { useSensors } from "../../lib/dnd/use-sensors";
import { useNodeStore } from "../../stores/node-store";

const DEBUG = false;

Expand Down Expand Up @@ -108,6 +107,7 @@ const DragDropContextClient = ({
const dispatch = useAppStore((s) => s.dispatch);
const resolveData = useAppStore((s) => s.resolveData);
const metadata = useAppStore((s) => s.metadata);
const appStore = useAppStoreApi();

const id = useId();

Expand Down Expand Up @@ -331,7 +331,7 @@ const DragDropContextClient = ({
if (thisPreview) {
zoneStore.setState({ previewIndex: {} });

const state = getAppStore().state;
const state = appStore.getState().state;

if (thisPreview.type === "insert") {
insertComponent(
Expand Down Expand Up @@ -427,7 +427,7 @@ const DragDropContextClient = ({
targetIndex = 0;
}

const path = useNodeStore.getState().nodes[target.id]?.path || [];
const path = appStore.getState().nodes.nodes[target.id]?.path || [];

// Abort if dragging over self or descendant
if (
Expand Down Expand Up @@ -464,7 +464,8 @@ const DragDropContextClient = ({

const item = getItem(
initialSelector.current,
getAppStore().state.data
appStore.getState().state.data,
{}
);

if (item) {
Expand Down Expand Up @@ -496,7 +497,7 @@ const DragDropContextClient = ({

if (source && source.type !== "void") {
const sourceData = source.data as ComponentDndData;
const { data } = getAppStore().state;
const { data } = appStore.getState().state;

const item = getItem(
{
Expand Down
18 changes: 9 additions & 9 deletions packages/core/components/DraggableComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import styles from "./styles.module.css";
import "./styles.css";
import getClassNameFactory from "../../lib/get-class-name-factory";
import { Copy, CornerLeftUp, Trash } from "lucide-react";
import { useAppStore } from "../../stores/app-store";
import { useAppStore, useAppStoreApi } from "../../store";
import { Loader } from "../Loader";
import { ActionBar } from "../ActionBar";

Expand All @@ -28,8 +28,6 @@ import { useSortableSafe } from "../../lib/dnd/dnd-kit/safe";
import { getDeepScrollPosition } from "../../lib/get-deep-scroll-position";
import { ZoneStoreContext } from "../DropZone/context";
import { useContextStore } from "../../lib/use-context-store";
import { useNodeStore } from "../../stores/node-store";
import { usePermissionsStore } from "../../stores/permissions-store";
import { useShallow } from "zustand/react/shallow";

const getClassName = getClassNameFactory("DraggableComponent", styles);
Expand Down Expand Up @@ -150,12 +148,12 @@ export const DraggableComponent = ({
const containsActiveZone =
Object.values(localZones).filter(Boolean).length > 0;

const path = useNodeStore((s) => s.nodes[id]?.path);
const path = useAppStore((s) => s.nodes.nodes[id]?.path);

const item = useNodeStore((s) => s.nodes[id]?.data);
const item = useAppStore((s) => s.nodes.nodes[id]?.data);

const permissions = usePermissionsStore(
useShallow((s) => s.getPermissions({ item }))
const permissions = useAppStore(
useShallow((s) => s.permissions.getPermissions({ item }))
);

const userIsDragging = useContextStore(
Expand Down Expand Up @@ -273,7 +271,7 @@ export const DraggableComponent = ({
}
}, [ref.current, userIsDragging]);

const registerNode = useNodeStore((s) => s.registerNode);
const registerNode = useAppStore((s) => s.nodes.registerNode);

useEffect(() => {
registerNode(id, { methods: { sync }, element: ref.current ?? null });
Expand Down Expand Up @@ -302,8 +300,10 @@ export const DraggableComponent = ({
[index, zoneCompound, id]
);

const appStore = useAppStoreApi();

const onSelectParent = useCallback(() => {
const { nodes } = useNodeStore.getState();
const { nodes } = appStore.getState().nodes;
const node = nodes[id];
const parentNode = node?.parentId ? nodes[node?.parentId] : null;

Expand Down
2 changes: 1 addition & 1 deletion packages/core/components/DropZone/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
useState,
} from "react";
import type { Draggable } from "@dnd-kit/dom";
import { useAppStore } from "../../stores/app-store";
import { useAppStore } from "../../store";
import { createStore, StoreApi } from "zustand";

export type PathData = Record<string, { path: string[]; label: string }>;
Expand Down
7 changes: 4 additions & 3 deletions packages/core/components/DropZone/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ZoneStoreContext,
dropZoneContext,
} from "./context";
import { useAppStore } from "../../stores/app-store";
import { useAppStore } from "../../store";
import { DropZoneProps } from "./types";
import { Content, DragAxis, PuckContext } from "../../types";

Expand All @@ -33,7 +33,6 @@ import { useContentIdsWithPreview } from "./lib/use-content-with-preview";
import { useDragAxis } from "./lib/use-drag-axis";
import { useContextStore } from "../../lib/use-context-store";
import { useShallow } from "zustand/react/shallow";
import { useNodeStore } from "../../stores/node-store";
import { renderContext } from "../Render";

const getClassName = getClassNameFactory("DropZone", styles);
Expand Down Expand Up @@ -217,7 +216,9 @@ const DropZoneEdit = forwardRef<HTMLDivElement, DropZoneProps>(
activeZones,
} = ctx!;

const path = useNodeStore((s) => (areaId ? s.nodes[areaId]?.path : null));
const path = useAppStore((s) =>
areaId ? s.nodes.nodes[areaId]?.path : null
);

let zoneCompound = rootDroppableId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useRenderedCallback } from "../../../lib/dnd/use-rendered-callback";
import { insert } from "../../../lib/insert";
import { ZoneStoreContext } from "../context";
import { useContextStore } from "../../../lib/use-context-store";
import { useAppStore } from "../../../stores/app-store";
import { useAppStore } from "../../../store";

export const useContentIdsWithPreview = (
contentIds: string[],
Expand Down
2 changes: 1 addition & 1 deletion packages/core/components/DropZone/lib/use-drag-axis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RefObject, useCallback, useEffect, useState } from "react";
import { DragAxis } from "../../../types";
import { useAppStore } from "../../../stores/app-store";
import { useAppStore } from "../../../store";

const GRID_DRAG_AXIS: DragAxis = "dynamic";
const FLEX_ROW_DRAG_AXIS: DragAxis = "x";
Expand Down
5 changes: 3 additions & 2 deletions packages/core/components/DropZone/lib/use-min-empty-height.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RefObject, useEffect, useState } from "react";
import { ZoneStoreContext } from "./../context";
import { useContextStore } from "../../../lib/use-context-store";
import { useNodeStore } from "../../../stores/node-store";
import { useAppStoreApi } from "../../../store";

export const useMinEmptyHeight = ({
zoneCompound,
Expand All @@ -12,6 +12,7 @@ export const useMinEmptyHeight = ({
userMinEmptyHeight: number;
ref: RefObject<HTMLDivElement | null>;
}) => {
const appStore = useAppStoreApi();
const [prevHeight, setPrevHeight] = useState(0);
const [isAnimating, setIsAnimating] = useState(false);
const { draggedItem, isZone } = useContextStore(ZoneStoreContext, (s) => {
Expand All @@ -36,7 +37,7 @@ export const useMinEmptyHeight = ({

setPrevHeight(0);
setTimeout(() => {
const { nodes } = useNodeStore.getState();
const { nodes } = appStore.getState().nodes;

Object.entries(nodes).forEach(([_, node]) => {
node?.methods.sync();
Expand Down
4 changes: 2 additions & 2 deletions packages/core/components/LayerTree/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { findZonesForArea } from "../../lib/find-zones-for-area";
import { getZoneId } from "../../lib/get-zone-id";
import { getFrame } from "../../lib/get-frame";
import { onScrollEnd } from "../../lib/on-scroll-end";
import { useNodeStore } from "../../stores/node-store";
import { useAppStore } from "../../store";

const getClassName = getClassNameFactory("LayerTree", styles);
const getClassNameLayer = getClassNameFactory("Layer", styles);
Expand All @@ -37,7 +37,7 @@ export const LayerTree = ({
const ctx = useContext(dropZoneContext);

// TODO change this for performance
const nodes = useNodeStore((s) => s.nodes);
const nodes = useAppStore((s) => s.nodes.nodes);

return (
<>
Expand Down
10 changes: 5 additions & 5 deletions packages/core/components/MenuBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PuckAction } from "../../reducer";
import type { Data } from "../../types";

import styles from "./styles.module.css";
import { useHistoryStore } from "../../stores/history-store";
import { useAppStore } from "../../store";

const getClassName = getClassNameFactory("MenuBar", styles);

Expand All @@ -22,10 +22,10 @@ export function MenuBar<UserData extends Data>({
renderHeaderActions?: () => ReactElement;
setMenuOpen: Dispatch<SetStateAction<boolean>>;
}) {
const back = useHistoryStore((s) => s.back);
const forward = useHistoryStore((s) => s.forward);
const hasFuture = useHistoryStore((s) => s.hasFuture());
const hasPast = useHistoryStore((s) => s.hasPast());
const back = useAppStore((s) => s.history.back);
const forward = useAppStore((s) => s.history.forward);
const hasFuture = useAppStore((s) => s.history.hasFuture());
const hasPast = useAppStore((s) => s.history.hasPast());

return (
<div
Expand Down
2 changes: 1 addition & 1 deletion packages/core/components/Puck/components/Canvas/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
useRef,
useState,
} from "react";
import { useAppStore } from "../../../../stores/app-store";
import { useAppStore } from "../../../../store";
import { ViewportControls } from "../../../ViewportControls";
import styles from "./styles.module.css";
import { getClassNameFactory } from "../../../../lib";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useComponentList } from "../../../../lib/use-component-list";
import { useAppStore } from "../../../../stores/app-store";
import { useAppStore } from "../../../../store";
import { ComponentList } from "../../../ComponentList";
import { useMemo } from "react";

Expand Down
Loading

0 comments on commit d65e62c

Please sign in to comment.