| 
									
										
										
										
											2024-08-08 15:29:11 +08:00
										 |  |  | import { useAsyncEffect } from 'ahooks' | 
					
						
							|  |  |  | import { appDefaultIconBackground } from '@/config' | 
					
						
							|  |  |  | import { searchEmoji } from '@/utils/emoji' | 
					
						
							| 
									
										
										
										
											2024-08-19 09:16:33 +08:00
										 |  |  | import type { AppIconType } from '@/types/app' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type UseAppFaviconOptions = { | 
					
						
							|  |  |  |   enable?: boolean | 
					
						
							| 
									
										
										
										
											2024-09-22 03:15:11 +08:00
										 |  |  |   icon_type?: AppIconType | null | 
					
						
							| 
									
										
										
										
											2024-08-19 09:16:33 +08:00
										 |  |  |   icon?: string | 
					
						
							| 
									
										
										
										
											2024-09-22 03:15:11 +08:00
										 |  |  |   icon_background?: string | null | 
					
						
							|  |  |  |   icon_url?: string | null | 
					
						
							| 
									
										
										
										
											2024-08-19 09:16:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function useAppFavicon(options: UseAppFaviconOptions) { | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     enable = true, | 
					
						
							|  |  |  |     icon_type = 'emoji', | 
					
						
							|  |  |  |     icon, | 
					
						
							|  |  |  |     icon_background, | 
					
						
							|  |  |  |     icon_url, | 
					
						
							|  |  |  |   } = options | 
					
						
							| 
									
										
										
										
											2024-08-08 15:29:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   useAsyncEffect(async () => { | 
					
						
							| 
									
										
										
										
											2024-10-11 12:07:39 +08:00
										 |  |  |     if (!enable || (icon_type === 'image' && !icon_url) || (icon_type === 'emoji' && !icon)) | 
					
						
							| 
									
										
										
										
											2024-08-08 15:29:11 +08:00
										 |  |  |       return | 
					
						
							| 
									
										
										
										
											2024-08-19 09:16:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const isValidImageIcon = icon_type === 'image' && icon_url | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 15:29:11 +08:00
										 |  |  |     const link: HTMLLinkElement = document.querySelector('link[rel*="icon"]') || document.createElement('link') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-19 09:16:33 +08:00
										 |  |  |     link.href = isValidImageIcon | 
					
						
							|  |  |  |       ? icon_url | 
					
						
							|  |  |  |       : 'data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22>' | 
					
						
							|  |  |  |             + `<rect width=%22100%25%22 height=%22100%25%22 fill=%22${encodeURIComponent(icon_background || appDefaultIconBackground)}%22 rx=%2230%22 ry=%2230%22 />` | 
					
						
							|  |  |  |             + `<text x=%2212.5%22 y=%221em%22 font-size=%2275%22>${ | 
					
						
							|  |  |  |               icon ? await searchEmoji(icon) : '🤖' | 
					
						
							|  |  |  |             }</text>`
 | 
					
						
							| 
									
										
										
										
											2024-08-08 15:29:11 +08:00
										 |  |  |             + '</svg>' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     link.rel = 'shortcut icon' | 
					
						
							|  |  |  |     link.type = 'image/svg' | 
					
						
							|  |  |  |     document.getElementsByTagName('head')[0].appendChild(link) | 
					
						
							|  |  |  |   }, [enable, icon, icon_background]) | 
					
						
							|  |  |  | } |