fix(webui): unify app logos and brand mentions

This commit is contained in:
Xubin Ren committed 2026-09-13 13:53:20 +08:00
1 parent bfb4f021ed
commit b6122b9018
10 files changed
+752 -89

No files matched your search

+41 -16
View File
@@ -10,6 +10,7 @@ import { logoFallbackUrls } from "@/lib/provider-brand";
import { sessionHandleColor } from "@/lib/session-handle";
import type { CliAppInfo, McpPresetInfo, SessionMention } from "@/lib/types";
import { cn } from "@/lib/utils";
import { composerMentionLabel } from "@/lib/composer-mention-text";
type CliAppMentionSegment =
| { kind: "text"; text: string }
@@ -185,7 +186,10 @@ function CliAppMentionToken({
}) {
const { t } = useTranslation();
const color = app.brand_color || INLINE_TOKEN_HIGHLIGHT_COLOR;
const mentionName = label.startsWith("@") ? label.slice(1) : label;
const displayName = app.display_name?.trim() || app.name;
const mentionName = variant === "message"
? displayName
: composerMentionLabel({ kind: "cli", text: label, app }).slice(1);
const logoUrls = useMemo(() => logoFallbackUrls(app.logo_url), [app.logo_url]);
const { logoUrl, onLogoError, onLogoLoad } = useLogoFallback(logoUrls);
const showLogo = Boolean(logoUrl);
@@ -194,12 +198,20 @@ function CliAppMentionToken({
return (
<InlineTokenHighlight
testId={`${testIdPrefix}-cli-mention-${app.name}`}
title={t("thread.composer.mentions.cliTitle", { name: app.display_name || app.name })}
title={t("thread.composer.mentions.cliTitle", {
name: variant === "message" ? `${displayName} (${label})` : displayName,
})}
color={color}
className={variant === "composer" ? "font-normal" : undefined}
className={variant === "composer" ? "font-normal" : "inline-flex max-w-full items-baseline [overflow-wrap:anywhere]"}
>
<span
className={cn("relative inline-block", showLogo && "text-transparent")}
className={cn(
"relative",
variant === "composer" ? "inline" : "inline-block",
variant === "message" && "shrink-0",
showLogo && "text-transparent",
showLogo && variant === "message" && "mr-1 w-[1.1em]",
)}
style={{ lineHeight: "inherit" }}
>
@
@@ -207,9 +219,10 @@ function CliAppMentionToken({
<span
data-testid={`${testIdPrefix}-cli-mention-logo-${app.name}`}
className={cn(
"absolute left-1/2 top-1/2 grid place-items-center overflow-hidden rounded-mark",
"-translate-x-1/2 -translate-y-1/2",
isHero ? "h-[0.74em] w-[0.74em]" : "h-[0.72em] w-[0.72em]",
"absolute left-0 top-1/2 grid -translate-y-1/2 place-items-center overflow-hidden rounded-[0.25em]",
variant === "message"
? "h-[1.1em] w-[1.1em]"
: isHero ? "h-[0.92em] w-[0.92em]" : "h-[0.9em] w-[0.9em]",
)}
>
<img
@@ -224,7 +237,7 @@ function CliAppMentionToken({
</span>
) : null}
</span>
{mentionName}
{variant === "message" ? <span className="min-w-0">{mentionName}</span> : mentionName}
</InlineTokenHighlight>
);
}
@@ -242,7 +255,10 @@ function McpPresetMentionToken({
}) {
const { t } = useTranslation();
const color = preset.brand_color || INLINE_TOKEN_HIGHLIGHT_COLOR;
const mentionName = label.startsWith("@") ? label.slice(1) : label;
const displayName = preset.display_name?.trim() || preset.name;
const mentionName = variant === "message"
? displayName
: composerMentionLabel({ kind: "mcp", text: label, preset }).slice(1);
const logoUrls = useMemo(() => logoFallbackUrls(preset.logo_url), [preset.logo_url]);
const { logoUrl, onLogoError, onLogoLoad } = useLogoFallback(logoUrls);
const showLogo = Boolean(logoUrl);
@@ -251,12 +267,20 @@ function McpPresetMentionToken({
return (
<InlineTokenHighlight
testId={`${testIdPrefix}-mcp-mention-${preset.name}`}
title={t("thread.composer.mentions.mcpTitle", { name: preset.display_name || preset.name })}
title={t("thread.composer.mentions.mcpTitle", {
name: variant === "message" ? `${displayName} (${label})` : displayName,
})}
color={color}
className={variant === "composer" ? "font-normal" : undefined}
className={variant === "composer" ? "font-normal" : "inline-flex max-w-full items-baseline [overflow-wrap:anywhere]"}
>
<span
className={cn("relative inline-block", showLogo && "text-transparent")}
className={cn(
"relative",
variant === "composer" ? "inline" : "inline-block",
variant === "message" && "shrink-0",
showLogo && "text-transparent",
showLogo && variant === "message" && "mr-1 w-[1.1em]",
)}
style={{ lineHeight: "inherit" }}
>
@
@@ -264,9 +288,10 @@ function McpPresetMentionToken({
<span
data-testid={`${testIdPrefix}-mcp-mention-logo-${preset.name}`}
className={cn(
"absolute left-1/2 top-1/2 grid place-items-center overflow-hidden rounded-mark",
"-translate-x-1/2 -translate-y-1/2",
isHero ? "h-[0.74em] w-[0.74em]" : "h-[0.72em] w-[0.72em]",
"absolute left-0 top-1/2 grid -translate-y-1/2 place-items-center overflow-hidden rounded-[0.25em]",
variant === "message"
? "h-[1.1em] w-[1.1em]"
: isHero ? "h-[0.92em] w-[0.92em]" : "h-[0.9em] w-[0.9em]",
)}
>
<img
@@ -281,7 +306,7 @@ function McpPresetMentionToken({
</span>
) : null}
</span>
{mentionName}
{variant === "message" ? <span className="min-w-0">{mentionName}</span> : mentionName}
</InlineTokenHighlight>
);
}
@@ -1342,8 +1342,8 @@ function McpPresetLogo({
return (
<span
className={cn(
"grid shrink-0 place-items-center border border-border/45 bg-background",
compact ? "h-10 w-10 rounded-control" : "h-11 w-11 rounded-compact",
"grid shrink-0 place-items-center overflow-hidden",
compact ? "h-10 w-10 rounded-control" : "h-9 w-9 rounded-[10px]",
)}
>
<img
@@ -1351,7 +1351,7 @@ function McpPresetLogo({
alt=""
decoding="async"
loading="lazy"
className={cn("object-contain", compact ? "h-[22px] w-[22px]" : "h-6 w-6")}
className="h-full w-full object-contain"
onLoad={onLogoLoad}
onError={onLogoError}
/>
@@ -1364,7 +1364,7 @@ function McpPresetLogo({
"grid shrink-0 place-items-center font-semibold text-white",
compact
? "h-10 w-10 rounded-control text-[12px]"
: "h-11 w-11 rounded-compact text-[13px]",
: "h-9 w-9 rounded-[10px] text-[12px]",
)}
style={{ backgroundColor: bg }}
>
@@ -1463,7 +1463,10 @@ function CliAppLogo({ app, showBrandLogos }: { app: CliAppInfo; showBrandLogos:
return (
<span
className="relative grid h-11 w-11 shrink-0 place-items-center overflow-hidden rounded-compact border border-border/45 bg-muted text-[13px] font-semibold"
className={cn(
"relative grid h-9 w-9 shrink-0 place-items-center overflow-hidden rounded-[10px] text-[12px] font-semibold",
showRemoteLogo && logoLoaded ? "bg-transparent" : "bg-muted",
)}
style={{ color: app.brand_color || "hsl(var(--muted-foreground))" }}
>
<span
@@ -1484,7 +1487,7 @@ function CliAppLogo({ app, showBrandLogos }: { app: CliAppInfo; showBrandLogos:
referrerPolicy="no-referrer"
draggable={false}
className={cn(
"absolute h-6 w-6 object-contain transition-opacity duration-150 motion-reduce:transition-none",
"absolute h-full w-full object-contain transition-opacity duration-150 motion-reduce:transition-none",
logoLoaded ? "opacity-100" : "opacity-0",
)}
onLoad={onLogoLoad}
+67 -46
View File
@@ -86,6 +86,7 @@ import {
type RestoredReadyImage,
} from "@/hooks/useAttachedImages";
import { useClipboardAndDrop } from "@/hooks/useClipboardAndDrop";
import { useComposerMentionInput } from "@/hooks/useComposerMentionInput";
import { useLogoFallback } from "@/hooks/useLogoFallback";
import { useMediaQuery } from "@/hooks/useMediaQuery";
import type { SendAttachment, SendOptions } from "@/hooks/useNanobotStream";
@@ -960,6 +961,7 @@ export function ThreadComposer({
const [queuedPrompts, setQueuedPrompts] = useState<QueuedPrompt[]>([]);
const hasTouchPrimaryPointer = useMediaQuery("(hover: none) and (pointer: coarse)");
const textareaRef = useRef<HTMLTextAreaElement>(null);
const mentionOverlayRef = useRef<HTMLDivElement>(null);
const formRef = useRef<HTMLFormElement>(null);
const fileInputRef = useRef<HTMLInputElement>(null);
const chipRefs = useRef(new Map<string, HTMLButtonElement>());
@@ -1283,6 +1285,20 @@ export function ThreadComposer({
() => splitCapabilityMentionSegments(value, cliApps, mcpPresets, selectedSessionMentions),
[cliApps, mcpPresets, selectedSessionMentions, value],
);
const editMentionInput = useCallback((next: string, cursor: number) => {
secondEnterPromptIdRef.current = null;
setValue(next);
setSlashMenuDismissed(false);
setCliAppMenuDismissed(false);
setCursorPosition(cursor);
}, []);
const mentionInput = useComposerMentionInput({
segments: mentionSegments,
inputRef: textareaRef,
onEdit: editMentionInput,
resetKey: pendingQueueKey,
});
const { rawSelection, replace: replaceMentionInput } = mentionInput;
const sessionDragInsertion = sessionDragPreview
? mentionInsertion(
value,
@@ -1298,7 +1314,7 @@ export function ThreadComposer({
mcpPresets,
[...selectedSessionMentions, sessionDragPreview.mention],
)
: mentionSegments;
: mentionInput.segments;
const activeSessionMentions = useMemo(() => {
const seen = new Set<string>();
return mentionSegments.flatMap((segment) => {
@@ -1502,6 +1518,14 @@ export function ThreadComposer({
});
}, []);
useLayoutEffect(() => {
const el = textareaRef.current;
if (!el || mentionInput.isComposing) return;
el.style.height = "auto";
el.style.height = `${Math.min(el.scrollHeight, 260)}px`;
if (mentionOverlayRef.current) mentionOverlayRef.current.scrollTop = el.scrollTop;
}, [mentionInput.isComposing, mentionInput.value]);
// Runs before paint so switching sessions never flashes stale draft text.
useLayoutEffect(() => {
if (previousPendingQueueKeyRef.current === pendingQueueKey) return;
@@ -1628,14 +1652,7 @@ export function ThreadComposer({
const inserted = `${command.command}${suffix.startsWith(" ") ? "" : " "}`;
const next = `${value.slice(0, skillQuery.start)}${inserted}${suffix}`;
const nextCursor = skillQuery.start + inserted.length;
setValue(next);
setCursorPosition(nextCursor);
requestAnimationFrame(() => {
const el = textareaRef.current;
if (!el) return;
el.focus();
el.setSelectionRange(nextCursor, nextCursor);
});
replaceMentionInput(next, nextCursor);
} else {
setValue(command.argHint ? `${command.command} ` : command.command);
}
@@ -1644,7 +1661,7 @@ export function ThreadComposer({
setInlineError(null);
resizeTextarea();
},
[isStreaming, onStop, recentSlashCommands, resizeTextarea, skillQuery, value],
[isStreaming, onStop, recentSlashCommands, replaceMentionInput, resizeTextarea, skillQuery, value],
);
const insertMentionCandidate = useCallback(
@@ -1664,20 +1681,13 @@ export function ThreadComposer({
]);
}
const insertion = mentionInsertion(value, candidate.name, start, end);
setValue(insertion.value);
setCursorPosition(insertion.cursor);
replaceMentionInput(insertion.value, insertion.cursor);
setCliAppMenuDismissed(true);
setSlashMenuDismissed(false);
setInlineError(null);
resizeTextarea();
requestAnimationFrame(() => {
const el = textareaRef.current;
if (!el) return;
el.focus();
el.setSelectionRange(insertion.cursor, insertion.cursor);
});
},
[activeSessionMentions, resizeTextarea, value],
[activeSessionMentions, replaceMentionInput, resizeTextarea, value],
);
const chooseMentionCandidate = useCallback(
@@ -1700,7 +1710,7 @@ export function ThreadComposer({
(candidate) => candidate.session_key === (sessionKey ?? preview?.mention.session_key),
);
if (!mention) return true;
const caret = preview?.start ?? textareaRef.current?.selectionStart ?? value.length;
const caret = preview?.start ?? rawSelection().start;
insertMentionCandidate(
{
kind: "session",
@@ -1709,13 +1719,14 @@ export function ThreadComposer({
mention,
},
caret,
preview?.end ?? textareaRef.current?.selectionEnd ?? caret,
preview?.end ?? rawSelection().end,
);
return true;
}, [
availableSessionMentions,
insertMentionCandidate,
interactionDisabled,
rawSelection,
sessionDragPreview,
value.length,
]);
@@ -1741,8 +1752,7 @@ export function ThreadComposer({
}
event.preventDefault();
event.dataTransfer.dropEffect = "copy";
const start = textareaRef.current?.selectionStart ?? value.length;
const end = textareaRef.current?.selectionEnd ?? start;
const { start, end } = rawSelection();
setSessionDragPreview((current) => (
current?.mention.session_key === mention.session_key
&& current.start === start
@@ -1751,7 +1761,7 @@ export function ThreadComposer({
: { mention, start, end }
));
return true;
}, [activeSessionMentions, availableSessionMentions, interactionDisabled, value.length]);
}, [activeSessionMentions, availableSessionMentions, interactionDisabled, rawSelection]);
useEffect(() => {
if (!sessionDragPreview) return;
@@ -1821,12 +1831,11 @@ export function ThreadComposer({
const editQueuedPrompt = useCallback((prompt: QueuedPrompt) => {
secondEnterPromptIdRef.current = null;
setQueuedPrompts((items) => items.filter((item) => item.id !== prompt.id));
setValue(prompt.text);
replaceMentionInput(prompt.text, prompt.text.length);
setSelectedSessionMentions(prompt.sessionMentions ?? []);
setInlineError(null);
setSlashMenuDismissed(false);
setCliAppMenuDismissed(false);
setCursorPosition(prompt.text.length);
onQuotedContextChange?.(prompt.quotedContext ?? null);
if (prompt.images?.length) {
restoreReadyImages(prompt.images as RestoredReadyImage[]);
@@ -1834,13 +1843,7 @@ export function ThreadComposer({
clear();
}
resizeTextarea();
requestAnimationFrame(() => {
const el = textareaRef.current;
if (!el) return;
el.focus();
el.setSelectionRange(prompt.text.length, prompt.text.length);
});
}, [clear, onQuotedContextChange, resizeTextarea, restoreReadyImages]);
}, [clear, onQuotedContextChange, replaceMentionInput, resizeTextarea, restoreReadyImages]);
const moveQueuedPrompt = useCallback((dragId: string, targetId: string) => {
if (dragId === targetId) return;
@@ -2058,6 +2061,8 @@ export function ThreadComposer({
]);
const onKeyDown = (e: ReactKeyboardEvent<HTMLTextAreaElement>) => {
mentionInput.onKeyDown(e);
if (e.defaultPrevented || e.nativeEvent.isComposing || mentionInput.isComposing) return;
if (showCliAppMenu) {
if (e.key === "ArrowDown") {
e.preventDefault();
@@ -2392,8 +2397,10 @@ export function ThreadComposer({
<div className="thread-composer-input relative min-w-0">
{hasMentionDecorations ? (
<ComposerCliMentionOverlay
overlayRef={mentionOverlayRef}
segments={displayMentionSegments}
isHero={isHero}
isComposing={mentionInput.isComposing}
className={inputTextClasses}
ghostRange={sessionDragInsertion
? { start: sessionDragInsertion.tokenStart, end: sessionDragInsertion.tokenEnd }
@@ -2402,25 +2409,26 @@ export function ThreadComposer({
) : null}
<textarea
ref={textareaRef}
value={value}
value={mentionInput.value}
onFocus={() => {
if (compactWhenIdle) setComposerFocused(true);
}}
onChange={(e) => {
secondEnterPromptIdRef.current = null;
setValue(e.target.value);
setSlashMenuDismissed(false);
setCliAppMenuDismissed(false);
setCursorPosition(e.target.selectionStart ?? e.target.value.length);
}}
onChange={mentionInput.onChange}
onCompositionStart={mentionInput.onCompositionStart}
onCompositionEnd={mentionInput.onCompositionEnd}
onBlur={() => {
secondEnterPromptIdRef.current = null;
}}
onInput={onInput}
onKeyDown={onKeyDown}
onKeyUp={(e) => setCursorPosition(e.currentTarget.selectionStart ?? e.currentTarget.value.length)}
onSelect={(e) => setCursorPosition(e.currentTarget.selectionStart ?? e.currentTarget.value.length)}
onClick={(e) => setCursorPosition(e.currentTarget.selectionStart ?? e.currentTarget.value.length)}
onKeyUp={() => setCursorPosition(rawSelection().start)}
onSelect={() => setCursorPosition(rawSelection().start)}
onClick={() => setCursorPosition(rawSelection().start)}
onCopy={mentionInput.onCopy}
onCut={mentionInput.onCut}
onScroll={(e) => {
if (mentionOverlayRef.current) mentionOverlayRef.current.scrollTop = e.currentTarget.scrollTop;
}}
onPaste={onPaste}
rows={1}
placeholder={sessionDragPreview ? "" : resolvedPlaceholder}
@@ -2428,7 +2436,7 @@ export function ThreadComposer({
aria-label={inputAriaLabel ?? t("thread.composer.inputAria")}
className={cn(
inputTextClasses,
"relative z-10 caret-foreground placeholder:text-muted-foreground/70",
"relative z-10 block caret-foreground placeholder:text-muted-foreground/70",
"focus:outline-none focus-visible:outline-none",
"disabled:cursor-not-allowed",
hasMentionDecorations && "text-transparent selection:bg-primary/20",
@@ -2830,23 +2838,31 @@ function QueuedPromptRow({
}
function ComposerCliMentionOverlay({
overlayRef,
segments,
isHero,
isComposing,
className,
ghostRange,
}: {
overlayRef: Ref<HTMLDivElement>;
segments: CapabilityMentionSegment[];
isHero: boolean;
isComposing: boolean;
className: string;
ghostRange?: { start: number; end: number } | null;
}) {
let offset = 0;
const occurrences = new Map<string, number>();
return (
<div
ref={overlayRef}
aria-hidden
className={cn(
className,
"pointer-events-none absolute inset-0 z-0 overflow-hidden whitespace-pre-wrap break-words text-foreground",
// Native IME selection backgrounds can be opaque; keep the mirrored glyphs visible.
isComposing && "z-20",
)}
>
{segments.map((segment, index) => {
@@ -2856,9 +2872,13 @@ function ComposerCliMentionOverlay({
return <span key={`text-${index}`}>{segment.text}</span>;
}
const isGhost = ghostRange?.start === start && ghostRange.end === offset;
const identity = `${segment.kind}-${segment.kind === "cli"
? segment.app.name : segment.kind === "mcp" ? segment.preset.name : segment.mention.session_key}`;
const occurrence = occurrences.get(identity) ?? 0;
occurrences.set(identity, occurrence + 1);
return (
<span
key={`${segment.kind}-${index}`}
key={`${identity}-${occurrence}`}
data-testid={isGhost ? "composer-session-drag-preview" : undefined}
className={cn(isGhost && "opacity-45 transition-opacity duration-100")}
>
@@ -2870,6 +2890,7 @@ function ComposerCliMentionOverlay({
</span>
);
})}
{segments.at(-1)?.text.endsWith("\n") ? "\u200b" : null}
</div>
);
}