Frontend School

Debounced Asset Search with Highlight

Machine Coding
hard

Debounced Asset Search with Highlight

Hard
React
TypeScript
Debounce
Search
Asynchronous Operations
UI/UX

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:

  1. Search Input: Create a functional search input field where users can type their query.
  2. 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).
  3. API Integration: Utilize the provided fetchAssets helper function to simulate an asynchronous API call based on the debounced search term.
  4. Loading State: Display a clear loading indicator while the API call is in progress.
  5. Display Results: Render the fetched Asset titles and descriptions in a list below the search input.
  6. No Results State: Show a user-friendly message when no assets are found for a given search term.
  7. Error Handling: Gracefully handle potential API errors (simulated by fetchAssets) and display an informative error message to the user.
  8. Search Term Highlighting: Within the displayed asset titles and descriptions, highlight all occurrences of the currently active search term (case-insensitive).
  9. 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.

Constraints

Constraints & assumptions

  • Debounce Delay: The debounce period must be exactly 300 milliseconds.
  • API Simulation: Use the provided fetchAssets function to simulate network requests. This function introduces random delays (200-800ms) and simulates network errors approximately 15% of the time.
  • Environment: Develop using React functional components and hooks.
  • Type Safety: Leverage TypeScript for all component props, state, and data structures.
  • Styling: Basic CSS is provided; focus on functionality and clear UI states, not elaborate styling.

Out of scope

  • Advanced accessibility features (e.g., full keyboard navigation beyond basic input focus).
  • Pagination, infinite scrolling, or virtualization for large result sets.
  • Complex state management libraries (Redux, Zustand) – useState, useEffect, useRef, useCallback are sufficient.
  • Implementing a custom debounce utility function from scratch (you can implement the debounce logic directly within the component using hooks).

What interviewers evaluate

  • Correctness and Efficiency: Proper implementation of the debouncing mechanism and efficient handling of asynchronous operations.
  • Robustness: Comprehensive management of loading, success, empty, and error states.
  • Code Quality: Clean, modular, and type-safe component design using modern React patterns.
  • User Experience: Attention to UI feedback, responsiveness, and basic accessibility considerations.

Loading editor...