videogen/apps/web/components/account/use-my-profile.ts
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

137 lines
4.0 KiB
TypeScript

"use client";
import * as React from "react";
import {
ApiError,
getCreatorProfile,
getMe,
updateMyProfile,
} from "@/lib/api";
import type {
CreatorProfile,
Me,
ProfileUpdateRequest,
} from "@/lib/types";
import { useApiToken } from "@/lib/use-api-token";
import { friendlyErrorMessage } from "@/src/shared/errors/friendly";
export type MyProfileStatus = "loading" | "ready" | "error";
export interface UseMyProfileResult {
status: MyProfileStatus;
/** Account facts (email, credit balance, lifetime job count). */
me: Me | null;
/** The caller's public creator profile, or `null` if none exists yet. */
profile: CreatorProfile | null;
errorMessage: string;
/** True while a profile save (PATCH /me/profile) is in flight. */
saving: boolean;
/** Re-fetch the account + profile. */
reload: () => void;
/**
* Persist handle / display name / bio. Returns the updated profile on
* success, or `null` on error (the error message is exposed via `saveError`).
*/
save: (body: ProfileUpdateRequest) => Promise<CreatorProfile | null>;
saveError: string;
}
function messageFor(err: unknown, fallback: string): string {
if (err instanceof ApiError) return friendlyErrorMessage(err);
if (err instanceof Error) return friendlyErrorMessage(err);
return fallback;
}
/**
* Owns the caller's account facts (GET /me) plus their public creator profile
* (GET /creators/{userId}). A missing profile (404) is treated as an empty,
* not-yet-created profile rather than an error, so the edit form still renders.
* `save` PATCHes /me/profile and replaces local state with the server's echo.
*/
export function useMyProfile(): UseMyProfileResult {
const getApiToken = useApiToken();
const [status, setStatus] = React.useState<MyProfileStatus>("loading");
const [me, setMe] = React.useState<Me | null>(null);
const [profile, setProfile] = React.useState<CreatorProfile | null>(null);
const [errorMessage, setErrorMessage] = React.useState("");
const [saving, setSaving] = React.useState(false);
const [saveError, setSaveError] = React.useState("");
const [nonce, setNonce] = React.useState(0);
const mountedRef = React.useRef(true);
React.useEffect(() => {
mountedRef.current = true;
return () => {
mountedRef.current = false;
};
}, []);
React.useEffect(() => {
let cancelled = false;
void (async () => {
setStatus("loading");
setErrorMessage("");
try {
const token = await getApiToken();
const account = await getMe({ token });
if (cancelled) return;
setMe(account);
// A creator profile may not exist yet — a 404 is expected, not fatal.
try {
const p = await getCreatorProfile(account.user_id, { token });
if (!cancelled) setProfile(p);
} catch (err) {
if (err instanceof ApiError && err.status === 404) {
if (!cancelled) setProfile(null);
} else {
throw err;
}
}
if (!cancelled) setStatus("ready");
} catch (err) {
if (cancelled) return;
setErrorMessage(messageFor(err, "Couldn't load your account."));
setStatus("error");
}
})();
return () => {
cancelled = true;
};
}, [getApiToken, nonce]);
const reload = React.useCallback(() => setNonce((n) => n + 1), []);
const save = React.useCallback(
async (body: ProfileUpdateRequest): Promise<CreatorProfile | null> => {
setSaving(true);
setSaveError("");
try {
const token = await getApiToken();
const updated = await updateMyProfile(body, { token });
if (mountedRef.current) setProfile(updated);
return updated;
} catch (err) {
if (mountedRef.current) {
setSaveError(messageFor(err, "Couldn't save your profile."));
}
return null;
} finally {
if (mountedRef.current) setSaving(false);
}
},
[getApiToken],
);
return {
status,
me,
profile,
errorMessage,
saving,
reload,
save,
saveError,
};
}