react-spa-patterns
React SPA patterns for Frappe-backed applications. Reference for TanStack Query, Axios, Jotai, shadcn/ui, Tailwind, Refine v4, Mantine v5, React Router, Vite config, and Frappe API integration in the Unity Parent App and Walsh Admin Portal.
What this skill does
# React SPA Patterns for Frappe
Quick reference for building React SPAs that integrate with a Frappe backend.
## Project Overview
| App | Path | Stack |
|-----|------|-------|
| Unity Parent App | `apps/unity_parent_app/new_frontend/` | React 18 + Vite 6 + shadcn/ui + Tailwind + TanStack Query v5 + Jotai + Axios |
| Walsh Admin Portal | `apps/edu_quality/walsh/` | React 18 + Vite 5 + Refine v4 + Mantine v5 + React Query v3 |
---
## Frappe API Integration
### Axios (Parent App)
```typescript
// src/utils/axiosInstance.ts — pre-configured with withCredentials: true
import axiosInstance from '@/utils/axiosInstance';
// Call a whitelisted method
const res = await axiosInstance.post(
'/api/method/app.module.function',
{ student_id: 'STU-001' }
);
const data = res.data.message; // Frappe wraps in .message
// Fetch a resource
const res = await axiosInstance.get('/api/resource/Student/STU-001');
const student = res.data.data;
```
### Refine Data Provider (Walsh)
```typescript
import { useList, useOne, useCreate, useUpdate, useDelete } from "@refinedev/core";
// List
const { data } = useList({
resource: "Student",
filters: [{ field: "enabled", operator: "eq", value: 1 }],
pagination: { pageSize: 50 },
meta: { dataProviderName: "default" }, // or "notices", "cmap"
});
// data.data = array of records
// Create
const { mutate: create } = useCreate();
create({ resource: "Student", values: { ... } });
// Custom method (not REST)
const res = await dataProvider.custom({
url: '/api/method/edu_quality.api.module.function',
method: 'post',
payload: { param: value },
});
```
---
## TanStack Query Patterns
### Parent App (v5)
```typescript
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
// Query
const { data, isLoading, error } = useQuery({
queryKey: ['students', academicYear],
queryFn: () =>
axiosInstance.post('/api/method/...', { academic_year: academicYear })
.then(r => r.data.message),
staleTime: 5 * 60 * 1000,
enabled: !!academicYear,
});
// Mutation with cache invalidation
const queryClient = useQueryClient();
const { mutate } = useMutation({
mutationFn: (data) => axiosInstance.post('/api/method/...', data).then(r => r.data.message),
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['students'] }),
});
```
### Walsh (v3)
```typescript
import { useQuery, useMutation } from 'react-query';
const { data } = useQuery(
['key', dep],
() => fetch('/api/method/...').then(r => r.json()).then(r => r.message),
{ staleTime: 60_000 }
);
```
---
## State Management
### Jotai (Parent App)
```typescript
// Define atom
import { atom } from 'jotai';
export const activeStudentAtom = atom<string | null>(null);
// Read + write in component
import { useAtom, useAtomValue, useSetAtom } from 'jotai';
const [student, setStudent] = useAtom(activeStudentAtom);
const student = useAtomValue(activeStudentAtom); // read-only
const setStudent = useSetAtom(activeStudentAtom); // write-only
```
---
## Routing
### React Router v6 (Parent App)
```tsx
// App.tsx
import { BrowserRouter, Routes, Route } from 'react-router-dom';
<BrowserRouter basename="/parent-app">
<Routes>
<Route path="/" element={<Home />} />
<Route path="/transport" element={<Transport />} />
</Routes>
</BrowserRouter>
// Navigation in component
import { useNavigate, useParams } from 'react-router-dom';
const navigate = useNavigate();
navigate('/transport?active_student=STU-001');
```
### Refine Router (Walsh)
```tsx
// Routes are driven by Refine resources + react-router-v6 integration
// Add to resources array in <Refine> component
resources={[
{
name: "notices",
list: "/notices",
create: "/notices/create",
edit: "/notices/edit/:id",
meta: { dataProviderName: "notices" },
},
]}
```
---
## UI Components
### shadcn/ui + Tailwind (Parent App)
```tsx
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Card, CardContent, CardHeader } from "@/components/ui/card";
import { cn } from "@/lib/utils"; // clsx + tailwind-merge
// Variants with CVA
import { cva } from "class-variance-authority";
const buttonVariants = cva("base-classes", {
variants: { size: { sm: "...", lg: "..." } },
});
```
### Mantine v5 (Walsh)
```tsx
import { Button, TextInput, Select, Stack, Group, Modal } from "@mantine/core";
import { useDisclosure } from "@mantine/hooks";
import { notifications } from "@mantine/notifications";
const [opened, { open, close }] = useDisclosure(false);
notifications.show({ title: "Saved", message: "Record updated", color: "green" });
```
---
## Real-Time (Parent App Only)
```typescript
// src/utils/frappeSocket.ts
import { subscribeToRoom, onRealtimeEvent, offRealtimeEvent, unsubscribeFromRoom } from '@/utils/frappeSocket';
// Subscribe to a room
subscribeToRoom(`transport_journey_${journeyId}`);
// Listen for events
const handler = (data: unknown) => { /* handle */ };
onRealtimeEvent('transport_gps_update', handler);
// Cleanup
offRealtimeEvent('transport_gps_update', handler);
unsubscribeFromRoom(`transport_journey_${journeyId}`);
```
---
## Build & Deploy
```bash
# Parent App
cd apps/unity_parent_app/new_frontend
npm run dev # Dev server :8080
npm run build # Build → public/new_frontend/ + copy www/parent-app.html
# Walsh
cd apps/edu_quality/walsh
yarn dev # Dev server :8080
yarn build # TS compile → Vite build → public/walsh/ + copy www/walsh.html
# After any build
bench --site <site> clear-cache
bench build --app <app> # Only needed if static assets changed
```
---
## Common Patterns
### Protected Route (Parent App)
```tsx
import { Navigate } from 'react-router-dom';
import { isLoggedIn } from '@/utils/cookies';
const ProtectedRoute = ({ children }) =>
isLoggedIn() ? children : <Navigate to="/login" replace />;
```
### Error Boundary
```tsx
// Wrap pages in ErrorBoundary from src/components/
import ErrorBoundary from '@/components/ErrorBoundary';
<ErrorBoundary fallback={<ErrorPage />}>
<MyPage />
</ErrorBoundary>
```
### Frappe File Upload
```typescript
const formData = new FormData();
formData.append('file', file);
formData.append('doctype', 'Student');
formData.append('docname', studentId);
const res = await axiosInstance.post('/api/method/upload_file', formData, {
headers: { 'Content-Type': 'multipart/form-data' },
});
```
Related in Design
contribute
IncludedLocal-only OSS contribution command center. Auto-refreshes the user's in-flight PR and issue state on invoke so conversations start with full context — no need to brief Claude on what's in flight. Helps the user find issues to contribute to on GitHub, builds per-repo dossiers of what each upstream expects (CLA, DCO, branch convention, AI policy, draft-first, review bots, issue templates), runs deterministic gates before any external action so AI-assisted contributions don't reach maintainers as slop. State is markdown-only: candidate files at ~/.contribute-system/candidates/, repo dossiers at ~/.contribute-system/research/, append-only event log at ~/.contribute-system/log.jsonl. No database, no cloud calls. Use when the user asks about their PRs / issues / contributions, wants to find new work to take on, claim an issue, build/refresh a repo's dossier, or draft a Design Issue or PR. Trigger with "/contribute", "what's my PR status", "find a contribution", "claim issue X", "draft a Design Issue for Y", "refresh dossier for Z".
architectural-analysis
IncludedUser-triggered deep architectural analysis of a codebase or scoped subtree across eight modes — information architecture, data flow, integration points, UI surfaces, interaction patterns, data model, control flow, and failure modes. This skill should be used when the user asks to "diagram this codebase," "map the architecture," "show the data flow," "give me an ERD," "trace control flow," "find the integration points," "verify the layout pattern," "audit the UX architecture," or any similar request whose primary deliverable is mermaid diagrams plus cited reports under docs/architecture/. Dispatches haiku/sonnet sub-agents in parallel for per-mode exploration, then verifies every citation mechanically before any node lands in a diagram. Not for one-off prose explanations of code (use code-explanation) or for high-level system design from scratch (use system-design).
mcp
IncludedModel Context Protocol (MCP) server development and tool management. Languages: Python, TypeScript. Capabilities: build MCP servers, integrate external APIs, discover/execute MCP tools, manage multi-server configs, design agent-centric tools. Actions: create, build, integrate, discover, execute, configure MCP servers/tools. Keywords: MCP, Model Context Protocol, MCP server, MCP tool, stdio transport, SSE transport, tool discovery, resource provider, prompt template, external API integration, Gemini CLI MCP, Claude MCP, agent tools, tool execution, server config. Use when: building MCP servers, integrating external APIs as MCP tools, discovering available MCP tools, executing MCP capabilities, configuring multi-server setups, designing tools for AI agents.
react-native-skia
IncludedDesign, build, debug, and optimise high-polish animated graphics in React Native or Expo using @shopify/react-native-skia, Reanimated, and Gesture Handler. Use when the user wants canvas-driven UI, shaders, paths, rich text, image filters, sprite fields, Skottie, video frames, snapshots, web CanvasKit setup, or performance tuning for custom motion-heavy elements such as loaders, hero art, cards, charts, progress indicators, particle systems, or gesture-driven surfaces. Also use when the user asks for fluid, glow, glass, blob, parallax, 60fps/120fps, or GPU-friendly animated effects in React Native, even if they do not explicitly say "Skia". Do not use for ordinary form/layout work with standard views.
plaid
IncludedProduct Led AI Development — guides founders from idea to launched product. Six capabilities: Idea (discover a product idea), Validate (pressure-test the idea against fatal flaws, problem reality, competition, and 2-week MVP feasibility), Plan (vision intake + document generation), Design (translate image references into a design.md spec), Launch (go-to-market strategy), and Build (roadmap execution). Use when someone says "PLAID", "plaid idea", "help me find an idea", "product idea", "idea from my business", "idea from my expertise", "plaid validate", "validate my idea", "pressure-test", "is this idea good", "find fatal flaws", "validate the problem", "plan a product", "define my vision", "generate a PRD", "product strategy", "plaid design", "design from image", "translate image to design", "create design.md", "extract design tokens", "plaid launch", "go-to-market", "launch plan", "GTM strategy", "launch playbook", "plaid build", "build the app", "start building", or "execute the roadmap".
nextjs-framer-motion-animations
IncludedAdds production-safe Motion for React or Framer Motion animations to Next.js apps, including reveal, hover and tap micro-interactions, whileInView, stagger, AnimatePresence, layout and layoutId transitions, reorder, scroll-linked UI, and lightweight route-content transitions. Use when the user asks to add, refactor, or debug Motion or Framer Motion in App Router or Pages Router codebases, especially around server/client boundaries, reduced motion, LazyMotion, bundle size, hydration, or route transitions. Avoid for GSAP-style timelines, WebGL or 3D scenes, heavy scroll storytelling, or CSS-only effects unless Motion is explicitly requested.