import type { ServiceAllowance, ServiceIdentityState, ServiceIdentityStatus, ServiceQuotaView } from '@trek/shared'; import { RefreshCw } from 'lucide-react'; import { useRef, useState } from 'react'; import { useTranslation } from '../../i18n'; import { useSettingsStore } from '../../store/settingsStore'; import type { TranslationFn } from '../../types'; import { getApiErrorMessage } from '../../utils/apiError'; import { formatClockTime } from '../../utils/formatters'; import ConfirmDialog from '../shared/ConfirmDialog'; import { useToast } from '../shared/Toast'; import { useServiceIdentity } from './useServiceIdentity'; const STATE_DOT: Record = { connected: 'bg-success', registering: 'bg-warning motion-safe:animate-pulse', legacy: 'bg-[var(--text-faint)]', off: 'bg-[var(--text-faint)]', revoked: 'bg-danger', }; /** Below a tenth of the day left the bar turns amber, at nothing left red. */ function barTone(quota: ServiceQuotaView): string { if (quota.remaining === 0) return 'bg-danger'; return quota.remaining < quota.limit / 10 ? 'bg-warning' : 'bg-accent'; } function Stat({ label, value }: { label: string; value: string }) { return (
{label}
{value}
); } function QuotaMeter({ quota, label }: { quota: ServiceQuotaView; label: string }) { const { t, locale } = useTranslation(); const is12h = useSettingsStore((s) => s.settings.time_format === '12h'); const number = new Intl.NumberFormat(locale); const share = quota.limit > 0 ? Math.min(100, (quota.used / quota.limit) * 100) : 100; const reset = new Date(quota.resetAt); const resetAt = formatClockTime( `${String(reset.getHours()).padStart(2, '0')}:${String(reset.getMinutes()).padStart(2, '0')}`, is12h ); return (
); } function rowNote(t: TranslationFn, allowance: ServiceAllowance): string | null { if (!allowance.inUse) return t('admin.trekApi.quota.notInUse'); return allowance.quota ? null : t('admin.trekApi.quota.unavailable'); } /** * One service under the key: its name, today's allowance where its host told * it, and whether this instance asks it at all. A service not in use is * dimmed rather than hidden, so the list reads the same on every install. */ function ServiceRow({ allowance }: { allowance: ServiceAllowance }) { const { t } = useTranslation(); const name = t(`admin.trekApi.quota.service.${allowance.service}`); const note = rowNote(t, allowance); return (
  • {name}

    {allowance.quota && } {note &&

    {note}

    }
  • ); } function stateHint(t: TranslationFn, status: ServiceIdentityStatus | null, failed: boolean): string | null { if (!status) return failed ? t('admin.trekApi.quota.unavailable') : null; return status.state === 'connected' ? null : t(`admin.trekApi.quota.hint.${status.state}`); } /** * How this installation stands with the TREK services: whether it has its key, * and for place search, address search and routing today's allowance against * what is used, and when the count starts again. * Read when the card is in view and refreshed every 30 seconds while it stays. * A managed install leaves the identity to its operator, so it offers no new one. */ export default function ServiceIdentityPanel({ managed = false }: { managed?: boolean }) { const { t } = useTranslation(); const toast = useToast(); const panel = useRef(null); const { status, failed, regenerating, regenerate } = useServiceIdentity(panel); const [confirming, setConfirming] = useState(false); const confirmRegenerate = async () => { setConfirming(false); try { await regenerate(); toast.success(t('admin.trekApi.quota.regenerated')); } catch (err) { toast.error(getApiErrorMessage(err, t('common.error'))); } }; const hint = stateHint(t, status, failed); const canRegenerate = !managed && status !== null && status.keySource !== 'env' && status.state !== 'off'; return (

    {status?.tier && ( {t(`admin.trekApi.quota.tier.${status.tier}`)} )}
    {status && status.services.length > 0 && (
      {status.services.map((allowance) => ( ))}
    )} {hint &&

    {hint}

    } {status?.keySource === 'env' && (

    {t('admin.trekApi.quota.envKey')}

    )} {canRegenerate && ( )} setConfirming(false)} onConfirm={() => void confirmRegenerate()} title={t('admin.trekApi.quota.regenerateTitle')} message={t('admin.trekApi.quota.regenerateBody')} confirmLabel={t('admin.trekApi.quota.regenerate')} danger={false} />
    ); }