Skip to content

Commit 59a8625

Browse files
only21milcodex
andcommitted
perf(web): skip favicon loading for custom project icons
Co-Authored-By: GPT-6.1 Sol <[email protected]>
1 parent 5ffe045 commit 59a8625

2 files changed

Lines changed: 64 additions & 7 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { RegistryContext } from "@effect/atom-react";
2+
import { EnvironmentId } from "@t3tools/contracts";
3+
import { Atom, AtomRegistry } from "effect/reactivity";
4+
import { expect, it, vi } from "vite-plus/test";
5+
6+
import { render } from "../../test/render";
7+
import { ProjectFavicon, type ProjectFaviconProject } from "./ProjectFavicon";
8+
9+
const state = vi.hoisted(() => ({ favicon: undefined as Atom.Atom<string | null> | undefined }));
10+
vi.mock(import("../state/assets"), () => ({
11+
projectFaviconUrlAtom: () => {
12+
if (!state.favicon) throw new Error("Favicon resource not configured");
13+
return state.favicon;
14+
},
15+
}));
16+
17+
it("only activates the favicon resource after removing a custom icon", async () => {
18+
const registry = AtomRegistry.make();
19+
const activeResources = new Set<string>();
20+
state.favicon = Atom.make((get) => {
21+
activeResources.add("favicon");
22+
get.addFinalizer(() => activeResources.delete("favicon"));
23+
return null;
24+
});
25+
const project: ProjectFaviconProject = {
26+
environmentId: EnvironmentId.make("test"),
27+
workspaceRoot: "/project",
28+
title: "Project",
29+
faviconPath: "icon.png",
30+
projectIcon: { kind: "monogram", text: "PR", color: "blue" },
31+
};
32+
const view = (projectIcon: ProjectFaviconProject["projectIcon"]) => (
33+
<RegistryContext value={registry}>
34+
<ProjectFavicon project={{ ...project, projectIcon }} />
35+
</RegistryContext>
36+
);
37+
try {
38+
await using screen = await render(view(project.projectIcon));
39+
expect(activeResources.size).toBe(0);
40+
await screen.rerender(view({ kind: "emoji", emoji: "🌱" }));
41+
expect(activeResources.size).toBe(0);
42+
await screen.rerender(view({ kind: "lucide", name: "leaf", color: "green" }));
43+
expect(activeResources.size).toBe(0);
44+
await screen.rerender(view(null));
45+
expect(activeResources.size).toBe(1);
46+
await screen.rerender(view(project.projectIcon));
47+
await screen.rerender(view(null));
48+
expect(activeResources.size).toBe(1);
49+
} finally {
50+
registry.dispose();
51+
}
52+
});

‎apps/web/src/components/ProjectFavicon.tsx‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ export function ProjectFavicon(input: {
4040
fallbackIcon?: ComponentType<{ className?: string }>;
4141
}) {
4242
const { project } = input;
43-
const src = useAtomValue(
44-
projectFaviconUrlAtom({
45-
environmentId: project.environmentId,
46-
cwd: project.workspaceRoot,
47-
faviconPath: project.faviconPath,
48-
}),
49-
);
5043
if (project.projectIcon?.kind === "monogram") {
5144
return (
5245
<ProjectMonogram
@@ -83,6 +76,18 @@ export function ProjectFavicon(input: {
8376
</span>
8477
);
8578
}
79+
return <AutomaticProjectFavicon {...input} />;
80+
}
81+
82+
function AutomaticProjectFavicon(input: Parameters<typeof ProjectFavicon>[0]) {
83+
const { project } = input;
84+
const src = useAtomValue(
85+
projectFaviconUrlAtom({
86+
environmentId: project.environmentId,
87+
cwd: project.workspaceRoot,
88+
faviconPath: project.faviconPath,
89+
}),
90+
);
8691
const FallbackIcon = input.fallbackIcon ?? FolderCodeIcon;
8792

8893
if (!src || isProjectFaviconFallbackUrl(src)) {

0 commit comments

Comments
 (0)