TanStack Query 状态管理
state-management
CoreAI Sandbox 的 TanStack Query + mock-JSON 数据层。适用于:编辑数据获取 hooks(`app/hooks/use*.ts`)、在 `app/data/` 中添加 mock 数据,或将组件连接到查询状态时。
- License 许可
- MIT
- 所属仓库
- github/replica
- 最近更新
- 2026-11-15
- Stars
- ★ 273
- Forks
- 35
01 前言 Front Matter
- name
- state-management
- description
- CoreAI Sandbox's TanStack Query + mock-JSON data layer. USE WHEN: editing data-fetching hooks (`app/hooks/use*.ts`), adding mock data in `app/data/`, or wiring a component to query state.
03 SKILL.md 正文
State Management Instructions — CoreAI Sandbox Demo
Paths below are relative to this app's directory.
Overview
The app uses TanStack Query v5 hooks over local JSON data to provide client-side loading, filtering, and caching for prototypes.
Architecture
1. QueryProvider Setup
The app is wrapped with QueryProvider in app/root.tsx:
import { QueryProvider } from './core/datafetching/QueryProvider';
export default function App() {
return (
<QueryProvider>
<AppContent>
<Outlet />
</AppContent>
</QueryProvider>
);
}
Configuration (app/core/datafetching/QueryProvider.tsx):
staleTime: 5 minutes- Data is considered freshgcTime: 10 minutes- Garbage collection timeretry: false- Don't retry failed requestsrefetchOnWindowFocus: false- Don't refetch on focus
2. Mock Data Layer
Location: app/data/
Files:
agents.json- agents with varied lifecycle statusestools.json- tools with categories and publish statusmodels.json- models with provider and category metadatahome.json- Home page endpoints, model actions, carousel models, and recent workindex.ts- TypeScript interfaces and utility functions
Data Types:
export interface Agent {
id: string;
name: string;
description: string;
status: 'draft' | 'published' | 'deployed';
tags: string[];
owner: string;
createdDate: string; // ISO 8601
updatedDate: string; // ISO 8601
version: string; // Semantic version
}
export interface Tool {
mark: string;
initialsColor: string;
id: string;
name: string;
description: string;
category: ToolCategory;
isPublished: boolean;
}
Utility Functions:
// Filter agents by status
filterAgentsByStatus(agents: Agent[], status: AgentStatus): Agent[]
// Filter tools by category
filterToolsByCategory(tools: Tool[], category: ToolCategory): Tool[]
// Filter tools by published status
filterToolsByPublished(tools: Tool[], isPublished: boolean): Tool[]
3. Custom Hooks Pattern
Location: app/hooks/
Custom hooks wrap TanStack Query's useQuery and import JSON data directly.
Example — useAgents.ts:
import type { Agent, AgentStatus } from '~/data';
import { useQuery } from '@tanstack/react-query';
import { agents, filterAgentsByStatus } from '~/data';
async function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export interface UseAgentsOptions {
status?: AgentStatus;
}
export function useAgents(options?: UseAgentsOptions) {
return useQuery({
queryKey: ['agents', options?.status],
queryFn: async (): Promise<Agent[]> => {
// Simulate network delay (400ms)
await delay(400);
// Return filtered or all agents
if (options?.status) {
return filterAgentsByStatus(agents, options.status);
}
return agents;
},
});
}
Available Hooks:
useAgents(options?)- Fetch agents, optionally filtered by statususeTools(options?)- Fetch tools, optionally filtered by category/publish statususeHomeData()- Fetch mock home page content
Query Keys:
['agents', status?] // e.g., ['agents', 'draft']
['tools', category?, isPublished?] // e.g., ['tools', 'Data', true]
['home'] // Home page mock content
4. Component Usage Pattern
Components use custom hooks directly in route components:
import { useAgents } from '~/hooks/useAgents';
import { CollectionPage } from '~/components/common';
import type { TableColumnDefinition } from '~/components/common';
import type { Agent } from '~/data';
export default function BuildAgents() {
const { data: agents, isLoading } = useAgents({ status: 'draft' });
const columns: TableColumnDefinition<Agent>[] = [
{
columnId: 'name',
renderHeaderCell: () => 'Name',
renderCell: (agent) => agent.name,
compare: (first, second) => first.name.localeCompare(second.name),
},
];
return (
<CollectionPage
headerProps={{ title: 'Agents' }}
dataGridProps={{
items: agents ?? [],
isLoading,
columns,
getRowId: (agent) => agent.id,
}}
/>
);
}
5. Route-Specific Filtering
Different routes show different subsets of the same data:
| Route | Hook Usage | Filter |
|---|---|---|
/build/agents |
useAgents() via AgentList |
All agents (50) |
/build/agents/drafts |
useAgents() via AgentList, then local status filtering |
Draft agents (14) |
/build/agents/published |
useAgents() via AgentList, then local status filtering |
Published agents (19) |
/operate/agents |
useAgents({ status: 'deployed' }) |
Deployed agents (17) |
/build/tools |
useTools() |
All tools (35) |
/discover/tools |
Direct tools import mapped into catalog items |
All tools (35; 29 published) |
6. Loading States
TanStack Query provides automatic loading state management:
import { DataGridWithFilters } from '~/components/common';
const { data, isLoading, error } = useAgents();
// The app-owned grid handles loading state; columns are defined by the consuming component.
<DataGridWithFilters columns={columns} isLoading={isLoading} items={data ?? []} />;
States Available:
isLoading- Initial fetch in progressisFetching- Any fetch in progress (including background refetch)isError- Query failederror- Error object if query faileddata- Query result data
Best Practices
DO:
- ✅ Use TanStack Query for ALL data fetching
- ✅ Use custom hooks (
useAgents,useTools) in components - ✅ Pass query results to the app-owned
DataGridWithFilters(or a page composition'sdataGridProps) withisLoading; the underlying CoreAIDataGriddoes not supply this app-owned loading API - ✅ Provide sensible empty states
- ✅ Use consistent query keys for caching
- ✅ Filter data via hook options when possible
DON'T:
- ❌ Don't fetch data in
useEffect - ❌ Don't use
useStatefor server data - ❌ Don't call TanStack Query's
useQuerydirectly - use custom hooks - ❌ Don't mutate imported JSON data
- ❌ Don't add SSR/loader patterns (this is client-only)
- ❌ Don't add real API calls or authentication
Simulated Delays
The mock query hooks useAgents, useTools, and useHomeData include a 400ms simulated delay:
async function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
This creates realistic loading states without actual network requests.
Adding New Data
1. Create JSON File
// app/data/newentity.json
[
{ "id": "entity-001", "name": "Example", ... }
]
2. Add Type to app/data/index.ts
export interface NewEntity {
id: string;
name: string;
// ...
}
import newEntityData from './newentity.json';
export const newEntities: NewEntity[] = newEntityData as NewEntity[];
3. Create Custom Hook
// app/hooks/useNewEntity.ts
import { useQuery } from '@tanstack/react-query';
import { newEntities } from '~/data';
async function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export function useNewEntity() {
return useQuery({
queryKey: ['newEntity'],
queryFn: async () => {
await delay(400);
return newEntities;
},
});
}
4. Use in Component
import { useNewEntity } from '~/hooks/useNewEntity';
const { data, isLoading } = useNewEntity();
Architecture Benefits
This simplified architecture provides:
- Consistent Patterns - Easy to port components to production
- Realistic UX - Loading states, caching behavior
- Type Safety - TypeScript throughout
- Low Complexity - No backend needed
- Fast Iteration - Change JSON, see results immediately
Migration to Production
When moving components to production:
- Custom hooks stay similar but fetch from real APIs
- Add resolver pattern and authentication
- Add SSR/loader prefetching
- Replace JSON imports with HTTP calls
- Add proper error handling and retry logic
- Configure appropriate
staleTimeper query
Related Files
app/core/datafetching/QueryProvider.tsx- TanStack Query providerapp/data/index.ts- Type definitions and exportsapp/data/agents.json- Mock agent dataapp/data/tools.json- Mock tool dataapp/data/models.json- Mock model dataapp/data/home.json- Mock home contentapp/hooks/useAgents.ts- Agents data hookapp/hooks/useTools.ts- Tools data hookapp/hooks/useHomeData.ts- Home content data hook
04 同仓库更多 Skills
create-prototype-from-production
Use when asked to copy, clone, replicate or recreate a live production website or web app as a Replica prototype from its URL, such as "create a prototype from https://example.com" or "replicate this site for a demo". Do not use for building a new design from a brief, or for changing an existing prototype.
页面视觉设计评审
visual-design-review
依据使用方自有的产品与设计系统指南,审查页面层级、分组、密度、操作及状态设计。当用户要求视觉设计评审、页面点评,或询问新页面是否清晰传达其任务时使用。
技能共享源配置与上下文起草
setup-skills
配置共享源,并使用独立的 writer 起草上下文。普通/全新设置会提供用户指定包的安装,并复用内置浏览器能力,仅在确有证据表明必要检查存在缺口时才添加浏览器工具。适用于设置、源修复、编写上下文或刷新草稿。仅配置模式会跳过工具安装和编写;安装和激活需要单独批准。
Hub 反馈设计主题提炼
review-hub-feedback
通过发现、独立判断和综合归纳,审查 Hub 反馈以提炼可复用的设计主题。不进行诊断或优化。
UI 响应式布局审计
responsive-audit
使用使用者上下文和浏览器工具,跨产品定义的视口审查 UI 布局、溢出和导航。当用户要求检查响应式、测试移动端布局或审查断点行为时使用。
Replica 原型控件创建与绑定
prototype-controls
创建并连接 Replica 原型控件。当用户要求添加或修改下拉框、滑块、输入框、切换开关或操作按钮(用于配置变体、场景、可见 UI、应用状态或数据源)时使用,即使未使用画布术语也一样。涵盖声明、类型化绑定、默认值、条件、重置和动作。仅涉及产品 UI 表单字段和筛选器时,应使用 create-prototype。