feat(sync): the folder loop, the hooks and the panel — it can be switched on now

The three layers underneath were already there and unreachable: the change-log
format (7d664bd), the identity column (724e68b) and the no-backfill decision
(c48294b). This is the wiring that gives the operator a switch.

Settings, all profile-scoped, because each profile can point at its own logbook:
the folder, this PC's name, the machine id minted once from it, the per-peer read
offsets and the tie-break counter. Scoping the machine id is what keeps two
profiles sharing one folder from writing two logbooks into one file.

Three hooks. Add and update publish asynchronously, down with the rest of the
after-the-fact work — a folder on a network share can block for seconds and a
contact belongs on screen long before another machine hears about it. Deletion
publishes SYNCHRONOUSLY and BEFORE the row goes, for the same reason
deleteRemoteCopies does: once it is gone its identity is gone with it and the
tombstone names nothing.

The apply path uses the repository directly and never AddQSO/UpdateQSO/DeleteQSO
— those publish, and a change applied here would be written straight back out,
two machines echoing each other for ever.

Saving writes a probe file to the chosen folder rather than asking whether it
exists. A read-only cloud folder, or a share whose credentials expired, exists
perfectly well and would swallow every contact in silence; if the probe fails the
switch goes back off instead of sitting on while nothing is written.

The panel is mostly status, and deliberately: every part of this runs on another
machine and on a sync client OpsLog cannot see, so "it is not working" has to be
answerable from the settings page — which PCs are in the folder, when each last
logged, what is waiting unread.

Four tests on the apply path, the middle two being the ones that matter: an edit
made on the other PC lands on the copy already here, matched on the contact
itself, instead of becoming a second row — that is what makes the no-backfill
decision safe — and a contact with the same station on another band stays a
separate contact.
This commit is contained in:
2026-08-17 01:34:00 +02:00
parent eab11db766
commit 91b21a4a36
9 changed files with 979 additions and 2 deletions
+119
View File
@@ -51,6 +51,7 @@ import {
GetUIPref, SetUIPref,
GetFlexState, GetFlexBandAntennas, SaveFlexBandAntennas, GetFlexBandPower, SaveFlexBandPower,
GetADIFMonitor, SaveADIFMonitor, PickADIFMonitorFile,
GetFolderSync, SaveFolderSync, PickFolderSyncFolder, GetFolderSyncStatus, SyncFolderNow,
GetRelayAuto, SaveRelayAuto, GetStationDevices,
GetAwardDefs, GetTrackedAwards, SaveTrackedAwards,
GetBandOpenSettings, SaveBandOpenSettings, GetPSKReporterStatus, GetChaseNewGrids, SetChaseNewGrids, GetChaseNew, SetChaseNew, GetGridCacheStatus, GetLinkedAmps, SetLinkedAmps, GetSpotTTLMinutes, SetSpotTTLMinutes,
@@ -188,6 +189,7 @@ type SectionId =
| 'external-services'
| 'udp'
| 'adifmon'
| 'foldersync'
| 'webpublish'
| 'lookup'
| 'lists-bands'
@@ -305,6 +307,7 @@ function buildTree(flexAvailable: boolean, t: (k: string) => string): TreeNode[]
{ kind: 'item', label: t('sec.cluster'), id: 'cluster' },
{ kind: 'item', label: t('sec.udp'), id: 'udp' },
{ kind: 'item', label: t('sec.adifmon'), id: 'adifmon' },
{ kind: 'item', label: t('sec.foldersync'), id: 'foldersync' },
{ kind: 'item', label: t('sec.webpublish'), id: 'webpublish' },
{ kind: 'item', label: t('sec.uscounties'), id: 'uscounties' },
{ kind: 'item', label: t('sec.database'), id: 'database' },
@@ -323,6 +326,7 @@ const SECTION_KEY: Partial<Record<SectionId, string>> = {
'external-services': 'sec.external', appearance: 'sec.appearance', lookup: 'sec.lookup', 'lists-bands': 'sec.bands', 'lists-modes': 'sec.modes',
cluster: 'sec.cluster', backup: 'sec.backup', database: 'sec.database', autostart: 'sec.autostart', udp: 'sec.udp',
adifmon: 'sec.adifmon',
foldersync: 'sec.foldersync',
webpublish: 'sec.webpublish',
uscounties: 'sec.uscounties',
awards: 'sec.awards', cat: 'sec.cat', rotator: 'sec.rotator', winkeyer: 'sec.winkeyer', antenna: 'sec.antenna',
@@ -705,6 +709,120 @@ function ADIFMonitorPanel() {
);
}
// FolderSyncPanel: one operator, several PCs, one logbook through a folder they
// already synchronise (Seafile, OneDrive, Dropbox, a NAS share).
//
// The status half is the point of the panel. Every part of this runs on someone
// else's machine and on a sync client OpsLog cannot see, so "it is not working"
// has to be answerable from here: which other PCs have written to the folder,
// when each last did, and whether anything is sitting there unread.
function FolderSyncPanel() {
const { t } = useI18n();
const [cfg, setCfg] = useState<{ enabled: boolean; folder: string; machine: string }>({ enabled: false, folder: '', machine: '' });
const [st, setSt] = useState<any>(null);
const [loaded, setLoaded] = useState(false);
const [err, setErr] = useState('');
const [msg, setMsg] = useState('');
const [busy, setBusy] = useState(false);
useEffect(() => {
GetFolderSync()
.then((c: any) => { if (c) setCfg({ enabled: !!c.enabled, folder: c.folder ?? '', machine: c.machine ?? '' }); })
.catch(() => {})
.finally(() => setLoaded(true));
}, []);
// Polled while the panel is open: a first setup is verified by watching the
// other PC appear in this list, and that happens on the sync client's clock,
// not on any action taken here.
useEffect(() => {
let alive = true;
const tick = () => GetFolderSyncStatus().then((s: any) => { if (alive) setSt(s); }).catch(() => {});
tick();
const h = window.setInterval(tick, 3000);
return () => { alive = false; window.clearInterval(h); };
}, []);
const save = async (next: typeof cfg) => {
setCfg(next);
setErr(''); setMsg('');
try {
await SaveFolderSync(next as any);
setMsg(t('sync.saved'));
GetFolderSyncStatus().then(setSt).catch(() => {});
} catch (e: any) {
setErr(String(e?.message ?? e));
// The switch goes back off rather than sitting on while nothing is
// written: a folder that refused the write test would lose every contact
// in silence, which is the one outcome this feature must never have.
setCfg({ ...next, enabled: false });
}
};
const pick = async () => {
try {
const p = await PickFolderSyncFolder();
if (p) save({ ...cfg, folder: p });
} catch { /* dialog cancelled */ }
};
const syncNow = async () => {
setBusy(true); setErr(''); setMsg('');
try {
const n = await SyncFolderNow();
setMsg(t('sync.applied').replace('{n}', String(n)));
GetFolderSyncStatus().then(setSt).catch(() => {});
} catch (e: any) {
setErr(String(e?.message ?? e));
} finally { setBusy(false); }
};
const when = (iso: string) => (iso ? new Date(iso).toLocaleString() : t('sync.never'));
return (
<div className="space-y-4 max-w-2xl">
<p className="text-xs text-muted-foreground">{t('sync.hint')}</p>
<label className="flex items-center gap-2 text-sm cursor-pointer">
<Checkbox checked={cfg.enabled} disabled={!loaded} onCheckedChange={(c) => save({ ...cfg, enabled: !!c })} />
{t('sync.enable')}
</label>
<div className="grid grid-cols-[130px_1fr] items-center gap-2">
<span className="text-sm">{t('sync.machine')}</span>
<Input value={cfg.machine} placeholder="shack" className="h-8"
onChange={(e) => setCfg({ ...cfg, machine: e.target.value })}
onBlur={() => save(cfg)} />
<span className="text-sm">{t('sync.folder')}</span>
<div className="flex items-center gap-2 min-w-0">
<span className="flex-1 font-mono text-xs truncate" title={cfg.folder}>{cfg.folder || '—'}</span>
<Button variant="outline" size="sm" onClick={pick}><FolderOpen className="size-3.5 mr-1" /> {t('sync.choose')}</Button>
</div>
</div>
{err && <p className="text-xs text-destructive">{err}</p>}
{msg && <p className="text-xs text-success">{msg}</p>}
<SectionHeader title={t('sync.state')} />
<div className="rounded-md border border-border bg-muted/20 p-3 space-y-2 text-xs">
<div className="flex flex-wrap gap-x-6 gap-y-1 text-muted-foreground">
<span>{t('sync.thisPc')}: <span className="font-mono text-foreground">{st?.machine_id || '—'}</span></span>
<span>{t('sync.lastSync')}: <span className="text-foreground">{when(st?.last_sync ?? '')}</span></span>
<span>{t('sync.sent')}: <span className="text-foreground">{st?.sent ?? 0}</span></span>
<span>{t('sync.received')}: <span className="text-foreground">{st?.received ?? 0}</span></span>
</div>
{st?.error && <p className="text-destructive">{st.error}</p>}
<div className="space-y-1">
{(st?.peers ?? []).length === 0 && <p className="italic text-muted-foreground">{t('sync.noPeers')}</p>}
{(st?.peers ?? []).map((p: any) => (
<div key={p.machine} className="flex items-center gap-2">
<span className="font-mono truncate">{p.machine}</span>
<span className="text-muted-foreground">{when(p.last_change)}</span>
{p.behind > 0 && <span className="text-warning">{t('sync.behind')}</span>}
</div>
))}
</div>
</div>
<Button variant="outline" size="sm" onClick={syncNow} disabled={busy || !cfg.enabled}>
{busy ? <Loader2 className="size-3.5 animate-spin mr-1.5" /> : null}
{t('sync.now')}
</Button>
</div>
);
}
// AmpUI mirrors the backend AmpConfig — one configured amplifier.
type AmpUI = { id: string; name: string; enabled: boolean; type: string; transport: string; host: string; port: number; com_port: string; baud: number;
// Band-follow (ACOM): a SECOND serial port on which OpsLog answers the amp's
@@ -6426,6 +6544,7 @@ export function SettingsModal({ onClose, onSaved, initialSection, onMainPaneChan
// panels below go through PanelHost instead — which is what now lets either
// kind hold hooks.
adifmon: () => <ADIFMonitorPanel />,
foldersync: () => <FolderSyncPanel />,
webpublish: () => <WebPublishPanel />,
relayauto: () => <RelayAutoPanel />,
backup: BackupPanel,