mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 09:38:54 +00:00
a7bd26722a
Saving one preference with the error toast on failure lives in one useSettingSaver hook; the desktop tab drops its eleven inline copies of it.
232 lines
8.8 KiB
TypeScript
232 lines
8.8 KiB
TypeScript
import { useState } from 'react'
|
|
import { ChevronDown, Languages, Map, Rocket } from 'lucide-react'
|
|
import { useTranslation, SUPPORTED_LANGUAGES } from '../../../i18n'
|
|
import { useSettingSaver } from '../../../components/Settings/useSettingSaver'
|
|
import { SYMBOLS, currenciesWith } from '../../../components/Budget/BudgetPanel.constants'
|
|
import { TRIP_TAB_IDS, TRIP_TAB_LABEL_KEYS, isTripTabId } from '../../../constants/tripTabs'
|
|
import { DEFAULT_START_PAGE, DEFAULT_START_TRIP_TAB, type StartPage } from '../../../utils/startDestination'
|
|
import type { Settings, DistanceUnit, WeekStart } from '../../../types'
|
|
import { DEFAULT_WEEK_START } from '@trek/shared'
|
|
import { weekStartOptions } from '../../../utils/calendarWeek'
|
|
import { MSetCard, MSetEyebrow, MSetHint, MSetSelectRow, MSetSegments, MSetRow } from './MSettingsUi'
|
|
import MToggle from '../../components/MToggle'
|
|
import MSetPickerSheet from './MSetPickerSheet'
|
|
import { preferredNavAppOptions } from '../../../components/Planner/placeNavigation'
|
|
|
|
/**
|
|
* "General" settings section — the demo's Language & region and Travel & map
|
|
* cards, wired to the real user preferences (DisplaySettingsTab parity).
|
|
*/
|
|
export default function MSettingsGeneral() {
|
|
const { t, locale } = useTranslation()
|
|
const { settings, save } = useSettingSaver()
|
|
const [currencyOpen, setCurrencyOpen] = useState(false)
|
|
const [langOpen, setLangOpen] = useState(false)
|
|
const [startTabOpen, setStartTabOpen] = useState(false)
|
|
const [navAppOpen, setNavAppOpen] = useState(false)
|
|
const navAppOptions = preferredNavAppOptions(t)
|
|
const navAppValue = settings.preferred_nav_app || ''
|
|
|
|
const startPage: StartPage = settings.start_page === 'active_trip' ? 'active_trip' : DEFAULT_START_PAGE
|
|
const startTripTab = isTripTabId(settings.start_trip_tab) ? settings.start_trip_tab : DEFAULT_START_TRIP_TAB
|
|
const currency = settings.default_currency || ''
|
|
const currencyLabel = currency ? `${currency} — ${SYMBOLS[currency] || currency}` : t('settings.currencyTrip')
|
|
const language = SUPPORTED_LANGUAGES.find((l) => l.value === settings.language) || SUPPORTED_LANGUAGES[0]
|
|
|
|
const chevron = <ChevronDown size={13} strokeWidth={2} className="flex-none text-m-faint" />
|
|
|
|
const travelRows: {
|
|
key: keyof Settings
|
|
label: string
|
|
sub: string
|
|
on: boolean
|
|
value: (on: boolean) => boolean
|
|
}[] = [
|
|
{
|
|
key: 'day_date_first',
|
|
label: t('settings.dayDateFirst'),
|
|
sub: t('settings.dayDateFirstHint'),
|
|
on: settings.day_date_first === true,
|
|
value: (on) => on,
|
|
},
|
|
{
|
|
key: 'map_booking_labels',
|
|
label: t('settings.bookingLabels'),
|
|
sub: t('settings.bookingLabelsHint'),
|
|
on: settings.map_booking_labels === true,
|
|
value: (on) => on,
|
|
},
|
|
{
|
|
key: 'map_always_show_routes',
|
|
label: t('settings.alwaysShowRoutes'),
|
|
sub: t('settings.alwaysShowRoutesHint'),
|
|
on: settings.map_always_show_routes === true,
|
|
value: (on) => on,
|
|
},
|
|
{
|
|
key: 'map_compact_unplanned',
|
|
label: t('settings.compactUnplanned'),
|
|
sub: t('settings.compactUnplannedHint'),
|
|
on: settings.map_compact_unplanned === true,
|
|
value: (on) => on,
|
|
},
|
|
{
|
|
key: 'map_poi_pill_enabled',
|
|
label: t('settings.mapPoiPill'),
|
|
sub: t('settings.mapPoiPillHint'),
|
|
on: settings.map_poi_pill_enabled !== false,
|
|
value: (on) => on,
|
|
},
|
|
{
|
|
key: 'blur_booking_codes',
|
|
label: t('settings.blurBookingCodes'),
|
|
sub: t('mobileSettings.blurBookingCodesHint'),
|
|
on: !!settings.blur_booking_codes,
|
|
value: (on) => on,
|
|
},
|
|
{
|
|
key: 'optimize_from_accommodation',
|
|
label: t('settings.optimizeFromAccommodation'),
|
|
sub: t('settings.optimizeFromAccommodationHint'),
|
|
on: settings.optimize_from_accommodation !== false,
|
|
value: (on) => on,
|
|
},
|
|
]
|
|
|
|
return (
|
|
<>
|
|
<MSetCard title={t('settings.general.startup')} icon={Rocket}>
|
|
<MSetEyebrow className="mb-[6px]">{t('settings.startPage')}</MSetEyebrow>
|
|
<MSetSegments<StartPage>
|
|
value={startPage}
|
|
onChange={(v) => save('start_page', v)}
|
|
options={[
|
|
{ value: 'dashboard', label: t('settings.startPageDashboard') },
|
|
{ value: 'active_trip', label: t('settings.startPageActiveTrip') },
|
|
]}
|
|
/>
|
|
{startPage === 'active_trip' && (
|
|
<>
|
|
<MSetEyebrow className="mb-[5px] mt-[14px]">{t('settings.startTripTab')}</MSetEyebrow>
|
|
<MSetSelectRow
|
|
label={t(TRIP_TAB_LABEL_KEYS[startTripTab])}
|
|
trailing={chevron}
|
|
onClick={() => setStartTabOpen(true)}
|
|
/>
|
|
</>
|
|
)}
|
|
</MSetCard>
|
|
|
|
<MSetCard title={t('settings.general.languageRegion')} icon={Languages} className="mt-3">
|
|
<MSetEyebrow className="mb-[5px]">{t('settings.currency')}</MSetEyebrow>
|
|
<MSetSelectRow label={currencyLabel} trailing={chevron} onClick={() => setCurrencyOpen(true)} />
|
|
|
|
<MSetEyebrow className="mb-[5px] mt-[14px]">{t('settings.language')}</MSetEyebrow>
|
|
<MSetSelectRow label={language.label} trailing={chevron} onClick={() => setLangOpen(true)} />
|
|
|
|
<MSetEyebrow className="mb-[6px] mt-[14px]">{t('settings.temperature')}</MSetEyebrow>
|
|
<MSetSegments
|
|
value={settings.temperature_unit || 'celsius'}
|
|
onChange={(v) => save('temperature_unit', v)}
|
|
options={[
|
|
{ value: 'celsius', label: '°C Celsius' },
|
|
{ value: 'fahrenheit', label: '°F Fahrenheit' },
|
|
]}
|
|
/>
|
|
|
|
<MSetEyebrow className="mb-[6px] mt-[14px]">{t('settings.distance')}</MSetEyebrow>
|
|
<MSetSegments<DistanceUnit>
|
|
value={settings.distance_unit || 'metric'}
|
|
onChange={(v) => save('distance_unit', v)}
|
|
options={[
|
|
{ value: 'metric', label: 'km Metric' },
|
|
{ value: 'imperial', label: 'mi Imperial' },
|
|
]}
|
|
/>
|
|
|
|
<MSetEyebrow className="mb-[6px] mt-[14px]">{t('settings.timeFormat')}</MSetEyebrow>
|
|
<MSetSegments
|
|
value={settings.time_format || '24h'}
|
|
onChange={(v) => save('time_format', v)}
|
|
options={[
|
|
{ value: '24h', label: '24h' },
|
|
{ value: '12h', label: '12h' },
|
|
]}
|
|
/>
|
|
|
|
<MSetEyebrow className="mb-[6px] mt-[14px]">{t('settings.weekStart')}</MSetEyebrow>
|
|
<MSetSegments<WeekStart>
|
|
value={settings.week_start || DEFAULT_WEEK_START}
|
|
onChange={(v) => save('week_start', v)}
|
|
options={weekStartOptions(locale)}
|
|
/>
|
|
</MSetCard>
|
|
|
|
<MSetCard title={t('settings.general.travelMap')} icon={Map} className="mt-3">
|
|
<MSetEyebrow className="mb-[5px]">{t('settings.preferredNavApp')}</MSetEyebrow>
|
|
<MSetSelectRow
|
|
label={navAppOptions.find(o => o.value === navAppValue)?.label ?? navAppOptions[0].label}
|
|
trailing={chevron}
|
|
onClick={() => setNavAppOpen(true)}
|
|
/>
|
|
<MSetHint className="mb-[10px]">{t('settings.preferredNavAppHint')}</MSetHint>
|
|
<div className="-mt-[6px]">
|
|
{travelRows.map((row) => (
|
|
<MSetRow
|
|
key={row.key as string}
|
|
label={row.label}
|
|
sub={row.sub}
|
|
trailing={
|
|
<MToggle
|
|
checked={row.on}
|
|
onChange={(on) => save(row.key, row.value(on))}
|
|
ariaLabel={row.label}
|
|
/>
|
|
}
|
|
/>
|
|
))}
|
|
</div>
|
|
</MSetCard>
|
|
|
|
<MSetPickerSheet
|
|
open={currencyOpen}
|
|
onClose={() => setCurrencyOpen(false)}
|
|
title={t('settings.currency')}
|
|
value={currency}
|
|
onSelect={(v) => save('default_currency', v)}
|
|
options={[
|
|
{ value: '', label: t('settings.currencyTrip') },
|
|
...currenciesWith(currency).map((c) => ({ value: c, label: `${c} — ${SYMBOLS[c] || c}` })),
|
|
]}
|
|
/>
|
|
|
|
<MSetPickerSheet
|
|
open={langOpen}
|
|
onClose={() => setLangOpen(false)}
|
|
title={t('settings.language')}
|
|
value={settings.language}
|
|
onSelect={(v) => save('language', v)}
|
|
options={SUPPORTED_LANGUAGES.map((l) => ({ value: l.value, label: l.label }))}
|
|
/>
|
|
|
|
<MSetPickerSheet
|
|
open={navAppOpen}
|
|
onClose={() => setNavAppOpen(false)}
|
|
title={t('settings.preferredNavApp')}
|
|
value={navAppValue}
|
|
onSelect={(v) => save('preferred_nav_app', v)}
|
|
options={navAppOptions}
|
|
/>
|
|
|
|
<MSetPickerSheet
|
|
open={startTabOpen}
|
|
onClose={() => setStartTabOpen(false)}
|
|
title={t('settings.startTripTab')}
|
|
value={startTripTab}
|
|
onSelect={(v) => save('start_trip_tab', v)}
|
|
options={TRIP_TAB_IDS.filter(id => id !== 'tour-planner').map((id) => ({ value: id, label: t(TRIP_TAB_LABEL_KEYS[id]) }))}
|
|
/>
|
|
</>
|
|
)
|
|
}
|