-
-
Notifications
You must be signed in to change notification settings - Fork 66
feat(web): add distance unit toggle between metric and imperial #173
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
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,151 @@ | ||
| /* | ||
| * Copyright (c) 2024-2025, s0up and the autobrr contributors. | ||
| * SPDX-License-Identifier: GPL-2.0-or-later | ||
| */ | ||
|
|
||
| import React, { useState } from "react"; | ||
| import { MapPinIcon, CheckIcon } from "@heroicons/react/24/outline"; | ||
| import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; | ||
| import { Button } from "@/components/ui/Button"; | ||
| import { | ||
| getDistanceSettings, | ||
| saveDistanceSettings, | ||
| formatDistance, | ||
| type DistanceSettings as DistanceSettingsType, | ||
| type DistanceUnit, | ||
| } from "@/utils/distanceSettings"; | ||
| import { showToast } from "@/components/common/Toast"; | ||
|
|
||
| export const DistanceSettings: React.FC = () => { | ||
| const [settings, setSettings] = useState<DistanceSettingsType>(getDistanceSettings); | ||
| const [hasChanges, setHasChanges] = useState(false); | ||
|
|
||
| const updateUnit = (unit: DistanceUnit) => { | ||
| setSettings({ unit }); | ||
| setHasChanges(true); | ||
| }; | ||
|
|
||
| const saveSettings = () => { | ||
| saveDistanceSettings(settings); | ||
| setHasChanges(false); | ||
| showToast("Distance settings saved", "success", { | ||
| description: "Changes will be applied across the application", | ||
| }); | ||
| }; | ||
|
|
||
| const resetToDefaults = () => { | ||
| setSettings({ unit: "km" }); | ||
| setHasChanges(true); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="space-y-6"> | ||
| <div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4"> | ||
| <div> | ||
| <h3 className="text-xl sm:text-2xl font-bold text-gray-900 dark:text-white flex items-center gap-2"> | ||
| <MapPinIcon className="w-6 h-6 text-gray-600 dark:text-gray-400" /> | ||
| Distance Settings | ||
| </h3> | ||
| <p className="mt-1 text-sm text-gray-600 dark:text-gray-400"> | ||
| Choose between metric and imperial units for distance display | ||
| </p> | ||
| </div> | ||
|
|
||
| {hasChanges && ( | ||
| <div className="flex items-center gap-2"> | ||
| <Button | ||
| onClick={saveSettings} | ||
| className="bg-blue-600 hover:bg-blue-700 text-white" | ||
| > | ||
| <CheckIcon className="w-4 h-4" /> | ||
| Save Changes | ||
| </Button> | ||
| <Button | ||
| onClick={() => { | ||
| setSettings(getDistanceSettings()); | ||
| setHasChanges(false); | ||
| }} | ||
| variant="secondary" | ||
| > | ||
| Cancel | ||
| </Button> | ||
| </div> | ||
| )} | ||
| </div> | ||
|
|
||
| <div className="grid grid-cols-1 lg:grid-cols-2 gap-6"> | ||
| <Card> | ||
| <CardHeader> | ||
| <CardTitle className="flex items-center gap-2"> | ||
| <MapPinIcon className="w-5 h-5 text-blue-600 dark:text-blue-400" /> | ||
| Distance Unit | ||
| </CardTitle> | ||
| </CardHeader> | ||
| <CardContent className="space-y-4"> | ||
| <div className="flex gap-3"> | ||
| <button | ||
| onClick={() => updateUnit("km")} | ||
| className={`flex-1 p-4 rounded-lg border transition-colors text-left ${ | ||
| settings.unit === "km" | ||
| ? "bg-blue-500/10 border-blue-400/50" | ||
| : "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" | ||
| }`} | ||
| > | ||
| <div className="font-medium text-gray-900 dark:text-white">Metric</div> | ||
| <div className="text-sm text-gray-600 dark:text-gray-400 mt-1">Kilometers (km)</div> | ||
| </button> | ||
| <button | ||
| onClick={() => updateUnit("mi")} | ||
| className={`flex-1 p-4 rounded-lg border transition-colors text-left ${ | ||
| settings.unit === "mi" | ||
| ? "bg-blue-500/10 border-blue-400/50" | ||
| : "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" | ||
| }`} | ||
| > | ||
| <div className="font-medium text-gray-900 dark:text-white">Imperial</div> | ||
| <div className="text-sm text-gray-600 dark:text-gray-400 mt-1">Miles (mi)</div> | ||
| </button> | ||
| </div> | ||
|
|
||
| <div className="p-3 bg-blue-50 dark:bg-blue-950/30 rounded-lg"> | ||
| <div className="flex items-start gap-2"> | ||
| <MapPinIcon className="w-4 h-4 text-blue-600 dark:text-blue-400 mt-0.5 flex-shrink-0" /> | ||
| <div className="text-sm"> | ||
| <div className="font-medium text-blue-900 dark:text-blue-300"> | ||
| Preview | ||
| </div> | ||
| <div className="text-blue-700 dark:text-blue-400 mt-1"> | ||
| 100 km = <span className="font-mono">{formatDistance(100, settings)}</span> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </CardContent> | ||
| </Card> | ||
| </div> | ||
|
|
||
| <Card> | ||
| <CardContent className="p-6"> | ||
| <div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4"> | ||
| <div> | ||
| <h4 className="font-medium text-gray-900 dark:text-white"> | ||
| Quick Actions | ||
| </h4> | ||
| <p className="text-sm text-gray-600 dark:text-gray-400 mt-1"> | ||
| Reset settings or apply common configurations | ||
| </p> | ||
| </div> | ||
| <div className="flex items-center gap-2"> | ||
| <Button | ||
| onClick={resetToDefaults} | ||
| variant="outline" | ||
| > | ||
| Reset to Defaults | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| </CardContent> | ||
| </Card> | ||
| </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
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,74 @@ | ||
| /* | ||
| * Copyright (c) 2024-2025, s0up and the autobrr contributors. | ||
| * SPDX-License-Identifier: GPL-2.0-or-later | ||
| */ | ||
|
|
||
| import React from "react"; | ||
|
|
||
| export type DistanceUnit = "km" | "mi"; | ||
|
|
||
| export interface DistanceSettings { | ||
| unit: DistanceUnit; | ||
| } | ||
|
|
||
| const DISTANCE_SETTINGS_KEY = "netronome-distance-settings"; | ||
| const DEFAULT_SETTINGS: DistanceSettings = { | ||
| unit: "km", | ||
| }; | ||
|
|
||
| const KM_TO_MI = 0.621371; | ||
|
|
||
| export const getDistanceSettings = (): DistanceSettings => { | ||
| try { | ||
| const saved = localStorage.getItem(DISTANCE_SETTINGS_KEY); | ||
| if (saved) { | ||
| const settings = JSON.parse(saved); | ||
| return { | ||
| unit: settings.unit === "mi" ? "mi" : "km", | ||
| }; | ||
| } | ||
| } catch (error) { | ||
| console.warn("Failed to load distance settings from localStorage:", error); | ||
| } | ||
| return DEFAULT_SETTINGS; | ||
| }; | ||
|
|
||
| export const saveDistanceSettings = (settings: DistanceSettings): void => { | ||
| try { | ||
| localStorage.setItem(DISTANCE_SETTINGS_KEY, JSON.stringify(settings)); | ||
| window.dispatchEvent(new CustomEvent("distanceSettingsChanged", { detail: settings })); | ||
| } catch (error) { | ||
| console.warn("Failed to save distance settings to localStorage:", error); | ||
| } | ||
| }; | ||
|
|
||
| export const formatDistance = (distanceKm: number, settings?: DistanceSettings): string => { | ||
| const currentSettings = settings || getDistanceSettings(); | ||
| if (currentSettings.unit === "mi") { | ||
| return `${Math.round(distanceKm * KM_TO_MI)} mi`; | ||
| } | ||
| return `${Math.round(distanceKm)} km`; | ||
| }; | ||
|
|
||
| export const useDistanceSettings = (): { settings: DistanceSettings; updateSettings: (newSettings: DistanceSettings) => void } => { | ||
| const [settings, setSettings] = React.useState<DistanceSettings>(getDistanceSettings); | ||
|
|
||
| React.useEffect(() => { | ||
| const handleSettingsChange = (event: CustomEvent<DistanceSettings>) => { | ||
| setSettings(event.detail); | ||
| }; | ||
|
|
||
| window.addEventListener("distanceSettingsChanged", handleSettingsChange as EventListener); | ||
|
|
||
| return () => { | ||
| window.removeEventListener("distanceSettingsChanged", handleSettingsChange as EventListener); | ||
| }; | ||
| }, []); | ||
|
|
||
| const updateSettings = (newSettings: DistanceSettings) => { | ||
| saveDistanceSettings(newSettings); | ||
| setSettings(newSettings); | ||
| }; | ||
|
|
||
| return { settings, updateSettings }; | ||
| }; |
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.