Files
model-platform/frontend/app/features/schedules/state/useSchedulesStore.ts
T

112 lines
3.2 KiB
TypeScript

// Root store: composes 5 slices into the same-named `useSchedulesStore`.
// External consumers keep importing `{ useSchedulesStore }` from this file.
//
// The legacy monolithic file exported both `useSchedulesStore` and
// `bindSchedulesApi`; we re-export the binding helper from `./helpers` here
// so `routes/platform.tsx` (and any future callers) get the same surface.
import { create } from "zustand";
import { bindSchedulesApi } from "./helpers";
import { createCanvasSlice } from "./canvasSlice";
import { createDialogSlice } from "./dialogSlice";
import { createListSlice } from "./listSlice";
import { createLogsSlice } from "./logsSlice";
import { createRunsSlice } from "./runsSlice";
import type { CanvasSliceActions, CanvasSliceState } from "./canvasSlice";
import type { DialogSliceActions, DialogSliceState } from "./dialogSlice";
import type { ListSliceActions, ListSliceState } from "./listSlice";
import type { LogsSliceActions, LogsSliceState } from "./logsSlice";
import type { RunsSliceActions, RunsSliceState } from "./runsSlice";
import type { WorkspaceBoundApi } from "../../../services/api";
// ---- Initial values (used by reset() to mirror the legacy monolithic `initial` const) ----
import { EMPTY_NODE_FORM, EMPTY_SCHEDULE_FORM } from "./types";
// ---- Combined types (re-used by each slice's StateCreator generic) ----
export type SchedulesState =
& ListSliceState
& CanvasSliceState
& DialogSliceState
& RunsSliceState
& LogsSliceState;
export type SchedulesActions =
& ListSliceActions
& CanvasSliceActions
& DialogSliceActions
& RunsSliceActions
& LogsSliceActions
& {
bindApi: (api: WorkspaceBoundApi | null) => void;
reset: () => void;
};
export type SchedulesStore = SchedulesState & SchedulesActions;
const INITIAL: Omit<SchedulesState, "schedule" | "busy"> = {
// List
schedules: [],
artifacts: [],
loading: true,
// Canvas
selectedNodeId: null,
selectedEdgeId: null,
linkSourceId: null,
scheduleKeyword: "",
artifactKeyword: "",
scheduleForm: EMPTY_SCHEDULE_FORM,
nodeForm: EMPTY_NODE_FORM,
contextMenu: null,
positionDraftCount: 0,
// Dialog
createDialogOpen: false,
newScheduleName: "",
renameTarget: null,
renameName: "",
cronResult: null,
// Runs
runs: [],
runsLoading: false,
expandedRunId: null,
runDetails: {},
detailLoadingId: null,
detailErrors: {},
artifactBusyKey: null,
artifactErrors: {},
// Logs
openLogNodeRunIds: new Set(),
logStates: {},
};
export const useSchedulesStore = create<SchedulesStore>()(
(set, get, store) => ({
...createListSlice(set, get, store),
...createCanvasSlice(set, get, store),
...createDialogSlice(set, get, store),
...createRunsSlice(set, get, store),
...createLogsSlice(set, get, store),
// ---- global actions (cross-slice) ----
bindApi: (api) => {
bindSchedulesApi(api);
},
reset: () => {
// Mirror legacy `set({ ...initial, loading: true })` — wipe every
// slice field back to its default. schedule/busy are explicitly set
// because their default values differ from the Omit above.
set({
...INITIAL,
schedule: null,
busy: null,
loading: true,
});
},
}),
);
export { bindSchedulesApi };