Skip to content

Commit

Permalink
feat: rename history.data to history.state (breaking change)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Sep 9, 2024
1 parent d917229 commit b09244c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions packages/core/components/Puck/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,12 @@ export function Puck<
const initialHistory: InitialHistory<AppState> = {
histories: [
...(_initialHistory?.histories || []),
{ data: generatedAppState },
{ state: generatedAppState },
],
index: _initialHistory?.index || 0,
};

const initialAppState = initialHistory?.histories[initialHistory.index].data;
const initialAppState = initialHistory?.histories[initialHistory.index].state;

const historyStore = useHistoryStore(initialHistory);

Expand Down
38 changes: 19 additions & 19 deletions packages/core/lib/__tests__/use-history-store.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("use-history-store", () => {
renderedHook = renderHook(() =>
useHistoryStore({
index: 0,
histories: [{ data: "Strawberries", id: "initial" }],
histories: [{ state: "Strawberries", id: "initial" }],
})
);
});
Expand All @@ -21,7 +21,7 @@ describe("use-history-store", () => {
expect(renderedHook.result.current.hasPast).toBe(false);
expect(renderedHook.result.current.hasFuture).toBe(false);
expect(renderedHook.result.current.histories.length).toBe(1);
expect(renderedHook.result.current.histories[0].data).toBe("Strawberries");
expect(renderedHook.result.current.histories[0].state).toBe("Strawberries");
});

test("should record the history", () => {
Expand All @@ -31,9 +31,9 @@ describe("use-history-store", () => {
expect(renderedHook.result.current.hasPast).toBe(true);
expect(renderedHook.result.current.hasFuture).toBe(false);
expect(renderedHook.result.current.histories.length).toBe(3);
expect(renderedHook.result.current.histories[1].data).toBe("Apples");
expect(renderedHook.result.current.histories[2].data).toBe("Oranges");
expect(renderedHook.result.current.currentHistory.data).toBe("Oranges");
expect(renderedHook.result.current.histories[1].state).toBe("Apples");
expect(renderedHook.result.current.histories[2].state).toBe("Oranges");
expect(renderedHook.result.current.currentHistory.state).toBe("Oranges");
});

test("should enable partial rewinds through history", () => {
Expand All @@ -43,7 +43,7 @@ describe("use-history-store", () => {

expect(renderedHook.result.current.hasPast).toBe(true);
expect(renderedHook.result.current.hasFuture).toBe(true);
expect(renderedHook.result.current.currentHistory.data).toBe("Apples");
expect(renderedHook.result.current.currentHistory.state).toBe("Apples");
});

test("should enable full rewinds through history", () => {
Expand All @@ -54,7 +54,7 @@ describe("use-history-store", () => {

expect(renderedHook.result.current.hasPast).toBe(false);
expect(renderedHook.result.current.hasFuture).toBe(true);
expect(renderedHook.result.current.currentHistory.data).toBe(
expect(renderedHook.result.current.currentHistory.state).toBe(
"Strawberries"
);
});
Expand All @@ -68,7 +68,7 @@ describe("use-history-store", () => {

expect(renderedHook.result.current.hasPast).toBe(true);
expect(renderedHook.result.current.hasFuture).toBe(true);
expect(renderedHook.result.current.currentHistory.data).toBe("Apples");
expect(renderedHook.result.current.currentHistory.state).toBe("Apples");
});

test("should enable full fast-forwards through history", () => {
Expand All @@ -81,7 +81,7 @@ describe("use-history-store", () => {

expect(renderedHook.result.current.hasPast).toBe(true);
expect(renderedHook.result.current.hasFuture).toBe(false);
expect(renderedHook.result.current.currentHistory.data).toBe("Oranges");
expect(renderedHook.result.current.currentHistory.state).toBe("Oranges");
});

test("should replace the history if record is triggered after back", () => {
Expand All @@ -93,9 +93,9 @@ describe("use-history-store", () => {
expect(renderedHook.result.current.hasPast).toBe(true);
expect(renderedHook.result.current.hasFuture).toBe(false);
expect(renderedHook.result.current.histories.length).toBe(3);
expect(renderedHook.result.current.histories[1].data).toBe("Apples");
expect(renderedHook.result.current.histories[2].data).toBe("Banana");
expect(renderedHook.result.current.currentHistory.data).toBe("Banana");
expect(renderedHook.result.current.histories[1].state).toBe("Apples");
expect(renderedHook.result.current.histories[2].state).toBe("Banana");
expect(renderedHook.result.current.currentHistory.state).toBe("Banana");
});

test("should reset histories and index on setHistories", () => {
Expand All @@ -105,16 +105,16 @@ describe("use-history-store", () => {
renderedHook.result.current.setHistories([
{
id: "1",
data: "Oreo",
state: "Oreo",
},
])
);

expect(renderedHook.result.current.hasPast).toBe(false);
expect(renderedHook.result.current.hasFuture).toBe(false);
expect(renderedHook.result.current.histories.length).toBe(1);
expect(renderedHook.result.current.histories[0].data).toBe("Oreo");
expect(renderedHook.result.current.currentHistory.data).toBe("Oreo");
expect(renderedHook.result.current.histories[0].state).toBe("Oreo");
expect(renderedHook.result.current.currentHistory.state).toBe("Oreo");
expect(renderedHook.result.current.index).toBe(0);
});

Expand All @@ -126,7 +126,7 @@ describe("use-history-store", () => {
expect(renderedHook.result.current.hasPast).toBe(false);
expect(renderedHook.result.current.hasFuture).toBe(true);
expect(renderedHook.result.current.histories.length).toBe(3);
expect(renderedHook.result.current.currentHistory.data).toBe(
expect(renderedHook.result.current.currentHistory.state).toBe(
"Strawberries"
);
expect(renderedHook.result.current.index).toBe(0);
Expand All @@ -140,9 +140,9 @@ describe("use-history-store-prefilled", () => {
renderedHook = renderHook(() =>
useHistoryStore({
histories: [
{ id: "0", data: {} },
{ id: "1", data: {} },
{ id: "2", data: {} },
{ id: "0", state: {} },
{ id: "1", state: {} },
{ id: "2", state: {} },
],
index: 2,
})
Expand Down
20 changes: 10 additions & 10 deletions packages/core/lib/__tests__/use-puck-history.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("use-puck-history", () => {
test("back function calls dispatch when there is a history", () => {
historyStore.hasPast = true;
historyStore.prevHistory = {
data: {
state: {
...defaultAppState,
ui: { ...defaultAppState.ui, leftSideBarVisible: false },
},
Expand All @@ -57,7 +57,7 @@ describe("use-puck-history", () => {
expect(historyStore.back).toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledWith({
type: "set",
state: historyStore.prevHistory?.data || initialAppState,
state: historyStore.prevHistory?.state || initialAppState,
});
});

Expand All @@ -79,7 +79,7 @@ describe("use-puck-history", () => {

test("forward function calls dispatch when there is a future", () => {
historyStore.nextHistory = {
data: {
state: {
...defaultAppState,
ui: { ...defaultAppState.ui, leftSideBarVisible: false },
},
Expand All @@ -96,7 +96,7 @@ describe("use-puck-history", () => {
expect(historyStore.forward).toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledWith({
type: "set",
state: historyStore.nextHistory?.data,
state: historyStore.nextHistory?.state,
});
});

Expand All @@ -108,14 +108,14 @@ describe("use-puck-history", () => {
const updatedHistories = [
{
id: "1",
data: {
state: {
one: "foo 1",
two: "bar 1",
},
},
{
id: "2",
data: {
state: {
one: "foo 2",
two: "bar 2",
},
Expand All @@ -129,22 +129,22 @@ describe("use-puck-history", () => {
expect(historyStore.setHistories).toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledWith({
type: "set",
state: updatedHistories[1].data,
state: updatedHistories[1].state,
});
});

test("setHistoryIndex calls dispatch on the history at that index", () => {
const updatedHistories = [
{
id: "1",
data: {
state: {
one: "foo 1",
two: "bar 1",
},
},
{
id: "2",
data: {
state: {
one: "foo 2",
two: "bar 2",
},
Expand All @@ -163,7 +163,7 @@ describe("use-puck-history", () => {
expect(historyStore.setHistoryIndex).toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledWith({
type: "set",
state: updatedHistories[0].data,
state: updatedHistories[0].state,
});
});

Expand Down
4 changes: 2 additions & 2 deletions packages/core/lib/use-history-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export function useHistoryStore<D = any>(initialHistory: {
const nextHistory = hasFuture ? histories[index + 1] : null;
const prevHistory = hasPast ? histories[index - 1] : null;

const record = useDebouncedCallback((data: D) => {
const record = useDebouncedCallback((state: D) => {
const history: History = {
data,
state,
id: generateId("history"),
};

Expand Down
8 changes: 4 additions & 4 deletions packages/core/lib/use-puck-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function usePuckHistory({
if (historyStore.hasPast) {
dispatch({
type: "set",
state: historyStore.prevHistory?.data || initialAppState,
state: historyStore.prevHistory?.state || initialAppState,
});

historyStore.back();
Expand All @@ -33,7 +33,7 @@ export function usePuckHistory({

const forward = () => {
if (historyStore.nextHistory) {
dispatch({ type: "set", state: historyStore.nextHistory.data });
dispatch({ type: "set", state: historyStore.nextHistory.state });

historyStore.forward();
}
Expand All @@ -43,7 +43,7 @@ export function usePuckHistory({
// dispatch the last history index or initial state
dispatch({
type: "set",
state: histories[histories.length - 1]?.data || initialAppState,
state: histories[histories.length - 1]?.state || initialAppState,
});

historyStore.setHistories(histories);
Expand All @@ -53,7 +53,7 @@ export function usePuckHistory({
if (historyStore.histories.length > index) {
dispatch({
type: "set",
state: historyStore.histories[index]?.data || initialAppState,
state: historyStore.histories[index]?.state || initialAppState,
});

historyStore.setHistoryIndex(index);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/types/API/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type Plugin = {
};

export type History<D = any> = {
data: D;
state: D;
id?: string;
};

Expand Down

0 comments on commit b09244c

Please sign in to comment.