data-fetching-architecture

Implements LobeHub data fetching with service layer, Zustand stores, and SWR hooks.

74|11|Updated Jul 4, 2024
One-click install
npx skills add https://github.com/OpenSourceAGI/qwksearch-research-agent --skill data-fetching-architecture-opensourceagi
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: data-fetching-architecture
Source: https://github.com/OpenSourceAGI/qwksearch-research-agent/tree/main/apps/qwk-in-lobe/.agents/skills/data-fetching-architecture
Command: npx skills add https://github.com/OpenSourceAGI/qwksearch-research-agent --skill data-fetching-architecture-opensourceagi

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? It standardizes how LobeHub frontend code fetches and mutates server data, replacing ad-hoc useEffect and direct lambdaClient calls with a consistent service layer, Zustand store slices, and SWR-based hooks. ## Core Features & Use Cases - Layered Architecture Guide: Defines the Component → Zustand Store → Service → lambdaClient (tRPC) pipeline with clear DO/DON'T rules. - Canonical Patterns: Provides copy-ready code for list fetching, detail maps, optimistic mutations, pagination, dependent and conditional fetching, and cross-domain cache refresh. - Async Failure Handling: Enforces an AsyncBoundary/AsyncError contract so failed requests never render as permanent skeletons, fake empty states, or false NotFound pages. - Use Case: When adding a new entity like a Benchmark or Dataset to the LobeHub eval module, follow the 6-step recipe (types → service → reducer → slice → selectors → component) to wire up fully typed, cache-aware data fetching. ## Quick Start Use the data-fetching-architecture skill to add a new useFetchDatasets hook with its service method, store slice, and component wiring for the eval module.

Frequently Asked Questions about data-fetching-architecture

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fetch server data in a LobeHub React component?

Call the store's useFetchXxx hook, which wraps useClientDataSWR and writes results into Zustand state via onSuccess. Read the data from the store with a selector, and never use useEffect or call lambdaClient directly in components.

How do I migrate useEffect data fetching to SWR in Zustand?

Move the API call into a service method, add a useFetchXxx hook in the store slice using useClientDataSWR with a constant cache key, then have the component call the hook and read state from the store. The skill includes a full before-and-after migration example.

How should mutations update SWR cache in Zustand stores?

Mutation actions call the service method, then run refreshXxx helpers that call SWR's mutate() on the matching cache key. Update and delete also apply optimistic reducer dispatches with per-item loading flags before refreshing from the server.

Why does my SWR hook show a permanent loading skeleton on error?

A success-only init flag never flips when the request fails, so the UI stays in a loading or fake empty state. Destructure error and mutate from the SWR response and render AsyncBoundary or AsyncError before any empty, NotFound, or zero-value branch.

When should I use a detail map versus a list array in Zustand state?

Use a simple array for list views and a Record<string, T> detail map keyed by id for caching individual entities. For lists scoped to a parent entity, key the map by the parent id as shown in the references walkthrough.