( imageFile: File | undefined, maskFile: File | undefined, imageName: string, enabled: boolean, inverseMask = false )
| 125 | } |
| 126 | |
| 127 | export function useMaskedThumbnail( |
| 128 | imageFile: File | undefined, |
| 129 | maskFile: File | undefined, |
| 130 | imageName: string, |
| 131 | enabled: boolean, |
| 132 | inverseMask = false |
| 133 | ): string | null { |
| 134 | const cacheKey = useMemo( |
| 135 | () => enabled && imageFile && maskFile |
| 136 | ? getMaskedThumbnailCacheKey(imageName, imageFile, maskFile, inverseMask) |
| 137 | : '', |
| 138 | [enabled, imageFile, imageName, maskFile, inverseMask] |
| 139 | ); |
| 140 | const [state, setState] = useState<{ cacheKey: string; url: string | null }>(() => ({ |
| 141 | cacheKey, |
| 142 | url: cacheKey ? maskedThumbnailCache.get(cacheKey) ?? null : null, |
| 143 | })); |
| 144 | |
| 145 | useEffect(() => { |
| 146 | let cancelled = false; |
| 147 | |
| 148 | if (!enabled || !imageFile || !maskFile || !cacheKey) { |
| 149 | return () => { |
| 150 | cancelled = true; |
| 151 | }; |
| 152 | } |
| 153 | |
| 154 | const cached = maskedThumbnailCache.get(cacheKey); |
| 155 | if (cached) { |
| 156 | return () => { |
| 157 | cancelled = true; |
| 158 | }; |
| 159 | } |
| 160 | |
| 161 | let load = maskedThumbnailLoads.get(cacheKey); |
| 162 | if (!load) { |
| 163 | const loadGeneration = maskedThumbnailCacheGeneration; |
| 164 | load = loadMaskedThumbnail(imageFile, maskFile, inverseMask).then((result) => { |
| 165 | maskedThumbnailLoads.delete(cacheKey); |
| 166 | if (loadGeneration !== maskedThumbnailCacheGeneration) { |
| 167 | if (result) URL.revokeObjectURL(result); |
| 168 | return null; |
| 169 | } |
| 170 | if (result) { |
| 171 | maskedThumbnailCache.set(cacheKey, result); |
| 172 | } |
| 173 | return result; |
| 174 | }); |
| 175 | maskedThumbnailLoads.set(cacheKey, load); |
| 176 | } |
| 177 | |
| 178 | load.then((result) => { |
| 179 | if (!cancelled) { |
| 180 | setState({ cacheKey, url: result }); |
| 181 | } |
| 182 | }); |
| 183 | |
| 184 | return () => { |
no test coverage detected