Files
multica/packages/core/workspace/hooks.ts
Alex 746c0c4456 MUL-2746 fix(avatar): normalize relative avatar urls in desktop/web (#3100)
* fix(avatar): normalize relative avatar urls in desktop/web

Co-authored-by: multica-agent <github@multica.ai>

* fix: test

Co-authored-by: multica-agent <github@multica.ai>

* fix(avatar): normalize avatar url in AvatarPicker preview

MUL-2746. The picker is used by create-agent and create-squad, and also
prefills from a template's `avatar_url` when duplicating an agent. The
upload result / template URL is root-relative in local-storage setups,
so on Desktop (file:// runtime) the preview <img> resolves against the
local filesystem and the avatar fails to render. Route the value through
`resolvePublicFileUrl` for rendering only; the stored URL stays raw so
the parent's create call still posts what the backend expects.

Co-authored-by: multica-agent <github@multica.ai>

---------

Co-authored-by: multica-agent <github@multica.ai>
Co-authored-by: J (Multica agent) <agents@multica.ai>
2026-05-27 16:02:08 +08:00

74 lines
2.4 KiB
TypeScript

"use client";
import { useCallback, useMemo } from "react";
import { useQuery } from "@tanstack/react-query";
import { useWorkspaceId } from "../hooks";
import { memberListOptions, agentListOptions, squadListOptions } from "./queries";
import { resolvePublicFileUrl } from "./avatar-url";
export function useActorName() {
const wsId = useWorkspaceId();
const { data: members = [] } = useQuery(memberListOptions(wsId));
const { data: agents = [] } = useQuery(agentListOptions(wsId));
const { data: squads = [] } = useQuery(squadListOptions(wsId));
const getMemberName = useCallback((userId: string) => {
const m = members.find((m) => m.user_id === userId);
return m?.name ?? "Unknown";
}, [members]);
const getAgentName = useCallback((agentId: string) => {
const a = agents.find((a) => a.id === agentId);
return a?.name ?? "Unknown Agent";
}, [agents]);
const getSquadName = useCallback((squadId: string) => {
const s = squads.find((s) => s.id === squadId);
return s?.name ?? "Unknown Squad";
}, [squads]);
const getActorName = useCallback((type: string, id: string) => {
if (type === "member") return getMemberName(id);
if (type === "agent") return getAgentName(id);
if (type === "squad") return getSquadName(id);
if (type === "system") return "Multica";
return "System";
}, [getAgentName, getMemberName, getSquadName]);
const getActorInitials = useCallback((type: string, id: string) => {
const name = getActorName(type, id);
return name
.split(" ")
.map((w) => w[0])
.join("")
.toUpperCase()
.slice(0, 2);
}, [getActorName]);
const getActorAvatarUrl = useCallback((type: string, id: string): string | null => {
if (type === "member") return resolvePublicFileUrl(members.find((m) => m.user_id === id)?.avatar_url);
if (type === "agent") return resolvePublicFileUrl(agents.find((a) => a.id === id)?.avatar_url);
if (type === "squad") return resolvePublicFileUrl(squads.find((s) => s.id === id)?.avatar_url);
return null;
}, [agents, members, squads]);
return useMemo(
() => ({
getMemberName,
getAgentName,
getSquadName,
getActorName,
getActorInitials,
getActorAvatarUrl,
}),
[
getActorAvatarUrl,
getActorInitials,
getActorName,
getAgentName,
getMemberName,
getSquadName,
],
);
}