-
Notifications
You must be signed in to change notification settings - Fork 8
fix: Remove unsafe type casting in useApiKey hook (#26) #46
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,10 @@ export const useApiKey = () => { | |
| if (stored) setApiKeyState(stored) | ||
|
|
||
| // Listen for local changes | ||
| const handleLocalChange = (e: CustomEvent) => { | ||
| setApiKeyState(e.detail) | ||
| const handleLocalChange = (e: Event) => { | ||
| if (e instanceof CustomEvent && typeof e.detail === "string") { | ||
| setApiKeyState(e.detail) | ||
| } | ||
| } | ||
|
|
||
| // Listen for other tabs (optional, but good) | ||
|
|
@@ -22,12 +24,11 @@ export const useApiKey = () => { | |
| } | ||
| } | ||
|
|
||
| // FIXME: Issue #26 (https://github.com/LDFLK/research/issues/26) - Unsafe Type Casting | ||
| window.addEventListener("local-apikey-change", handleLocalChange as EventListener) | ||
| window.addEventListener("local-apikey-change", handleLocalChange) | ||
|
Contributor
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. To improve maintainability and avoid 'magic strings', consider defining For example: const LOCAL_API_KEY_CHANGE_EVENT = 'local-apikey-change';
// ... inside useEffect
window.addEventListener(LOCAL_API_KEY_CHANGE_EVENT, handleLocalChange); |
||
| window.addEventListener("storage", handleStorage) | ||
|
|
||
| return () => { | ||
| window.removeEventListener("local-apikey-change", handleLocalChange as EventListener) | ||
| window.removeEventListener("local-apikey-change", handleLocalChange) | ||
| window.removeEventListener("storage", handleStorage) | ||
| } | ||
| }, []) | ||
|
|
||
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.
@d3varaja Thanks for contributing to this project. Since this is a completely research project by Vibe Coding, and not for production, we configured it for easy use. Your changes are good. You can check that the
windowis notundefinedbefore accessingsessionStoragein the useEffect for safe access. Thanks again!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.
@ChanukaUOJ Thanks for the suggestion :) I’ll add the window check in the useEffect