| 213 | } |
| 214 | |
| 215 | export function ColorSearchView({ |
| 216 | filteredItems, |
| 217 | exactMatches = new Set(), |
| 218 | closestMatches = new Set(), |
| 219 | listBoxClassName |
| 220 | }: ColorSearchViewProps) { |
| 221 | const [copiedId, setCopiedId] = useState<string | null>(null); |
| 222 | const timeout = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 223 | |
| 224 | useEffect(() => { |
| 225 | return () => { |
| 226 | if (timeout.current) { |
| 227 | clearTimeout(timeout.current); |
| 228 | } |
| 229 | }; |
| 230 | }, []); |
| 231 | |
| 232 | const handleCopyColor = useCallback((colorName: string, itemId: string) => { |
| 233 | if (timeout.current) { |
| 234 | clearTimeout(timeout.current); |
| 235 | } |
| 236 | navigator.clipboard |
| 237 | .writeText(colorName) |
| 238 | .then(() => { |
| 239 | setCopiedId(itemId); |
| 240 | timeout.current = setTimeout(() => setCopiedId(null), 2000); |
| 241 | }) |
| 242 | .catch(() => { |
| 243 | // noop |
| 244 | }); |
| 245 | }, []); |
| 246 | |
| 247 | const sections = filteredItems |
| 248 | .map(section => ({ |
| 249 | ...section, |
| 250 | items: section.items.map(item => ({ |
| 251 | ...item, |
| 252 | id: `${section.id}-${item.name}` |
| 253 | })) |
| 254 | })) |
| 255 | .filter(section => section.items.length > 0); |
| 256 | |
| 257 | if (sections.length === 0) { |
| 258 | return ( |
| 259 | <IllustratedMessage styles={style({marginX: 'auto', marginY: 32})}> |
| 260 | <NoSearchResults /> |
| 261 | <Heading>No results</Heading> |
| 262 | <Content>Try a different search term.</Content> |
| 263 | </IllustratedMessage> |
| 264 | ); |
| 265 | } |
| 266 | |
| 267 | return ( |
| 268 | <> |
| 269 | <InfoMessage> |
| 270 | Press a color to copy its name. See <Link href="styling">styling</Link> for more |
| 271 | information. |
| 272 | </InfoMessage> |