|
| 1 | +import { h } from 'preact'; |
| 2 | +import { useState } from 'preact/hooks'; |
| 3 | +import { DDG_DEFAULT_ICON_SIZE, DDG_FALLBACK_ICON, DDG_FALLBACK_ICON_DARK } from '../favorites/constants.js'; |
| 4 | +import styles from '../favorites/components/Tile.module.css'; |
| 5 | +import { urlToColor } from '../favorites/getColorForString.js'; |
| 6 | +import cn from 'classnames'; |
| 7 | + |
| 8 | +/** |
| 9 | + * @typedef {'loading_favicon_src' |
| 10 | + * | 'did_load_favicon_src' |
| 11 | + * | 'loading_fallback_img' |
| 12 | + * | 'did_load_fallback_img' |
| 13 | + * | 'using_fallback_text' |
| 14 | + * | 'fallback_img_failed' |
| 15 | + * } ImgState |
| 16 | + */ |
| 17 | +const states = /** @type {Record<ImgState, ImgState>} */ ({ |
| 18 | + loading_favicon_src: 'loading_favicon_src', |
| 19 | + did_load_favicon_src: 'did_load_favicon_src', |
| 20 | + |
| 21 | + loading_fallback_img: 'loading_fallback_img', |
| 22 | + did_load_fallback_img: 'did_load_fallback_img', |
| 23 | + fallback_img_failed: 'fallback_img_failed', |
| 24 | + |
| 25 | + using_fallback_text: 'using_fallback_text', |
| 26 | +}); |
| 27 | + |
| 28 | +/** |
| 29 | + * |
| 30 | + * Loads and displays an image for a given webpage. |
| 31 | + * |
| 32 | + * @param {Object} props - The props for the image loader. |
| 33 | + * @param {string|null|undefined} props.faviconSrc - The URL of the favicon image to load. |
| 34 | + * @param {number} props.faviconMax - The maximum size this icon be displayed as |
| 35 | + * @param {string} props.title - The title associated with the image. |
| 36 | + * @param {'light' | 'dark'} props.theme - the currently applied theme |
| 37 | + * @param {'favorite-tile' | 'history-favicon'} props.displayKind |
| 38 | + * @param {string|null} props.etldPlusOne - The relevant domain section of the url |
| 39 | + */ |
| 40 | +export function ImageWithState({ faviconSrc, faviconMax, title, etldPlusOne, theme, displayKind }) { |
| 41 | + const size = Math.min(faviconMax, DDG_DEFAULT_ICON_SIZE); |
| 42 | + const sizeClass = displayKind === 'favorite-tile' ? styles.faviconLarge : styles.faviconSmall; |
| 43 | + |
| 44 | + // try to use the defined image source |
| 45 | + // prettier-ignore |
| 46 | + const imgsrc = faviconSrc |
| 47 | + ? faviconSrc + '?preferredSize=' + size |
| 48 | + : null; |
| 49 | + |
| 50 | + // prettier-ignore |
| 51 | + const initialState = (() => { |
| 52 | + /** |
| 53 | + * If the favicon has `src`, always prefer it |
| 54 | + */ |
| 55 | + if (imgsrc) return states.loading_favicon_src; |
| 56 | + /** |
| 57 | + * Failing that, use fallback text if possible |
| 58 | + */ |
| 59 | + if (etldPlusOne) return states.using_fallback_text; |
| 60 | + /** |
| 61 | + * If we get here, we have no favicon src, and no chance of using fallback text |
| 62 | + */ |
| 63 | + return states.loading_fallback_img; |
| 64 | + })(); |
| 65 | + |
| 66 | + const [state, setState] = useState(/** @type {ImgState} */ (initialState)); |
| 67 | + |
| 68 | + switch (state) { |
| 69 | + /** |
| 70 | + * These are the happy paths, where we are loading the favicon source and it does not 404 |
| 71 | + */ |
| 72 | + case states.loading_favicon_src: |
| 73 | + case states.did_load_favicon_src: { |
| 74 | + if (!imgsrc) { |
| 75 | + console.warn('unreachable - must have imgsrc here'); |
| 76 | + return null; |
| 77 | + } |
| 78 | + return ( |
| 79 | + <img |
| 80 | + src={imgsrc} |
| 81 | + class={cn(styles.favicon, sizeClass)} |
| 82 | + alt="" |
| 83 | + data-state={state} |
| 84 | + onLoad={() => setState(states.did_load_favicon_src)} |
| 85 | + onError={() => { |
| 86 | + if (etldPlusOne) { |
| 87 | + setState(states.using_fallback_text); |
| 88 | + } else { |
| 89 | + setState(states.loading_fallback_img); |
| 90 | + } |
| 91 | + }} |
| 92 | + /> |
| 93 | + ); |
| 94 | + } |
| 95 | + /** |
| 96 | + * A fallback can be applied when the `etldPlusOne` is there. For example, |
| 97 | + * if `etldPlusOne = 'example.com'`, we can display `Ex` and use the domain name |
| 98 | + * to select a background color. |
| 99 | + */ |
| 100 | + case states.using_fallback_text: { |
| 101 | + if (!etldPlusOne) { |
| 102 | + console.warn('unreachable - must have etld+1 here'); |
| 103 | + return null; |
| 104 | + } |
| 105 | + /** @type {Record<string, string>|undefined} */ |
| 106 | + let style; |
| 107 | + const fallbackColor = urlToColor(etldPlusOne); |
| 108 | + if (fallbackColor) { |
| 109 | + style = { background: fallbackColor }; |
| 110 | + } |
| 111 | + const chars = etldPlusOne.slice(0, 2); |
| 112 | + return ( |
| 113 | + <div class={cn(styles.favicon, sizeClass, styles.faviconText)} style={style} data-state={state}> |
| 114 | + <span>{chars[0]}</span> |
| 115 | + <span>{chars[1]}</span> |
| 116 | + </div> |
| 117 | + ); |
| 118 | + } |
| 119 | + /** |
| 120 | + * If we get here, we couldn't load the favicon source OR the fallback text |
| 121 | + * So, we default to a globe icon |
| 122 | + */ |
| 123 | + case states.loading_fallback_img: |
| 124 | + case states.did_load_fallback_img: { |
| 125 | + return ( |
| 126 | + <img |
| 127 | + src={theme === 'light' ? DDG_FALLBACK_ICON : DDG_FALLBACK_ICON_DARK} |
| 128 | + class={cn(styles.favicon, sizeClass)} |
| 129 | + alt="" |
| 130 | + data-state={state} |
| 131 | + onLoad={() => setState(states.did_load_fallback_img)} |
| 132 | + onError={() => setState(states.fallback_img_failed)} |
| 133 | + /> |
| 134 | + ); |
| 135 | + } |
| 136 | + default: |
| 137 | + return null; |
| 138 | + } |
| 139 | +} |
0 commit comments