Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
"use client";
|
|
|
|
import * as React from "react";
|
|
|
|
const LIMIT = 100;
|
|
|
|
interface HistoryState<T> {
|
|
past: T[];
|
|
present: T;
|
|
future: T[];
|
|
}
|
|
|
|
/**
|
|
* Immutable undo/redo stack. `set(next, { merge: true })` replaces the present
|
|
* without a new undo step (used while dragging so one drag = one step).
|
|
*/
|
|
export function useHistory<T>(initial: T) {
|
|
const [state, setState] = React.useState<HistoryState<T>>({ past: [], present: initial, future: [] });
|
|
|
|
const set = React.useCallback((next: T | ((prev: T) => T), opts?: { merge?: boolean }) => {
|
|
setState((s) => {
|
|
const value = typeof next === "function" ? (next as (prev: T) => T)(s.present) : next;
|
|
if (Object.is(value, s.present)) return s;
|
|
if (opts?.merge) return { ...s, present: value };
|
|
return { past: [...s.past, s.present].slice(-LIMIT), present: value, future: [] };
|
|
});
|
|
}, []);
|
|
|
|
/** Mark the current present as a checkpoint (call at drag start). */
|
|
const checkpoint = React.useCallback(() => {
|
|
setState((s) => ({ past: [...s.past, s.present].slice(-LIMIT), present: s.present, future: [] }));
|
|
}, []);
|
|
|
|
const undo = React.useCallback(() => {
|
|
setState((s) => {
|
|
const prev = s.past[s.past.length - 1];
|
|
if (prev === undefined) return s;
|
|
return { past: s.past.slice(0, -1), present: prev, future: [s.present, ...s.future] };
|
|
});
|
|
}, []);
|
|
|
|
const redo = React.useCallback(() => {
|
|
setState((s) => {
|
|
const next = s.future[0];
|
|
if (next === undefined) return s;
|
|
return { past: [...s.past, s.present], present: next, future: s.future.slice(1) };
|
|
});
|
|
}, []);
|
|
|
|
const reset = React.useCallback((value: T) => setState({ past: [], present: value, future: [] }), []);
|
|
|
|
return {
|
|
value: state.present,
|
|
set,
|
|
checkpoint,
|
|
undo,
|
|
redo,
|
|
reset,
|
|
canUndo: state.past.length > 0,
|
|
canRedo: state.future.length > 0,
|
|
};
|
|
}
|