Fix the state picker for an activity feed
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:
idle,loadingorpending:"loading".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.success:"empty"whenitem_countis 0 or less, otherwise"loaded".- 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) -> strExample 1
Input
resolve_feed_state("loading", 0, "")Output
"loading"Example 2
Input
resolve_feed_state("success", 3, "")Output
"loaded"Example 3
Input
resolve_feed_state("success", 0, "")Output
"empty"