Files
TREK/client/src/components/Admin/serviceIdentityModel.ts
T
Maurice a3620f9e05 feat(mobile): the admin services section gets what the desktop tab has
- Place search and routing as one card, as on the desktop: TREK's own
  services, today's allowance with a new identity behind a confirm sheet
  (MAdminServiceQuota), sharing place details (MAdminPlaceContrib), the transit
  backend and the place search provider as TREK selects, and the search log.
- The key card holds Google (with its options and the daily request cap,
  MAdminGoogleDailyLimit), Unsplash and Amap, in the desktop order.
- The logic both views need moves into one place: the transit switch into
  useAdminSettingsActions, the allowance figures into serviceIdentityModel,
  and the share-place-details panel into usePlaceContribPanel.
- MAdminSettingsSection renders each section in a function of its own.
- Tests follow the split admin sections (Save order, 2FA label, field marker).
2026-11-14 11:09:22 +01:00

50 lines
2.4 KiB
TypeScript

import type { ServiceAllowance, ServiceIdentityStatus, ServiceQuotaView } from '@trek/shared';
import type { TranslationFn } from '../../types';
import { formatClockTime } from '../../utils/formatters';
/**
* What the allowance panels (desktop ServiceIdentityPanel, phone MAdminServiceQuota)
* read from the service identity status. Pure, so both views show the same thing.
*/
/** How much of a service's day is left: plenty, below a tenth, or nothing. */
export type QuotaLevel = 'ok' | 'low' | 'out';
export function quotaLevel(quota: ServiceQuotaView): QuotaLevel {
if (quota.remaining === 0) return 'out';
return quota.remaining < quota.limit / 10 ? 'low' : 'ok';
}
/** The share of the day used, for the bar; a quota without a limit reads as full. */
export function quotaShare(quota: ServiceQuotaView): number {
return quota.limit > 0 ? Math.min(100, (quota.used / quota.limit) * 100) : 100;
}
/** The clock time the counts start again, in the user's format; one for all services, which share the day. */
export function quotaResetClock(status: ServiceIdentityStatus | null, is12h: boolean): string | null {
const at = status?.services.find((s) => s.quota)?.quota?.resetAt;
if (!at) return null;
const reset = new Date(at);
return formatClockTime(
`${String(reset.getHours()).padStart(2, '0')}:${String(reset.getMinutes()).padStart(2, '0')}`,
is12h
);
}
/** A service that is not in use, or whose count the service did not report, says so under its row. */
export function allowanceNote(t: TranslationFn, allowance: ServiceAllowance): string | null {
if (!allowance.inUse) return t('admin.trekApi.quota.notInUse');
return allowance.quota ? null : t('admin.trekApi.quota.unavailable');
}
/** A line about the identity itself, when it is not simply connected (or could not be read). */
export function identityHint(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}`);
}
/** A new key can be asked for unless the operator holds it (managed, environment) or the services are off. */
export function canRegenerateKey(status: ServiceIdentityStatus | null, managed: boolean): boolean {
return !managed && status !== null && status.keySource !== 'env' && status.state !== 'off';
}