videogen/apps/web/lib/use-retention-policy.ts
caelum0x d56639a594
Some checks are pending
build-images / api (push) Waiting to run
build-images / worker-gpu (push) Waiting to run
build-images / web (push) Waiting to run
codeql / Analyze (javascript-typescript) (push) Waiting to run
codeql / Analyze (python) (push) Waiting to run
perf / API hot-path micro-smoke (budgeted) (push) Waiting to run
perf / Desktop engine crates (release build check) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Waiting to run
vision2026-ci / API phase modules (ruff + pytest) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Waiting to run
vision2026-ci / Python SDK resource tests (push) Waiting to run
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Waiting to run
vision2026-ci / Alembic upgrade + drift check (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Waiting to run
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

105 lines
3.6 KiB
TypeScript

"use client";
import * as React from "react";
import { ApiError, getRetentionPolicy, setRetentionPolicy } from "@/lib/api";
import type { RetentionPolicy, RetentionUpdateRequest } from "@/lib/types";
import { useApiToken } from "@/lib/use-api-token";
import { friendlyErrorMessage } from "@/src/shared/errors/friendly";
export type RetentionStatus = "loading" | "ready" | "error";
export interface UseRetentionPolicyResult {
status: RetentionStatus;
/** The workspace's retention policy, or a keep-forever default. */
policy: RetentionPolicy | null;
errorMessage: string;
/** HTTP status of the last load error (e.g. 403 when the caller lacks settings). */
errorStatus: number | null;
/**
* Replace the workspace's retention window. Resolves to the persisted policy,
* which replaces local state. Gated by `workspace.settings` server-side.
*/
save: (body: RetentionUpdateRequest) => Promise<RetentionPolicy>;
reload: () => void;
}
function messageFor(err: unknown, fallback: string): string {
if (err instanceof ApiError) return friendlyErrorMessage(err);
if (err instanceof Error) return friendlyErrorMessage(err);
return fallback;
}
/**
* Owns a single workspace's data-retention policy
* (GET/PUT /me/workspaces/{id}/retention). Reading/writing is gated on
* `workspace.settings` server-side; a caller lacking it surfaces via
* `errorStatus === 403`. A `null` window means keep-forever. The policy always
* belongs to the selected workspace alone — no other tenant's data is touched.
* State is only ever replaced with the server's authoritative value.
*/
export function useRetentionPolicy(
workspaceId: string | null,
): UseRetentionPolicyResult {
const getApiToken = useApiToken();
const [status, setStatus] = React.useState<RetentionStatus>("loading");
const [policy, setPolicy] = React.useState<RetentionPolicy | null>(null);
const [errorMessage, setErrorMessage] = React.useState("");
const [errorStatus, setErrorStatus] = React.useState<number | null>(null);
const [reloadNonce, setReloadNonce] = React.useState(0);
React.useEffect(() => {
if (!workspaceId) {
setStatus("ready");
setPolicy(null);
setErrorMessage("");
setErrorStatus(null);
return;
}
let cancelled = false;
const controller = new AbortController();
void (async () => {
setStatus("loading");
setErrorMessage("");
setErrorStatus(null);
try {
const token = await getApiToken();
const result = await getRetentionPolicy(workspaceId, {
token,
signal: controller.signal,
});
if (cancelled) return;
setPolicy(result);
setStatus("ready");
} catch (err) {
if (cancelled) return;
if (err instanceof DOMException && err.name === "AbortError") return;
setErrorMessage(messageFor(err, "Couldn't load the retention policy."));
setErrorStatus(err instanceof ApiError ? err.status : null);
setStatus("error");
}
})();
return () => {
cancelled = true;
controller.abort();
};
}, [getApiToken, workspaceId, reloadNonce]);
const save = React.useCallback(
async (body: RetentionUpdateRequest): Promise<RetentionPolicy> => {
if (!workspaceId) throw new Error("No workspace selected.");
const token = await getApiToken();
const saved = await setRetentionPolicy(workspaceId, body, { token });
setPolicy(saved);
setStatus("ready");
return saved;
},
[getApiToken, workspaceId],
);
const reload = React.useCallback(() => setReloadNonce((n) => n + 1), []);
return { status, policy, errorMessage, errorStatus, save, reload };
}