|
| 1 | +/** |
| 2 | + * Generic cancellable progress dialog, mirroring DataLab Qt's |
| 3 | + * ``QProgressDialog`` behaviour: the dialog only becomes visible |
| 4 | + * after ``minDuration`` (default 400 ms) has elapsed, so short |
| 5 | + * operations don't flash a modal at the user. |
| 6 | + * |
| 7 | + * A single :class:`ProgressProvider` is mounted near the application |
| 8 | + * root (see ``src/main.tsx``) and exposes :func:`useProgress`, which |
| 9 | + * returns a ``runWithProgress`` function. Callers describe a sequence |
| 10 | + * of asynchronous steps; the hook iterates over them, updates the |
| 11 | + * bar, and surfaces a Cancel button that aborts the iteration at the |
| 12 | + * next step boundary. |
| 13 | + * |
| 14 | + * Example usage:: |
| 15 | + * |
| 16 | + * const runWithProgress = useProgress(); |
| 17 | + * const { results, cancelled } = await runWithProgress({ |
| 18 | + * title: "Opening files…", |
| 19 | + * total: files.length, |
| 20 | + * step: async (i, { signal, setLabel }) => { |
| 21 | + * setLabel(files[i].name); |
| 22 | + * return await openOne(files[i], signal); |
| 23 | + * }, |
| 24 | + * }); |
| 25 | + */ |
| 26 | + |
| 27 | +import { |
| 28 | + createContext, |
| 29 | + useCallback, |
| 30 | + useContext, |
| 31 | + useEffect, |
| 32 | + useMemo, |
| 33 | + useRef, |
| 34 | + useState, |
| 35 | + type ReactNode, |
| 36 | +} from "react"; |
| 37 | + |
| 38 | +// --------------------------------------------------------------------- |
| 39 | +// Public API |
| 40 | +// --------------------------------------------------------------------- |
| 41 | + |
| 42 | +export interface ProgressStepContext { |
| 43 | + /** Aborted when the user clicks Cancel. */ |
| 44 | + signal: AbortSignal; |
| 45 | + /** Update the secondary label shown under the progress bar. */ |
| 46 | + setLabel: (label: string) => void; |
| 47 | +} |
| 48 | + |
| 49 | +export interface ProgressOptions<T> { |
| 50 | + title: string; |
| 51 | + /** Total number of steps. Used both for the bar and the iteration. */ |
| 52 | + total: number; |
| 53 | + /** |
| 54 | + * Per-step worker. Receives the step index (0-based) and a context |
| 55 | + * with an :class:`AbortSignal` plus a label setter. Throw to abort. |
| 56 | + */ |
| 57 | + step: (index: number, ctx: ProgressStepContext) => Promise<T>; |
| 58 | + /** |
| 59 | + * Minimum elapsed milliseconds before the dialog becomes visible. |
| 60 | + * Mirrors ``QProgressDialog.setMinimumDuration``. Defaults to 400. |
| 61 | + */ |
| 62 | + minDuration?: number; |
| 63 | + /** Show the Cancel button. Defaults to ``true``. */ |
| 64 | + cancellable?: boolean; |
| 65 | + /** Initial secondary label (shown under the progress bar). */ |
| 66 | + initialLabel?: string; |
| 67 | +} |
| 68 | + |
| 69 | +export interface ProgressResult<T> { |
| 70 | + /** Successful per-step results, in order. Length may be < total on cancel. */ |
| 71 | + results: T[]; |
| 72 | + /** ``true`` if the user clicked Cancel before completion. */ |
| 73 | + cancelled: boolean; |
| 74 | +} |
| 75 | + |
| 76 | +export type RunWithProgressFn = <T>( |
| 77 | + options: ProgressOptions<T>, |
| 78 | +) => Promise<ProgressResult<T>>; |
| 79 | + |
| 80 | +// --------------------------------------------------------------------- |
| 81 | +// Internal state shape |
| 82 | +// --------------------------------------------------------------------- |
| 83 | + |
| 84 | +interface ProgressState { |
| 85 | + title: string; |
| 86 | + value: number; |
| 87 | + total: number; |
| 88 | + label: string; |
| 89 | + cancellable: boolean; |
| 90 | + onCancel: () => void; |
| 91 | +} |
| 92 | + |
| 93 | +// --------------------------------------------------------------------- |
| 94 | +// Presentational dialog |
| 95 | +// --------------------------------------------------------------------- |
| 96 | + |
| 97 | +interface ProgressDialogProps { |
| 98 | + state: ProgressState; |
| 99 | +} |
| 100 | + |
| 101 | +/** Pure presentational progress dialog. */ |
| 102 | +export function ProgressDialog({ state }: ProgressDialogProps) { |
| 103 | + const { title, value, total, label, cancellable, onCancel } = state; |
| 104 | + const pct = total > 0 ? Math.min(100, Math.round((value / total) * 100)) : 0; |
| 105 | + useEffect(() => { |
| 106 | + if (!cancellable) return; |
| 107 | + const handler = (e: KeyboardEvent) => { |
| 108 | + if (e.key === "Escape") onCancel(); |
| 109 | + }; |
| 110 | + window.addEventListener("keydown", handler); |
| 111 | + return () => window.removeEventListener("keydown", handler); |
| 112 | + }, [cancellable, onCancel]); |
| 113 | + return ( |
| 114 | + <div className="overlay" role="dialog" aria-modal="true"> |
| 115 | + <div className="card progress-dialog"> |
| 116 | + <h2>{title}</h2> |
| 117 | + <div |
| 118 | + className="progress-bar" |
| 119 | + role="progressbar" |
| 120 | + aria-valuemin={0} |
| 121 | + aria-valuemax={total} |
| 122 | + aria-valuenow={value} |
| 123 | + > |
| 124 | + <div className="progress-bar-fill" style={{ width: `${pct}%` }} /> |
| 125 | + </div> |
| 126 | + <div className="progress-meta"> |
| 127 | + <span className="progress-count"> |
| 128 | + {value} / {total} |
| 129 | + </span> |
| 130 | + <span className="progress-pct">{pct}%</span> |
| 131 | + </div> |
| 132 | + {label && <p className="progress-label">{label}</p>} |
| 133 | + {cancellable && ( |
| 134 | + <div className="actions"> |
| 135 | + <button onClick={onCancel}>Cancel</button> |
| 136 | + </div> |
| 137 | + )} |
| 138 | + </div> |
| 139 | + </div> |
| 140 | + ); |
| 141 | +} |
| 142 | + |
| 143 | +// --------------------------------------------------------------------- |
| 144 | +// Provider + hook |
| 145 | +// --------------------------------------------------------------------- |
| 146 | + |
| 147 | +const ProgressContext = createContext<RunWithProgressFn | null>(null); |
| 148 | + |
| 149 | +export function ProgressProvider(props: { children: ReactNode }) { |
| 150 | + const [state, setState] = useState<ProgressState | null>(null); |
| 151 | + // Keep latest state in a ref so the runWithProgress closure can mutate |
| 152 | + // without being re-created on every render of consumers. |
| 153 | + const stateRef = useRef<ProgressState | null>(null); |
| 154 | + stateRef.current = state; |
| 155 | + |
| 156 | + const runWithProgress = useCallback<RunWithProgressFn>(async (options) => { |
| 157 | + const { |
| 158 | + title, |
| 159 | + total, |
| 160 | + step, |
| 161 | + minDuration = 400, |
| 162 | + cancellable = true, |
| 163 | + initialLabel = "", |
| 164 | + } = options; |
| 165 | + |
| 166 | + const controller = new AbortController(); |
| 167 | + const results: unknown[] = []; |
| 168 | + let visible = false; |
| 169 | + let labelText = initialLabel; |
| 170 | + const current: ProgressState = { |
| 171 | + title, |
| 172 | + value: 0, |
| 173 | + total, |
| 174 | + label: labelText, |
| 175 | + cancellable, |
| 176 | + onCancel: () => controller.abort(), |
| 177 | + }; |
| 178 | + |
| 179 | + const showTimer = window.setTimeout(() => { |
| 180 | + visible = true; |
| 181 | + setState({ ...current }); |
| 182 | + }, minDuration); |
| 183 | + |
| 184 | + const refresh = () => { |
| 185 | + if (visible) setState({ ...current }); |
| 186 | + }; |
| 187 | + |
| 188 | + try { |
| 189 | + for (let i = 0; i < total; i++) { |
| 190 | + if (controller.signal.aborted) break; |
| 191 | + const ctx: ProgressStepContext = { |
| 192 | + signal: controller.signal, |
| 193 | + setLabel: (l: string) => { |
| 194 | + labelText = l; |
| 195 | + current.label = l; |
| 196 | + refresh(); |
| 197 | + }, |
| 198 | + }; |
| 199 | + try { |
| 200 | + const r = await step(i, ctx); |
| 201 | + results.push(r); |
| 202 | + } catch (err) { |
| 203 | + if (controller.signal.aborted) break; |
| 204 | + throw err; |
| 205 | + } |
| 206 | + current.value = i + 1; |
| 207 | + refresh(); |
| 208 | + } |
| 209 | + } finally { |
| 210 | + window.clearTimeout(showTimer); |
| 211 | + setState(null); |
| 212 | + } |
| 213 | + |
| 214 | + return { |
| 215 | + results: results as never[], |
| 216 | + cancelled: controller.signal.aborted, |
| 217 | + }; |
| 218 | + }, []); |
| 219 | + |
| 220 | + const value = useMemo(() => runWithProgress, [runWithProgress]); |
| 221 | + |
| 222 | + return ( |
| 223 | + <ProgressContext.Provider value={value}> |
| 224 | + {props.children} |
| 225 | + {state && <ProgressDialog state={state} />} |
| 226 | + </ProgressContext.Provider> |
| 227 | + ); |
| 228 | +} |
| 229 | + |
| 230 | +export function useProgress(): RunWithProgressFn { |
| 231 | + const ctx = useContext(ProgressContext); |
| 232 | + if (!ctx) { |
| 233 | + throw new Error("useProgress must be used within a ProgressProvider"); |
| 234 | + } |
| 235 | + return ctx; |
| 236 | +} |
0 commit comments