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
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.
93 lines
2.6 KiB
TypeScript
93 lines
2.6 KiB
TypeScript
"use client";
|
|
|
|
import * as React from "react";
|
|
import type { Brand, BrandUpdateRequest } from "@/lib/types";
|
|
import { ApiError, getBrand, updateBrand } from "@/lib/api";
|
|
import { useApiToken } from "@/lib/use-api-token";
|
|
import { friendlyErrorMessage } from "@/src/shared/errors/friendly";
|
|
|
|
export type BrandStatus = "loading" | "ready" | "error";
|
|
|
|
export interface UseBrandResult {
|
|
status: BrandStatus;
|
|
brand: Brand | null;
|
|
errorMessage: string;
|
|
/** True while a save (PUT /me/brand) is in flight. */
|
|
saving: boolean;
|
|
/** Re-fetch the caller's brand settings. */
|
|
reload: () => void;
|
|
/**
|
|
* Persist a partial update. Resolves with the updated brand on success or
|
|
* `null` on failure (the thrown message is surfaced via `errorMessage`).
|
|
*/
|
|
save: (patch: BrandUpdateRequest) => Promise<Brand | null>;
|
|
}
|
|
|
|
function messageFor(err: unknown, fallback: string): string {
|
|
return friendlyErrorMessage(err, fallback);
|
|
}
|
|
|
|
/**
|
|
* Owns the caller's brand watermark: loads GET /me/brand and exposes a `save`
|
|
* that PUTs a partial update, keeping the local `brand` in sync with the server
|
|
* response. State is updated immutably (never mutated in place).
|
|
*/
|
|
export function useBrand(): UseBrandResult {
|
|
const getApiToken = useApiToken();
|
|
const [status, setStatus] = React.useState<BrandStatus>("loading");
|
|
const [brand, setBrand] = React.useState<Brand | null>(null);
|
|
const [errorMessage, setErrorMessage] = React.useState("");
|
|
const [saving, setSaving] = React.useState(false);
|
|
|
|
const reload = React.useCallback(async () => {
|
|
setStatus("loading");
|
|
setErrorMessage("");
|
|
try {
|
|
const token = await getApiToken();
|
|
const result = await getBrand({ token });
|
|
setBrand(result);
|
|
setStatus("ready");
|
|
} catch (err) {
|
|
setErrorMessage(
|
|
messageFor(err, "Could not load your brand settings. Please try again."),
|
|
);
|
|
setStatus("error");
|
|
}
|
|
}, [getApiToken]);
|
|
|
|
React.useEffect(() => {
|
|
void reload();
|
|
}, [reload]);
|
|
|
|
const save = React.useCallback(
|
|
async (patch: BrandUpdateRequest): Promise<Brand | null> => {
|
|
setSaving(true);
|
|
setErrorMessage("");
|
|
try {
|
|
const token = await getApiToken();
|
|
const updated = await updateBrand(patch, { token });
|
|
setBrand(updated);
|
|
setStatus("ready");
|
|
return updated;
|
|
} catch (err) {
|
|
setErrorMessage(
|
|
messageFor(err, "Could not save your brand settings. Please try again."),
|
|
);
|
|
return null;
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
},
|
|
[getApiToken],
|
|
);
|
|
|
|
return {
|
|
status,
|
|
brand,
|
|
errorMessage,
|
|
saving,
|
|
reload: () => void reload(),
|
|
save,
|
|
};
|
|
}
|