@celestia-island/hikari 0.55.441 → 0.55.442
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/components/HkAdminHeader.tsx +12 -14
- package/src/components/HkAvatar.test.tsx +107 -0
- package/src/components/HkAvatar.tsx +8 -2
- package/src/components/HkAvatarButton.test.tsx +69 -0
- package/src/components/HkAvatarButton.tsx +7 -10
- package/src/components/HkMenuIdentityItem.test.tsx +88 -0
- package/src/components/HkMenuIdentityItem.tsx +11 -2
- package/src/components/avatarImage.ts +50 -0
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { computed, defineComponent, ref,
|
|
1
|
+
import { computed, defineComponent, ref, type PropType, type Ref, type SlotsType, type VNode } from "vue";
|
|
2
2
|
import { Camera, ExternalLink, Languages, LogOut, Menu } from "lucide-vue-next";
|
|
3
3
|
import { HBadge, HButton, HSpinner } from "@celestia-island/hikari";
|
|
4
4
|
import { useI18n } from "../i18n/context";
|
|
5
5
|
import { CHROME_BAND_DRAG_REGION } from "../runtime/windowChrome";
|
|
6
6
|
import HkMenu, { type HkMenuItem } from "./HkMenu";
|
|
7
7
|
import { HkAvatarButton } from "./HkAvatarButton";
|
|
8
|
+
import { useAvatarImage } from "./avatarImage";
|
|
8
9
|
|
|
9
10
|
export interface LocaleOption {
|
|
10
11
|
code: string;
|
|
@@ -154,6 +155,7 @@ export const HkAdminHeader = defineComponent({
|
|
|
154
155
|
},
|
|
155
156
|
setup(props, { emit, slots }) {
|
|
156
157
|
const { t } = useI18n();
|
|
158
|
+
const menuAvatar = useAvatarImage(() => props.avatarUrl);
|
|
157
159
|
const userMenuOpen = ref(false);
|
|
158
160
|
const userTriggerRef = ref<HTMLElement>();
|
|
159
161
|
const avatarModalOpen = ref(false);
|
|
@@ -162,16 +164,12 @@ export const HkAdminHeader = defineComponent({
|
|
|
162
164
|
// THIS popup instead of the built-in children cascade — anchored to
|
|
163
165
|
// the avatar trigger, so the popup opens exactly where the menu was.
|
|
164
166
|
const localePickerOpen = ref(false);
|
|
165
|
-
// The DROPDOWN identity image
|
|
166
|
-
//
|
|
167
|
-
//
|
|
168
|
-
//
|
|
169
|
-
//
|
|
170
|
-
|
|
171
|
-
watch(
|
|
172
|
-
() => props.avatarUrl,
|
|
173
|
-
() => { menuAvatarFailed.value = false; },
|
|
174
|
-
);
|
|
167
|
+
// The DROPDOWN identity image rides the SAME failed-load latch as the
|
|
168
|
+
// trigger (HkAvatarButton) and the avatar primitives — an avatar
|
|
169
|
+
// re-upload or account switch must heal BOTH faces, and one shared
|
|
170
|
+
// implementation is what keeps them from drifting (R2's S2 on #909
|
|
171
|
+
// was the missing reset; R2 on #1429 caught this fourth hand-rolled
|
|
172
|
+
// copy).
|
|
175
173
|
|
|
176
174
|
// The identity cluster is INJECTED, not assumed (2026-10-24): the
|
|
177
175
|
// trigger renders only when the host actually supplied an identity
|
|
@@ -384,12 +382,12 @@ export const HkAdminHeader = defineComponent({
|
|
|
384
382
|
// s-user-header profile variant).
|
|
385
383
|
<div class="s-user-header s-user-header--profile">
|
|
386
384
|
<span class="s-user-avatar-chip" aria-hidden="true">
|
|
387
|
-
{
|
|
385
|
+
{menuAvatar.src.value ? (
|
|
388
386
|
<img
|
|
389
|
-
src={
|
|
387
|
+
src={menuAvatar.src.value}
|
|
390
388
|
alt=""
|
|
391
389
|
class="s-user-avatar-img"
|
|
392
|
-
onError={
|
|
390
|
+
onError={menuAvatar.onError}
|
|
393
391
|
/>
|
|
394
392
|
) : (
|
|
395
393
|
<span class="s-user-avatar">
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
2
|
+
import { createApp, defineComponent, h, nextTick, ref } from "vue";
|
|
3
|
+
|
|
4
|
+
import HkAvatar from "./HkAvatar";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HkAvatar — the shared avatar primitive.
|
|
8
|
+
*
|
|
9
|
+
* The 2026-11-20 report: a member row whose stored `/static/avatars/…` URL
|
|
10
|
+
* had gone stale rendered the browser's broken-picture glyph instead of the
|
|
11
|
+
* default letter avatar. The primitive must treat "the URL 404s" exactly like
|
|
12
|
+
* "no URL was ever stored": same letter chip, same centred glyph. The latch
|
|
13
|
+
* is per-URL, so an account switch / fresh upload gets a real retry.
|
|
14
|
+
*
|
|
15
|
+
* (Repo test convention: raw createApp + container queries, no
|
|
16
|
+
* @vue/test-utils dependency.)
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const mounts: Array<{ app: ReturnType<typeof createApp>; container: HTMLElement }> = [];
|
|
20
|
+
|
|
21
|
+
function mount(node: ReturnType<typeof h>) {
|
|
22
|
+
const container = document.createElement("div");
|
|
23
|
+
document.body.appendChild(container);
|
|
24
|
+
const app = createApp({ render: () => node });
|
|
25
|
+
app.mount(container);
|
|
26
|
+
mounts.push({ app, container });
|
|
27
|
+
return container;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
afterEach(() => {
|
|
31
|
+
for (const { app, container } of mounts.splice(0)) {
|
|
32
|
+
app.unmount();
|
|
33
|
+
container.remove();
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe("HkAvatar", () => {
|
|
38
|
+
it("renders the letter fallback when no src is given", () => {
|
|
39
|
+
const c = mount(h(HkAvatar, { name: "yuzu" }));
|
|
40
|
+
expect(c.querySelector(".hk-avatar-fallback")?.textContent).toBe("Y");
|
|
41
|
+
expect(c.querySelector("img")).toBeNull();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("renders the image with the name as alt", () => {
|
|
45
|
+
const c = mount(h(HkAvatar, { name: "yuzu", src: "/static/avatars/y.webp" }));
|
|
46
|
+
const img = c.querySelector("img") as HTMLImageElement | null;
|
|
47
|
+
expect(img).toBeTruthy();
|
|
48
|
+
expect(img!.getAttribute("src")).toBe("/static/avatars/y.webp");
|
|
49
|
+
expect(img!.getAttribute("alt")).toBe("yuzu");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("swaps a broken image for the SAME letter fallback instead of the broken-picture glyph", async () => {
|
|
53
|
+
const c = mount(h(HkAvatar, { name: "demiurge", src: "/static/avatars/gone.webp" }));
|
|
54
|
+
const img = c.querySelector("img") as HTMLImageElement;
|
|
55
|
+
expect(img).toBeTruthy();
|
|
56
|
+
img.dispatchEvent(new Event("error"));
|
|
57
|
+
await nextTick();
|
|
58
|
+
expect(c.querySelector("img"), "a 404 must not leave a broken <img>").toBeNull();
|
|
59
|
+
expect(c.querySelector(".hk-avatar-fallback")?.textContent).toBe("D");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("retries on the same instance when the src changes", async () => {
|
|
63
|
+
// A remount proves nothing (a fresh instance starts unlatched): the host
|
|
64
|
+
// re-renders the avatar with a new URL while it stays mounted, which is
|
|
65
|
+
// the account-switch / avatar-upload path.
|
|
66
|
+
const src = ref("/a.webp");
|
|
67
|
+
const Host = defineComponent({
|
|
68
|
+
setup: () => () => h(HkAvatar, { name: "yuzu", src: src.value }),
|
|
69
|
+
});
|
|
70
|
+
const c = mount(h(Host));
|
|
71
|
+
(c.querySelector("img") as HTMLImageElement).dispatchEvent(new Event("error"));
|
|
72
|
+
await nextTick();
|
|
73
|
+
expect(c.querySelector("img")).toBeNull();
|
|
74
|
+
|
|
75
|
+
src.value = "/b.webp";
|
|
76
|
+
await nextTick();
|
|
77
|
+
await nextTick();
|
|
78
|
+
const retry = c.querySelector("img") as HTMLImageElement | null;
|
|
79
|
+
expect(retry, "the latch reset — the new URL gets a fresh attempt").toBeTruthy();
|
|
80
|
+
expect(retry!.getAttribute("src")).toBe("/b.webp");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("renders the NORMALIZED src, not the raw prop", () => {
|
|
84
|
+
// Pins the BINDING (mirrors the other two call sites): a padded URL
|
|
85
|
+
// must reach the <img> trimmed. A revert to the raw prop renders the
|
|
86
|
+
// padded value and fails this test — a whitespace-ONLY input cannot
|
|
87
|
+
// discriminate, because the guard hides the <img> in both
|
|
88
|
+
// implementations.
|
|
89
|
+
const c = mount(h(HkAvatar, { name: "yuzu", src: " /a.webp " }));
|
|
90
|
+
const img = c.querySelector("img") as HTMLImageElement | null;
|
|
91
|
+
expect(img).toBeTruthy();
|
|
92
|
+
expect(img!.getAttribute("src")).toBe("/a.webp");
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("treats a whitespace-only src as absent", () => {
|
|
96
|
+
// The shared latch normalizes the URL; master rendered `<img src=" ">`
|
|
97
|
+
// (a broken glyph) for a padded value.
|
|
98
|
+
const c = mount(h(HkAvatar, { name: "yuzu", src: " " }));
|
|
99
|
+
expect(c.querySelector("img")).toBeNull();
|
|
100
|
+
expect(c.querySelector(".hk-avatar-fallback")?.textContent).toBe("Y");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("falls back to ? when the name is absent", () => {
|
|
104
|
+
const c = mount(h(HkAvatar, {}));
|
|
105
|
+
expect(c.querySelector(".hk-avatar-fallback")?.textContent).toBe("?");
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { computed, defineComponent, type PropType } from "vue";
|
|
2
2
|
import { useI18n } from "../i18n/context";
|
|
3
|
+
import { useAvatarImage } from "./avatarImage";
|
|
3
4
|
import "./HkAvatar.scss";
|
|
4
5
|
|
|
5
6
|
export default defineComponent({
|
|
@@ -15,11 +16,16 @@ export default defineComponent({
|
|
|
15
16
|
"hk-avatar",
|
|
16
17
|
`hk-avatar-${props.size}`,
|
|
17
18
|
]);
|
|
19
|
+
// A stored URL is not a guarantee: the blob can 404 (deleted upload,
|
|
20
|
+
// external provider unreachable, mirror cold). The failed-load latch
|
|
21
|
+
// swaps the broken <img> for the SAME letter chip the absent-src branch
|
|
22
|
+
// renders, so the roster never shows a browser broken-picture glyph.
|
|
23
|
+
const avatar = useAvatarImage(() => props.src);
|
|
18
24
|
|
|
19
25
|
return () => (
|
|
20
26
|
<div class={cls.value}>
|
|
21
|
-
{
|
|
22
|
-
<img src={
|
|
27
|
+
{avatar.src.value ? (
|
|
28
|
+
<img src={avatar.src.value} alt={props.name} onError={avatar.onError} />
|
|
23
29
|
) : (
|
|
24
30
|
<span class="hk-avatar-fallback">{props.name?.charAt(0)?.toUpperCase() ?? t("hikari::avatar.fallback", "?")}</span>
|
|
25
31
|
)}
|
|
@@ -182,6 +182,75 @@ describe("HkAdminHeader × HkAvatarButton / identity slot", () => {
|
|
|
182
182
|
expect(c.querySelector(".s-admin-header-user")).toBeTruthy();
|
|
183
183
|
});
|
|
184
184
|
|
|
185
|
+
it("renders the NORMALIZED url in the dropdown identity image", () => {
|
|
186
|
+
// Pins the BINDING of the admin-header call site (R3 mutation D): a
|
|
187
|
+
// padded URL must reach the <img> trimmed; a raw-prop revert renders
|
|
188
|
+
// the padded value and fails this test.
|
|
189
|
+
const c = mount(h(HkAdminHeader, { username: "yuzu", avatarUrl: " /a.png ", title: "D" }));
|
|
190
|
+
trigger(c).dispatchEvent(new MouseEvent("click", { bubbles: true }));
|
|
191
|
+
return Promise.resolve().then(async () => {
|
|
192
|
+
await nextTick();
|
|
193
|
+
await nextTick();
|
|
194
|
+
const img = document.body.querySelector(
|
|
195
|
+
".s-user-header--profile img.s-user-avatar-img",
|
|
196
|
+
) as HTMLImageElement | null;
|
|
197
|
+
expect(img).toBeTruthy();
|
|
198
|
+
expect(img!.getAttribute("src")).toBe("/a.png");
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("treats a whitespace-only avatarUrl as absent — the dropdown <img> never mounts", () => {
|
|
203
|
+
const c = mount(h(HkAdminHeader, { username: "yuzu", avatarUrl: " ", title: "D" }));
|
|
204
|
+
trigger(c).dispatchEvent(new MouseEvent("click", { bubbles: true }));
|
|
205
|
+
return Promise.resolve().then(async () => {
|
|
206
|
+
await nextTick();
|
|
207
|
+
await nextTick();
|
|
208
|
+
expect(
|
|
209
|
+
document.body.querySelector(".s-user-header--profile img.s-user-avatar-img"),
|
|
210
|
+
).toBeNull();
|
|
211
|
+
expect(
|
|
212
|
+
document.body.querySelector(".s-user-header--profile .s-user-avatar")?.textContent,
|
|
213
|
+
).toBe("Y");
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("heals the dropdown identity image with the shared latch", async () => {
|
|
218
|
+
// The fourth copy R2 caught: HkAdminHeader's in-menu identity image
|
|
219
|
+
// used its own failed-load ref. It must behave exactly like the trigger
|
|
220
|
+
// — an error shows the letter chip, and a URL change retries on the
|
|
221
|
+
// SAME mounted header (account switch / avatar re-upload).
|
|
222
|
+
const url = ref("/a.png");
|
|
223
|
+
const Host = defineComponent({
|
|
224
|
+
setup: () => () => h(HkAdminHeader, { username: "yuzu", avatarUrl: url.value, title: "D" }),
|
|
225
|
+
});
|
|
226
|
+
const c = mount(h(Host));
|
|
227
|
+
trigger(c).dispatchEvent(new MouseEvent("click", { bubbles: true }));
|
|
228
|
+
await nextTick();
|
|
229
|
+
await nextTick();
|
|
230
|
+
const first = document.body.querySelector(
|
|
231
|
+
".s-user-header--profile img.s-user-avatar-img",
|
|
232
|
+
) as HTMLImageElement | null;
|
|
233
|
+
expect(first, "the dropdown identity image renders").toBeTruthy();
|
|
234
|
+
first!.dispatchEvent(new Event("error"));
|
|
235
|
+
await nextTick();
|
|
236
|
+
expect(
|
|
237
|
+
document.body.querySelector(".s-user-header--profile img.s-user-avatar-img"),
|
|
238
|
+
"a broken identity image leaves the letter chip, not a broken glyph",
|
|
239
|
+
).toBeNull();
|
|
240
|
+
expect(
|
|
241
|
+
document.body.querySelector(".s-user-header--profile .s-user-avatar")?.textContent,
|
|
242
|
+
).toBe("Y");
|
|
243
|
+
|
|
244
|
+
url.value = "/b.png";
|
|
245
|
+
await nextTick();
|
|
246
|
+
await nextTick();
|
|
247
|
+
const retry = document.body.querySelector(
|
|
248
|
+
".s-user-header--profile img.s-user-avatar-img",
|
|
249
|
+
) as HTMLImageElement | null;
|
|
250
|
+
expect(retry, "the latch reset — the new URL gets a fresh attempt").toBeTruthy();
|
|
251
|
+
expect(retry!.getAttribute("src")).toBe("/b.png");
|
|
252
|
+
});
|
|
253
|
+
|
|
185
254
|
it("an identity slot replaces the built-in dropdown block; the pending branch stays built-in", async () => {
|
|
186
255
|
const opened = h(HkAdminHeader, {
|
|
187
256
|
username: "yuzu",
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { defineComponent,
|
|
1
|
+
import { defineComponent, type PropType } from "vue";
|
|
2
|
+
import { useAvatarImage } from "./avatarImage";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* HkAvatarButton — the avatar trigger button, ONE component for every
|
|
@@ -40,13 +41,9 @@ export const HkAvatarButton = defineComponent({
|
|
|
40
41
|
click: (_e: MouseEvent) => true,
|
|
41
42
|
},
|
|
42
43
|
setup(props, { emit }) {
|
|
43
|
-
const avatarFailed = ref(false);
|
|
44
44
|
// A new URL deserves a fresh attempt (account switch, avatar upload)
|
|
45
|
-
// — the
|
|
46
|
-
|
|
47
|
-
() => props.avatarUrl,
|
|
48
|
-
() => { avatarFailed.value = false; },
|
|
49
|
-
);
|
|
45
|
+
// — the shared failed-load latch owns the reset (avatarImage.ts).
|
|
46
|
+
const avatar = useAvatarImage(() => props.avatarUrl);
|
|
50
47
|
|
|
51
48
|
return () => (
|
|
52
49
|
<button
|
|
@@ -56,12 +53,12 @@ export const HkAvatarButton = defineComponent({
|
|
|
56
53
|
aria-expanded={props.expanded}
|
|
57
54
|
onClick={(e: MouseEvent) => { emit("click", e); }}
|
|
58
55
|
>
|
|
59
|
-
{
|
|
56
|
+
{avatar.src.value ? (
|
|
60
57
|
<img
|
|
61
|
-
src={
|
|
58
|
+
src={avatar.src.value}
|
|
62
59
|
alt={props.username}
|
|
63
60
|
class="s-admin-header-avatar-img"
|
|
64
|
-
onError={
|
|
61
|
+
onError={avatar.onError}
|
|
65
62
|
/>
|
|
66
63
|
) : (
|
|
67
64
|
// Letter fallback on the shared avatar primitive — the same
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
2
|
+
import { createApp, defineComponent, h, nextTick, ref } from "vue";
|
|
3
|
+
|
|
4
|
+
import HkMenuIdentityItem from "./HkMenuIdentityItem";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HkMenuIdentityItem — the standard menu identity row.
|
|
8
|
+
*
|
|
9
|
+
* Same 2026-11-20 rule as HkAvatar: a stored avatar URL that 404s must fall
|
|
10
|
+
* back to the initial-letter chip, never to the browser's broken-picture
|
|
11
|
+
* glyph, and a new URL on the same instance must retry.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const mounts: Array<{ app: ReturnType<typeof createApp>; container: HTMLElement }> = [];
|
|
15
|
+
|
|
16
|
+
function mount(node: ReturnType<typeof h>) {
|
|
17
|
+
const container = document.createElement("div");
|
|
18
|
+
document.body.appendChild(container);
|
|
19
|
+
const app = createApp({ render: () => node });
|
|
20
|
+
app.mount(container);
|
|
21
|
+
mounts.push({ app, container });
|
|
22
|
+
return container;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
for (const { app, container } of mounts.splice(0)) {
|
|
27
|
+
app.unmount();
|
|
28
|
+
container.remove();
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe("HkMenuIdentityItem", () => {
|
|
33
|
+
it("renders the initial fallback when no avatar URL is given", () => {
|
|
34
|
+
const c = mount(h(HkMenuIdentityItem, { name: "yuzu", subtitle: "y@zu" }));
|
|
35
|
+
expect(c.querySelector(".hk-menu-identity-item__fallback")?.textContent).toBe("Y");
|
|
36
|
+
expect(c.querySelector("img")).toBeNull();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("swaps a broken avatar for the initial fallback", async () => {
|
|
40
|
+
const c = mount(h(HkMenuIdentityItem, {
|
|
41
|
+
name: "yuzu",
|
|
42
|
+
subtitle: "y@zu",
|
|
43
|
+
avatarUrl: "/static/avatars/gone.webp",
|
|
44
|
+
}));
|
|
45
|
+
const img = c.querySelector("img") as HTMLImageElement;
|
|
46
|
+
expect(img).toBeTruthy();
|
|
47
|
+
img.dispatchEvent(new Event("error"));
|
|
48
|
+
await nextTick();
|
|
49
|
+
expect(c.querySelector("img")).toBeNull();
|
|
50
|
+
expect(c.querySelector(".hk-menu-identity-item__fallback")?.textContent).toBe("Y");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("renders the NORMALIZED url, not the raw prop", () => {
|
|
54
|
+
// Pins the BINDING, not just the decision (R3 mutation C): a padded
|
|
55
|
+
// URL must reach the <img> trimmed. A revert to the raw prop renders
|
|
56
|
+
// ` /a.webp ` in the src attribute and fails this test (a
|
|
57
|
+
// whitespace-ONLY input cannot discriminate — the guard hides the img
|
|
58
|
+
// in both implementations).
|
|
59
|
+
const c = mount(h(HkMenuIdentityItem, { name: "yuzu", avatarUrl: " /a.webp " }));
|
|
60
|
+
const img = c.querySelector("img.hk-menu-identity-item__img") as HTMLImageElement | null;
|
|
61
|
+
expect(img).toBeTruthy();
|
|
62
|
+
expect(img!.getAttribute("src")).toBe("/a.webp");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("treats a whitespace-only avatarUrl as absent — the <img> never mounts", () => {
|
|
66
|
+
const c = mount(h(HkMenuIdentityItem, { name: "yuzu", avatarUrl: " " }));
|
|
67
|
+
expect(c.querySelector("img.hk-menu-identity-item__img")).toBeNull();
|
|
68
|
+
expect(c.querySelector(".hk-menu-identity-item__fallback")?.textContent).toBe("Y");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("retries when the avatar URL changes on the same instance", async () => {
|
|
72
|
+
const url = ref("/a.webp");
|
|
73
|
+
const Host = defineComponent({
|
|
74
|
+
setup: () => () => h(HkMenuIdentityItem, { name: "yuzu", avatarUrl: url.value }),
|
|
75
|
+
});
|
|
76
|
+
const c = mount(h(Host));
|
|
77
|
+
(c.querySelector("img") as HTMLImageElement).dispatchEvent(new Event("error"));
|
|
78
|
+
await nextTick();
|
|
79
|
+
expect(c.querySelector("img")).toBeNull();
|
|
80
|
+
|
|
81
|
+
url.value = "/b.webp";
|
|
82
|
+
await nextTick();
|
|
83
|
+
await nextTick();
|
|
84
|
+
const retry = c.querySelector("img") as HTMLImageElement | null;
|
|
85
|
+
expect(retry, "the latch reset — the new URL gets a fresh attempt").toBeTruthy();
|
|
86
|
+
expect(retry!.getAttribute("src")).toBe("/b.webp");
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { defineComponent } from "vue";
|
|
2
|
+
import { useAvatarImage } from "./avatarImage";
|
|
2
3
|
import "./HkMenuIdentityItem.scss";
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -18,11 +19,19 @@ export default defineComponent({
|
|
|
18
19
|
avatarAlt: { type: String, default: "" },
|
|
19
20
|
},
|
|
20
21
|
setup(props, { slots }) {
|
|
22
|
+
// Same failed-load latch as HkAvatar: a stored URL that 404s must not
|
|
23
|
+
// leave the browser's broken-picture glyph in the menu header.
|
|
24
|
+
const avatar = useAvatarImage(() => props.avatarUrl);
|
|
21
25
|
return () => (
|
|
22
26
|
<div class="hk-menu-identity-item" role="presentation">
|
|
23
27
|
<div class="hk-menu-identity-item__avatar" aria-hidden="true">
|
|
24
|
-
{
|
|
25
|
-
<img
|
|
28
|
+
{avatar.src.value ? (
|
|
29
|
+
<img
|
|
30
|
+
src={avatar.src.value}
|
|
31
|
+
alt={props.avatarAlt || props.name}
|
|
32
|
+
class="hk-menu-identity-item__img"
|
|
33
|
+
onError={avatar.onError}
|
|
34
|
+
/>
|
|
26
35
|
) : (
|
|
27
36
|
<span class="hk-menu-identity-item__fallback">
|
|
28
37
|
{(props.name || "?").charAt(0).toUpperCase()}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// ------
|
|
2
|
+
// Shared avatar image state
|
|
3
|
+
// ------
|
|
4
|
+
//
|
|
5
|
+
// ONE failed-load latch for every avatar surface in the library. An
|
|
6
|
+
// `<img>` that 404s renders the browser's broken-picture glyph — the exact
|
|
7
|
+
// "missing image" look 2026-11-20 user report called out for the workspace
|
|
8
|
+
// roster (demiurge's `/static/avatars/…` blob had gone stale; the component
|
|
9
|
+
// kept the broken image instead of its own letter chip). A stored avatar URL
|
|
10
|
+
// can always 404 (deleted upload, external provider unreachable, mirror cache
|
|
11
|
+
// cold), so "the URL exists" is not "there is an avatar": every face must
|
|
12
|
+
// degrade to the SAME letter fallback it renders when the URL is absent.
|
|
13
|
+
//
|
|
14
|
+
// The latch is per-URL: a new `src` (account switch, avatar re-upload) gets a
|
|
15
|
+
// fresh attempt, or one broken URL would poison the chip for the rest of the
|
|
16
|
+
// component's life. Same contract as HkAvatarButton's hand-rolled latch
|
|
17
|
+
// (2026-10-29), now shared so the four call sites cannot drift (HkAvatar,
|
|
18
|
+
// HkAvatarButton, HkMenuIdentityItem, HkAdminHeader's dropdown image).
|
|
19
|
+
|
|
20
|
+
import { computed, ref, watch, type ComputedRef } from "vue";
|
|
21
|
+
|
|
22
|
+
/** `src` getter — pass the component prop, e.g. `() => props.src`. */
|
|
23
|
+
export interface AvatarImageState {
|
|
24
|
+
/** The URL to render, or "" when the letter fallback must show instead. */
|
|
25
|
+
src: ComputedRef<string>;
|
|
26
|
+
/** Wire to the `<img @error>` binding. */
|
|
27
|
+
onError: () => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Failed-load latch shared by HkAvatar / HkAvatarButton / HkMenuIdentityItem /
|
|
31
|
+
* HkAdminHeader's dropdown identity image. */
|
|
32
|
+
export function useAvatarImage(source: () => string | undefined): AvatarImageState {
|
|
33
|
+
const failed = ref(false);
|
|
34
|
+
// A new URL deserves a fresh attempt: reset on every source change. The
|
|
35
|
+
// getter makes this work for prop-driven updates on the same instance
|
|
36
|
+
// (a remount would restart unlatched anyway and prove nothing).
|
|
37
|
+
watch(source, () => {
|
|
38
|
+
failed.value = false;
|
|
39
|
+
});
|
|
40
|
+
const src = computed(() => {
|
|
41
|
+
const url = (source() ?? "").trim();
|
|
42
|
+
return url && !failed.value ? url : "";
|
|
43
|
+
});
|
|
44
|
+
return {
|
|
45
|
+
src,
|
|
46
|
+
onError: () => {
|
|
47
|
+
failed.value = true;
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|