-
Notifications
You must be signed in to change notification settings - Fork 16
refactor: Replace NotificationContext with useNotificationStore for improved state management (fixes #290, resolves #292). #293
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {create} from "zustand"; | ||
|
||
import {WithId} from "../typings/common"; | ||
import {PopUpMessage} from "../typings/notifications"; | ||
|
||
|
||
interface NotificationValues { | ||
popUpMessages: WithId<PopUpMessage>[]; | ||
} | ||
|
||
interface NotificationActions { | ||
handlePopUpMessageClose: (messageId: number) => void; | ||
postPopUp: (message: PopUpMessage) => void; | ||
} | ||
|
||
type NotificationState = NotificationValues & NotificationActions; | ||
|
||
const NOTIFICATION_STORE_DEFAULT: NotificationValues = { | ||
popUpMessages: [], | ||
}; | ||
|
||
let nextPopUpMessageId = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Consider persisting the ID counter. The simple counter approach works well for generating unique IDs, but it resets on page reload. While likely acceptable for this use case, you might consider persisting it if maintaining unique IDs across sessions becomes important. Also applies to: 34-40 🤖 Prompt for AI Agents
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The IDs are not expected to be persisted across page reloads. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @junhaoliao Got it, thanks for clarifying. That makes sense for this use case since notifications are typically transient UI elements that would be cleared on reload anyway. |
||
|
||
const useNotificationStore = create<NotificationState>((set) => ({ | ||
...NOTIFICATION_STORE_DEFAULT, | ||
|
||
handlePopUpMessageClose: (messageId: number) => { | ||
set((state) => ({ | ||
popUpMessages: state.popUpMessages.filter((m) => m.id !== messageId), | ||
})); | ||
}, | ||
|
||
postPopUp: (message: PopUpMessage) => { | ||
const newMessage = { | ||
id: nextPopUpMessageId, | ||
...message, | ||
}; | ||
|
||
nextPopUpMessageId++; | ||
|
||
set((state) => ({ | ||
popUpMessages: [ | ||
newMessage, | ||
...state.popUpMessages, | ||
], | ||
})); | ||
}, | ||
})); | ||
|
||
export type {PopUpMessage}; | ||
export default useNotificationStore; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unused import
The
VIEW_STORE_DEFAULT
import is defined but never used in this file, which is causing linting errors in the CI pipeline.📝 Committable suggestion
🧰 Tools
🪛 GitHub Check: lint-check
[failure] 30-30:
'VIEW_STORE_DEFAULT' is defined but never used
🪛 GitHub Actions: lint
[error] 30-30: ESLint: 'VIEW_STORE_DEFAULT' is defined but never used. (@typescript-eslint/no-unused-vars)
🤖 Prompt for AI Agents