mirror of
https://github.com/aykhans/slash-e.git
synced 2025-04-20 22:07:15 +00:00
chore: update favicon getter
This commit is contained in:
parent
8ef7d5f0d0
commit
6126701025
@ -1,8 +1,7 @@
|
|||||||
import type { Shortcut } from "@/types/proto/api/v2/shortcut_service";
|
import type { Shortcut } from "@/types/proto/api/v2/shortcut_service";
|
||||||
import { useStorage } from "@plasmohq/storage/hook";
|
import { useStorage } from "@plasmohq/storage/hook";
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
import { useEffect, useState } from "react";
|
import { getFaviconWithGoogleS2 } from "@/helpers/utils";
|
||||||
import useFaviconStore from "../stores/favicon";
|
|
||||||
import Icon from "./Icon";
|
import Icon from "./Icon";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@ -11,17 +10,8 @@ interface Props {
|
|||||||
|
|
||||||
const ShortcutView = (props: Props) => {
|
const ShortcutView = (props: Props) => {
|
||||||
const { shortcut } = props;
|
const { shortcut } = props;
|
||||||
const faviconStore = useFaviconStore();
|
|
||||||
const [domain] = useStorage<string>("domain", "");
|
const [domain] = useStorage<string>("domain", "");
|
||||||
const [favicon, setFavicon] = useState<string | undefined>(undefined);
|
const favicon = getFaviconWithGoogleS2(shortcut.link);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
faviconStore.getOrFetchUrlFavicon(shortcut.link).then((url) => {
|
|
||||||
if (url) {
|
|
||||||
setFavicon(url);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, [shortcut.link]);
|
|
||||||
|
|
||||||
const handleShortcutLinkClick = () => {
|
const handleShortcutLinkClick = () => {
|
||||||
const shortcutLink = `${domain}/s/${shortcut.name}`;
|
const shortcutLink = `${domain}/s/${shortcut.name}`;
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
import { Storage } from "@plasmohq/storage";
|
|
||||||
import axios from "axios";
|
|
||||||
|
|
||||||
const storage = new Storage();
|
|
||||||
|
|
||||||
export const getUrlFavicon = async (url: string) => {
|
|
||||||
const domain = await storage.getItem<string>("domain");
|
|
||||||
const accessToken = await storage.getItem<string>("access_token");
|
|
||||||
return axios.get<string>(`${domain}/api/v1/url/favicon?url=${url}`, {
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${accessToken}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
@ -3,3 +3,12 @@ import { isNull, isUndefined } from "lodash-es";
|
|||||||
export const isNullorUndefined = (value: any) => {
|
export const isNullorUndefined = (value: any) => {
|
||||||
return isNull(value) || isUndefined(value);
|
return isNull(value) || isUndefined(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getFaviconWithGoogleS2 = (url: string) => {
|
||||||
|
try {
|
||||||
|
const urlObject = new URL(url);
|
||||||
|
return `https://www.google.com/s2/favicons?sz=128&domain=${urlObject.hostname}`;
|
||||||
|
} catch (error) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
@ -86,7 +86,8 @@ const IndexPopup = () => {
|
|||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<div className="w-full flex flex-col justify-start items-center">
|
<div className="w-full flex flex-col justify-start items-center">
|
||||||
<p>No domain and access token found.</p>
|
<Icon.Cookie strokeWidth={1} className="w-20 h-auto mb-4 text-gray-400" />
|
||||||
|
<p>Please set your domain and access token first.</p>
|
||||||
<div className="w-full flex flex-row justify-center items-center py-4">
|
<div className="w-full flex flex-row justify-center items-center py-4">
|
||||||
<Button size="sm" color="primary" onClick={handleSettingButtonClick}>
|
<Button size="sm" color="primary" onClick={handleSettingButtonClick}>
|
||||||
<Icon.Settings className="w-5 h-auto mr-1" /> Setting
|
<Icon.Settings className="w-5 h-auto mr-1" /> Setting
|
||||||
|
@ -1,41 +0,0 @@
|
|||||||
import { create } from "zustand";
|
|
||||||
import { persist } from "zustand/middleware";
|
|
||||||
import { getUrlFavicon } from "../helpers/api";
|
|
||||||
|
|
||||||
interface FaviconState {
|
|
||||||
cache: {
|
|
||||||
[key: string]: string;
|
|
||||||
};
|
|
||||||
getOrFetchUrlFavicon: (url: string) => Promise<string>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const useFaviconStore = create<FaviconState>()(
|
|
||||||
persist(
|
|
||||||
(set, get) => ({
|
|
||||||
cache: {},
|
|
||||||
getOrFetchUrlFavicon: async (url: string) => {
|
|
||||||
const cache = get().cache;
|
|
||||||
if (cache[url]) {
|
|
||||||
return cache[url];
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const { data: favicon } = await getUrlFavicon(url);
|
|
||||||
if (favicon) {
|
|
||||||
cache[url] = favicon;
|
|
||||||
set(cache);
|
|
||||||
return favicon;
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
name: "favicon_cache",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
export default useFaviconStore;
|
|
Loading…
x
Reference in New Issue
Block a user