Implementing multiple spaces and the ability to switch between them in Codex.Docs requires significant changes across the backend and frontend. The current codebase doesn't have a concept of spaces. Here's a breakdown of the required steps:
1. Database Changes:
Add a space field to the pages collection: This field will store the ID of the space to which a page belongs. The type should be EntityId to match existing ID fields. Consider adding an index to this field for performance.
Add a spaces collection: This collection will store information about each space, such as:
_id: Space ID (EntityId)
name: Space name (string)
description: (Optional) Space description (string)
owner: (Optional) User ID of the space owner (EntityId - requires a users collection if not already present)
2. Backend Changes (src/backend):
Models:
Create a new Space model (space.ts) similar to the Page model, with methods for creating, retrieving, updating, and deleting spaces.
Modify the Page model to include the space field and update relevant methods (e.g., save, get, getAll). Queries for pages should now include the space ID.
Controllers:
Create a new Spaces controller (spaces.ts) to handle space-related operations.
Modify the Pages controller to handle the space field when creating, updating, and retrieving pages. Ensure that users can only access pages within their allowed spaces.
Routes:
Add new API routes for space management (e.g., /api/space, /api/space/:id).
Modify existing page routes to include the space ID (e.g., /api/space/:spaceId/page, /api/space/:spaceId/page/:id). This will require changes to URL structures and handling in the frontend.
Middleware: Implement middleware to check space access permissions.
3. Frontend Changes (src/frontend):
State Management: Implement a way to store the currently active space in the frontend (e.g., using local storage, a global variable, or a more robust state management solution like Redux).
UI Changes:
Add a space selector to the UI, allowing users to switch between spaces. This could be a dropdown or a dedicated section in the sidebar.
Update the page creation/edit forms to include a space selector.
Display the current space prominently in the UI.
API Calls: Update all API calls related to pages to include the currently active space ID.
Routing: Update the frontend routing to handle space IDs in URLs. Consider using a URL structure like /space/:spaceId/page/:pageId.
4. Authentication and Authorization:
If not already implemented, you'll need user authentication to manage space ownership and access permissions.
Implement logic to determine which spaces a user can access and display only those spaces in the space selector.
Example Code Snippets (Backend - pages.ts):
typescript
// In Pages.getAllPages()
public static async getAllPages(spaceId: EntityId): Promise<Page[]> {
return Page.getAll({ space: spaceId });
}
// In Pages.insert()
public static async insert(data: PageData, spaceId: EntityId): Promise<Page> {
// ... existing validation logic
const page = new Page({ ...data, space: spaceId });
// ...
}
Example Code Snippets (Frontend - API call):
javascript
// Fetching pages for the current space
const spaceId = getCurrentSpaceId(); // Implement this function
fetch(`/api/space/${spaceId}/pages`)
.then(// ...);
This is a high-level overview. The actual implementation will involve more detailed coding and testing. Remember to handle edge cases, error handling, and user experience considerations throughout the development process. Consider incremental development, starting with a basic implementation and gradually adding features like space management and access control.