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

refactor: Rework undo to typescript #44479

Merged
merged 9 commits into from
Jun 22, 2024
Merged

refactor: Rework undo to typescript #44479

merged 9 commits into from
Jun 22, 2024

Conversation

uladzimirdev
Copy link
Contributor

@uladzimirdev uladzimirdev commented Jun 20, 2024

Closes #44433

Description

Add types to redux/undo and UndoListing

How to verify

  • ci is green
  • types are correctly specified

Checklist

  • Tests have been added/updated to cover changes in this PR

Copy link

github-actions bot commented Jun 20, 2024

Codenotify: Notifying subscribers in CODENOTIFY files for diff 674ac97...825ea9c.

Notify File(s)
@ranquild frontend/src/metabase/home/components/CustomHomePageModal/CustomHomePageModal.tsx

Copy link

replay-io bot commented Jun 20, 2024

Status Complete ↗︎
Commit 825ea9c
Results
⚠️ 6 Flaky
2682 Passed

@uladzimirdev uladzimirdev requested review from kamilmielnik and a team June 20, 2024 14:31
@uladzimirdev uladzimirdev added the no-backport Do not backport this PR to any branch label Jun 20, 2024
Copy link
Contributor

@kamilmielnik kamilmielnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 🎖️

@@ -16,12 +19,18 @@ export const addUndo = createThunkAction(ADD_UNDO, undo => {
const { icon = "check", timeout = 5000, canDismiss = true } = undo;
const id = undo.id ?? nextUndoId++;
// if we're overwriting an existing undo, clear its timeout
const currentUndo = getUndo(getState(), id);
clearTimeoutForUndo(currentUndo);
const currentUndo = getUndo(getState().undo, id);
Copy link
Contributor

@kamilmielnik kamilmielnik Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: getUndo is a selector and selectors usually receive the entire state

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 49 to 51
if (undo.timeoutId) {
clearTimeout(undo.timeoutId);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use clearTimeoutForUndo here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -202,7 +214,7 @@ export default function (state = [], { type, payload, error }) {
return state;
}

const clearTimeoutForUndo = undo => {
const clearTimeoutForUndo = (undo: Undo) => {
if (undo?.timeoutId) {
Copy link
Contributor

@kamilmielnik kamilmielnik Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove ? now or we can allow undefined to be passed here and then we don't have to call this function conditionally anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

frontend/src/metabase/redux/undo.ts Outdated Show resolved Hide resolved
export default function (state = [], { type, payload, error }) {
export function undoReducer(
state: Undo[] = [],
{ type, payload, error }: Action<Undo>,
Copy link
Contributor

@kamilmielnik kamilmielnik Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ For DISMISS_UNDO the payload will be Undo["id"].

Suggested change
{ type, payload, error }: Action<Undo>,
{ type, payload, error }: Action<Undo | Undo["id"]>,

WDYT about adding 1 (isUndoId) or 2 (isUndoId + isUndo) type guards? So that this reducer can look like this:

if (type === ADD_UNDO && !isUndoId(payload) {
  // ...
} else if (type === DISMISS_UNDO && isUndoId(payload) {
  // ...
} else // ...

or this:

if (type === ADD_UNDO && isUndo(payload) {
  // ...
} else if (type === DISMISS_UNDO && isUndoId(payload) {
  // ...
} else // ...

And then we can remove the as unknown as Undo["id"] cast

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it as well, good point. I came up to the idea that we'll need to rework this reducer to RTK anyway and builder.addCase supports custom payload type, so I decided not to waste time on this check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kamilmielnik kamilmielnik requested a review from a team June 21, 2024 07:19
@uladzimirdev uladzimirdev enabled auto-merge (squash) June 22, 2024 06:50
@uladzimirdev uladzimirdev merged commit 981d69a into master Jun 22, 2024
111 checks passed
@uladzimirdev uladzimirdev deleted the rework-undo-to-ts branch June 22, 2024 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-backport Do not backport this PR to any branch .Team/QueryingComponents
Projects
None yet
Development

Successfully merging this pull request may close these issues.

convert redux/undo and UndoListing.jsx to TS
3 participants