Overview
In Adobe's asset management platforms, efficiently searching through vast libraries of digital assets is crucial. Users need a responsive search experience that provides immediate feedback without overwhelming the backend API with excessive requests with every keystroke. This problem challenges you to build a robust, debounced search component for an asset library, focusing on performance, user experience, and error handling.
What you need to do / Task
Your primary task is to develop a React component that implements a debounced search functionality for a simulated asset API. Specifically, you must:
- Search Input: Create a functional search input field where users can type their query.
- Debounce Logic: Implement a custom debounce mechanism (e.g., using
useEffect and useRef) that delays the search API call until the user has stopped typing for a specified duration (300ms).
- API Integration: Utilize the provided
fetchAssets helper function to simulate an asynchronous API call based on the debounced search term.
- Loading State: Display a clear loading indicator while the API call is in progress.
- Display Results: Render the fetched
Asset titles and descriptions in a list below the search input.
- No Results State: Show a user-friendly message when no assets are found for a given search term.
- Error Handling: Gracefully handle potential API errors (simulated by
fetchAssets) and display an informative error message to the user.
- Search Term Highlighting: Within the displayed asset titles and descriptions, highlight all occurrences of the currently active search term (case-insensitive).
- Clear Button: Add a button next to the search input to quickly clear the current search term.
UI/UX expectations
- Search Input: A standard text input field with a clear placeholder and an accessible
aria-label.
- Clear Button: A small, unobtrusive button (e.g., an 'X' icon) that appears when the input has text and clears it when clicked.
- Loading Indicator: A simple text message like "Loading assets..." or a spinner icon.
- Results List: Display each asset with its title (as an
<h3>) and description (as a <p>) within an unordered list (<ul>).
- No Results Message: A clear message, e.g., "No assets found for 'your query'."
- Error Message: A distinct message, e.g., "Failed to fetch assets. Please try again."
- Accessibility: Ensure basic ARIA attributes are used for live regions (loading, error, no results) and input fields.
Acceptance criteria
- Typing rapidly (e.g., "foo" then "foob" then "foobar") into the search box results in only one API call for "foobar" after the 300ms debounce delay.
- The loading indicator appears immediately after the debounce delay and disappears when results or an error are received.
- Search results update dynamically to reflect the debounced query.
- When
fetchAssets returns an empty array, the "No assets found" message is displayed correctly.
- When
fetchAssets rejects, the error message is displayed, and previous results are cleared.
- The active search term is visually highlighted within the title and description of each matching asset.
- The clear button correctly resets the search input and clears results/messages.
- The component behaves responsively and provides clear user feedback across all states.