SliceJobTrackerContext.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * Background slice-job tracker.
  3. *
  4. * SliceModal calls `trackJob(id, kind)` after enqueuing and closes
  5. * immediately. This context keeps the job-id list, polls each one, and
  6. * shows toasts on terminal state. Lives at app level so polling continues
  7. * across navigation — slice can run in the background while the user does
  8. * other things.
  9. *
  10. * Each tracked job also gets a persistent toast (`slice-job-{id}`) with a
  11. * spinner + elapsed-time counter that updates every second so the user has
  12. * a continuous visual indicator while a long slice is running. The toast
  13. * is replaced by a transient success/error toast on terminal state.
  14. */
  15. import { createContext, useCallback, useContext, useEffect, useRef, useState, type ReactNode } from 'react';
  16. import { useTranslation } from 'react-i18next';
  17. import { useQueryClient } from '@tanstack/react-query';
  18. import { api, type SliceJobProgress, type SliceJobState, type SliceJobStatus } from '../api/client';
  19. import { useToast } from './ToastContext';
  20. interface TrackedJob {
  21. id: number;
  22. kind: 'libraryFile' | 'archive';
  23. sourceName: string;
  24. }
  25. interface SliceJobTrackerContextValue {
  26. trackJob: (id: number, kind: 'libraryFile' | 'archive', sourceName: string) => void;
  27. activeJobs: TrackedJob[];
  28. }
  29. const SliceJobTrackerContext = createContext<SliceJobTrackerContextValue | null>(null);
  30. const POLL_INTERVAL_MS = 1500;
  31. const TICK_INTERVAL_MS = 1000;
  32. const toastIdFor = (jobId: number) => `slice-job-${jobId}`;
  33. /** Decode percent-encoded characters in a filename so the toast doesn't
  34. * show `stormtrooper-helmet%20h2d.3mf` for files that came from a source
  35. * with URL-encoded names (MakerWorld API, S3 path tails, etc.). The
  36. * MakerWorld import path now decodes at persist time, but already-imported
  37. * rows still carry the encoded form — this is a belt-and-suspenders
  38. * decode at display time so old rows look right too. Wrapped in try/catch
  39. * because malformed encodings (`%XY` where XY isn't hex) throw URIError. */
  40. function prettifyFilename(name: string): string {
  41. try {
  42. return decodeURIComponent(name);
  43. } catch {
  44. return name;
  45. }
  46. }
  47. function formatElapsed(seconds: number): string {
  48. const s = Math.max(0, Math.floor(seconds));
  49. if (s < 60) return `${s}s`;
  50. const m = Math.floor(s / 60);
  51. const remS = s % 60;
  52. if (m < 60) return `${m}m ${remS}s`;
  53. const h = Math.floor(m / 60);
  54. const remM = m % 60;
  55. return `${h}h ${remM}m`;
  56. }
  57. export function SliceJobTrackerProvider({ children }: { children: ReactNode }) {
  58. const { t } = useTranslation();
  59. const { showToast, showPersistentToast, dismissToast } = useToast();
  60. const queryClient = useQueryClient();
  61. const [activeJobs, setActiveJobs] = useState<TrackedJob[]>([]);
  62. // Stable mutable ref so the polling effect can read the current list
  63. // without re-subscribing every time it changes.
  64. const activeJobsRef = useRef<TrackedJob[]>([]);
  65. activeJobsRef.current = activeJobs;
  66. // Per-job start time, latest phase, and latest progress snapshot,
  67. // kept in refs so the 1s tick doesn't need to re-render on every
  68. // update. Keyed by job id.
  69. const startedAtRef = useRef<Map<number, number>>(new Map());
  70. const phaseRef = useRef<Map<number, SliceJobStatus>>(new Map());
  71. const progressRef = useRef<Map<number, SliceJobProgress | null>>(new Map());
  72. const renderProgressToast = useCallback(
  73. (job: TrackedJob) => {
  74. const startedAt = startedAtRef.current.get(job.id);
  75. if (startedAt == null) return;
  76. const elapsedSecs = (Date.now() - startedAt) / 1000;
  77. const phase = phaseRef.current.get(job.id) ?? 'pending';
  78. const elapsedStr = formatElapsed(elapsedSecs);
  79. const progress = progressRef.current.get(job.id) ?? null;
  80. // When the sidecar has emitted at least one progress frame, weave
  81. // the stage label + percent into the toast — that's what makes the
  82. // wait feel professional ("Generating G-code 75%" beats "Slicing X
  83. // — 47s"). Falls back to the elapsed-time-only message in three
  84. // cases: queued/pending phase before the slicer has started,
  85. // missing or zero progress (Initializing), or sidecar without
  86. // --pipe support.
  87. const hasUseful = progress && progress.stage && progress.total_percent > 0;
  88. if (phase === 'running' && hasUseful) {
  89. showPersistentToast(
  90. toastIdFor(job.id),
  91. t(
  92. 'slice.runningWithProgress',
  93. '{{name}} — {{stage}} ({{percent}}%) — {{elapsed}}',
  94. {
  95. name: prettifyFilename(job.sourceName),
  96. stage: progress.stage,
  97. percent: Math.min(100, Math.max(0, Math.round(progress.total_percent))),
  98. elapsed: elapsedStr,
  99. },
  100. ),
  101. 'loading',
  102. );
  103. return;
  104. }
  105. const messageKey = phase === 'pending' ? 'slice.queuedToast' : 'slice.runningToast';
  106. const fallback =
  107. phase === 'pending'
  108. ? 'Queued: {{name}} — {{elapsed}}'
  109. : 'Slicing {{name}} — {{elapsed}}';
  110. showPersistentToast(
  111. toastIdFor(job.id),
  112. t(messageKey, fallback, { name: prettifyFilename(job.sourceName), elapsed: elapsedStr }),
  113. 'loading',
  114. );
  115. },
  116. [showPersistentToast, t],
  117. );
  118. const trackJob = useCallback(
  119. (id: number, kind: 'libraryFile' | 'archive', sourceName: string) => {
  120. setActiveJobs((prev) => (prev.some((j) => j.id === id) ? prev : [...prev, { id, kind, sourceName }]));
  121. startedAtRef.current.set(id, Date.now());
  122. phaseRef.current.set(id, 'pending');
  123. progressRef.current.set(id, null);
  124. // Render the initial frame immediately so the user sees the toast
  125. // before the first tick lands (~1s delay otherwise).
  126. renderProgressToast({ id, kind, sourceName });
  127. },
  128. [renderProgressToast],
  129. );
  130. const completeJob = useCallback(
  131. (job: TrackedJob, state: SliceJobState) => {
  132. setActiveJobs((prev) => prev.filter((j) => j.id !== job.id));
  133. startedAtRef.current.delete(job.id);
  134. phaseRef.current.delete(job.id);
  135. progressRef.current.delete(job.id);
  136. // Replace the persistent progress toast with a transient
  137. // success/error toast (auto-dismisses after 3s, same as showToast).
  138. dismissToast(toastIdFor(job.id));
  139. if (state.status === 'completed') {
  140. // `used_embedded_settings` still comes back on the result for tests
  141. // and observability, but the warning toast that surfaced it was
  142. // firing on essentially every slice (3MF inputs trigger the
  143. // embedded-settings fallback as a normal path) and just added
  144. // noise — see the trailing yellow toast complaint, removed.
  145. showToast(
  146. t('slice.completedToast', 'Sliced {{name}}', { name: prettifyFilename(job.sourceName) }),
  147. 'success',
  148. );
  149. } else if (state.status === 'failed') {
  150. const detail = state.error_detail || t('slice.failed');
  151. showToast(t('slice.failedToast', 'Slicing {{name}} failed: {{detail}}', { name: prettifyFilename(job.sourceName), detail }), 'error');
  152. }
  153. // Refresh whichever list owns the result. Both are cheap to invalidate.
  154. queryClient.invalidateQueries({ queryKey: ['library-files'] });
  155. queryClient.invalidateQueries({ queryKey: ['archives'] });
  156. },
  157. [dismissToast, queryClient, showToast, t],
  158. );
  159. // Status polling. Updates phase on each successful poll and triggers
  160. // completeJob on terminal states.
  161. useEffect(() => {
  162. if (activeJobs.length === 0) return;
  163. let cancelled = false;
  164. const interval = setInterval(async () => {
  165. if (cancelled) return;
  166. const snapshot = [...activeJobsRef.current];
  167. for (const job of snapshot) {
  168. try {
  169. const state = await api.getSliceJob(job.id);
  170. phaseRef.current.set(job.id, state.status);
  171. // Capture the latest progress snapshot if the sidecar fed
  172. // one through. The 1s tick re-renders the toast off this ref.
  173. if (state.progress) {
  174. progressRef.current.set(job.id, state.progress);
  175. }
  176. if (state.status === 'completed' || state.status === 'failed') {
  177. completeJob(job, state);
  178. }
  179. } catch {
  180. // Transient poll failure — stay tracked, retry next tick.
  181. }
  182. }
  183. }, POLL_INTERVAL_MS);
  184. return () => {
  185. cancelled = true;
  186. clearInterval(interval);
  187. };
  188. }, [activeJobs.length, completeJob]);
  189. // 1Hz tick that re-renders each persistent progress toast with the
  190. // current elapsed time. Independent of the status poll so the counter
  191. // stays smooth even while the backend is slow to respond.
  192. useEffect(() => {
  193. if (activeJobs.length === 0) return;
  194. const tick = setInterval(() => {
  195. for (const job of activeJobsRef.current) {
  196. renderProgressToast(job);
  197. }
  198. }, TICK_INTERVAL_MS);
  199. return () => clearInterval(tick);
  200. }, [activeJobs.length, renderProgressToast]);
  201. return (
  202. <SliceJobTrackerContext.Provider value={{ trackJob, activeJobs }}>
  203. {children}
  204. </SliceJobTrackerContext.Provider>
  205. );
  206. }
  207. export function useSliceJobTracker(): SliceJobTrackerContextValue {
  208. const ctx = useContext(SliceJobTrackerContext);
  209. if (!ctx) {
  210. throw new Error('useSliceJobTracker must be used inside SliceJobTrackerProvider');
  211. }
  212. return ctx;
  213. }