-
-
Notifications
You must be signed in to change notification settings - Fork 83
Refactor: Centralize localStorage operations in a storage utility #197
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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // src/utils/storage.js | ||
|
|
||
| export const STORAGE_KEYS = { | ||
| PAT: "oe_pat", | ||
| RATE_LIMIT: "oe_rate_limit", | ||
| RECENT_SEARCHES: "oe_recent", | ||
| }; | ||
|
|
||
| export const storage = { | ||
| get: (key) => { | ||
| try { | ||
| const value = localStorage.getItem(key); | ||
| if (!value) return null; | ||
| try { | ||
| return JSON.parse(value); | ||
| } catch { | ||
| return value; | ||
| } | ||
|
Comment on lines
+14
to
+18
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. 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win Keep non-PAT storage reads type-safe.
Proposed fix } catch {
- return value
+ return key === STORAGE_KEYS.PAT ? value : null
}Also validate the expected shapes at the rate-limit and recent-search read sites. 🤖 Prompt for AI Agents |
||
| } catch (error) { | ||
| console.error(`Error reading ${key} from localStorage:`, error); | ||
| return null; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| }, | ||
|
|
||
| set: (key, value) => { | ||
| try { | ||
| localStorage.setItem(key, JSON.stringify(value)); | ||
| } catch (error) { | ||
| console.error(`Error writing ${key} to localStorage:`, error); | ||
| } | ||
| }, | ||
|
|
||
| remove: (key) => { | ||
| try { | ||
| localStorage.removeItem(key); | ||
| } catch (error) { | ||
| console.error(`Error removing ${key} from localStorage:`, error); | ||
| } | ||
| }, | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.