|
| 1 | +--- |
| 2 | +description: Rules for the JavaScript/React codebase of the Content Control Plugin |
| 3 | +globs: ["packages/**/*.{js,jsx,ts,tsx}"] |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | + |
| 7 | +# Content Control Plugin - JavaScript/React Codebase Rules |
| 8 | + |
| 9 | +These rules are specific to the JavaScript/React codebase of the Content Control plugin, providing guidance for AI agents working with the frontend logic and user interface components. |
| 10 | + |
| 11 | +## JavaScript/React Architecture Overview |
| 12 | + |
| 13 | +The Content Control plugin utilizes React and WordPress's `@wordpress/scripts` build tooling for its admin user interface. The frontend codebase is located within the `packages/` directory and is structured as a monorepo managed by NX. |
| 14 | + |
| 15 | +### Key Packages |
| 16 | + |
| 17 | +- **`packages/block-editor/`**: Contains code related to Block Editor (Gutenberg) integration. |
| 18 | + - **`src/block-extensions/`**: Extends existing blocks with Content Control settings. |
| 19 | + - **`src/components/`**: Reusable React components specific to the block editor integration. |
| 20 | + - **`src/contexts/`**: React Contexts for managing state within the block editor UI. |
| 21 | +- **`packages/components/`**: Contains reusable React components used throughout the plugin's admin UI (Settings Page, Block Editor, etc.). These are general-purpose components. |
| 22 | +- **`packages/core-data/`**: Implements Redux-like data stores using `@wordpress/data` for managing client-side application state. Stores are organized by feature (license, restrictions, settings, url-search). |
| 23 | +- **`packages/data/`**: A wrapper around `@wordpress/data`, potentially for custom extensions or utilities. |
| 24 | +- **`packages/fields/`**: React components for rendering various form field types used in settings and rule configurations. |
| 25 | +- **`packages/icons/`**: Custom SVG icons used in the React UI. |
| 26 | +- **`packages/query-builder/`**: Contains React components, contexts, and reducers for building the visual rule query builder UI. |
| 27 | +- **`packages/settings-page/`**: The main React application for the plugin's settings page. |
| 28 | + - **`src/restrictions-view/`**: Components for managing and displaying restrictions within the settings page. |
| 29 | + - **`src/settings-view/`**: Components for displaying and managing general plugin settings. |
| 30 | + - **`src/upgrades-view/`**: Components related to plugin upgrades and license management. |
| 31 | +- **`packages/utils/`**: General JavaScript utility functions used across packages. |
| 32 | +- **`packages/widget-editor/`**: Code for integration with the legacy widget editor (if applicable). |
| 33 | + |
| 34 | +### Important JavaScript/React Components and Modules |
| 35 | + |
| 36 | +- **`packages/settings-page/src/App.tsx`**: The main entry point for the Settings Page React application. |
| 37 | +- **`packages/block-editor/src/block-extensions/block-controls/edit.tsx`**: The main component that adds Content Control UI to the Block Editor inspector controls. |
| 38 | +- **`packages/core-data/src/*/index.ts`**: Entry points for each data store slice (e.g., `restrictions`, `settings`, `license`). |
| 39 | +- **`packages/components/src/entity-select-control/`**: Reusable component for selecting WordPress entities (posts, terms, users) with search and multi-select capabilities. |
| 40 | +- **`packages/query-builder/src/components/`**: Components that make up the rule query builder UI (groups, rules, operators, etc.). |
| 41 | +- **`packages/components/src/controlled-tab-panel/`**: Reusable tab panel component used in the settings page. |
| 42 | + |
| 43 | +### Coding Standards and Best Practices (JavaScript/React) |
| 44 | + |
| 45 | +- **React Best Practices:** Follow established React best practices for component design, state management, and performance. |
| 46 | + - Use functional components and hooks where appropriate. |
| 47 | + - Keep components small, focused, and reusable. |
| 48 | + - Optimize component rendering and avoid unnecessary re-renders. |
| 49 | + - Use keys for lists of components. |
| 50 | +- **WordPress JavaScript Coding Standards:** Adhere to WordPress JavaScript coding standards. |
| 51 | + - Use spaces for indentation (typically 2 spaces). |
| 52 | + - Use single quotes for strings. |
| 53 | + - Avoid long lines of code. |
| 54 | + - Document components and functions with JSDoc-style comments. |
| 55 | +- **TypeScript:** The codebase uses TypeScript. Write type-safe code and utilize TypeScript features effectively. |
| 56 | + - Define clear interfaces and types for components, data structures, and API interactions. |
| 57 | + - Use type checking to catch errors early in development. |
| 58 | +- **`@wordpress/scripts` Build Tooling:** Utilize the tools and configurations provided by `@wordpress/scripts` for building, linting, and testing JavaScript code. |
| 59 | + - Use `npm run start` for development with hot reloading. |
| 60 | + - Use `npm run build` for production builds. |
| 61 | + - Use `npm run lint:js` for JavaScript linting. |
| 62 | + - Use `npm run format:js` for code formatting. |
| 63 | +- **State Management with `@wordpress/data`:** Understand and utilize `@wordpress/data` for managing application state. |
| 64 | + - Use `useSelect` to access data from stores. |
| 65 | + - Use `useDispatch` to dispatch actions to stores. |
| 66 | + - Define clear selectors and actions for data store interactions. |
| 67 | +- **Component Reusability:** Design and build reusable components in the `packages/components/` directory to promote code sharing and consistency across the UI. |
| 68 | +- **Context API for State Management:** Utilize React Context API for managing component-level or feature-specific state, especially within the Block Editor and Query Builder packages. |
| 69 | + |
| 70 | +### Important JavaScript/React Functions and Hooks |
| 71 | + |
| 72 | +- **React Hooks:** Utilize common React hooks like `useState`, `useEffect`, `useContext`, `useCallback`, `useMemo`. |
| 73 | +- **`@wordpress/data` Hooks:** |
| 74 | + - `useSelect`: Hook to select data from WordPress data stores. |
| 75 | + - `useDispatch`: Hook to dispatch actions to WordPress data stores. |
| 76 | + - `withSelect`: Higher-order component for connecting components to data stores (less common in modern React). |
| 77 | + - `withDispatch`: Higher-order component for connecting components to dispatch actions (less common in modern React). |
| 78 | +- **WordPress Components (`@wordpress/components`):** Utilize pre-built WordPress components from the `@wordpress/components` library for UI elements (buttons, inputs, selects, modals, etc.). |
| 79 | +- **WordPress Block Editor APIs (`@wordpress/block-editor`, `@wordpress/blocks`):** When working with Block Editor integration, use APIs from `@wordpress/block-editor` and `@wordpress/blocks` to extend blocks, register block attributes, and interact with the editor. |
| 80 | +- **REST API Interactions:** Use `wp.apiFetch` (or standard `fetch` API) for making requests to WordPress REST API endpoints. |
| 81 | + |
| 82 | +### Debugging JavaScript/React Code |
| 83 | + |
| 84 | +- **Browser Developer Tools:** Use browser developer tools (Chrome DevTools, Firefox Developer Tools) extensively for debugging JavaScript code. |
| 85 | + - **Console:** Use `console.log()`, `console.warn()`, `console.error()` for logging messages. |
| 86 | + - **Debugger:** Set breakpoints in the code and step through execution. |
| 87 | + - **React Developer Tools Extension:** Use the React Developer Tools browser extension to inspect React component trees, props, and state. |
| 88 | + - **Network Tab:** Inspect network requests to REST API endpoints. |
| 89 | +- **`WP_DEBUG` (WordPress):** Enabling `WP_DEBUG` in `wp-config.php` can sometimes provide additional JavaScript error information in the browser console. |
| 90 | +- **Source Maps:** Ensure source maps are enabled in development builds to debug against the original source code instead of bundled code. |
0 commit comments