This commit is contained in:
2025-11-14 14:47:26 +00:00
parent 69ecf2c7c1
commit 3b4876793e
104 changed files with 231517 additions and 1029 deletions
+99 -56
View File
@@ -3,6 +3,7 @@ import { useAuth } from './AuthContext';
import { useUser } from './UserContext';
import { logger } from '../debugConfig';
import { CCUserNodeProps, CCCalendarNodeProps, CCUserTeacherTimetableNodeProps } from '../utils/tldraw/cc-base/cc-graph/cc-graph-types';
import { DatabaseNameService } from '../services/graph/databaseNameService';
import { CalendarStructure, WorkerStructure } from '../types/navigation';
import { useNavigationStore } from '../stores/navigationStore';
@@ -11,7 +12,7 @@ export interface CalendarNode {
id: string;
label: string;
title: string;
tldraw_snapshot: string;
node_storage_path: string;
type?: CCCalendarNodeProps['__primarylabel__'];
nodeData?: CCCalendarNodeProps;
}
@@ -20,7 +21,7 @@ export interface WorkerNode {
id: string;
label: string;
title: string;
tldraw_snapshot: string;
node_storage_path: string;
type?: CCUserTeacherTimetableNodeProps['__primarylabel__'];
nodeData?: CCUserTeacherTimetableNodeProps;
}
@@ -162,8 +163,8 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
backgroundColor: '#ffffff',
isLocked: false,
__primarylabel__: 'UserTeacherTimetable',
unique_id: '',
tldraw_snapshot: '',
uuid_string: '',
node_storage_path: '',
created: new Date().toISOString(),
merged: new Date().toISOString(),
state: {
@@ -177,7 +178,34 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
// Initialize context when dependencies are ready
useEffect(() => {
if (!isUserInitialized || !profile || isInitialized || initializationRef.current.hasStarted) {
logger.debug('neo-user-context', '🔄 useEffect triggered', {
isUserInitialized,
hasProfile: !!profile,
hasUser: !!user,
isInitialized,
hasStarted: initializationRef.current.hasStarted
});
if (!isUserInitialized) {
logger.debug('neo-user-context', '⏳ Waiting for user context initialization');
return;
}
if (!profile) {
if (!initializationRef.current.isComplete) {
setIsLoading(false);
setIsInitialized(true);
initializationRef.current.isComplete = true;
logger.debug('neo-user-context', '️ No profile available; marking context initialized');
}
return;
}
if (isInitialized || initializationRef.current.hasStarted) {
logger.debug('neo-user-context', '️ Initialization already in progress or complete', {
isInitialized,
hasStarted: initializationRef.current.hasStarted
});
return;
}
@@ -189,8 +217,8 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
// Set database names
const userDb = profile.user_db_name || (user?.email ?
`cc.users.${user.email.replace('@', 'at').replace(/\./g, 'dot')}` : null);
DatabaseNameService.getStoredUserDatabase() || null : null);
if (!userDb) {
throw new Error('No user database name available');
}
@@ -199,27 +227,42 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
logger.debug('neo-user-context', '🔄 Starting context initialization');
// Initialize user node
await navigationStore.switchContext({
main: 'profile',
base: 'profile',
extended: 'overview'
}, userDb, profile.school_db_name);
try {
await navigationStore.switchContext({
main: 'profile',
base: 'profile',
extended: 'overview'
}, userDb, profile.school_db_name);
const userNavigationNode = navigationStore.context.node;
if (userNavigationNode?.data) {
const userNodeData: CCUserNodeProps = {
...getBaseNodeProps(),
__primarylabel__: 'User',
unique_id: userNavigationNode.id,
tldraw_snapshot: userNavigationNode.tldraw_snapshot || '',
title: String(userNavigationNode.data?.user_name || 'User'),
user_name: String(userNavigationNode.data?.user_name || 'User'),
user_email: user?.email || '',
user_type: 'User',
user_id: userNavigationNode.id,
worker_node_data: JSON.stringify(userNavigationNode.data || {})
};
setUserNode(userNodeData);
const userNavigationNode = navigationStore.context.node;
if (userNavigationNode?.id && userNavigationNode?.data) {
const userNodeData: CCUserNodeProps = {
...getBaseNodeProps(),
__primarylabel__: 'User',
uuid_string: userNavigationNode.id,
node_storage_path: userNavigationNode.node_storage_path || '',
title: String(userNavigationNode.data?.user_name || 'User'),
user_name: String(userNavigationNode.data?.user_name || 'User'),
user_email: user?.email || '',
user_type: 'User',
user_id: userNavigationNode.id,
worker_node_data: JSON.stringify(userNavigationNode.data || {})
};
setUserNode(userNodeData);
logger.debug('neo-user-context', '✅ User node loaded from navigation store');
} else if (userNavigationNode?.id) {
logger.debug('neo-user-context', '️ User node exists but data not yet loaded - will retry later', {
nodeId: userNavigationNode.id,
hasData: !!userNavigationNode.data
});
} else {
logger.debug('neo-user-context', '️ No user node in navigation store yet - will retry later');
}
} catch (navError) {
logger.warn('neo-user-context', '⚠️ Navigation store initialization failed - continuing without user node', {
error: navError instanceof Error ? navError.message : String(navError)
});
// Continue without user node - this is not critical for basic functionality
}
// Set final state
@@ -228,7 +271,7 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
setIsInitialized(true);
setIsLoading(false);
initializationRef.current.isComplete = true;
logger.debug('neo-user-context', '✅ Context initialization complete');
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Failed to initialize user context';
@@ -241,7 +284,7 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
};
initializeContext();
}, [user?.email, profile, isUserInitialized, navigationStore, isInitialized]);
}, [user, profile, isUserInitialized, navigationStore, isInitialized]);
// Calendar Navigation Functions
const navigateToDay = async (id: string) => {
@@ -258,8 +301,8 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
const nodeData: CCCalendarNodeProps = {
...getBaseNodeProps(),
__primarylabel__: 'CalendarDay',
unique_id: id || node.id,
tldraw_snapshot: node.tldraw_snapshot || '',
uuid_string: id || node.id,
node_storage_path: node.node_storage_path || '',
title: node.label,
name: node.label,
calendar_type: 'day',
@@ -272,7 +315,7 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
id: id || node.id,
label: node.label,
title: node.label,
tldraw_snapshot: node.tldraw_snapshot || '',
node_storage_path: node.node_storage_path || '',
type: 'CalendarDay',
nodeData
});
@@ -298,8 +341,8 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
const nodeData: CCCalendarNodeProps = {
...getBaseNodeProps(),
__primarylabel__: 'CalendarWeek',
unique_id: id || node.id,
tldraw_snapshot: node.tldraw_snapshot || '',
uuid_string: id || node.id,
node_storage_path: node.node_storage_path || '',
title: node.label,
name: node.label,
calendar_type: 'week',
@@ -312,7 +355,7 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
id: id || node.id,
label: node.label,
title: node.label,
tldraw_snapshot: node.tldraw_snapshot || '',
node_storage_path: node.node_storage_path || '',
type: 'CalendarWeek',
nodeData
});
@@ -338,8 +381,8 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
const nodeData: CCCalendarNodeProps = {
...getBaseNodeProps(),
__primarylabel__: 'CalendarMonth',
unique_id: id || node.id,
tldraw_snapshot: node.tldraw_snapshot || '',
uuid_string: id || node.id,
node_storage_path: node.node_storage_path || '',
title: node.label,
name: node.label,
calendar_type: 'month',
@@ -352,7 +395,7 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
id: id || node.id,
label: node.label,
title: node.label,
tldraw_snapshot: node.tldraw_snapshot || '',
node_storage_path: node.node_storage_path || '',
type: 'CalendarMonth',
nodeData
});
@@ -378,8 +421,8 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
const nodeData: CCCalendarNodeProps = {
...getBaseNodeProps(),
__primarylabel__: 'CalendarYear',
unique_id: id || node.id,
tldraw_snapshot: node.tldraw_snapshot || '',
uuid_string: id || node.id,
node_storage_path: node.node_storage_path || '',
title: node.label,
name: node.label,
calendar_type: 'year',
@@ -392,7 +435,7 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
id: id || node.id,
label: node.label,
title: node.label,
tldraw_snapshot: node.tldraw_snapshot || '',
node_storage_path: node.node_storage_path || '',
type: 'CalendarYear',
nodeData
});
@@ -419,8 +462,8 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
const nodeData: CCUserTeacherTimetableNodeProps = {
...getBaseNodeProps(),
__primarylabel__: 'UserTeacherTimetable',
unique_id: id || node.id,
tldraw_snapshot: node.tldraw_snapshot || '',
uuid_string: id || node.id,
node_storage_path: node.node_storage_path || '',
title: node.label,
school_db_name: workerDbName || '',
school_timetable_id: id || node.id
@@ -430,7 +473,7 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
id: id || node.id,
label: node.label,
title: node.label,
tldraw_snapshot: node.tldraw_snapshot || '',
node_storage_path: node.node_storage_path || '',
type: 'UserTeacherTimetable',
nodeData
});
@@ -456,8 +499,8 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
const nodeData: CCUserTeacherTimetableNodeProps = {
...getBaseNodeProps(),
__primarylabel__: 'UserTeacherTimetable',
unique_id: id || node.id,
tldraw_snapshot: node.tldraw_snapshot || '',
uuid_string: id || node.id,
node_storage_path: node.node_storage_path || '',
title: node.label,
school_db_name: workerDbName || '',
school_timetable_id: id || node.id
@@ -467,7 +510,7 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
id: id || node.id,
label: node.label,
title: node.label,
tldraw_snapshot: node.tldraw_snapshot || '',
node_storage_path: node.node_storage_path || '',
type: 'UserTeacherTimetable',
nodeData
});
@@ -493,8 +536,8 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
const nodeData: CCUserTeacherTimetableNodeProps = {
...getBaseNodeProps(),
__primarylabel__: 'UserTeacherTimetable',
unique_id: id || node.id,
tldraw_snapshot: node.tldraw_snapshot || '',
uuid_string: id || node.id,
node_storage_path: node.node_storage_path || '',
title: node.label,
school_db_name: workerDbName || '',
school_timetable_id: id || node.id
@@ -504,7 +547,7 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
id: id || node.id,
label: node.label,
title: node.label,
tldraw_snapshot: node.tldraw_snapshot || '',
node_storage_path: node.node_storage_path || '',
type: 'UserTeacherTimetable',
nodeData
});
@@ -531,8 +574,8 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
const nodeData: CCUserTeacherTimetableNodeProps = {
...getBaseNodeProps(),
__primarylabel__: 'UserTeacherTimetable',
unique_id: node.id,
tldraw_snapshot: node.tldraw_snapshot || '',
uuid_string: node.id,
node_storage_path: node.node_storage_path || '',
title: node.label,
school_db_name: workerDbName || '',
school_timetable_id: node.id
@@ -542,7 +585,7 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
id: node.id,
label: node.label,
title: node.label,
tldraw_snapshot: node.tldraw_snapshot || '',
node_storage_path: node.node_storage_path || '',
type: 'UserTeacherTimetable',
nodeData
});
@@ -569,8 +612,8 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
const nodeData: CCUserTeacherTimetableNodeProps = {
...getBaseNodeProps(),
__primarylabel__: 'UserTeacherTimetable',
unique_id: node.id,
tldraw_snapshot: node.tldraw_snapshot || '',
uuid_string: node.id,
node_storage_path: node.node_storage_path || '',
title: node.label,
school_db_name: workerDbName || '',
school_timetable_id: node.id
@@ -580,7 +623,7 @@ export const NeoUserProvider: React.FC<{ children: ReactNode }> = ({ children })
id: node.id,
label: node.label,
title: node.label,
tldraw_snapshot: node.tldraw_snapshot || '',
node_storage_path: node.node_storage_path || '',
type: 'UserTeacherTimetable',
nodeData
});