Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions legislation/ui/hooks/useApiKey.ts
Copy link
Copy Markdown
Member

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 thewindow is not undefined before accessing sessionStorage in the useEffect for safe access. Thanks again!

Copy link
Copy Markdown
Author

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

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To improve maintainability and avoid 'magic strings', consider defining 'local-apikey-change' as a constant. This string is used in multiple places (here, line 31, and line 39). Using a constant makes the code easier to read and maintain, and prevents potential typos.

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)
}
}, [])
Expand Down