import { useAsyncEffect } from 'ahooks' import { appDefaultIconBackground } from '@/config' import { searchEmoji } from '@/utils/emoji' import type { AppIconType } from '@/types/app' type UseAppFaviconOptions = { enable?: boolean icon_type?: AppIconType icon?: string icon_background?: string icon_url?: string } export function useAppFavicon(options: UseAppFaviconOptions) { const { enable = true, icon_type = 'emoji', icon, icon_background, icon_url, } = options useAsyncEffect(async () => { if (!enable) return const isValidImageIcon = icon_type === 'image' && icon_url const link: HTMLLinkElement = document.querySelector('link[rel*="icon"]') || document.createElement('link') link.href = isValidImageIcon ? icon_url : 'data:image/svg+xml,' + `` + `${ icon ? await searchEmoji(icon) : '🤖' }` + '' link.rel = 'shortcut icon' link.type = 'image/svg' document.getElementsByTagName('head')[0].appendChild(link) }, [enable, icon, icon_background]) }