Files
TREK/client/src/components/Settings/DawarichConnectionSection.tsx
T
Maurice 44c6fbf372 fix(settings): let every date follow a date format of the user's choice
Every date in TREK was written in the interface language's format, and
English is en-US, so an English interface always showed and read
mm/dd/yyyy with no way to change it. A few native date inputs followed
the browser instead, so one screen could mix two formats.

date_format is now a user setting with an admin default, next to the
time format: automatic (the language's format, as before), YYYY-MM-DD,
YYYY/MM/DD, DD.MM.YYYY, DD/MM/YYYY or MM/DD/YYYY. The date picker shows,
pre-fills, hints and reads typed dates in it, and every date written in
numbers across the app (lists, cards, admin panels, CSV and PDF exports)
goes through one formatter. Dates written with a month name keep the
language's wording. The remaining native date inputs are the shared
picker now. The display settings MCP tool reads and writes the key.

Closes #2971
2026-11-03 23:38:37 +00:00

188 lines
6.9 KiB
TypeScript

import React from 'react'
import { Loader2, Save, Plug, RefreshCw, Unplug } from 'lucide-react'
import { useTranslation } from '../../i18n'
import { useFormatDate } from '../../hooks/useDateFormat'
import { useDawarichConnection } from '../../hooks/useDawarichConnection'
import DawarichIcon from '../shared/DawarichIcon'
import Section from './Section'
import ToggleSwitch from './ToggleSwitch'
import { SETTINGS_BUTTON, SETTINGS_BUTTON_DANGER, SETTINGS_BUTTON_PRIMARY, SettingRow, SettingRows, SettingsHint, StatusPill } from './settingsKit'
import { EditorField, GRID_2, INPUT } from '../shared/dialogParts'
import { fs } from '../shared/DialogShell'
/**
* Settings → Integrations → Dawarich.
*
* All of the behaviour is in `useDawarichConnection`, shared with the phone
* twin; this file is markup. The layout follows the AirTrail and LLM sections
* so the three read as one shelf: fields side by side, the switches as rows,
* the actions under them and the connection state as a pill in the band.
*
* The status block below the fields is the part that earns its space: a
* connection to somebody else's server fails in ways they can act on — a wrong
* key, a certificate, an instance that is simply off — and "not connected" with
* no reason is the version of this card that generates support questions.
*/
export default function DawarichConnectionSection(): React.ReactElement {
const { t } = useTranslation()
const S = useDawarichConnection()
return (
<Section
title={t('dawarich.title')}
icon={DawarichIcon}
badge={
<StatusPill tone={S.connected ? 'success' : 'neutral'} icon={<span className="h-1.5 w-1.5 rounded-full bg-current" />}>
{S.connected ? t('dawarich.connected') : t('dawarich.notConnected')}
</StatusPill>
}
>
<SettingsHint>{t('dawarich.intro')}</SettingsHint>
<div className={GRID_2}>
<EditorField label={t('dawarich.url')} htmlFor="dawarich-url">
<input
id="dawarich-url"
type="url"
value={S.url}
onChange={e => S.setUrl(e.target.value)}
placeholder="https://dawarich.example.com"
className={INPUT}
/>
</EditorField>
<EditorField label={t('dawarich.apiKey')} htmlFor="dawarich-key" hint={t('dawarich.apiKeyHint')}>
<input
id="dawarich-key"
type="password"
value={S.apiKey}
onChange={e => S.setApiKey(e.target.value)}
autoComplete="off"
placeholder={S.connected && !S.apiKey ? '••••••••' : t('dawarich.apiKeyPlaceholder')}
className={INPUT}
/>
</EditorField>
</div>
<SettingRows>
<SettingRow
label={t('dawarich.syncEnabled')}
hint={t('dawarich.syncEnabledHint')}
control={<ToggleSwitch on={S.syncEnabled} onToggle={S.toggleSync} label={t('dawarich.syncEnabled')} />}
/>
<SettingRow
label={t('dawarich.allowInsecureTls')}
hint={t('dawarich.allowInsecureTlsHint')}
control={<ToggleSwitch on={S.allowInsecureTls} onToggle={S.toggleInsecureTls} label={t('dawarich.allowInsecureTls')} />}
/>
</SettingRows>
<div className="flex flex-wrap items-center gap-2" style={fs(13, 'body')}>
<button
type="button"
onClick={S.save}
disabled={S.saving || S.loading || !S.canSave}
className={SETTINGS_BUTTON_PRIMARY}
>
<Save size={14} strokeWidth={2.2} /> {t('common.save')}
</button>
<button
type="button"
onClick={S.test}
disabled={S.testing || S.loading || !S.url.trim()}
className={SETTINGS_BUTTON}
>
{S.testing
? <Loader2 size={14} strokeWidth={2.2} className="animate-spin" />
: <Plug size={14} strokeWidth={2.2} />}
{t('dawarich.test.button')}
</button>
{S.connected && (
<button
type="button"
onClick={S.syncNow}
disabled={S.syncing}
className={SETTINGS_BUTTON}
>
<RefreshCw size={14} strokeWidth={2.2} className={S.syncing ? 'animate-spin' : ''} />
{t('dawarich.syncNow')}
</button>
)}
{/* The way out sits apart from the everyday actions, on the far right. */}
{S.connected && (
<button
type="button"
onClick={S.disconnect}
disabled={S.saving}
className={`${SETTINGS_BUTTON_DANGER} ms-auto`}
>
<Unplug size={14} strokeWidth={2.2} /> {t('dawarich.disconnect')}
</button>
)}
</div>
<DawarichConnectionStatus state={S} />
</Section>
)
}
/**
* Where the connection stands, in the place where it helps someone decide what
* to do next: what the last sync did, and which parts of their instance TREK
* could actually reach.
*
* Its own component because the phone twin renders the same three facts in a
* different frame, and because it is the bit that keeps growing.
*/
function DawarichConnectionStatus({
state,
}: {
state: ReturnType<typeof useDawarichConnection>
}): React.ReactElement | null {
const { t } = useTranslation()
const { formatDateTime } = useFormatDate()
if (!state.connected && !state.probeMessage) return null
const missing: string[] = []
if (state.capabilities) {
if (!state.capabilities.visits) missing.push(t('dawarich.capability.visits'))
if (!state.capabilities.tracks && !state.capabilities.points) missing.push(t('dawarich.capability.track'))
if (!state.capabilities.locations) missing.push(t('dawarich.capability.locations'))
if (!state.capabilities.visitedCities) missing.push(t('dawarich.capability.visitedCities'))
}
return (
<div className="flex flex-col gap-1 rounded-[12px] border border-edge-faint bg-surface-card px-3.5 py-3" style={fs(12, 'body')}>
{state.probeMessage && <p className="m-0 font-medium text-content">{state.probeMessage}</p>}
{state.connected && (
<p className="m-0 text-content-secondary">
{state.lastSyncAt
? t('dawarich.lastSync', { when: formatDateTime(new Date(state.lastSyncAt)) })
: t('dawarich.neverSynced')}
{state.lastSyncState === 'partial' && `, ${t('dawarich.syncPartial')}`}
</p>
)}
{state.lastSyncError && (
<p className="m-0 text-danger">{state.lastSyncError}</p>
)}
{state.capabilities?.serverVersion && (
<p className="m-0 font-geist tabular-nums text-content-muted">
{t('dawarich.serverVersion', { version: state.capabilities.serverVersion })}
</p>
)}
{missing.length > 0 && (
<p className="m-0 text-content-muted">
{t('dawarich.capability.missing', { features: missing.join(', ') })}
</p>
)}
</div>
)
}