Skip to content

Commit

Permalink
refactor: don't record state history by default
Browse files Browse the repository at this point in the history
Recording state history by default destroys the forward history if you rewind and click on something
  • Loading branch information
chrisvxd committed Oct 16, 2023
1 parent bb4b386 commit 28920a2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
5 changes: 4 additions & 1 deletion packages/core/components/Puck/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ export function Puck({

const setItemSelector = useCallback(
(newItemSelector: ItemSelector | null) => {
dispatch({ type: "setState", state: { itemSelector: newItemSelector } });
dispatch({
type: "setState",
state: { itemSelector: newItemSelector },
});
},
[]
);
Expand Down
5 changes: 3 additions & 2 deletions packages/core/reducer/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export type UnregisterZoneAction = {
zone: string;
};

export type PuckAction =
export type PuckAction = { recordHistory?: boolean } & (
| ReorderAction
| InsertAction
| MoveAction
Expand All @@ -77,4 +77,5 @@ export type PuckAction =
| SetDataAction
| SetStateAction
| RegisterZoneAction
| UnregisterZoneAction;
| UnregisterZoneAction
);
18 changes: 13 additions & 5 deletions packages/core/reducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ export type ActionType = "insert" | "reorder";
export type StateReducer = Reducer<AppData, PuckAction>;

const storeInterceptor = (reducer: StateReducer) => {
return (data, action) => {
const newAppData = reducer(data, action);
return (appData: AppData, action: PuckAction) => {
const newAppData = reducer(appData, action);

const isValidType = ![
"registerZone",
"unregisterZone",
"setData",
"setState",
"set",
].includes(action.type);

if (
!["registerZone", "unregisterZone", "setData", "set"].includes(
action.type
)
typeof action.recordHistory !== "undefined"
? action.recordHistory
: isValidType
) {
recordDiff(newAppData);
}
Expand Down

0 comments on commit 28920a2

Please sign in to comment.