-
Notifications
You must be signed in to change notification settings - Fork 0
Make TrainerRoad metrics configurable via settings #28
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,117 @@ | ||
| import { useEffect, useRef } from "react"; | ||
| import { X } from "lucide-react"; | ||
|
|
||
| const FOCUSABLE_SELECTOR = | ||
| 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'; | ||
|
|
||
| interface SettingsPanelProps { | ||
| open: boolean; | ||
| onOpenChange: (open: boolean) => void; | ||
| showTrainerRoadMetrics: boolean; | ||
| onShowTrainerRoadMetricsChange: (value: boolean) => void; | ||
| } | ||
|
|
||
| export function SettingsPanel({ | ||
| open, | ||
| onOpenChange, | ||
| showTrainerRoadMetrics, | ||
| onShowTrainerRoadMetricsChange, | ||
| }: SettingsPanelProps) { | ||
| const dialogRef = useRef<HTMLDivElement>(null); | ||
|
|
||
| useEffect(() => { | ||
| if (!open) return; | ||
|
|
||
| const previouslyFocused = document.activeElement as HTMLElement | null; | ||
| const getFocusable = () => | ||
| dialogRef.current | ||
| ? Array.from(dialogRef.current.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR)) | ||
| : []; | ||
|
|
||
| getFocusable()[0]?.focus(); | ||
|
|
||
| function handleKeyDown(e: KeyboardEvent) { | ||
| if (e.key === "Escape") { | ||
| onOpenChange(false); | ||
| return; | ||
| } | ||
| if (e.key !== "Tab") return; | ||
|
|
||
| const focusable = getFocusable(); | ||
| if (focusable.length === 0) return; | ||
| const first = focusable[0]; | ||
| const last = focusable[focusable.length - 1]; | ||
|
|
||
| if (e.shiftKey && document.activeElement === first) { | ||
| e.preventDefault(); | ||
| last.focus(); | ||
| } else if (!e.shiftKey && document.activeElement === last) { | ||
| e.preventDefault(); | ||
| first.focus(); | ||
| } | ||
| } | ||
|
|
||
| document.addEventListener("keydown", handleKeyDown); | ||
| return () => { | ||
| document.removeEventListener("keydown", handleKeyDown); | ||
| previouslyFocused?.focus(); | ||
| }; | ||
| }, [open, onOpenChange]); | ||
|
|
||
| if (!open) return null; | ||
|
|
||
| return ( | ||
| <div | ||
| className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4" | ||
| onClick={() => onOpenChange(false)} | ||
| > | ||
| <div | ||
| ref={dialogRef} | ||
| role="dialog" | ||
| aria-modal="true" | ||
| aria-labelledby="settings-panel-title" | ||
| className="w-full max-w-sm rounded-lg bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 shadow-lg p-5" | ||
| onClick={(e) => e.stopPropagation()} | ||
| > | ||
| <div className="flex items-center justify-between mb-4"> | ||
| <h2 id="settings-panel-title" className="text-lg font-semibold text-gray-900 dark:text-white"> | ||
| Settings | ||
| </h2> | ||
| <button | ||
| type="button" | ||
| onClick={() => onOpenChange(false)} | ||
| aria-label="Close settings" | ||
| className="p-1 rounded-md text-gray-500 hover:text-gray-900 hover:bg-gray-100 dark:text-gray-400 dark:hover:text-white dark:hover:bg-gray-700 transition-colors" | ||
| > | ||
| <X className="w-4 h-4" /> | ||
| </button> | ||
| </div> | ||
|
|
||
| <div className="flex items-start justify-between gap-4"> | ||
| <div> | ||
| <p className="text-sm font-medium text-gray-900 dark:text-white">Show TrainerRoad metrics</p> | ||
| <p className="text-xs text-gray-500 dark:text-gray-400 mt-1"> | ||
| Show TR-RPE and TR-LGT fields on the form and in generated markdown. | ||
| </p> | ||
| </div> | ||
| <button | ||
| type="button" | ||
| role="switch" | ||
| aria-checked={showTrainerRoadMetrics} | ||
| aria-label="Show TrainerRoad metrics" | ||
| onClick={() => onShowTrainerRoadMetricsChange(!showTrainerRoadMetrics)} | ||
| className={`relative inline-flex h-6 w-11 shrink-0 items-center rounded-full transition-colors ${ | ||
| showTrainerRoadMetrics ? "bg-brand-blue" : "bg-gray-300 dark:bg-gray-600" | ||
| }`} | ||
| > | ||
| <span | ||
| className={`inline-block h-4 w-4 transform rounded-full bg-white transition-transform ${ | ||
| showTrainerRoadMetrics ? "translate-x-6" : "translate-x-1" | ||
| }`} | ||
| /> | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
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,76 @@ | ||
| import { useState } from "react"; | ||
|
|
||
| export interface Settings { | ||
| showTrainerRoadMetrics: boolean; | ||
| } | ||
|
|
||
| interface PersistedSettings { | ||
| version: 1; | ||
| data: Settings; | ||
| } | ||
|
|
||
| interface UseSettingsOptions { | ||
| key: string; | ||
| defaults: Settings; | ||
| } | ||
|
|
||
| export function useSettings(options: UseSettingsOptions): { | ||
| settings: Settings; | ||
| setSetting: <K extends keyof Settings>(key: K, value: Settings[K]) => void; | ||
| } { | ||
| const { key, defaults } = options; | ||
|
|
||
| const [settings, setSettings] = useState<Settings>(() => { | ||
| try { | ||
| const stored = localStorage.getItem(key); | ||
| if (stored) { | ||
| const parsed: unknown = JSON.parse(stored); | ||
| if ( | ||
| typeof parsed !== "object" || | ||
| parsed === null || | ||
| (parsed as Record<string, unknown>).version !== 1 || | ||
| typeof (parsed as Record<string, unknown>).data !== "object" || | ||
| (parsed as Record<string, unknown>).data === null | ||
| ) { | ||
| // Malformed or wrong version — fall back to defaults and overwrite | ||
| persistRaw(key, defaults); | ||
| return { ...defaults }; | ||
| } | ||
| const data = (parsed as PersistedSettings).data; | ||
| // Merge: use defaults for missing keys, drop unknown keys | ||
| const merged: Settings = { ...defaults }; | ||
| for (const settingKey of Object.keys(defaults) as (keyof Settings)[]) { | ||
| if (typeof data[settingKey] === "boolean") { | ||
| merged[settingKey] = data[settingKey]; | ||
| } | ||
| } | ||
| return merged; | ||
| } | ||
| } catch (err) { | ||
| console.error("[use-settings] Failed to restore settings:", err); | ||
| persistRaw(key, defaults); | ||
| } | ||
| return { ...defaults }; | ||
|
aidmax marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| function setSetting<K extends keyof Settings>(settingKey: K, value: Settings[K]) { | ||
| setSettings((prev) => { | ||
| const next = { ...prev, [settingKey]: value }; | ||
| persistRaw(key, next); | ||
| return next; | ||
| }); | ||
| } | ||
|
|
||
| return { settings, setSetting }; | ||
| } | ||
|
|
||
| function persistRaw(key: string, data: Settings) { | ||
| try { | ||
| const toStore: PersistedSettings = { version: 1, data }; | ||
| localStorage.setItem(key, JSON.stringify(toStore)); | ||
| } catch (err) { | ||
| if (err instanceof DOMException && err.name === "QuotaExceededError") { | ||
| console.error("[use-settings] localStorage quota exceeded"); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.