Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Templates: faces sit on the card's colour, a failed picture is tried again, a failed Add scrolls into view once

milind-soni8e1206c91adf
diff --git a/server/team-library-pictures.test.ts b/server/team-library-pictures.test.ts
index 2e522e0eca..d948073830 100644
--- a/server/team-library-pictures.test.ts
+++ b/server/team-library-pictures.test.ts
@@ -28,8 +28,8 @@ describe("the gallery's pictures", () => {
expect(fake!.picture).toBeUndefined();
expect(gone!.picture).toBeUndefined();
expect(fetcher).toHaveBeenCalledTimes(3);
- // Read once: a second catalog load reuses what was read.
- await withCatalogPictures(catalogWith(["teams/studio/avatars/real.png"]), fetcher);
- expect(fetcher).toHaveBeenCalledTimes(3);
+ // A picture read is kept; one that failed is tried again on the next load.
+ await withCatalogPictures(catalogWith(["teams/studio/avatars/real.png", "teams/studio/avatars/gone.png"]), fetcher);
+ expect(fetcher).toHaveBeenCalledTimes(4);
});
});
diff --git a/server/team-library-pictures.ts b/server/team-library-pictures.ts
index 8e25aa6553..527b1e10b2 100644
--- a/server/team-library-pictures.ts
+++ b/server/team-library-pictures.ts
@@ -1,7 +1,7 @@
// The community gallery's pictures. The catalog names each bot's picture by its path in the templates repository; the
// renderer never fetches from GitHub itself, so the catalog route reads each picture here (https to
// raw.githubusercontent.com only, no redirects, 64 KB at most, checked to be the picture its name says) and hands it on
-// as a data: URL. A picture that can't be read is left out, and the card draws that bot's mascot instead.
+// as a data: URL. A picture that can't be read is left out this time, and the card draws that bot's mascot instead.
import { TEAM_LIBRARY_RAW_ROOT, type TeamCatalog } from "./team-library.ts";
type Fetcher = typeof fetch;
@@ -18,8 +18,8 @@ const looksLike = (kind: keyof typeof TYPES, bytes: Uint8Array) => {
return bytes.length > 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff;
};
-/** Pictures already read, by path: a data: URL, or null when it couldn't be. */
-const cache = new Map<string, string | null>();
+/** Pictures already read, by path. One that couldn't be read is tried again on the next catalog load. */
+const cache = new Map<string, string>();
async function readPicture(path: string, fetcher: Fetcher): Promise<string | null> {
const kind = path.slice(path.lastIndexOf(".") + 1) as keyof typeof TYPES;
@@ -43,6 +43,7 @@ export async function withCatalogPictures(catalog: TeamCatalog, fetcher: Fetcher
.slice(0, MAX_PICTURES);
await Promise.all(paths.filter((path) => !cache.has(path)).map(async (path) => {
const picture = await readPicture(path, fetcher);
+ if (!picture) return;
if (cache.size >= MAX_CACHED) cache.delete(cache.keys().next().value!);
cache.set(path, picture);
}));
diff --git a/src/components/TeamCatalogCard.tsx b/src/components/TeamCatalogCard.tsx
index 1a300fe4ce..36010a6035 100644
--- a/src/components/TeamCatalogCard.tsx
+++ b/src/components/TeamCatalogCard.tsx
@@ -21,7 +21,7 @@ export function TeamCatalogCard({ entry, busy, disabled, onAdd }: {
return (
<article data-catalog-template={entry.slug} className="flex flex-col rounded-2xl border border-hairline/40 bg-card p-4">
<div className="flex items-center justify-between gap-3">
- <TemplateFaces avatars={catalogFaces(entry)} size={36} />
+ <TemplateFaces avatars={catalogFaces(entry)} size={36} on="card" />
<button
type="button"
onClick={onAdd}
diff --git a/src/components/TeamLibraryPanel.tsx b/src/components/TeamLibraryPanel.tsx
index 80266ca642..a21ca13bd8 100644
--- a/src/components/TeamLibraryPanel.tsx
+++ b/src/components/TeamLibraryPanel.tsx
@@ -46,6 +46,7 @@ export function TeamLibraryPanel({
}) {
const { dispatch } = useStore();
const dialogRef = useRef<HTMLDivElement>(null);
+ const galleryErrorRef = useRef<HTMLDivElement>(null);
const [catalog, setCatalog] = useState<TeamCatalog | null>(null);
const [catalogLoading, setCatalogLoading] = useState(true);
const [catalogError, setCatalogError] = useState("");
@@ -77,6 +78,11 @@ export function TeamLibraryPanel({
void loadCatalog();
}, [loadCatalog]);
+ // A failed Add shows its message above the cards, out of view when the card was further down.
+ useEffect(() => {
+ if (error) galleryErrorRef.current?.scrollIntoView({ block: "nearest" });
+ }, [error]);
+
const loadOrgLibrary = useCallback(async () => {
try {
// SAFETY: this endpoint is owned by the app and returns OrgLibraryListing.
@@ -348,7 +354,7 @@ export function TeamLibraryPanel({
<div className="mb-3 text-[12px] font-medium text-ink-tertiary">
{search ? t("templates.searchResults") : t("templates.communityTemplates")}
</div>
- {error && <div ref={(node) => node?.scrollIntoView({ block: "nearest" })} role="alert" className="mb-3 rounded-lg bg-danger/10 px-3 py-2 text-[13px] text-danger">{error}</div>}
+ {error && <div ref={galleryErrorRef} role="alert" className="mb-3 rounded-lg bg-danger/10 px-3 py-2 text-[13px] text-danger">{error}</div>}
{catalogLoading && (
<div className="flex items-center justify-center gap-2 py-24 text-[13px] text-ink-secondary">
<Loader2 size={16} className="animate-spin" /> {t("templates.loading")}
diff --git a/src/components/TemplateCard.tsx b/src/components/TemplateCard.tsx
index 3eda62ec01..fac3ae1ce9 100644
--- a/src/components/TemplateCard.tsx
+++ b/src/components/TemplateCard.tsx
@@ -42,13 +42,14 @@ function Face({ avatar, size }: { avatar: TemplateAvatar; size: number }) {
...(avatar.picture ? { avatarCrop: "circle" as const } : {}), mascotBody: avatar.mascotBody ? botBodyId(avatar.mascotBody) : null }} />;
}
-/** Up to four faces, overlapping, then "+N". */
-export function TemplateFaces({ avatars, size = 32 }: { avatars: readonly TemplateAvatar[]; size?: number }) {
+/** Up to four faces, overlapping, then "+N". `on` is what they sit on, so the gap between two faces is its colour. */
+export function TemplateFaces({ avatars, size = 32, on = "panel" }: { avatars: readonly TemplateAvatar[]; size?: number; on?: "panel" | "card" }) {
const shown = avatars.slice(0, FACES);
+ const ring = on === "card" ? "ring-card" : "ring-panel";
return (
<span className="flex shrink-0 items-center -space-x-2">
- {shown.map((avatar) => <span key={avatar.key} className="rounded-full ring-2 ring-panel"><Face avatar={avatar} size={size} /></span>)}
- {avatars.length > FACES && <span className="flex items-center justify-center rounded-full bg-raised text-[11px] text-ink-secondary ring-2 ring-panel tabular-nums"
+ {shown.map((avatar) => <span key={avatar.key} className={cn("rounded-full ring-2", ring)}><Face avatar={avatar} size={size} /></span>)}
+ {avatars.length > FACES && <span className={cn("flex items-center justify-center rounded-full bg-raised text-[11px] text-ink-secondary ring-2 tabular-nums", ring)}
style={{ width: size, height: size }}>+{avatars.length - FACES}</span>}
</span>
);