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
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.
110 lines
3.8 KiB
TypeScript
110 lines
3.8 KiB
TypeScript
"use client";
|
|
|
|
import * as React from "react";
|
|
import { ApiError, getSecurityPolicy, putSecurityPolicy } from "@/lib/api";
|
|
import type { SecurityPolicy, SecurityPolicyRequest } from "@/lib/types";
|
|
import { useApiToken } from "@/lib/use-api-token";
|
|
import { friendlyErrorMessage } from "@/src/shared/errors/friendly";
|
|
|
|
// Re-exported so the security-policy write fetcher is reachable from this module
|
|
// alongside the hook (the page mutates through `save` below).
|
|
export { putSecurityPolicy };
|
|
|
|
export type SecurityPolicyStatus = "loading" | "ready" | "error";
|
|
|
|
export interface UseSecurityPolicyResult {
|
|
status: SecurityPolicyStatus;
|
|
/** The workspace's security policy, or `null` when none is configured yet. */
|
|
policy: SecurityPolicy | null;
|
|
errorMessage: string;
|
|
/** HTTP status of the last load error (e.g. 403 when the caller lacks settings). */
|
|
errorStatus: number | null;
|
|
/**
|
|
* Apply a PARTIAL policy update (only supplied fields change). Resolves to the
|
|
* persisted policy, which replaces local state.
|
|
*/
|
|
save: (body: SecurityPolicyRequest) => Promise<SecurityPolicy>;
|
|
/** Re-fetch the workspace's policy. */
|
|
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 login security policy
|
|
* (GET/PUT /me/workspaces/{id}/security). The policy is cert-free / secret-free.
|
|
* `save` performs a PARTIAL update — only the supplied fields change. Gated by
|
|
* `workspace.settings` server-side; a caller lacking it surfaces via
|
|
* `errorStatus === 403`. State is only ever replaced with the server's
|
|
* authoritative value, never mutated in place.
|
|
*/
|
|
export function useSecurityPolicy(
|
|
workspaceId: string | null,
|
|
): UseSecurityPolicyResult {
|
|
const getApiToken = useApiToken();
|
|
const [status, setStatus] = React.useState<SecurityPolicyStatus>("loading");
|
|
const [policy, setPolicy] = React.useState<SecurityPolicy | 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 getSecurityPolicy(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 security policy."));
|
|
setErrorStatus(err instanceof ApiError ? err.status : null);
|
|
setStatus("error");
|
|
}
|
|
})();
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
controller.abort();
|
|
};
|
|
}, [getApiToken, workspaceId, reloadNonce]);
|
|
|
|
const save = React.useCallback(
|
|
async (body: SecurityPolicyRequest): Promise<SecurityPolicy> => {
|
|
if (!workspaceId) throw new Error("No workspace selected.");
|
|
const token = await getApiToken();
|
|
const saved = await putSecurityPolicy(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 };
|
|
}
|