mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-05-02 17:55:07 +00:00
dc982772af
## Phase 1: Monolithic File Refactoring ✅ - Refactored 8 large files (300-500 LOC) into 40+ modular components/hooks - All files now <150 LOC per file (max 125 LOC) - CanvasSettings: 343 → 7 components - SecuritySettings: 273 → 6 components - NotificationSettings: 239 → 6 components - Editor/Toolbar: 258 → 7 components - InfiniteCanvas: 239 → 10 modules - WorkflowCard: 320 → 5 components + custom hook - useProjectCanvas: 322 → 8 hooks - projectSlice: 335 → 4 Redux slices ## Phase 2: Business Logic Extraction ✅ - Extracted logic from 5 components into 8 custom hooks - register/page.tsx: 235 → 167 LOC (-29%) - login/page.tsx: 137 → 100 LOC (-27%) - MainLayout.tsx: 216 → 185 LOC (-14%) - ProjectSidebar.tsx: 200 → 200 LOC (refactored) - page.tsx (Dashboard): 197 → 171 LOC (-13%) - New hooks: useAuthForm, usePasswordValidation, useLoginLogic, useRegisterLogic, useHeaderLogic, useResponsiveSidebar, useProjectSidebarLogic, useDashboardLogic ## Phase 3: Dead Code Analysis & Implementation ✅ - Identified and documented 3 unused hooks (244 LOC) - Removed useRealtimeService from exports - Cleaned 8 commented lines in useProject.ts - Documented useExecution stub methods - Removed 3 commented dispatch calls in useCanvasKeyboard - Fixed 3 'as any' type assertions ## Phase 4: Stub Code Implementation ✅ - Fully implemented useExecution methods: execute(), stop(), getDetails(), getStats(), getHistory() - Integrated useCanvasKeyboard into InfiniteCanvas with Redux dispatch - Verified useCanvasVirtualization for 100+ items - Enhanced useRealtimeService documentation for Phase 4 WebSocket integration ## Backend Updates - Added SQLAlchemy models: Workspace, Project, ProjectCanvasItem - Added Flask API endpoints for CRUD operations - Configured multi-tenant filtering for all queries - Added database migrations for new entities ## Build Verification ✅ - TypeScript strict mode: 0 errors - Production build: ✅ Successful (161 kB First Load JS) - No breaking changes - 100% backward compatibility maintained ## Documentation Generated - 6 comprehensive guides (70+ KB total) - Test templates for all new implementations - Quick reference for all 42 hooks - Implementation checklist and deployment guide Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
184 lines
5.5 KiB
TypeScript
184 lines
5.5 KiB
TypeScript
/**
|
|
* useWorkspace Hook
|
|
* Manages workspace state and operations
|
|
*/
|
|
|
|
import { useCallback, useEffect, useState } from 'react';
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
import { AppDispatch, RootState } from '../store/store';
|
|
import {
|
|
setWorkspaces,
|
|
addWorkspace,
|
|
updateWorkspace,
|
|
removeWorkspace,
|
|
setCurrentWorkspace,
|
|
setLoading,
|
|
setError,
|
|
selectWorkspaces,
|
|
selectCurrentWorkspace,
|
|
selectCurrentWorkspaceId,
|
|
selectWorkspaceIsLoading,
|
|
selectWorkspaceError
|
|
} from '../store/slices/workspaceSlice';
|
|
import workspaceService from '../services/workspaceService';
|
|
import { workspaceDB } from '../db/schema';
|
|
import { Workspace, CreateWorkspaceRequest, UpdateWorkspaceRequest } from '../types/project';
|
|
import { useUI } from './useUI';
|
|
|
|
export function useWorkspace() {
|
|
const dispatch = useDispatch<AppDispatch>();
|
|
const { showNotification } = useUI() as any;
|
|
const [isInitialized, setIsInitialized] = useState(false);
|
|
|
|
// Selectors
|
|
const workspaces = useSelector((state: RootState) => selectWorkspaces(state));
|
|
const currentWorkspace = useSelector((state: RootState) => selectCurrentWorkspace(state));
|
|
const currentWorkspaceId = useSelector((state: RootState) => selectCurrentWorkspaceId(state));
|
|
const isLoading = useSelector((state: RootState) => selectWorkspaceIsLoading(state));
|
|
const error = useSelector((state: RootState) => selectWorkspaceError(state));
|
|
|
|
// Get tenant ID from localStorage or default
|
|
const getTenantId = useCallback(() => {
|
|
return localStorage.getItem('tenantId') || 'default';
|
|
}, []);
|
|
|
|
// Load workspaces on mount
|
|
useEffect(() => {
|
|
if (!isInitialized) {
|
|
loadWorkspaces();
|
|
setIsInitialized(true);
|
|
}
|
|
}, [isInitialized]);
|
|
|
|
// Load workspaces from server
|
|
const loadWorkspaces = useCallback(async () => {
|
|
dispatch(setLoading(true));
|
|
try {
|
|
const tenantId = getTenantId();
|
|
const response = await workspaceService.listWorkspaces(tenantId);
|
|
dispatch(setWorkspaces(response.workspaces));
|
|
|
|
// Cache in IndexedDB
|
|
await Promise.all(response.workspaces.map((ws) => workspaceDB.update(ws)));
|
|
|
|
// Set default current workspace if not set
|
|
if (!currentWorkspaceId && response.workspaces.length > 0) {
|
|
dispatch(setCurrentWorkspace(response.workspaces[0].id));
|
|
localStorage.setItem('currentWorkspaceId', response.workspaces[0].id);
|
|
}
|
|
|
|
dispatch(setError(null));
|
|
} catch (err) {
|
|
const errorMsg = err instanceof Error ? err.message : 'Failed to load workspaces';
|
|
dispatch(setError(errorMsg));
|
|
showNotification(errorMsg, 'error');
|
|
} finally {
|
|
dispatch(setLoading(false));
|
|
}
|
|
}, [dispatch, getTenantId, currentWorkspaceId, showNotification]);
|
|
|
|
// Create workspace
|
|
const createWorkspace = useCallback(
|
|
async (data: CreateWorkspaceRequest) => {
|
|
dispatch(setLoading(true));
|
|
try {
|
|
const tenantId = getTenantId();
|
|
const workspace = await workspaceService.createWorkspace({
|
|
...data,
|
|
tenantId
|
|
});
|
|
|
|
dispatch(addWorkspace(workspace));
|
|
await workspaceDB.create(workspace);
|
|
|
|
showNotification(`Workspace "${workspace.name}" created successfully`, 'success');
|
|
return workspace;
|
|
} catch (err) {
|
|
const errorMsg = err instanceof Error ? err.message : 'Failed to create workspace';
|
|
dispatch(setError(errorMsg));
|
|
showNotification(errorMsg, 'error');
|
|
throw err;
|
|
} finally {
|
|
dispatch(setLoading(false));
|
|
}
|
|
},
|
|
[dispatch, getTenantId, showNotification]
|
|
);
|
|
|
|
// Update workspace
|
|
const updateWorkspaceData = useCallback(
|
|
async (id: string, data: UpdateWorkspaceRequest) => {
|
|
dispatch(setLoading(true));
|
|
try {
|
|
const updated = await workspaceService.updateWorkspace(id, data);
|
|
dispatch(updateWorkspace(updated));
|
|
await workspaceDB.update(updated);
|
|
|
|
showNotification(`Workspace "${updated.name}" updated successfully`, 'success');
|
|
return updated;
|
|
} catch (err) {
|
|
const errorMsg = err instanceof Error ? err.message : 'Failed to update workspace';
|
|
dispatch(setError(errorMsg));
|
|
showNotification(errorMsg, 'error');
|
|
throw err;
|
|
} finally {
|
|
dispatch(setLoading(false));
|
|
}
|
|
},
|
|
[dispatch, showNotification]
|
|
);
|
|
|
|
// Delete workspace
|
|
const deleteWorkspace = useCallback(
|
|
async (id: string) => {
|
|
dispatch(setLoading(true));
|
|
try {
|
|
await workspaceService.deleteWorkspace(id);
|
|
dispatch(removeWorkspace(id));
|
|
await workspaceDB.delete(id);
|
|
|
|
showNotification('Workspace deleted successfully', 'success');
|
|
} catch (err) {
|
|
const errorMsg = err instanceof Error ? err.message : 'Failed to delete workspace';
|
|
dispatch(setError(errorMsg));
|
|
showNotification(errorMsg, 'error');
|
|
throw err;
|
|
} finally {
|
|
dispatch(setLoading(false));
|
|
}
|
|
},
|
|
[dispatch, showNotification]
|
|
);
|
|
|
|
// Switch workspace
|
|
const switchWorkspace = useCallback(
|
|
(id: string | null) => {
|
|
dispatch(setCurrentWorkspace(id));
|
|
if (id) {
|
|
localStorage.setItem('currentWorkspaceId', id);
|
|
} else {
|
|
localStorage.removeItem('currentWorkspaceId');
|
|
}
|
|
},
|
|
[dispatch]
|
|
);
|
|
|
|
return {
|
|
// State
|
|
workspaces,
|
|
currentWorkspace,
|
|
currentWorkspaceId,
|
|
isLoading,
|
|
error,
|
|
|
|
// Actions
|
|
loadWorkspaces,
|
|
createWorkspace,
|
|
updateWorkspace: updateWorkspaceData,
|
|
deleteWorkspace,
|
|
switchWorkspace
|
|
};
|
|
}
|
|
|
|
export default useWorkspace;
|