Files
raucao f341c228b8 Fix site icons cached from non-image responses
A response with no Content-Type reached FileReader as application/octet-stream, which the browser refuses to render, and a mismatched mime field previously marked such records as usable. Judge cached icons by their data URL MIME, sniff image bytes when the content type is missing or generic, reject markup, and fall back to the initial when an image fails to load.
2026-10-30 10:20:37 +04:00

119 lines
3.3 KiB
Plaintext

import Component from '@glimmer/component';
import { action } from '@ember/object';
import { on } from '@ember/modifier';
import { tracked } from '@glimmer/tracking';
import setStyle from '../../modifiers/set-style';
import { safeColor } from '../../utils/safe-color';
export default class SiteLinksItemComponent extends Component {
@tracked imgFailed = false;
get label() {
return this.args.item?.label ?? 'Site';
}
get unreadCount() {
return this.args.item?.unreadCount ?? 0;
}
get hasUnread() {
return this.unreadCount > 0;
}
get isError() {
return this.args.item?.status === 'error';
}
get link() {
return this.hasUnread && this.args.item?.notificationsHref
? this.args.item.notificationsHref
: this.args.item.href;
}
get icon() {
return this.args.item?.icon;
}
// A cached icon whose bytes aren't a decodable image fails silently in the
// browser, so fall back to the initial when the image errors.
get hasIcon() {
return Boolean(this.icon) && !this.imgFailed;
}
@action
handleImageError() {
this.imgFailed = true;
}
get initial() {
const first = this.label.trim()?.[0];
return first ? first.toUpperCase() : '?';
}
get imageClass() {
return this.icon?.maskable
? 'size-full object-cover'
: 'size-7 rounded-md object-contain';
}
get tileColor() {
return safeColor(this.icon?.themeColor);
}
get tooltip() {
if (this.isError && this.args.item?.error) {
return `${this.label} — ${this.args.item.error}`;
}
return this.label;
}
get tileOpacity() {
return this.hasUnread
? 'opacity-100'
: 'opacity-70 hover:opacity-100 focus-visible:opacity-100';
}
<template>
<a
href={{this.link}}
title={{this.tooltip}}
aria-label={{this.tooltip}}
data-status={{@item.status}}
data-error-type={{@item.errorType}}
class="relative block size-10 shrink-0 rounded-xl text-white transition-opacity focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-white
{{this.tileOpacity}}"
...attributes
>
<span
data-test-site-links-tile
class="icon-edge flex size-full items-center justify-center overflow-hidden rounded-xl bg-gray-950/40 backdrop-blur
{{if this.isError 'ring-2 ring-red-400/70'}}"
{{setStyle "background-color" this.tileColor}}
>
{{#if this.hasIcon}}
<img
src={{this.icon.dataUrl}}
alt=""
aria-hidden="true"
data-test-site-links-icon
class={{this.imageClass}}
{{on "error" this.handleImageError}}
/>
{{else}}
<span
data-test-site-links-fallback
class="flex size-full items-center justify-center bg-indigo-500 text-sm font-semibold"
>{{this.initial}}</span>
{{/if}}
</span>
{{#if this.hasUnread}}
<span
class="absolute -right-1 -bottom-1 inline-flex h-4 min-w-4 items-center justify-center rounded-full bg-red-500 px-1 text-xs font-semibold tabular-nums text-white shadow-[0_1px_2px_rgb(0_0_0_/_45%)]"
data-test-site-links-count
>{{this.unreadCount}}</span>
{{/if}}
</a>
</template>
}