Skip to content

Commit 2e5c601

Browse files
authored
🐛 修复脚本图标展示 #1052 (#1104)
* 🐛 删除脚本图标的背景色和边框 #1052 * 图片加载失败回退逻辑 * 修复二次刷新时图标加载问题
1 parent 9c67e4a commit 2e5c601

File tree

4 files changed

+21
-30
lines changed

4 files changed

+21
-30
lines changed

src/pages/install/App.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
Avatar,
32
Button,
43
Dropdown,
54
Message,
@@ -33,6 +32,7 @@ import { useSearchParams } from "react-router-dom";
3332
import { CACHE_KEY_SCRIPT_INFO } from "@App/app/cache_key";
3433
import { cacheInstance } from "@App/app/cache";
3534
import { formatBytes, prettyUrl } from "@App/pkg/utils/utils";
35+
import { ScriptIcons } from "../options/routes/utils";
3636

3737
const backgroundPromptShownKey = "background_prompt_shown";
3838

@@ -766,11 +766,7 @@ function App() {
766766
</Modal>
767767
<div className="tw-flex tw-flex-row tw-gap-x-3 tw-pt-3 tw-pb-3">
768768
<div className="tw-grow-1 tw-shrink-1 tw-flex tw-flex-row tw-justify-start tw-items-center">
769-
{upsertScript?.metadata.icon && (
770-
<Avatar size={32} shape="square" style={{ marginRight: "8px" }}>
771-
<img src={upsertScript.metadata.icon[0]} alt={upsertScript.name} />
772-
</Avatar>
773-
)}
769+
{upsertScript?.metadata.icon && <ScriptIcons script={upsertScript} size={32} />}
774770
{upsertScript && (
775771
<Tooltip position="tl" content={i18nName(upsertScript)}>
776772
<Typography.Text bold className="tw-text-size-lg tw-truncate tw-w-0 tw-grow-1">

src/pages/options/routes/ScriptList/components.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,21 @@ EnableSwitch.displayName = "EnableSwitch";
3434

3535
// Memoized Avatar component to prevent unnecessary re-renders
3636
export const MemoizedAvatar = React.memo(
37-
({ match, icon, website, ...rest }: { match: string; icon?: string; website?: string; [key: string]: any }) => (
38-
<Avatar
39-
shape="square"
40-
style={{
41-
backgroundColor: "unset",
42-
borderWidth: 1,
43-
}}
44-
className={website ? "tw-cursor-pointer" : "tw-cursor-default"}
45-
{...rest}
46-
>
47-
{icon ? <img title={match} src={icon} /> : <TbWorldWww title={match} color="#aaa" size={24} />}
48-
</Avatar>
49-
),
37+
({ match, icon, website, ...rest }: { match: string; icon?: string; website?: string; [key: string]: any }) => {
38+
return (
39+
<Avatar
40+
shape="square"
41+
style={{
42+
backgroundColor: "unset",
43+
borderWidth: 0,
44+
}}
45+
className={website ? "tw-cursor-pointer" : "tw-cursor-default"}
46+
{...rest}
47+
>
48+
{icon ? <img title={match} src={icon} /> : <TbWorldWww title={match} color="#aaa" size={24} />}
49+
</Avatar>
50+
);
51+
},
5052
(prevProps, nextProps) => {
5153
return (
5254
prevProps.match === nextProps.match &&

src/pages/options/routes/utils.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,17 @@ export type ScriptIconsProps = {
166166
};
167167

168168
export function ScriptIcons({ script, size = 32, style }: ScriptIconsProps) {
169+
const [imageError, setImageError] = useState(false);
169170
style = style || {};
170171
style.display = style.display || "inline-block";
171172
style.marginRight = style.marginRight || "8px";
173+
style.backgroundColor = style.backgroundColor || "unset";
172174
const m = script.metadata;
173175
const [icon] = m.icon || m.iconurl || m.icon64 || m.icon64url || [];
174-
if (icon) {
176+
if (icon && !imageError) {
175177
return (
176178
<Avatar size={size || 32} shape="square" style={style}>
177-
<img src={icon} alt={script?.name} />
179+
<img src={icon} alt={script?.name} onError={() => setImageError(true)} />
178180
</Avatar>
179181
);
180182
}

src/pages/store/favicons.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { type Script, ScriptDAO } from "@App/app/repo/scripts";
2-
import { cacheInstance } from "@App/app/cache";
3-
import { CACHE_KEY_FAVICON } from "@App/app/cache_key";
42
import { FaviconDAO, type FaviconFile, type FaviconRecord } from "@App/app/repo/favicon";
53
import { v5 as uuidv5 } from "uuid";
64
import { getFaviconRootFolder } from "@App/app/service/service_worker/utils";
@@ -225,12 +223,6 @@ export const loadFavicon = async (iconUrl: string): Promise<string> => {
225223
const directoryHandle = await getFaviconRootFolder();
226224
// 使用url的uuid作为文件名
227225
const filename = `icon_${uuidv5(iconUrl, uuidv5.URL)}.dat`;
228-
try {
229-
const sessionBlobUrl = await cacheInstance.get<string>(`${CACHE_KEY_FAVICON}${filename}`);
230-
if (sessionBlobUrl) return sessionBlobUrl;
231-
} catch {
232-
// 即使报错也不影响
233-
}
234226
// 检查文件是否存在
235227
let fileHandle: FileSystemFileHandle | undefined;
236228
try {
@@ -251,7 +243,6 @@ export const loadFavicon = async (iconUrl: string): Promise<string> => {
251243
const opfsRet = { dirs: ["cached_favicons"], filename: filename };
252244
const file = await getFileFromOPFS(opfsRet);
253245
const blobUrl = URL.createObjectURL(file);
254-
cacheInstance.set(`${CACHE_KEY_FAVICON}${filename}`, blobUrl); // 不用等待。针对SW重启 - blobURL 的生命周期依附于页面
255246
return blobUrl;
256247
};
257248

0 commit comments

Comments
 (0)