mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-02 21:25:26 +09:00
en.json drops from 8107 to 6624 keys: repeated UI copy (Cancel x95, Status x67, Refresh x54, ...) now lives under common.actions / common.labels / common.loading, and extractor-era suffix duplicates (refresh2, status3) within a component collapse onto one key. Trailing ... defaults are normalized to the ellipsis character. Translations are preserved: all 16 locales were re-keyed, carrying existing per-component translations onto their new common.* keys (majority vote where sources disagreed), and stale keys were pruned. Verified: npm run lint (0 errors), node --test (140 pass), vite build. edit
151 lines
6.4 KiB
React
151 lines
6.4 KiB
React
import { Pill, MetricCard, KpiBand, statusKind, statusLabel } from '@/components/ds';
|
|
import { Switch } from '@/components/ui/switch';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Archive, Clock, DollarSign, HardDrive, Play, ExternalLink, Loader2 } from 'lucide-react';
|
|
import { humanSize, formatMoney, formatWhen, formatDateTime } from './format';
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
// Card 1 of the backup "Protection" panel: a presentational status summary.
|
|
// Derives a single status (Protected / Pending / Failed / Running / Off) from
|
|
// the policy snapshot, then renders the toggle, status line, KPIs, and actions.
|
|
// All data is pre-shaped by the parent (policyView); this component holds no
|
|
// state and triggers behavior purely through the supplied callbacks.
|
|
|
|
// Pill colour + label come from the ONE shared status vocabulary (ds/status).
|
|
|
|
// Pick the status key from the policy snapshot. Failure wins over everything so
|
|
// a broken last run is never hidden behind an "enabled" badge.
|
|
function resolveStatus(policy, isRunning) {
|
|
if (policy.last_status === 'failed') return 'failed';
|
|
if (policy.enabled && (policy.last_status === 'running' || isRunning)) return 'running';
|
|
if (!policy.enabled) return 'off';
|
|
if (!policy.last_run) return 'pending';
|
|
if (policy.last_status === 'success') return 'protected';
|
|
// Enabled but no definitive status yet (e.g. queued first run).
|
|
return 'pending';
|
|
}
|
|
|
|
const ProtectionStatusCard = ({
|
|
policyView,
|
|
onToggle,
|
|
onBackupNow,
|
|
onViewGlobal,
|
|
onViewJobs,
|
|
busy,
|
|
backingUp,
|
|
}) => {
|
|
const { t } = useTranslation();
|
|
if (!policyView) {
|
|
return (
|
|
<div className="app-panel protection-status-card">
|
|
<div className="app-panel-header">
|
|
<Archive size={16} />
|
|
<span>{t('app.protectionStatusCard.protection', 'Protection')}</span>
|
|
</div>
|
|
<div className="app-panel-body" />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const policy = policyView.policy || {};
|
|
const enabled = !!policy.enabled;
|
|
const isRunning = !!policyView.is_running;
|
|
const nextRunAt = policyView.next_run_at;
|
|
|
|
const statusKey = resolveStatus(policy, isRunning);
|
|
const statusPill = <Pill kind={statusKind(statusKey)}>{statusLabel(statusKey)}</Pill>;
|
|
|
|
const subtitle = enabled
|
|
? 'Backups run on the configured schedule.'
|
|
: 'Turn on to back up this site automatically.';
|
|
|
|
// Inline status line (and, for the failed state, a deep-link to Jobs).
|
|
let statusLine = null;
|
|
let statusAction = null;
|
|
if (statusKey === 'failed') {
|
|
statusLine = `Last backup failed at ${formatWhen(policy.last_run_at)}`;
|
|
statusAction = (
|
|
<Button variant="ghost" size="sm" onClick={onViewJobs}>
|
|
{t('app.protectionStatusCard.viewInJobs', 'View in Jobs')}
|
|
</Button>
|
|
);
|
|
} else if (statusKey === 'running') {
|
|
statusLine = 'A backup is in progress…';
|
|
} else if (statusKey === 'protected') {
|
|
const cost = formatMoney((policy.last_cost_local || 0) + (policy.last_cost_remote || 0));
|
|
statusLine = `Last backup: ${formatWhen(policy.last_run_at)} · ${humanSize(policy.last_size)} · ${cost}`;
|
|
} else if (statusKey === 'pending') {
|
|
statusLine = nextRunAt
|
|
? `First backup will run at ${formatDateTime(nextRunAt)}`
|
|
: 'First backup will run soon';
|
|
} else {
|
|
statusLine = 'Backups are not running automatically';
|
|
}
|
|
|
|
const nextBackupValue = enabled && nextRunAt ? formatDateTime(nextRunAt) : '—';
|
|
|
|
return (
|
|
<div className="app-panel protection-status-card">
|
|
<div className="app-panel-header">
|
|
<Archive size={16} />
|
|
<span>{t('app.protectionStatusCard.protection', 'Protection')}</span>
|
|
<span className="app-panel-header-actions">{statusPill}</span>
|
|
</div>
|
|
<div className="app-panel-body">
|
|
<div className="protection-status-card__toggle">
|
|
<Switch
|
|
id="auto-backups"
|
|
checked={enabled}
|
|
onCheckedChange={onToggle}
|
|
disabled={busy}
|
|
/>
|
|
<label htmlFor="auto-backups">
|
|
<span className="protection-status-card__toggle-title">{t('app.protectionStatusCard.automaticBackups', 'Automatic backups')}</span>
|
|
<span className="protection-status-card__toggle-sub">{subtitle}</span>
|
|
</label>
|
|
</div>
|
|
|
|
<div className="protection-status-card__status">
|
|
{statusPill}
|
|
<span>{statusLine}</span>
|
|
{statusAction}
|
|
</div>
|
|
|
|
<KpiBand>
|
|
<MetricCard
|
|
tone="accent"
|
|
icon={<Clock size={16} />}
|
|
value={nextBackupValue}
|
|
label={t('app.protectionStatusCard.nextBackup', 'Next backup')}
|
|
/>
|
|
<MetricCard
|
|
tone="green"
|
|
icon={<DollarSign size={16} />}
|
|
value={policyView.monthly_cost_display}
|
|
label={t('app.protectionStatusCard.monthlyCost', 'Monthly cost')}
|
|
/>
|
|
<MetricCard
|
|
tone="cyan"
|
|
icon={<HardDrive size={16} />}
|
|
value={policyView.storage_used_human}
|
|
label={t('app.protectionStatusCard.storageUsed', 'Storage used')}
|
|
/>
|
|
</KpiBand>
|
|
|
|
<div className="protection-status-card__actions">
|
|
<Button variant="primary" size="sm" onClick={onBackupNow} disabled={backingUp}>
|
|
{backingUp ? <Loader2 size={14} className="spin" /> : <Play size={14} />}
|
|
{t('app.protectionStatusCard.backUpNow', 'Back up now')}
|
|
</Button>
|
|
<Button variant="ghost" size="sm" onClick={onViewGlobal}>
|
|
<ExternalLink size={14} />
|
|
{t('app.protectionStatusCard.viewInGlobalBackups', 'View in global backups')}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default ProtectionStatusCard;
|