|
| 1 | +/* |
| 2 | + * Copyright (c) 2024-2025, s0up and the autobrr contributors. |
| 3 | + * SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import React, { useState } from "react"; |
| 7 | +import { MapPinIcon, CheckIcon } from "@heroicons/react/24/outline"; |
| 8 | +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; |
| 9 | +import { Button } from "@/components/ui/Button"; |
| 10 | +import { |
| 11 | + getDistanceSettings, |
| 12 | + saveDistanceSettings, |
| 13 | + formatDistance, |
| 14 | + type DistanceSettings as DistanceSettingsType, |
| 15 | + type DistanceUnit, |
| 16 | +} from "@/utils/distanceSettings"; |
| 17 | +import { showToast } from "@/components/common/Toast"; |
| 18 | + |
| 19 | +export const DistanceSettings: React.FC = () => { |
| 20 | + const [settings, setSettings] = useState<DistanceSettingsType>(getDistanceSettings); |
| 21 | + const [hasChanges, setHasChanges] = useState(false); |
| 22 | + |
| 23 | + const updateUnit = (unit: DistanceUnit) => { |
| 24 | + setSettings({ unit }); |
| 25 | + setHasChanges(true); |
| 26 | + }; |
| 27 | + |
| 28 | + const saveSettings = () => { |
| 29 | + saveDistanceSettings(settings); |
| 30 | + setHasChanges(false); |
| 31 | + showToast("Distance settings saved", "success", { |
| 32 | + description: "Changes will be applied across the application", |
| 33 | + }); |
| 34 | + }; |
| 35 | + |
| 36 | + const resetToDefaults = () => { |
| 37 | + setSettings({ unit: "km" }); |
| 38 | + setHasChanges(true); |
| 39 | + }; |
| 40 | + |
| 41 | + return ( |
| 42 | + <div className="space-y-6"> |
| 43 | + <div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4"> |
| 44 | + <div> |
| 45 | + <h3 className="text-xl sm:text-2xl font-bold text-gray-900 dark:text-white flex items-center gap-2"> |
| 46 | + <MapPinIcon className="w-6 h-6 text-gray-600 dark:text-gray-400" /> |
| 47 | + Distance Settings |
| 48 | + </h3> |
| 49 | + <p className="mt-1 text-sm text-gray-600 dark:text-gray-400"> |
| 50 | + Choose between metric and imperial units for distance display |
| 51 | + </p> |
| 52 | + </div> |
| 53 | + |
| 54 | + {hasChanges && ( |
| 55 | + <div className="flex items-center gap-2"> |
| 56 | + <Button |
| 57 | + onClick={saveSettings} |
| 58 | + className="bg-blue-600 hover:bg-blue-700 text-white" |
| 59 | + > |
| 60 | + <CheckIcon className="w-4 h-4" /> |
| 61 | + Save Changes |
| 62 | + </Button> |
| 63 | + <Button |
| 64 | + onClick={() => { |
| 65 | + setSettings(getDistanceSettings()); |
| 66 | + setHasChanges(false); |
| 67 | + }} |
| 68 | + variant="secondary" |
| 69 | + > |
| 70 | + Cancel |
| 71 | + </Button> |
| 72 | + </div> |
| 73 | + )} |
| 74 | + </div> |
| 75 | + |
| 76 | + <div className="grid grid-cols-1 lg:grid-cols-2 gap-6"> |
| 77 | + <Card> |
| 78 | + <CardHeader> |
| 79 | + <CardTitle className="flex items-center gap-2"> |
| 80 | + <MapPinIcon className="w-5 h-5 text-blue-600 dark:text-blue-400" /> |
| 81 | + Distance Unit |
| 82 | + </CardTitle> |
| 83 | + </CardHeader> |
| 84 | + <CardContent className="space-y-4"> |
| 85 | + <div className="flex gap-3"> |
| 86 | + <button |
| 87 | + onClick={() => updateUnit("km")} |
| 88 | + className={`flex-1 p-4 rounded-lg border transition-colors text-left ${ |
| 89 | + settings.unit === "km" |
| 90 | + ? "bg-blue-500/10 border-blue-400/50" |
| 91 | + : "bg-gray-200/50 dark:bg-gray-800/50 border-gray-300 dark:border-gray-800 hover:bg-gray-300/50 dark:hover:bg-gray-800" |
| 92 | + }`} |
| 93 | + > |
| 94 | + <div className="font-medium text-gray-900 dark:text-white">Metric</div> |
| 95 | + <div className="text-sm text-gray-600 dark:text-gray-400 mt-1">Kilometers (km)</div> |
| 96 | + </button> |
| 97 | + <button |
| 98 | + onClick={() => updateUnit("mi")} |
| 99 | + className={`flex-1 p-4 rounded-lg border transition-colors text-left ${ |
| 100 | + settings.unit === "mi" |
| 101 | + ? "bg-blue-500/10 border-blue-400/50" |
| 102 | + : "bg-gray-200/50 dark:bg-gray-800/50 border-gray-300 dark:border-gray-800 hover:bg-gray-300/50 dark:hover:bg-gray-800" |
| 103 | + }`} |
| 104 | + > |
| 105 | + <div className="font-medium text-gray-900 dark:text-white">Imperial</div> |
| 106 | + <div className="text-sm text-gray-600 dark:text-gray-400 mt-1">Miles (mi)</div> |
| 107 | + </button> |
| 108 | + </div> |
| 109 | + |
| 110 | + <div className="p-3 bg-blue-50 dark:bg-blue-950/30 rounded-lg"> |
| 111 | + <div className="flex items-start gap-2"> |
| 112 | + <MapPinIcon className="w-4 h-4 text-blue-600 dark:text-blue-400 mt-0.5 flex-shrink-0" /> |
| 113 | + <div className="text-sm"> |
| 114 | + <div className="font-medium text-blue-900 dark:text-blue-300"> |
| 115 | + Preview |
| 116 | + </div> |
| 117 | + <div className="text-blue-700 dark:text-blue-400 mt-1"> |
| 118 | + 100 km = <span className="font-mono">{formatDistance(100, settings)}</span> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + </CardContent> |
| 124 | + </Card> |
| 125 | + </div> |
| 126 | + |
| 127 | + <Card> |
| 128 | + <CardContent className="p-6"> |
| 129 | + <div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4"> |
| 130 | + <div> |
| 131 | + <h4 className="font-medium text-gray-900 dark:text-white"> |
| 132 | + Quick Actions |
| 133 | + </h4> |
| 134 | + <p className="text-sm text-gray-600 dark:text-gray-400 mt-1"> |
| 135 | + Reset settings or apply common configurations |
| 136 | + </p> |
| 137 | + </div> |
| 138 | + <div className="flex items-center gap-2"> |
| 139 | + <Button |
| 140 | + onClick={resetToDefaults} |
| 141 | + variant="outline" |
| 142 | + > |
| 143 | + Reset to Defaults |
| 144 | + </Button> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + </CardContent> |
| 148 | + </Card> |
| 149 | + </div> |
| 150 | + ); |
| 151 | +}; |
0 commit comments