mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-21 06:26:26 +00:00
2c2d008dc2
* fix(security): mint idempotency keys properly, and resolve the extractor by path Three findings out of a sweep of every open Sonar security issue. The other 49 turned out to be false positives and are documented in sonar-project.properties rather than changed — each entry scoped to a file, never a whole rule, so a future Math.random() on an auth path still fails. The idempotency key is the one that can lose data. Both producers fell back to Math.random when crypto.randomUUID was missing, and randomUUID needs a secure context — TREK's own quickstart is http://localhost:3000 and LAN installs run on http://192.168.x.x, so on the deployments the README documents the fallback is what actually runs. The axios one was `Math.random().toString(36).slice(2)`, which is not length-stable: 0.5 yields a single character and 0 yields the empty string, which the interceptor reads as "no key" and skips deduplication for entirely. Two writes sharing a key collapse into one — the second gets the first one's cached response and the edit is gone with no error anywhere. Both now come from utils/randomId, which has the getRandomValues rung that works in insecure contexts too. The extractor probe stored the bare binary name, so every extraction re-resolved it through whatever PATH held at that moment. The official image sets KITINERARY_EXTRACTOR_PATH and never reaches that branch, but a bare-metal install without it ran an unqualified name from a PATH it does not control. It now walks the search path itself and keeps the absolute path of the file it verified; execFileSync also drops the /bin/sh hop the old probe needed. PATH comes through app-config because process.env is walled off in src/. * fix: four tests and sorts that were not doing what they claimed Sonar reported 43 high-impact reliability issues. Reading all of them, four are real; the rest are cosmetic or would be made worse by the suggested change. budget.test.ts hand-built `paid: true`, but both emitters send `paid: body.paid ? 1 : 0` and the handler stores the value verbatim — the test asserted a payload the server never sends. The line below it already used a number. admin.controller.test.ts had a `.resolves` assertion with no await. The promise was dropped, so that line could not fail the test. The two to-do category lists sorted with a bare .sort(), which is byte order: every accented name files behind the whole ASCII range, so in a German trip "Übernachtung" lands after "Zelt". TREK ships 23 locales, so this is real data. One side effect worth knowing: katColor picks a category's dot colour by its index, so re-sorting can move which category shows which colour — it already moves whenever a category is added, and the colour carries no meaning. Deliberately not changed: the two `| 0` truncations (Math.trunc is not equivalent — `| 0` wraps at 32 bits and these carry millisecond timestamps), the two .map() callbacks whose second parameter is declared and used, and the sha1 in thumbnail.service (its own comment says a swap would orphan every existing thumbnail). * refactor: give every button a type, and prefer the Number statics Two mechanical sweeps, no behaviour change intended in either. A <button> with no type attribute is type="submit" per the HTML spec. Outside a form that does nothing today, but it turns into a surprise the moment the markup is wrapped in one, which is what Sonar's S9011 is warning about. 870 buttons get an explicit type="button". Two things were left alone on purpose: buttons that take a {...spread}, because the spread may carry a type and the merge order decides which wins, and anything inside a <form> — there the answer is a judgement call, and checking all eleven form files found every real submit button already carrying type="submit". The Number sweep covers only the three globals that are exactly equivalent to their Number twin: parseInt, parseFloat and NaN. isNaN is NOT in it — the global coerces its argument, so isNaN('abc') is true where Number.isNaN('abc') is false, and renaming it would change what the code does. It reaches server and plugin-sdk as well as the client; both copies of egress-policy.ts got the identical edit, so the mirror the SDK's parity test guards stays intact.
229 lines
12 KiB
TypeScript
229 lines
12 KiB
TypeScript
import { createPortal } from 'react-dom'
|
|
import { useEffect, useRef, useState } from 'react'
|
|
import { useNavigate } from 'react-router'
|
|
import { Loader2, CheckCircle2, AlertCircle, X } from 'lucide-react'
|
|
import { useTranslation } from '../../i18n'
|
|
import { addListener, removeListener } from '../../api/websocket'
|
|
import { reservationsApi, healthApi } from '../../api/client'
|
|
import { saveImportFiles } from '../../db/offlineDb'
|
|
import { useBackgroundTasksStore, type BackgroundImportTask } from '../../store/backgroundTasksStore'
|
|
|
|
/**
|
|
* Global, route-independent widget (bottom-right) that tracks background booking
|
|
* imports. Mounted once at the app root so it survives navigation. It listens to the
|
|
* user's WebSocket for import:progress / import:done / import:error and reflects each
|
|
* job; a finished job offers a "review" action that takes the user to the trip, where
|
|
* the per-item review flow opens. Polls running jobs as a backstop for missed pushes.
|
|
*/
|
|
export default function BackgroundTasksWidget() {
|
|
const { t } = useTranslation()
|
|
const navigate = useNavigate()
|
|
const tasks = useBackgroundTasksStore((s) => s.tasks)
|
|
const setProgress = useBackgroundTasksStore((s) => s.setProgress)
|
|
const setDone = useBackgroundTasksStore((s) => s.setDone)
|
|
const setError = useBackgroundTasksStore((s) => s.setError)
|
|
const requestReview = useBackgroundTasksStore((s) => s.requestReview)
|
|
const dismiss = useBackgroundTasksStore((s) => s.dismiss)
|
|
const addTask = useBackgroundTasksStore((s) => s.addTask)
|
|
|
|
const [aiParsing, setAiParsing] = useState(false)
|
|
useEffect(() => {
|
|
healthApi.features().then((f) => setAiParsing(!!f.aiParsing)).catch(() => setAiParsing(false))
|
|
}, [])
|
|
|
|
// Re-runs the same files with force-ai: the LLM sees every file, kitinerary is skipped.
|
|
const [retrying, setRetrying] = useState<string | null>(null)
|
|
const retryWithAi = async (task: BackgroundImportTask) => {
|
|
const files = task.sourceFiles
|
|
if (!files || files.length === 0 || retrying === task.id) return
|
|
setRetrying(task.id)
|
|
try {
|
|
const { jobId } = await reservationsApi.importBookingAsync(task.tripId, files, 'force-ai')
|
|
// Same as the modal's first submit: the review attaches each source document to the
|
|
// booking it created, and only IndexedDB survives a reload mid-parse.
|
|
await saveImportFiles(jobId, files)
|
|
dismiss(task.id)
|
|
addTask({ id: jobId, tripId: task.tripId, label: task.label, total: files.length, files, mode: 'force-ai' })
|
|
} catch (err) {
|
|
// 409 when the addon is enabled but this user has no model configured.
|
|
const message = (err as { response?: { data?: { error?: string } } })?.response?.data?.error
|
|
setError(task.id, task.tripId, message ?? t('reservations.import.error'))
|
|
} finally {
|
|
setRetrying(null)
|
|
}
|
|
}
|
|
|
|
// On (re)load, reconcile tasks restored from localStorage with the server: a parse
|
|
// that was still running when the page reloaded must keep its widget, so re-fetch each
|
|
// job's real status (and its parsed items) once. A job the server has since dropped
|
|
// (404, expired) is removed so no stale card lingers.
|
|
const didRehydrate = useRef(false)
|
|
useEffect(() => {
|
|
if (didRehydrate.current) return
|
|
didRehydrate.current = true
|
|
const restored = useBackgroundTasksStore.getState().tasks
|
|
for (const task of restored) {
|
|
reservationsApi
|
|
.importJobStatus(task.tripId, task.id)
|
|
.then((s) => {
|
|
if (s.status === 'done') setDone(task.id, task.tripId, (s.result?.items ?? []) as never, s.result?.warnings ?? [])
|
|
else if (s.status === 'error') setError(task.id, task.tripId, s.error ?? 'error')
|
|
else setProgress(task.id, task.tripId, s.done, s.total)
|
|
})
|
|
.catch((err: { response?: { status?: number } }) => {
|
|
if (err?.response?.status === 404) dismiss(task.id)
|
|
})
|
|
}
|
|
// run once on mount against whatever was rehydrated from storage
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [])
|
|
|
|
// Server pushes import:* to the user on whatever page they're on.
|
|
useEffect(() => {
|
|
const handler = (e: Record<string, unknown>) => {
|
|
const type = typeof e.type === 'string' ? e.type : ''
|
|
if (!type.startsWith('import:')) return
|
|
const id = String(e.jobId ?? '')
|
|
const tripId = String(e.tripId ?? '')
|
|
if (!id) return
|
|
if (type === 'import:progress') setProgress(id, tripId, Number(e.done ?? 0), Number(e.total ?? 1))
|
|
else if (type === 'import:done') {
|
|
const result = e.result as { items?: unknown[]; warnings?: string[] } | undefined
|
|
setDone(id, tripId, (result?.items ?? []) as never, result?.warnings ?? [])
|
|
} else if (type === 'import:error') setError(id, tripId, String(e.message ?? 'error'))
|
|
}
|
|
addListener(handler)
|
|
return () => removeListener(handler)
|
|
}, [setProgress, setDone, setError])
|
|
|
|
// Backstop: poll jobs whose state we still need — running ones (in case a WebSocket push
|
|
// was missed) and a restored 'done' task whose items haven't been re-fetched yet (so a
|
|
// failed one-shot rehydrate self-heals instead of getting stuck on "preview empty").
|
|
useEffect(() => {
|
|
const pending = tasks.filter((task) => task.status === 'running' || (task.status === 'done' && task.items === undefined))
|
|
if (pending.length === 0) return
|
|
const iv = setInterval(() => {
|
|
for (const task of pending) {
|
|
reservationsApi
|
|
.importJobStatus(task.tripId, task.id)
|
|
.then((s) => {
|
|
if (s.status === 'done') setDone(task.id, task.tripId, (s.result?.items ?? []) as never, s.result?.warnings ?? [])
|
|
else if (s.status === 'error') setError(task.id, task.tripId, s.error ?? 'error')
|
|
else setProgress(task.id, task.tripId, s.done, s.total)
|
|
})
|
|
.catch((err: { response?: { status?: number } }) => {
|
|
// The server 404s a job once it has expired (or after a restart). Nothing is
|
|
// ever coming, so end the card instead of spinning and re-polling forever.
|
|
// Not the parse-failure message — the file was fine, the job is just gone.
|
|
if (err?.response?.status === 404) setError(task.id, task.tripId, t('common.unknownError'))
|
|
})
|
|
}
|
|
}, 5000)
|
|
return () => clearInterval(iv)
|
|
}, [tasks, setProgress, setDone, setError, t])
|
|
|
|
if (tasks.length === 0) return null
|
|
|
|
const review = (task: BackgroundImportTask) => {
|
|
requestReview(task.id)
|
|
navigate(`/trips/${task.tripId}`)
|
|
}
|
|
|
|
return createPortal(
|
|
<div
|
|
style={{ position: 'fixed', right: 16, bottom: 16, zIndex: 50000, display: 'flex', flexDirection: 'column', gap: 8, width: 380, maxWidth: 'calc(100vw - 32px)', fontFamily: 'var(--font-system)' }}
|
|
>
|
|
{tasks.map((task) => (
|
|
<div
|
|
key={task.id}
|
|
className="bg-surface-card"
|
|
style={{ borderRadius: 12, border: '1px solid var(--border-primary)', boxShadow: '0 8px 24px rgba(0,0,0,0.18)', padding: '11px 13px', backdropFilter: 'blur(8px)', display: 'flex', gap: 10, alignItems: 'flex-start' }}
|
|
>
|
|
<div style={{ flexShrink: 0, marginTop: 1 }}>
|
|
{(task.status === 'running' || (task.status === 'done' && task.items === undefined)) && <Loader2 size={16} className="animate-spin" color="var(--accent)" />}
|
|
{task.status === 'done' && task.items !== undefined && <CheckCircle2 size={16} color="#10b981" />}
|
|
{task.status === 'error' && <AlertCircle size={16} color="#ef4444" />}
|
|
</div>
|
|
|
|
<div style={{ flex: 1, minWidth: 0 }}>
|
|
<div style={{ fontSize: 'calc(12.5px * var(--fs-scale-body, 1))', fontWeight: 600, color: 'var(--text-primary)', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
|
|
{task.label}
|
|
</div>
|
|
|
|
{task.status === 'running' && (
|
|
<div style={{ fontSize: 'calc(11px * var(--fs-scale-caption, 1))', color: 'var(--text-faint)', marginTop: 1 }}>
|
|
{t('reservations.import.parsing')}
|
|
{task.total > 1 ? ` · ${task.done}/${task.total}` : ''}
|
|
</div>
|
|
)}
|
|
|
|
{task.status === 'done' && (
|
|
task.items === undefined ? (
|
|
// Restored from a reload; items are being re-fetched (see the poll backstop).
|
|
<div style={{ fontSize: 'calc(11px * var(--fs-scale-caption, 1))', color: 'var(--text-faint)', marginTop: 1 }}>{t('reservations.import.parsing')}</div>
|
|
) : task.items.length > 0 ? (
|
|
<div>
|
|
<button type="button"
|
|
onClick={() => review(task)}
|
|
className="bg-accent text-accent-text"
|
|
style={{ marginTop: 4, border: 'none', borderRadius: 8, padding: '4px 12px', fontSize: 'calc(11.5px * var(--fs-scale-caption, 1))', fontWeight: 600, cursor: 'pointer', fontFamily: 'inherit' }}
|
|
>
|
|
{t('common.import')}
|
|
</button>
|
|
{/* A partly-understood import warns too. Warnings used to be
|
|
shown only when nothing at all was found, so anything the
|
|
parse could not place — a station it could not locate, say
|
|
(#1969) — was dropped without a word on exactly the imports
|
|
that did produce something. */}
|
|
{(task.warnings?.length ?? 0) > 0 && (
|
|
<div style={{ fontSize: 'calc(11px * var(--fs-scale-caption, 1))', color: '#b45309', marginTop: 3, whiteSpace: 'pre-wrap', wordBreak: 'break-word', maxHeight: 96, overflowY: 'auto' }}>
|
|
{task.warnings!.join('\n')}
|
|
</div>
|
|
)}
|
|
</div>
|
|
) : (
|
|
<div>
|
|
<div style={{ fontSize: 'calc(11px * var(--fs-scale-caption, 1))', color: 'var(--text-faint)', marginTop: 1 }}>
|
|
{t('reservations.import.previewEmpty')}
|
|
{(task.warnings?.length ?? 0) > 0 && (
|
|
<div style={{ color: '#b45309', marginTop: 3, whiteSpace: 'pre-wrap', wordBreak: 'break-word', maxHeight: 96, overflowY: 'auto' }}>
|
|
{task.warnings!.join('\n')}
|
|
</div>
|
|
)}
|
|
</div>
|
|
{aiParsing && task.mode !== 'force-ai' && task.sourceFiles && task.sourceFiles.length > 0 && (
|
|
<button type="button"
|
|
onClick={() => retryWithAi(task)}
|
|
disabled={retrying === task.id}
|
|
className="bg-surface-tertiary text-content"
|
|
style={{ marginTop: 4, border: 'none', borderRadius: 8, padding: '4px 12px', fontSize: 'calc(11.5px * var(--fs-scale-caption, 1))', fontWeight: 600, cursor: retrying === task.id ? 'default' : 'pointer', opacity: retrying === task.id ? 0.6 : 1, fontFamily: 'inherit' }}
|
|
>
|
|
{t('reservations.import.tryAi')}
|
|
</button>
|
|
)}
|
|
</div>
|
|
)
|
|
)}
|
|
|
|
{task.status === 'error' && (
|
|
<div style={{ fontSize: 'calc(11px * var(--fs-scale-caption, 1))', color: '#b91c1c', marginTop: 1, whiteSpace: 'pre-wrap' }}>{task.error}</div>
|
|
)}
|
|
</div>
|
|
|
|
{task.status !== 'running' && (
|
|
<button type="button"
|
|
onClick={() => dismiss(task.id)}
|
|
className="bg-transparent text-content-faint"
|
|
style={{ flexShrink: 0, border: 'none', cursor: 'pointer', padding: 2, borderRadius: 6, display: 'flex', alignItems: 'center' }}
|
|
aria-label={t('common.close')}
|
|
>
|
|
<X size={13} />
|
|
</button>
|
|
)}
|
|
</div>
|
|
))}
|
|
</div>,
|
|
document.body
|
|
)
|
|
}
|