Skip to main content

Collapsible JSON Config Viewer

MediumPremium

You're building an internal tool for a team that runs event log streams. Each stream has a config: how long to keep events, how many partitions it writes to, and so on. The team wants a table of every stream where an engineer can open any row to read its raw JSON config without leaving the page.

The scaffold gives you a stubbed component, a mock endpoint, and tested helper functions.

Starter code and solution
JavaScript (React 18, Node 18+)
Download code

What's in the codebase

  • src/ConfigTable.jsx. The component you build. The starter renders the heading and column headers with an empty body. This is the only file you change.
  • src/configLogic.js. Finished helpers: toggleExpanded, expandAllIds, applyFilter, and visibleConfigs. Use them as they are.
  • src/mockApi.js. fetchConfigs() resolves five configs after 50 ms, and fetchConfigsFailing() rejects so you can test the error state. One config has a nested object, and one has an empty config.
  • test_configLogic.js and test_component.js. Ten helper tests and six render tests, run with Node's built-in test runner.
  • demo.js. Exercises the helpers end to end with plain Node.
  • src/index.jsx and public/index.html. The entry point and page shell. The files use React.createElement instead of JSX syntax, so the tests run without a build step.

Out of the box, the ten helper tests and one render test pass, and five render tests fail. demo.js already prints PASS, because it only checks the helpers.

Your task

Build ConfigTable. On mount, call fetchFn (which defaults to fetchConfigs) inside useEffect, and ignore a response that arrives after the component unmounts. Show Loading configs... while fetching, Failed to load configs: plus the error message on failure, and No configs found. for an empty list.

Render one row per config with Name, Stream, Enabled (Yes or No), and a toggle button reading Show config or Hide config. An open row shows JSON.stringify(config, null, 2) in a <pre> element. Keep expansion as a Set of config IDs, and key rows by ID. Add a filter input with the placeholder Filter by name or stream, plus Expand all and Collapse all buttons. Expand all opens only the rows the filter currently shows. When nothing matches, show No configs match your filter. Memoize the row with React.memo, the filtered list and JSON strings with useMemo, and the handlers with useCallback.

The render tests use server rendering, which never runs effects, so they pass data in through two more props. Support both: initialConfigs renders that list straight away and skips the fetch, and initialExpanded is a Set of IDs that start open.

Filter "PAY" -> Payment Events only Filter "audit", then Expand all -> only cfg-audit is expanded Expand Search Events, filter "payments", then clear the filter -> Search Events is still expanded Expand Notification Events -> <pre> shows {} fetchFn = fetchConfigsFailing -> Failed to load configs: Failed to fetch configs

You're done when npm test reports 16 passing tests and the table behaves as described when you click through it.

The scaling follow-up

The interviewer asks what changes if the endpoint returns 5,000 configs instead of 5. Talk through what breaks first, the fetch, the filter, or the render, what you'd change first, and what expand all should mean when the user can see only part of the list.

Using AI on this problem

Frontend screens like this usually allow AI for documentation lookups, such as how useEffect cleanup runs or when React.memo skips a render. Decide the state model yourself: a Set of IDs, keys by ID, and expand all scoped to the visible rows are what the interviewer wants to hear you reason about. If AI drafts the component, check that its list keys aren't array indexes.