|
| 1 | +import type { JSX } from 'preact' |
| 2 | +// biome-ignore lint/correctness/noUnusedImports: Needed for JSX types |
| 3 | +import * as React from 'preact/compat' |
| 4 | +import { useState } from 'preact/hooks' |
| 5 | + |
| 6 | +const STORAGE_SECTIONS = [ |
| 7 | + { |
| 8 | + name: 'Splits', |
| 9 | + icon: '📏', |
| 10 | + description: 'Distance, target times, and comparison settings', |
| 11 | + keys: [ |
| 12 | + 'runner-dashboard:splits.distanceName', |
| 13 | + 'runner-dashboard:splits.time1', |
| 14 | + 'runner-dashboard:splits.showSecondTime', |
| 15 | + 'runner-dashboard:splits.time2', |
| 16 | + ], |
| 17 | + }, |
| 18 | + { |
| 19 | + name: 'Nutrition Planner', |
| 20 | + icon: '🍯', |
| 21 | + description: 'Weight, distance, estimated time, and gel settings', |
| 22 | + keys: [ |
| 23 | + 'runner-dashboard:nutrition.weight', |
| 24 | + 'runner-dashboard:nutrition.distance', |
| 25 | + 'runner-dashboard:nutrition.estimatedHours', |
| 26 | + 'runner-dashboard:nutrition.estimatedMinutes', |
| 27 | + 'runner-dashboard:nutrition.glucidPerGel', |
| 28 | + 'runner-dashboard:nutrition.numberOfGels', |
| 29 | + ], |
| 30 | + }, |
| 31 | + { |
| 32 | + name: 'Heart Zones', |
| 33 | + icon: '❤️', |
| 34 | + description: 'Maximum heart rate', |
| 35 | + keys: ['runner-dashboard:heartZones.maxHr'], |
| 36 | + }, |
| 37 | +] |
| 38 | + |
| 39 | +export default function SettingsComponent(): JSX.Element { |
| 40 | + const [confirming, setConfirming] = useState<string | null>(null) |
| 41 | + |
| 42 | + const resetSection = (keys: string[]) => { |
| 43 | + keys.forEach(key => window.localStorage.removeItem(key)) |
| 44 | + window.location.reload() |
| 45 | + } |
| 46 | + |
| 47 | + const resetAll = () => { |
| 48 | + Object.keys(window.localStorage) |
| 49 | + .filter(key => key.startsWith('runner-dashboard:')) |
| 50 | + .forEach(key => window.localStorage.removeItem(key)) |
| 51 | + window.location.reload() |
| 52 | + } |
| 53 | + |
| 54 | + return ( |
| 55 | + <div class="space-y-8"> |
| 56 | + <div class="rounded-xl bg-white p-6 shadow-lg dark:bg-gray-800"> |
| 57 | + <h2 class="text-xl font-semibold text-gray-900 dark:text-white">Saved Preferences</h2> |
| 58 | + <p class="mt-2 text-sm text-gray-500 dark:text-gray-400"> |
| 59 | + Your preferences are saved in your browser's local storage. You can reset them individually by section or all |
| 60 | + at once. |
| 61 | + </p> |
| 62 | + </div> |
| 63 | + |
| 64 | + <div class="space-y-4"> |
| 65 | + {STORAGE_SECTIONS.map(section => ( |
| 66 | + <div |
| 67 | + key={section.name} |
| 68 | + class="flex items-center justify-between rounded-xl bg-white p-5 shadow-lg dark:bg-gray-800" |
| 69 | + > |
| 70 | + <div class="flex items-center gap-4"> |
| 71 | + <span class="text-2xl">{section.icon}</span> |
| 72 | + <div> |
| 73 | + <h3 class="font-medium text-gray-900 dark:text-white">{section.name}</h3> |
| 74 | + <p class="text-sm text-gray-500 dark:text-gray-400">{section.description}</p> |
| 75 | + </div> |
| 76 | + </div> |
| 77 | + |
| 78 | + {confirming === section.name ? ( |
| 79 | + <div class="flex items-center gap-2"> |
| 80 | + <span class="text-sm text-gray-500 dark:text-gray-400">Are you sure?</span> |
| 81 | + <button |
| 82 | + type="button" |
| 83 | + onClick={() => resetSection(section.keys)} |
| 84 | + class="cursor-pointer rounded-lg bg-red-500 px-3 py-1.5 text-sm font-medium text-white transition-all hover:bg-red-600" |
| 85 | + > |
| 86 | + Confirm |
| 87 | + </button> |
| 88 | + <button |
| 89 | + type="button" |
| 90 | + onClick={() => setConfirming(null)} |
| 91 | + class="cursor-pointer rounded-lg bg-gray-100 px-3 py-1.5 text-sm font-medium text-gray-700 transition-all hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600" |
| 92 | + > |
| 93 | + Cancel |
| 94 | + </button> |
| 95 | + </div> |
| 96 | + ) : ( |
| 97 | + <button |
| 98 | + type="button" |
| 99 | + onClick={() => setConfirming(section.name)} |
| 100 | + class="cursor-pointer rounded-lg bg-gray-100 px-4 py-2 text-sm font-medium text-gray-700 transition-all hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600" |
| 101 | + > |
| 102 | + Reset |
| 103 | + </button> |
| 104 | + )} |
| 105 | + </div> |
| 106 | + ))} |
| 107 | + </div> |
| 108 | + |
| 109 | + <div class="rounded-xl bg-white p-6 shadow-lg dark:bg-gray-800"> |
| 110 | + {confirming === 'all' ? ( |
| 111 | + <div class="flex items-center justify-between"> |
| 112 | + <span class="text-sm text-gray-500 dark:text-gray-400"> |
| 113 | + This will reset all preferences and return to the default tab. |
| 114 | + </span> |
| 115 | + <div class="flex items-center gap-2"> |
| 116 | + <button |
| 117 | + type="button" |
| 118 | + onClick={resetAll} |
| 119 | + class="cursor-pointer rounded-lg bg-red-500 px-4 py-2 text-sm font-medium text-white transition-all hover:bg-red-600" |
| 120 | + > |
| 121 | + Confirm Reset All |
| 122 | + </button> |
| 123 | + <button |
| 124 | + type="button" |
| 125 | + onClick={() => setConfirming(null)} |
| 126 | + class="cursor-pointer rounded-lg bg-gray-100 px-4 py-2 text-sm font-medium text-gray-700 transition-all hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600" |
| 127 | + > |
| 128 | + Cancel |
| 129 | + </button> |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + ) : ( |
| 133 | + <button |
| 134 | + type="button" |
| 135 | + onClick={() => setConfirming('all')} |
| 136 | + class="w-full cursor-pointer rounded-lg bg-red-50 px-4 py-3 text-sm font-medium text-red-600 transition-all hover:bg-red-100 dark:bg-red-900/20 dark:text-red-400 dark:hover:bg-red-900/30" |
| 137 | + > |
| 138 | + Reset All Preferences |
| 139 | + </button> |
| 140 | + )} |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + ) |
| 144 | +} |
0 commit comments