caelum0x d56639a594
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
Menivor — open source release (MIT), free to use and fork
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.
2026-12-04 02:10:26 +03:00

222 lines
6.6 KiB
TypeScript

"use client";
import * as React from "react";
import { ApiError } from "@/lib/api";
import {
getCalendar,
rescheduleEntry,
type CalendarEntry,
} from "@/lib/calendar";
import { isCancelableSchedule } from "@/lib/types";
import { useApiToken } from "@/lib/use-api-token";
import {
buildDays,
bucketEntriesByDay,
rangeForView,
shiftRef,
startOfDay,
type CalendarDay,
type CalendarView,
} from "@/components/calendar/calendar-month";
// Reuses the board grid's pure "same local time, new day" helper rather than
// re-deriving it; it has no dependency on that hook's state.
import { sameTimeOnDay } from "@/components/calendar/use-calendar-board";
import { friendlyErrorMessage } from "@/src/shared/errors/friendly";
export type CalendarLoadStatus = "idle" | "loading" | "ready" | "error";
export interface UseCalendarResult {
view: CalendarView;
setView: (view: CalendarView) => void;
/** The reference date the current view is anchored to (local). */
ref: Date;
goPrev: () => void;
goNext: () => void;
goToday: () => void;
/** The day cells for the current view. */
days: readonly CalendarDay[];
/** Entries grouped by local-day key (see {@link bucketEntriesByDay}). */
buckets: Map<string, CalendarEntry[]>;
status: CalendarLoadStatus;
error: string;
reload: () => void;
/** Move a still-pending entry to a new UTC instant. Resolves on success. */
reschedule: (id: string, scheduledAtISO: string) => Promise<boolean>;
/**
* Drag-and-drop entry point: move entry `id` to `day`, keeping its original
* time-of-day. Resolves `false` for an unknown id, a non-reschedulable
* status, or a drop on the day it is already on (none of these call the
* API); otherwise delegates to {@link reschedule} and resolves its result.
*/
moveToDay: (id: string, day: Date) => Promise<boolean>;
/** Id currently being rescheduled (for a spinner / disabled state). */
reschedulingId: string | null;
rescheduleError: string;
clearRescheduleError: () => void;
}
function errorMessageOf(err: unknown, fallback: string): string {
if (err instanceof ApiError) return friendlyErrorMessage(err);
if (err instanceof Error) return friendlyErrorMessage(err);
return fallback;
}
/**
* Loads the caller's calendar window (GET /me/calendar) for the current view and
* exposes month/week navigation plus a `reschedule` mutator (PATCH
* /me/calendar/{id}). State is only ever replaced, never mutated in place; a
* successful reschedule swaps the affected entry for the server's returned copy
* so the grid re-buckets without a full refetch.
*/
export function useCalendar(
initialView: CalendarView = "month",
): UseCalendarResult {
const getApiToken = useApiToken();
const [view, setViewState] = React.useState<CalendarView>(initialView);
const [ref, setRef] = React.useState<Date>(() => startOfDay(new Date()));
const [entries, setEntries] = React.useState<readonly CalendarEntry[]>([]);
const [status, setStatus] = React.useState<CalendarLoadStatus>("idle");
const [error, setError] = React.useState<string>("");
const [reloadNonce, setReloadNonce] = React.useState(0);
const [reschedulingId, setReschedulingId] = React.useState<string | null>(
null,
);
const [rescheduleError, setRescheduleError] = React.useState<string>("");
const mountedRef = React.useRef(true);
React.useEffect(() => {
mountedRef.current = true;
return () => {
mountedRef.current = false;
};
}, []);
const refTime = ref.getTime();
React.useEffect(() => {
let cancelled = false;
const controller = new AbortController();
const { startISO, endISO } = rangeForView(view, new Date(refTime));
const load = async (): Promise<void> => {
setStatus("loading");
setError("");
try {
const token = await getApiToken();
const list = await getCalendar(startISO, endISO, {
token,
signal: controller.signal,
});
if (cancelled) return;
setEntries(list);
setStatus("ready");
} catch (err) {
if (cancelled) return;
if (err instanceof DOMException && err.name === "AbortError") return;
setError(errorMessageOf(err, "Couldn't load your calendar."));
setStatus("error");
}
};
void load();
return () => {
cancelled = true;
controller.abort();
};
}, [getApiToken, view, refTime, reloadNonce]);
const days = React.useMemo(
() => buildDays(view, new Date(refTime)),
[view, refTime],
);
const buckets = React.useMemo(
() => bucketEntriesByDay(entries),
[entries],
);
const setView = React.useCallback((next: CalendarView) => {
setViewState(next);
}, []);
const goPrev = React.useCallback(() => {
setRef((prev) => shiftRef(view, prev, -1));
}, [view]);
const goNext = React.useCallback(() => {
setRef((prev) => shiftRef(view, prev, 1));
}, [view]);
const goToday = React.useCallback(() => {
setRef(startOfDay(new Date()));
}, []);
const reload = React.useCallback(() => {
setReloadNonce((n) => n + 1);
}, []);
const clearRescheduleError = React.useCallback(() => {
setRescheduleError("");
}, []);
const reschedule = React.useCallback(
async (id: string, scheduledAtISO: string): Promise<boolean> => {
setReschedulingId(id);
setRescheduleError("");
try {
const token = await getApiToken();
const updated = await rescheduleEntry(id, scheduledAtISO, { token });
if (!mountedRef.current) return true;
setEntries((prev) =>
prev.map((entry) => (entry.id === id ? updated : entry)),
);
return true;
} catch (err) {
if (!mountedRef.current) return false;
setRescheduleError(
errorMessageOf(err, "Failed to reschedule this publish."),
);
return false;
} finally {
if (mountedRef.current) setReschedulingId(null);
}
},
[getApiToken],
);
const moveToDay = React.useCallback(
async (id: string, day: Date): Promise<boolean> => {
const entry = entries.find((e) => e.id === id);
if (!entry || !entry.scheduled_at || !isCancelableSchedule(entry.status)) {
return false;
}
const target = sameTimeOnDay(entry.scheduled_at, day);
if (target.toISOString() === new Date(entry.scheduled_at).toISOString()) {
return false;
}
return reschedule(id, target.toISOString());
},
[entries, reschedule],
);
return {
view,
setView,
ref,
goPrev,
goNext,
goToday,
days,
buckets,
status,
error,
reload,
reschedule,
moveToDay,
reschedulingId,
rescheduleError,
clearRescheduleError,
};
}