Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing off for setPuckInitialHistory's histories #673

Open
mkilpatrick opened this issue Oct 30, 2024 · 4 comments
Open

Typing off for setPuckInitialHistory's histories #673

mkilpatrick opened this issue Oct 30, 2024 · 4 comments

Comments

@mkilpatrick
Copy link
Contributor

If you try to pass histories (type History[]) to setPuckInitialHistory when appendData is false, there is a type error.

Type 'History[]' is not assignable to type '[History<Partial<AppState<Data<DefaultComponentProps, DefaultRootFieldProps>>>>, ...History<...>[]]'.
  Source provides no match for required element at position 0 in target.

If appendData is true it works fine though.

The documentation shows the type as History[] too.

Why does InitialHistory's type need the InitialHistoryNoAppend version? The histories type between InitialHistoryNoAppend and InitialHistoryAppend are [History<AS>, ...History<AS>[]] and History<AS>[], respectively.

@chrisvxd
Copy link
Member

chrisvxd commented Nov 6, 2024

I believe if appendData is false, then Puck assumes you need at least one item in the histories.

It would be useful to understand your use-case for this in particular

@mkilpatrick
Copy link
Contributor Author

mkilpatrick commented Nov 6, 2024

const [puckInitialHistory, setPuckInitialHistory] = useState<InitialHistory | undefined>();
const localHistories = JSON.parse(localHistoryArray) as History[];
const localHistoryIndex = localHistories.length - 1;
setPuckInitialHistory({
    histories: localHistories,
    index: localHistoryIndex,
    appendData: false,
});

Screenshot 2024-11-06 at 10 02 37 AM

@chrisvxd
Copy link
Member

Puck always requires one initial item in the history. It derives an initial app state (called generatedAppState) based on default Puck state.

  1. If appendData: true, the generatedAppState is always inserted after the initial histories provided by the user. This ensures there is always at least one item in the history.
  2. If appendData: false, then the generatedAppState is not inserted at all. Instead, the user must specify at least one history item.

If you're not providing any history or appending data, then Puck will be unable to determine the initial state. To counter this, I suggest inserting an empty history item.

@mkilpatrick
Copy link
Contributor Author

The localHistories I'm passing to histories is not empty. If I change it to

setPuckInitialHistory({
    histories: [localHistories[0]],
    index: localHistoryIndex,
    appendData: false,
});

then TS is happy, but I have more than one item in the array.

This also gives a TS error.

setPuckInitialHistory({
    histories: [...localHistories],
    index: localHistoryIndex,
    appendData: false,
});

Perhaps I'm missing something obvious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants