forked from Guepard-Corp/qwery-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace-provider.tsx
More file actions
151 lines (125 loc) · 4.07 KB
/
workspace-provider.tsx
File metadata and controls
151 lines (125 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use client';
import React, { useEffect, useMemo, useState } from 'react';
import { v4 as uuidv4 } from 'uuid';
import type { Workspace } from '@qwery/domain/entities';
import type { Repositories } from '@qwery/domain/repositories';
import { LoadingOverlay } from '@qwery/ui/loading-overlay';
import { Trans } from '@qwery/ui/trans';
import { WorkspaceContext } from '~/lib/context/workspace-context';
import { useWorkspaceMode } from '~/lib/hooks/use-workspace-mode';
import { createRepositories } from '~/lib/repositories/repositories-factory';
import { WorkspaceService } from '~/lib/services/workspace-service';
import {
getWorkspaceFromLocalStorage,
setWorkspaceInLocalStorage,
} from '~/lib/workspace/workspace-helper';
const STORAGE_KEYS: (keyof Workspace)[] = [
'id',
'userId',
'username',
'organizationId',
'projectId',
'isAnonymous',
'mode',
];
function workspaceStorageEqual(a: Workspace, b: Workspace): boolean {
return STORAGE_KEYS.every((k) => a[k] === b[k]);
}
export function WorkspaceProvider(props: React.PropsWithChildren) {
const [localWorkspace, setLocalWorkspace] = useState<Workspace>(
getWorkspaceFromLocalStorage(),
);
const [workspace, setWorkspace] = useState<Workspace | null>(null);
useEffect(() => {
const handleStorageChange = () => {
const updated = getWorkspaceFromLocalStorage();
setLocalWorkspace((prev) =>
workspaceStorageEqual(prev, updated) ? prev : updated,
);
setWorkspace((prev) => {
const next = prev ? { ...prev, ...updated } : null;
if (!prev || !next) return next;
return workspaceStorageEqual(prev, next) ? prev : next;
});
};
window.addEventListener('storage', handleStorageChange);
window.addEventListener('workspace-updated', handleStorageChange);
return () => {
window.removeEventListener('storage', handleStorageChange);
window.removeEventListener('workspace-updated', handleStorageChange);
};
}, []);
const workspaceQuery = useWorkspaceMode(localWorkspace);
const [repositories, setRepositories] = useState<Repositories | null>(null);
const [isInitializing, setIsInitializing] = useState(true);
useEffect(() => {
if (!workspaceQuery.data) {
return;
}
let cancelled = false;
createRepositories().then((repos) => {
if (!cancelled) {
setRepositories(repos);
}
});
return () => {
cancelled = true;
};
}, [workspaceQuery.data]);
useEffect(() => {
if (!workspaceQuery.data) {
return;
}
const initWorkspace = async () => {
setIsInitializing(true);
try {
const workspaceService = new WorkspaceService();
const runtime = await workspaceService.execute();
const currentStored = getWorkspaceFromLocalStorage();
const nextUserId = currentStored.userId || uuidv4();
const nextWorkspaceId = currentStored.id || uuidv4();
const workspaceData: Workspace = {
id: nextWorkspaceId,
userId: nextUserId,
username: currentStored.username,
organizationId: currentStored.organizationId,
projectId: currentStored.projectId,
isAnonymous: currentStored.isAnonymous,
mode: currentStored.mode as Workspace['mode'],
runtime: runtime,
};
setWorkspaceInLocalStorage(workspaceData);
setWorkspace(workspaceData);
} finally {
setIsInitializing(false);
}
};
initWorkspace();
}, [workspaceQuery.data]);
const contextValue = useMemo(() => {
if (!repositories || !workspace) {
return null;
}
return {
repositories,
workspace,
};
}, [repositories, workspace]);
const isLoading =
workspaceQuery.isLoading || !repositories || isInitializing || !workspace;
if (isLoading) {
return (
<LoadingOverlay fullPage>
<Trans i18nKey="common:initializing" />
</LoadingOverlay>
);
}
if (!contextValue) {
return null;
}
return (
<WorkspaceContext.Provider value={contextValue}>
{props.children}
</WorkspaceContext.Provider>
);
}