Client-Side Structure
How to structure your PR when contributing to Unkey dashboard
Client-Side Structure Contribution Guidelines
Overview
When contributing to the Unkey dashboard or any client app in the Unkey repository, we follow a feature-based architecture. This guide will help you understand how to structure your code to maintain consistency across the project.
Directory Structure
Each feature (typically corresponding to a Next.js page) should be organized as a self-contained module with the following structure:
Key Principles
-
Feature Isolation
- Keep all related code within the feature directory
- Don't import feature-specific components into other features
- Use shared components from the global
/components
directory orunkey/ui
package for common UI elements
-
Component Organization
- Simple components can be single files (No need to break everything into 50 different files, follow your common sense)
- Complex components should have their own directory with an index.tsx
- Keep component-specific styles, tests, and utilities close to the component
-
Code Colocation
- Place related code as close as possible to where it's used
- If a utility is only used by one component, keep it in the component's directory
Example Page Structure
Here's an example of how to structure a typical page component:
Best Practices
-
File Naming
- Use kebab-case for directory and file names
- The directory structure itself provides context, so explicit suffixes are optional
- If you choose to use suffixes for additional clarity, common patterns include:
auth.schema.ts
or justauth-schema.ts
for validation schemasauth.type.ts
or justauth-types.ts
for type definitions.client.tsx
for client-specific components.server.ts
for server-only code.action.ts
for server actions
-
Code Organization
- Keep files focused and single-purpose
- Use index.ts files to expose public API of complex components
- Colocate tests with the code they test
- Place shared types in the feature's
types
directory
-
Imports and Exports
- Use absolute imports for shared components (
@/components
) - Never use default exports unless it's absolutely necessary
- Use relative imports within a feature
- Export complex components through index files
- Avoid circular dependencies
- Use absolute imports for shared components (
Shared Code
Global shared code should be placed in root-level directories:
Only place code in these directories if it's used across multiple features.
Example Feature Implementation
Here's a practical example of how to structure a feature:
Questions?
If you're unsure about where to place certain code or how to structure your feature, please don't hesitate to ask in our Discord community or in your pull request. We're here to help!
Last updated on