-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: persist user settings across sessions #324
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
Merged
siddharthvaddem
merged 4 commits into
siddharthvaddem:main
from
JasonOA888:fix/306-persist-user-settings
Apr 5, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d5f59a7
fix: persist user settings across sessions
JasonOA888 7d74619
fix: persist user settings across sessions (closes #306)
JasonOA888 4f48ecd
fix: address code review feedback for settings persistence
JasonOA888 a8427b9
fix: resolve lint errors for CI
JasonOA888 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import type { ExportFormat, ExportQuality } from "@/lib/exporter"; | ||
| import type { AspectRatio } from "@/utils/aspectRatioUtils"; | ||
|
|
||
| const PREFS_KEY = "openscreen_user_preferences"; | ||
|
|
||
| const VALID_ASPECT_RATIOS: readonly string[] = [ | ||
| "16:9", | ||
| "9:16", | ||
| "1:1", | ||
| "4:3", | ||
| "4:5", | ||
| "16:10", | ||
| "10:16", | ||
| "native", | ||
| ]; | ||
|
|
||
| export interface UserPreferences { | ||
| /** Default padding % */ | ||
| padding: number; | ||
| /** Default aspect ratio */ | ||
| aspectRatio: AspectRatio; | ||
| /** Default export quality */ | ||
| exportQuality: ExportQuality; | ||
| /** Default export format */ | ||
| exportFormat: ExportFormat; | ||
| } | ||
|
|
||
| const DEFAULT_PREFS: UserPreferences = { | ||
| padding: 50, | ||
| aspectRatio: "16:9", | ||
| exportQuality: "good", | ||
| exportFormat: "mp4", | ||
| }; | ||
|
|
||
| function safeJsonParse(text: string | null): Record<string, unknown> | null { | ||
| if (!text) return null; | ||
| try { | ||
| return JSON.parse(text); | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Load persisted user preferences from localStorage. | ||
| * Returns defaults for any missing or invalid fields. | ||
| */ | ||
| export function loadUserPreferences(): UserPreferences { | ||
| let raw: Record<string, unknown> | null = null; | ||
| try { | ||
| raw = safeJsonParse(localStorage.getItem(PREFS_KEY)); | ||
| } catch { | ||
| return { ...DEFAULT_PREFS }; | ||
| } | ||
| if (!raw || typeof raw !== "object") return { ...DEFAULT_PREFS }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| return { | ||
| padding: | ||
| typeof raw.padding === "number" && | ||
| Number.isFinite(raw.padding) && | ||
| raw.padding >= 0 && | ||
| raw.padding <= 100 | ||
| ? raw.padding | ||
| : DEFAULT_PREFS.padding, | ||
| aspectRatio: | ||
| typeof raw.aspectRatio === "string" && VALID_ASPECT_RATIOS.includes(raw.aspectRatio) | ||
| ? (raw.aspectRatio as AspectRatio) | ||
| : DEFAULT_PREFS.aspectRatio, | ||
| exportQuality: | ||
| raw.exportQuality === "medium" || | ||
| raw.exportQuality === "good" || | ||
| raw.exportQuality === "source" | ||
| ? (raw.exportQuality as ExportQuality) | ||
| : DEFAULT_PREFS.exportQuality, | ||
| exportFormat: | ||
| raw.exportFormat === "gif" || raw.exportFormat === "mp4" | ||
| ? (raw.exportFormat as ExportFormat) | ||
| : DEFAULT_PREFS.exportFormat, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Persist user preferences to localStorage. | ||
| * Only the explicitly provided fields are updated. | ||
| */ | ||
| export function saveUserPreferences(partial: Partial<UserPreferences>): void { | ||
| const current = loadUserPreferences(); | ||
| const merged = { ...current, ...partial }; | ||
| try { | ||
| localStorage.setItem(PREFS_KEY, JSON.stringify(merged)); | ||
| } catch { | ||
| // localStorage may be unavailable (e.g. private browsing quota exceeded) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.