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.
100 lines
3.0 KiB
TypeScript
100 lines
3.0 KiB
TypeScript
"use client";
|
|
|
|
import * as React from "react";
|
|
import { ApiError, getMyPermissions } from "@/lib/api";
|
|
import type { Permission, Permissions, WorkspaceRole } from "@/lib/types";
|
|
import { useApiToken } from "@/lib/use-api-token";
|
|
import { friendlyErrorMessage } from "@/src/shared/errors/friendly";
|
|
|
|
export type PermissionsStatus = "idle" | "loading" | "ready" | "error";
|
|
|
|
export interface UseWorkspacePermissionsResult {
|
|
status: PermissionsStatus;
|
|
/** The caller's role in the workspace, or `null` for a non-member. */
|
|
role: WorkspaceRole | null;
|
|
/** The caller's effective permissions (empty for a non-member). */
|
|
permissions: readonly Permission[];
|
|
errorMessage: string;
|
|
/** True when the caller holds `permission` in the selected workspace. */
|
|
has: (permission: Permission) => boolean;
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Loads the caller's effective permissions for `workspaceId`
|
|
* (GET /me/permissions). A blank id stays `idle` (nothing to load). The server
|
|
* is always the source of truth — this is used only to drive UI affordances
|
|
* (which controls to render). State is replaced, never mutated in place.
|
|
*/
|
|
export function useWorkspacePermissions(
|
|
workspaceId: string | null,
|
|
): UseWorkspacePermissionsResult {
|
|
const getApiToken = useApiToken();
|
|
const [status, setStatus] = React.useState<PermissionsStatus>("idle");
|
|
const [data, setData] = React.useState<Permissions | null>(null);
|
|
const [errorMessage, setErrorMessage] = React.useState("");
|
|
|
|
const [reloadNonce, setReloadNonce] = React.useState(0);
|
|
const reload = React.useCallback(() => setReloadNonce((n) => n + 1), []);
|
|
|
|
React.useEffect(() => {
|
|
if (!workspaceId) {
|
|
setStatus("idle");
|
|
setData(null);
|
|
setErrorMessage("");
|
|
return;
|
|
}
|
|
let cancelled = false;
|
|
const controller = new AbortController();
|
|
|
|
void (async () => {
|
|
setStatus("loading");
|
|
setErrorMessage("");
|
|
try {
|
|
const token = await getApiToken();
|
|
const result = await getMyPermissions(workspaceId, {
|
|
token,
|
|
signal: controller.signal,
|
|
});
|
|
if (cancelled) return;
|
|
setData(result);
|
|
setStatus("ready");
|
|
} catch (err) {
|
|
if (cancelled) return;
|
|
if (err instanceof DOMException && err.name === "AbortError") return;
|
|
setErrorMessage(messageFor(err, "Couldn't load your permissions."));
|
|
setStatus("error");
|
|
}
|
|
})();
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
controller.abort();
|
|
};
|
|
}, [getApiToken, workspaceId, reloadNonce]);
|
|
|
|
const permissions = React.useMemo<readonly Permission[]>(
|
|
() => data?.permissions ?? [],
|
|
[data],
|
|
);
|
|
const has = React.useCallback(
|
|
(permission: Permission) => permissions.includes(permission),
|
|
[permissions],
|
|
);
|
|
|
|
return {
|
|
status,
|
|
role: data?.role ?? null,
|
|
permissions,
|
|
errorMessage,
|
|
has,
|
|
reload,
|
|
};
|
|
}
|