skills/react-refactor/SKILL.md
Refactor large React components into a well-organized project structure. Use when the user has oversized React files (typically 300+ lines) and wants them broken into meaningful abstractions following a standard directory layout with shared components, page-level components, and shared frontend/backend types. Prefers Material UI, avoids over-splitting small components, and aims for a healthy balance of medium-sized files.
npx skillsauth add TravisBumgarner/claude-brain react-refactorInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Break down large React components into a well-structured, maintainable codebase. This skill applies opinionated conventions around directory structure, component granularity, and shared code organization.
App.tsx or a giant page component.The goal is medium-sized files (roughly 80–250 lines each), not a proliferation of tiny ones.
All refactors must target this directory layout:
project-root/
├── shared/
│ └── src/
│ ├── types/ # Types shared between frontend AND backend
│ │ ├── api.ts # Request/response shapes
│ │ ├── models.ts # Domain models, entities
│ │ └── index.ts # Barrel export
│ ├── constants.ts # Shared constants, enums
│ └── utils.ts # Pure utility functions used by both sides
│
├── frontend/
│ └── src/
│ ├── App.tsx
│ │
│ ├── components/ # Components used directly by App.tsx
│ │ ├── AppHeader.tsx
│ │ ├── AppSidebar.tsx
│ │ └── AppLayout.tsx
│ │
│ ├── sharedComponents/ # Components reused across multiple pages/features
│ │ ├── DataTable.tsx
│ │ ├── ConfirmDialog.tsx
│ │ ├── LoadingOverlay.tsx
│ │ └── StatusChip.tsx
│ │
│ ├── hooks/ # Shared custom hooks
│ │ └── useAuth.ts
│ │
│ └── pages/
│ ├── Dashboard/
│ │ ├── Dashboard.tsx
│ │ ├── index.ts # Re-exports Dashboard.tsx
│ │ └── components/ # Components ONLY used by Dashboard
│ │ ├── MetricsCard.tsx
│ │ └── ActivityFeed.tsx
│ │
│ └── Settings/
│ ├── Settings.tsx
│ ├── index.ts
│ └── components/
│ └── SettingsForm.tsx
| What it is | Where it goes |
|---|---|
| Types/interfaces used by both frontend and backend (API contracts, domain models, shared enums) | shared/src/types/ |
| Pure utility functions or constants used by both frontend and backend | shared/src/ |
| A component used by 2+ pages or features | frontend/src/sharedComponents/ |
| A component used only by App.tsx (layout, nav, top-level wrappers) | frontend/src/components/ |
| A page-level component (a route target) | frontend/src/pages/[PageName]/[PageName].tsx with an index.ts |
| A component used only within one page | frontend/src/pages/[PageName]/components/ |
| Custom hooks shared across pages | frontend/src/hooks/ |
| Custom hooks used only by one page | Co-locate in the page directory |
Every page directory must have an index.ts that re-exports the page component:
// frontend/src/pages/Dashboard/index.ts
export { default } from './Dashboard';
This keeps imports clean: import Dashboard from '@/pages/Dashboard'.
Before writing any code, read all target files and identify:
useState/useReducer blocks, context usage.sharedComponents/. Cross-file duplication always trumps the line-count heuristics below — extract it regardless of size.Outline the target files and what goes in each. Apply these judgment calls:
useDashboardFilters or useFormValidation.shared/ when the same interface appears in both API route handlers and frontend components.<Box> and <Typography> with no logic. Inline JSX with no independent state is fine to keep in the parent.<Button> variant. Use sx props or a small helper in the same file.Ask yourself: would this extraction result in two medium files, or one medium file and one tiny file? If the latter, skip it.
shared/src/types/. Keep component-local prop types in the component file.frontend/src/sharedComponents/ — things like data tables, dialogs, status indicators that are used (or will be used) in multiple places.frontend/src/pages/[Page]/components/ — sections of a page that are large enough to warrant their own file.index.ts barrel.<div>, <button>, <input> when MUI equivalents exist.index.ts) for directories when they have 3+ exports.Always prefer MUI components over raw HTML elements:
| Instead of | Use |
|---|---|
| <div> (for layout) | <Box>, <Stack>, <Grid>, <Container> |
| <button> | <Button>, <IconButton>, <LoadingButton> |
| <input> | <TextField>, <Select>, <Autocomplete> |
| <table> | <Table> + components, or <DataGrid> |
| <ul>/<li> | <List>, <ListItem>, <ListItemText> |
| <h1>–<h6>, <p>, <span> | <Typography variant="..."> |
| <a> | <Link> (MUI) or router <Link> |
| <dialog> / custom modals | <Dialog>, <DialogTitle>, <DialogContent>, <DialogActions> |
| Custom loading spinners | <CircularProgress>, <LinearProgress>, <Skeleton> |
| Custom tooltips | <Tooltip> |
| CSS media queries for layout | MUI useMediaQuery, <Grid>, or sx breakpoints |
Styling approach:
sx prop for one-off styles.styled() for components that are reused with consistent custom styling.sx or styled.These are guidelines, not hard rules. Use judgment.
| Signal | Action |
|---|---|
| Two+ files have structurally similar JSX/logic blocks | Always extract to sharedComponents/ — duplication overrides size rules |
| File is 500+ lines | Almost certainly needs splitting |
| File is 300–500 lines | Likely needs splitting — look for 2-3 natural seams |
| File is 150–300 lines | Maybe split if there are clearly distinct concerns; otherwise leave it |
| File is under 150 lines | Leave it alone unless it's doing two completely unrelated things |
| A JSX block is 80+ lines with its own state | Extract to a component |
| A JSX block is 30 lines, no state | Probably leave inline |
| A hook body is 40+ lines | Extract to a custom hook file |
| A hook body is 15 lines | Keep it in the component |
| 3+ components would use the same UI pattern | Put it in sharedComponents/ |
| 2 components use the same UI pattern | Put it in sharedComponents/ — don't wait for a third consumer |
| Only 1 component uses a UI pattern | Keep it local to that page's components/ |
Before: One 600-line page with header, filters, data table, and detail modal all inline.
After:
pages/Orders/
├── Orders.tsx (~120 lines — orchestrates layout, manages selected order state)
├── index.ts
└── components/
├── OrderFilters.tsx (~100 lines — filter state, MUI TextFields/Selects)
├── OrderTable.tsx (~120 lines — columns config, row rendering, pagination)
└── OrderDetail.tsx (~80 lines — MUI Dialog showing order info)
If multiple pages have similar tables (sortable, filterable, paginated), extract once:
sharedComponents/
└── DataTable.tsx (~200 lines — generic sortable/filterable table using MUI DataGrid)
Pages then use <DataTable columns={...} rows={...} /> instead of reimplementing.
Before: A 400-line component with 15 form fields, validation logic, and submission handling.
After:
pages/CreateUser/
├── CreateUser.tsx (~80 lines — layout, submit handler, success/error states)
├── index.ts
└── components/
└── UserForm.tsx (~180 lines — form fields, validation, MUI TextFields)
shared/src/types/
└── user.ts (CreateUserRequest, CreateUserResponse, UserFormData)
Before: App.tsx is 500 lines with routing, auth checks, sidebar, header, theme, and error boundaries.
After:
App.tsx (~80 lines — ThemeProvider, Router, AuthProvider, <AppLayout>)
components/
├── AppLayout.tsx (~60 lines — sidebar + header + main content area)
├── AppHeader.tsx (~100 lines — logo, nav, user menu)
└── AppSidebar.tsx (~120 lines — nav links, collapse logic)
Before delivering the refactored code:
index.ts barrels and pure type files).shared/src/types/.sharedComponents/.pages/[Page]/components/.index.ts.App.tsx-level components are in frontend/src/components/.development
Use this agent when designing visual interfaces, creating design systems, building component libraries, or refining user-facing aesthetics requiring expert visual design, interaction patterns, and accessibility considerations. Specifically: <example> Context: Product team needs a complete design system for a new fintech application with dark mode, multiple device sizes, and strict accessibility requirements. user: "We need to create a comprehensive design system for our fintech app. Include component library, design tokens, typography scale, color palette, interactive patterns, and dark mode support. Must meet WCAG 2.1 AA." assistant: "I'll create a complete design system starting with understanding your brand guidelines and user base through the context-manager. I'll build a modular component library with documented specs, design tokens in multiple formats (CSS, JSON, Figma), responsive patterns across web and mobile, dark mode variants, and comprehensive accessibility annotations. I'll deliver Figma files, design documentation, and developer handoff specifications." <commentary> Use ui-designer when you need to establish or refine a complete design system, including component libraries, design tokens, and comprehensive visual standards. This agent excels at creating reusable design assets and documentation that scale across teams and platforms. </commentary> </example> <example> Context: A frontend team is building a new feature but needs UI design and interaction patterns before development. user: "Design the booking flow for our travel app. Need 3 screen variations, empty/loading/error states, mobile and desktop layouts, and clear interaction patterns for each step." assistant: "I'll design a complete booking flow with wireframes, visual mockups, and interaction specifications. First, let me check the context-manager for your existing design system and style guide. Then I'll create all screen states, document interaction patterns, provide motion design specifications, and ensure alignment with your design language and accessibility standards." <commentary> Invoke ui-designer when your frontend developers need detailed visual designs, interaction specifications, and asset files before implementation. The agent provides a clear handoff package that developers can build from. </commentary> </example> <example> Context: A redesign initiative is underway and you need UI improvements to an existing product. user: "Our dashboard needs a visual refresh. The functionality is fine but the UX feels dated. Improve the visual hierarchy, modernize colors and typography, add micro-interactions, and ensure it works on mobile." assistant: "I'll analyze your current dashboard using the context-manager, identify visual improvement opportunities, redesign layouts for better hierarchy and scannability, update colors and typography to modern standards, add meaningful micro-interactions, and ensure responsive design. I'll provide before/after comparisons, design rationale, and implementation specifications for your developers." <commentary> Use ui-designer for visual refinements, redesigns, and aesthetic improvements to existing interfaces. This agent modernizes dated UIs while respecting existing functionality and providing clear upgrade paths. </commentary> </example>
development
Expert TypeScript developer specializing in advanced type system usage, full-stack development, and build optimization. Masters type-safe patterns for both frontend and backend with emphasis on developer experience and runtime safety.
development
Scaffold a full CRUD route — prompts for resource name, HTTP methods, and access level, then generates backend routes, shared schemas, frontend API functions, react-query hooks, and query keys.
development
Audit the codebase against all setup skills (00-09). Reports what is done, partial, not started, or N/A for every checklist item with a summary table.