videogen/apps/web/lib/use-my-workspaces.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

75 lines
2.2 KiB
TypeScript

"use client";
import * as React from "react";
import { ApiError, listMyWorkspaces } from "@/lib/api";
import type { Workspace } from "@/lib/types";
import { useApiToken } from "@/lib/use-api-token";
import { friendlyErrorMessage } from "@/src/shared/errors/friendly";
export type MyWorkspacesStatus = "loading" | "ready" | "error";
export interface UseMyWorkspacesResult {
status: MyWorkspacesStatus;
workspaces: readonly Workspace[];
errorMessage: string;
/** Re-fetch the caller's workspaces. */
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;
}
/**
* Read-only view of the caller's workspaces (GET /workspaces). Unlike the
* team page's `useWorkspaces` (which also owns a create mutator), this is a
* lightweight loader reusable by any surface that only needs the list — e.g.
* the wizard's "Charge to" selector and the preset "Share to team" picker.
* State is replaced, never mutated in place.
*/
export function useMyWorkspaces(): UseMyWorkspacesResult {
const getApiToken = useApiToken();
const [status, setStatus] = React.useState<MyWorkspacesStatus>("loading");
const [workspaces, setWorkspaces] = React.useState<readonly Workspace[]>([]);
const [errorMessage, setErrorMessage] = React.useState("");
const mountedRef = React.useRef(true);
React.useEffect(() => {
mountedRef.current = true;
return () => {
mountedRef.current = false;
};
}, []);
const reload = React.useCallback(async () => {
setStatus("loading");
setErrorMessage("");
try {
const token = await getApiToken();
const result = await listMyWorkspaces({ token });
if (!mountedRef.current) return;
setWorkspaces(result);
setStatus("ready");
} catch (err) {
if (!mountedRef.current) return;
setErrorMessage(
messageFor(err, "Couldn't load your workspaces. Please try again."),
);
setStatus("error");
}
}, [getApiToken]);
React.useEffect(() => {
void reload();
}, [reload]);
return {
status,
workspaces,
errorMessage,
reload: () => void reload(),
};
}