Fix the state picker for an activity feed

Review the AI's codeEasy10 min

Your task

An AI built a "recent activity" panel that only handled loaded data, because its demo had three seeded items. On first render the data was still undefined, and the dashboard crashed. A new user with no activity saw a blank panel, and a failed request looked like nothing had happened. Your team now picks one of four states before rendering anything.

An assistant wrote resolve_feed_state(status, item_count, error_message). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • status: the status word the data hook reports.
  • item_count: how many items the hook returned.
  • error_message: the hook's error message, or "".
  • Return "loading", "error", "empty" or "loaded".

Rules the tests check. Trim whitespace from status and lowercase it, then return the first that applies:

  1. idle, loading or pending: "loading".
  2. error, or an error message that is not empty after trimming: "error". This holds even when there are items, so a failed refresh never looks healthy.
  3. success: "empty" when item_count is 0 or less, otherwise "loaded".
  4. Any other status, including an empty one: "loading". A status you do not recognize must never render the list.

Examples

resolve_feed_state(status: str, item_count: int, error_message: str) -> str
Example 1
Inputresolve_feed_state("loading", 0, "")
Output"loading"
Example 2
Inputresolve_feed_state("success", 3, "")
Output"loaded"
Example 3
Inputresolve_feed_state("success", 0, "")
Output"empty"