@powerhousedao/reactor-browser 6.0.0-dev.27 → 6.0.0-dev.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/dist/src/actions/dispatch.js +1 -1
  2. package/dist/src/actions/dispatch.js.map +1 -1
  3. package/dist/src/actions/document.d.ts +16 -4
  4. package/dist/src/actions/document.d.ts.map +1 -1
  5. package/dist/src/actions/document.js +141 -355
  6. package/dist/src/actions/document.js.map +1 -1
  7. package/dist/src/actions/drive.d.ts +1 -1
  8. package/dist/src/actions/drive.d.ts.map +1 -1
  9. package/dist/src/actions/drive.js +61 -208
  10. package/dist/src/actions/drive.js.map +1 -1
  11. package/dist/src/actions/queue.d.ts +2 -2
  12. package/dist/src/actions/queue.d.ts.map +1 -1
  13. package/dist/src/actions/queue.js +9 -38
  14. package/dist/src/actions/queue.js.map +1 -1
  15. package/dist/src/actions/sign.d.ts.map +1 -1
  16. package/dist/src/actions/sign.js +2 -7
  17. package/dist/src/actions/sign.js.map +1 -1
  18. package/dist/src/hooks/add-ph-event-handlers.d.ts.map +1 -1
  19. package/dist/src/hooks/add-ph-event-handlers.js +1 -7
  20. package/dist/src/hooks/add-ph-event-handlers.js.map +1 -1
  21. package/dist/src/hooks/config/connect.d.ts +2 -2
  22. package/dist/src/hooks/connect.d.ts +2 -2
  23. package/dist/src/hooks/connect.d.ts.map +1 -1
  24. package/dist/src/hooks/connect.js +2 -2
  25. package/dist/src/hooks/connect.js.map +1 -1
  26. package/dist/src/hooks/index.d.ts +2 -2
  27. package/dist/src/hooks/index.d.ts.map +1 -1
  28. package/dist/src/hooks/index.js +2 -2
  29. package/dist/src/hooks/index.js.map +1 -1
  30. package/dist/src/hooks/reactor.d.ts +7 -27
  31. package/dist/src/hooks/reactor.d.ts.map +1 -1
  32. package/dist/src/hooks/reactor.js +19 -28
  33. package/dist/src/hooks/reactor.js.map +1 -1
  34. package/dist/src/hooks/use-feature-flags.d.ts +0 -27
  35. package/dist/src/hooks/use-feature-flags.d.ts.map +1 -1
  36. package/dist/src/hooks/use-feature-flags.js +0 -54
  37. package/dist/src/hooks/use-feature-flags.js.map +1 -1
  38. package/dist/src/hooks/use-get-switchboard-link.d.ts.map +1 -1
  39. package/dist/src/hooks/use-get-switchboard-link.js +14 -4
  40. package/dist/src/hooks/use-get-switchboard-link.js.map +1 -1
  41. package/dist/src/hooks/vetra-packages.d.ts +1 -1
  42. package/dist/src/hooks/vetra-packages.d.ts.map +1 -1
  43. package/dist/src/hooks/vetra-packages.js +14 -4
  44. package/dist/src/hooks/vetra-packages.js.map +1 -1
  45. package/dist/src/pglite/drop.d.ts +1 -0
  46. package/dist/src/pglite/drop.d.ts.map +1 -1
  47. package/dist/src/pglite/drop.js +9 -1
  48. package/dist/src/pglite/drop.js.map +1 -1
  49. package/dist/src/pglite/index.d.ts +1 -1
  50. package/dist/src/pglite/index.d.ts.map +1 -1
  51. package/dist/src/pglite/index.js +1 -1
  52. package/dist/src/pglite/index.js.map +1 -1
  53. package/dist/src/reactor.d.ts +2 -3
  54. package/dist/src/reactor.d.ts.map +1 -1
  55. package/dist/src/reactor.js +1 -28
  56. package/dist/src/reactor.js.map +1 -1
  57. package/dist/src/renown/utils.d.ts +1 -2
  58. package/dist/src/renown/utils.d.ts.map +1 -1
  59. package/dist/src/renown/utils.js +8 -9
  60. package/dist/src/renown/utils.js.map +1 -1
  61. package/dist/src/types/global.d.ts +2 -8
  62. package/dist/src/types/global.d.ts.map +1 -1
  63. package/dist/src/utils/drives.d.ts +3 -33
  64. package/dist/src/utils/drives.d.ts.map +1 -1
  65. package/dist/src/utils/drives.js +10 -131
  66. package/dist/src/utils/drives.js.map +1 -1
  67. package/dist/test/drop.test.js +3 -6
  68. package/dist/test/drop.test.js.map +1 -1
  69. package/dist/tsconfig.tsbuildinfo +1 -1
  70. package/package.json +6 -6
  71. package/dist/src/hooks/processor-manager.d.ts +0 -9
  72. package/dist/src/hooks/processor-manager.d.ts.map +0 -1
  73. package/dist/src/hooks/processor-manager.js +0 -9
  74. package/dist/src/hooks/processor-manager.js.map +0 -1
@@ -1,79 +1,45 @@
1
- import { addFile as baseAddFile, addFolder as baseAddFolder, copyNode as baseCopyNode, deleteNode as baseDeleteNode, moveNode as baseMoveNode, updateFile as baseUpdateFile, generateNodesCopy, handleTargetNameCollisions, isFileNode, isFolderNode, logger, updateNode, } from "document-drive";
1
+ import { addFolder as baseAddFolder, copyNode as baseCopyNode, deleteNode as baseDeleteNode, updateFile as baseUpdateFile, generateNodesCopy, handleTargetNameCollisions, isFileNode, isFolderNode, logger, updateNode, } from "document-drive";
2
2
  import { documentModelDocumentType } from "document-model";
3
3
  import { baseLoadFromInput, baseSaveToFileHandle, createPresignedHeader, createZip, generateId, replayDocument, } from "document-model/core";
4
4
  import { UnsupportedDocumentTypeError } from "../errors.js";
5
- import { isLegacyReadEnabledSync, isLegacyWriteEnabledSync, } from "../hooks/use-feature-flags.js";
6
5
  import { isDocumentTypeSupported } from "../utils/documents.js";
7
6
  import { getUserPermissions } from "../utils/user.js";
8
7
  import { queueActions, queueOperations, uploadOperations } from "./queue.js";
9
8
  async function isDocumentInLocation(document, driveId, parentFolder) {
10
- const useLegacy = isLegacyReadEnabledSync();
9
+ const reactorClient = window.ph?.reactorClient;
10
+ if (!reactorClient) {
11
+ return { isDuplicate: false };
12
+ }
13
+ // Get the drive and check its nodes
14
+ let drive;
11
15
  try {
12
- if (useLegacy) {
13
- const reactor = window.ph?.legacyReactor;
14
- if (!reactor) {
15
- throw new Error("Legacy reactor not initialized");
16
- }
17
- // Get the drive and check its nodes
18
- const drive = await reactor.getDrive(driveId);
19
- // Case 1: Check for duplicate by ID
20
- const nodeById = drive.state.global.nodes.find((node) => node.id === document.header.id);
21
- if (nodeById && nodeById.parentFolder === (parentFolder ?? null)) {
22
- return {
23
- isDuplicate: true,
24
- duplicateType: "id",
25
- nodeId: nodeById.id,
26
- };
27
- }
28
- // Case 2: Check for duplicate by name + type in same parent folder
29
- const nodeByNameAndType = drive.state.global.nodes.find((node) => isFileNode(node) &&
30
- node.name === document.header.name &&
31
- node.documentType === document.header.documentType &&
32
- node.parentFolder === (parentFolder ?? null));
33
- if (nodeByNameAndType) {
34
- return {
35
- isDuplicate: true,
36
- duplicateType: "name",
37
- nodeId: nodeByNameAndType.id,
38
- };
39
- }
40
- return { isDuplicate: false };
41
- }
42
- else {
43
- const reactorClient = window.ph?.reactorClient;
44
- if (!reactorClient) {
45
- throw new Error("ReactorClient not initialized");
46
- }
47
- // Get the drive and check its nodes
48
- const drive = await reactorClient.get(driveId);
49
- // Case 1: Check for duplicate by ID
50
- const nodeById = drive.state.global.nodes.find((node) => node.id === document.header.id);
51
- if (nodeById && nodeById.parentFolder === (parentFolder ?? null)) {
52
- return {
53
- isDuplicate: true,
54
- duplicateType: "id",
55
- nodeId: nodeById.id,
56
- };
57
- }
58
- // Case 2: Check for duplicate by name + type in same parent folder
59
- const nodeByNameAndType = drive.state.global.nodes.find((node) => isFileNode(node) &&
60
- node.name === document.header.name &&
61
- node.documentType === document.header.documentType &&
62
- node.parentFolder === (parentFolder ?? null));
63
- if (nodeByNameAndType) {
64
- return {
65
- isDuplicate: true,
66
- duplicateType: "name",
67
- nodeId: nodeByNameAndType.id,
68
- };
69
- }
70
- return { isDuplicate: false };
71
- }
16
+ drive = await reactorClient.get(driveId);
72
17
  }
73
18
  catch {
74
- // Drive doesn't exist or other error
75
19
  return { isDuplicate: false };
76
20
  }
21
+ // Case 1: Check for duplicate by ID
22
+ const nodeById = drive.state.global.nodes.find((node) => node.id === document.header.id);
23
+ if (nodeById && nodeById.parentFolder === (parentFolder ?? null)) {
24
+ return {
25
+ isDuplicate: true,
26
+ duplicateType: "id",
27
+ nodeId: nodeById.id,
28
+ };
29
+ }
30
+ // Case 2: Check for duplicate by name + type in same parent folder
31
+ const nodeByNameAndType = drive.state.global.nodes.find((node) => isFileNode(node) &&
32
+ node.name === document.header.name &&
33
+ node.documentType === document.header.documentType &&
34
+ node.parentFolder === (parentFolder ?? null));
35
+ if (nodeByNameAndType) {
36
+ return {
37
+ isDuplicate: true,
38
+ duplicateType: "name",
39
+ nodeId: nodeByNameAndType.id,
40
+ };
41
+ }
42
+ return { isDuplicate: false };
77
43
  }
78
44
  function getDocumentTypeIcon(document) {
79
45
  const documentType = document.header.documentType;
@@ -126,41 +92,20 @@ async function getDocumentExtension(document) {
126
92
  if (documentType === documentModelDocumentType) {
127
93
  return "phdm";
128
94
  }
129
- // For document instances, look up the module's extension
130
- const useLegacy = isLegacyReadEnabledSync();
131
95
  let rawExtension;
132
- if (useLegacy) {
133
- const reactor = window.ph?.legacyReactor;
134
- if (reactor) {
135
- const documentModelModules = reactor.getDocumentModelModules();
136
- const module = documentModelModules.find((m) => m.documentModel.global.id === documentType);
137
- rawExtension = module?.utils.fileExtension;
138
- }
139
- }
140
- else {
141
- const reactorClient = window.ph?.reactorClient;
142
- if (reactorClient) {
143
- const { results: documentModelModules } = await reactorClient.getDocumentModelModules();
144
- const module = documentModelModules.find((m) => m.documentModel.global.id === documentType);
145
- rawExtension = module?.utils.fileExtension;
146
- }
96
+ const reactorClient = window.ph?.reactorClient;
97
+ if (reactorClient) {
98
+ const { results: documentModelModules } = await reactorClient.getDocumentModelModules();
99
+ const module = documentModelModules.find((m) => m.documentModel.global.id === documentType);
100
+ rawExtension = module?.utils.fileExtension;
147
101
  }
148
102
  // Clean the extension (remove leading/trailing dots) and fallback to "phdm"
149
103
  const cleanExtension = (rawExtension ?? "phdm").replace(/^\.+|\.+$/g, "");
150
104
  return cleanExtension || "phdm";
151
105
  }
152
106
  export async function exportFile(document, suggestedName) {
153
- // Ensure we have either reactor available for consistency
154
- const useLegacy = isLegacyReadEnabledSync();
155
- if (useLegacy) {
156
- if (!window.ph?.legacyReactor) {
157
- throw new Error("Legacy reactor not initialized");
158
- }
159
- }
160
- else {
161
- if (!window.ph?.reactorClient) {
162
- throw new Error("ReactorClient not initialized");
163
- }
107
+ if (!window.ph?.reactorClient) {
108
+ throw new Error("ReactorClient not initialized");
164
109
  }
165
110
  // Get the extension from the document model module
166
111
  const extension = await getDocumentExtension(document);
@@ -184,101 +129,51 @@ export async function exportFile(document, suggestedName) {
184
129
  }
185
130
  }
186
131
  export async function loadFile(path) {
187
- const useLegacy = isLegacyReadEnabledSync();
188
132
  const baseDocument = await baseLoadFromInput(path, (state) => state, { checkHashes: true });
189
- if (useLegacy) {
190
- const reactor = window.ph?.legacyReactor;
191
- if (!reactor) {
192
- throw new Error("Legacy reactor not initialized");
193
- }
194
- const documentModelModules = reactor.getDocumentModelModules();
195
- const documentModelModule = documentModelModules.find((module) => module.documentModel.global.id === baseDocument.header.documentType);
196
- if (!documentModelModule) {
197
- throw new Error(`Document "${baseDocument.header.documentType}" is not supported`);
198
- }
199
- return documentModelModule.utils.loadFromInput(path);
133
+ const reactorClient = window.ph?.reactorClient;
134
+ if (!reactorClient) {
135
+ throw new Error("ReactorClient not initialized");
200
136
  }
201
- else {
202
- const reactorClient = window.ph?.reactorClient;
203
- if (!reactorClient) {
204
- throw new Error("ReactorClient not initialized");
205
- }
206
- const { results: documentModelModules } = await reactorClient.getDocumentModelModules();
207
- const documentModelModule = documentModelModules.find((module) => module.documentModel.global.id === baseDocument.header.documentType);
208
- if (!documentModelModule) {
209
- throw new Error(`Document "${baseDocument.header.documentType}" is not supported`);
210
- }
211
- return documentModelModule.utils.loadFromInput(path);
137
+ const { results: documentModelModules } = await reactorClient.getDocumentModelModules();
138
+ const documentModelModule = documentModelModules.find((module) => module.documentModel.global.id === baseDocument.header.documentType);
139
+ if (!documentModelModule) {
140
+ throw new Error(`Document "${baseDocument.header.documentType}" is not supported`);
212
141
  }
142
+ return documentModelModule.utils.loadFromInput(path);
213
143
  }
214
144
  export async function addDocument(driveId, name, documentType, parentFolder, document, id, preferredEditor) {
215
145
  const { isAllowedToCreateDocuments } = getUserPermissions();
216
146
  if (!isAllowedToCreateDocuments) {
217
147
  throw new Error("User is not allowed to create documents");
218
148
  }
219
- const useLegacy = isLegacyWriteEnabledSync();
220
- if (useLegacy) {
221
- const reactor = window.ph?.legacyReactor;
222
- if (!reactor) {
223
- throw new Error("Legacy reactor not initialized");
224
- }
225
- const drive = await reactor.getDrive(driveId);
226
- const documentId = id ?? generateId();
227
- const reactorDocumentModelModules = reactor.getDocumentModelModules();
228
- const documentModelModuleFromReactor = reactorDocumentModelModules.find((module) => module.documentModel.global.id === documentType);
229
- if (!documentModelModuleFromReactor) {
230
- throw new Error(`Document model module for type ${documentType} not found`);
231
- }
232
- const newDocument = documentModelModuleFromReactor.utils.createDocument({
233
- ...document?.state,
234
- });
235
- newDocument.header = createPresignedHeader(documentId, documentType);
236
- newDocument.header.name = name;
237
- const documentMeta = preferredEditor ? { preferredEditor } : undefined;
238
- await reactor.addDocument(newDocument, documentMeta);
239
- const action = baseAddFile({
240
- id: documentId,
241
- name,
242
- documentType,
243
- parentFolder: parentFolder ?? null,
244
- });
245
- const unsafeCastAsDrive = (await queueActions(drive, action));
246
- const node = unsafeCastAsDrive.state.global.nodes.find((node) => node.id === documentId);
247
- if (!node || !isFileNode(node)) {
248
- throw new Error("There was an error adding document");
249
- }
250
- return node;
149
+ const reactorClient = window.ph?.reactorClient;
150
+ if (!reactorClient) {
151
+ throw new Error("ReactorClient not initialized");
251
152
  }
252
- else {
253
- const reactorClient = window.ph?.reactorClient;
254
- if (!reactorClient) {
255
- throw new Error("ReactorClient not initialized");
256
- }
257
- // get the module
258
- const documentModelModule = await reactorClient.getDocumentModelModule(documentType);
259
- // create - use passed document's state if available
260
- const newDocument = documentModelModule.utils.createDocument({
261
- ...document?.state,
262
- });
263
- newDocument.header.name = name;
264
- // Create document using ReactorClient
265
- let newDoc;
266
- try {
267
- newDoc = await reactorClient.createDocumentInDrive(driveId, newDocument, parentFolder);
268
- }
269
- catch (e) {
270
- logger.error("Error adding document", e);
271
- throw new Error("There was an error adding document");
272
- }
273
- // Return a file node structure for compatibility
274
- return {
275
- id: newDoc.header.id,
276
- name: newDoc.header.name,
277
- documentType,
278
- parentFolder: parentFolder ?? null,
279
- kind: "file",
280
- };
153
+ // get the module
154
+ const documentModelModule = await reactorClient.getDocumentModelModule(documentType);
155
+ // create - use passed document's state if available
156
+ const newDocument = documentModelModule.utils.createDocument({
157
+ ...document?.state,
158
+ });
159
+ newDocument.header.name = name;
160
+ // Create document using ReactorClient
161
+ let newDoc;
162
+ try {
163
+ newDoc = await reactorClient.createDocumentInDrive(driveId, newDocument, parentFolder);
281
164
  }
165
+ catch (e) {
166
+ logger.error("Error adding document", e);
167
+ throw new Error("There was an error adding document");
168
+ }
169
+ // Return a file node structure for compatibility
170
+ return {
171
+ id: newDoc.header.id,
172
+ name: newDoc.header.name,
173
+ documentType,
174
+ parentFolder: parentFolder ?? null,
175
+ kind: "file",
176
+ };
282
177
  }
283
178
  export async function addFile(file, driveId, name, parentFolder) {
284
179
  logger.verbose(`addFile(drive: ${driveId}, name: ${name}, folder: ${parentFolder})`);
@@ -286,40 +181,18 @@ export async function addFile(file, driveId, name, parentFolder) {
286
181
  if (!isAllowedToCreateDocuments) {
287
182
  throw new Error("User is not allowed to create files");
288
183
  }
289
- const useLegacy = isLegacyWriteEnabledSync();
290
184
  const document = await loadFile(file);
291
185
  let duplicateId = false;
292
- if (useLegacy) {
293
- const reactor = window.ph?.legacyReactor;
294
- if (!reactor) {
295
- throw new Error("Legacy reactor not initialized");
296
- }
297
- const documentModule = reactor
298
- .getDocumentModelModules()
299
- .find((module) => module.documentModel.global.id === document.header.documentType);
300
- if (!documentModule) {
301
- throw new Error(`Document model module for type ${document.header.documentType} not found`);
302
- }
303
- try {
304
- await reactor.getDocument(document.header.id);
305
- duplicateId = true;
306
- }
307
- catch {
308
- // document id not found
309
- }
186
+ const reactorClient = window.ph?.reactorClient;
187
+ if (!reactorClient) {
188
+ throw new Error("ReactorClient not initialized");
310
189
  }
311
- else {
312
- const reactorClient = window.ph?.reactorClient;
313
- if (!reactorClient) {
314
- throw new Error("ReactorClient not initialized");
315
- }
316
- try {
317
- await reactorClient.get(document.header.id);
318
- duplicateId = true;
319
- }
320
- catch {
321
- // document id not found
322
- }
190
+ try {
191
+ await reactorClient.get(document.header.id);
192
+ duplicateId = true;
193
+ }
194
+ catch {
195
+ // document id not found
323
196
  }
324
197
  const documentId = duplicateId ? generateId() : document.header.id;
325
198
  const header = createPresignedHeader(documentId, document.header.documentType);
@@ -344,7 +217,7 @@ export async function addFile(file, driveId, name, parentFolder) {
344
217
  }
345
218
  export async function addFileWithProgress(file, driveId, name, parentFolder, onProgress, documentTypes, resolveConflict) {
346
219
  logger.verbose(`addFileWithProgress(drive: ${driveId}, name: ${name}, folder: ${parentFolder})`);
347
- const reactor = window.ph?.legacyReactor;
220
+ const reactor = window.ph?.reactorClient;
348
221
  if (!reactor) {
349
222
  return;
350
223
  }
@@ -356,9 +229,6 @@ export async function addFileWithProgress(file, driveId, name, parentFolder, onP
356
229
  try {
357
230
  onProgress?.({ stage: "loading", progress: 0 });
358
231
  const document = await loadFile(file);
359
- if (!document) {
360
- throw new Error("No document loaded");
361
- }
362
232
  // Check for duplicate in same location
363
233
  const duplicateCheck = await isDocumentInLocation(document, driveId, parentFolder);
364
234
  if (duplicateCheck.isDuplicate && !resolveConflict) {
@@ -394,17 +264,13 @@ export async function addFileWithProgress(file, driveId, name, parentFolder, onP
394
264
  });
395
265
  throw new UnsupportedDocumentTypeError(document.header.documentType);
396
266
  }
397
- const documentModule = reactor
398
- .getDocumentModelModules()
399
- .find((module) => module.documentModel.global.id === document.header.documentType);
400
- if (!documentModule) {
401
- throw new Error(`Document model module for type ${document.header.documentType} not found`);
402
- }
267
+ // ensure we have the module + can load it (throws if not found)
268
+ await reactor.getDocumentModelModule(document.header.documentType);
403
269
  // Initializing stage (10-20%)
404
270
  onProgress?.({ stage: "initializing", progress: 10 });
405
271
  let duplicateId = false;
406
272
  try {
407
- await reactor.getDocument(document.header.id);
273
+ await reactor.get(document.header.id);
408
274
  duplicateId = true;
409
275
  }
410
276
  catch {
@@ -466,7 +332,7 @@ export async function addFileWithProgress(file, driveId, name, parentFolder, onP
466
332
  }
467
333
  }
468
334
  export async function updateFile(driveId, nodeId, documentType, name, parentFolder) {
469
- const reactor = window.ph?.legacyReactor;
335
+ const reactor = window.ph?.reactorClient;
470
336
  if (!reactor) {
471
337
  return;
472
338
  }
@@ -474,7 +340,7 @@ export async function updateFile(driveId, nodeId, documentType, name, parentFold
474
340
  if (!isAllowedToCreateDocuments) {
475
341
  throw new Error("User is not allowed to update files");
476
342
  }
477
- const drive = await reactor.getDrive(driveId);
343
+ const drive = await reactor.get(driveId);
478
344
  const unsafeCastAsDrive = (await queueActions(drive, baseUpdateFile({
479
345
  id: nodeId,
480
346
  name: name ?? undefined,
@@ -492,174 +358,94 @@ export async function addFolder(driveId, name, parentFolder) {
492
358
  if (!isAllowedToCreateDocuments) {
493
359
  throw new Error("User is not allowed to create folders");
494
360
  }
495
- const useLegacy = isLegacyWriteEnabledSync();
496
- if (useLegacy) {
497
- const reactor = window.ph?.legacyReactor;
498
- if (!reactor) {
499
- throw new Error("Legacy reactor not initialized");
500
- }
501
- const folderId = generateId();
502
- const drive = await reactor.getDrive(driveId);
503
- const unsafeCastAsDrive = (await queueActions(drive, baseAddFolder({
361
+ const reactorClient = window.ph?.reactorClient;
362
+ if (!reactorClient) {
363
+ throw new Error("ReactorClient not initialized");
364
+ }
365
+ // Get the drive document and add folder action
366
+ const drive = await reactorClient.get(driveId);
367
+ const folderId = generateId();
368
+ const updatedDrive = await reactorClient.execute(driveId, "main", [
369
+ baseAddFolder({
504
370
  id: folderId,
505
371
  name,
506
372
  parentFolder,
507
- })));
508
- const node = unsafeCastAsDrive.state.global.nodes.find((node) => node.id === folderId);
509
- if (!node || !isFolderNode(node)) {
510
- throw new Error("There was an error adding folder");
511
- }
512
- return node;
513
- }
514
- else {
515
- const reactorClient = window.ph?.reactorClient;
516
- if (!reactorClient) {
517
- throw new Error("ReactorClient not initialized");
518
- }
519
- // Get the drive document and add folder action
520
- const drive = await reactorClient.get(driveId);
521
- const folderId = generateId();
522
- const updatedDrive = await reactorClient.execute(driveId, "main", [
523
- baseAddFolder({
524
- id: folderId,
525
- name,
526
- parentFolder,
527
- }),
528
- ]);
529
- const node = updatedDrive.state.global.nodes.find((node) => node.id === folderId);
530
- if (!node || !isFolderNode(node)) {
531
- throw new Error("There was an error adding folder");
532
- }
533
- return node;
373
+ }),
374
+ ]);
375
+ const node = updatedDrive.state.global.nodes.find((node) => node.id === folderId);
376
+ if (!node || !isFolderNode(node)) {
377
+ throw new Error("There was an error adding folder");
534
378
  }
379
+ return node;
535
380
  }
536
381
  export async function deleteNode(driveId, nodeId) {
537
382
  const { isAllowedToCreateDocuments } = getUserPermissions();
538
383
  if (!isAllowedToCreateDocuments) {
539
384
  throw new Error("User is not allowed to delete documents");
540
385
  }
541
- const useLegacy = isLegacyWriteEnabledSync();
542
- if (useLegacy) {
543
- const reactor = window.ph?.legacyReactor;
544
- if (!reactor) {
545
- throw new Error("Legacy reactor not initialized");
546
- }
547
- const drive = await reactor.getDrive(driveId);
548
- await queueActions(drive, baseDeleteNode({ id: nodeId }));
549
- // now delete the document
550
- await reactor.deleteDocument(nodeId);
551
- }
552
- else {
553
- const reactorClient = window.ph?.reactorClient;
554
- if (!reactorClient) {
555
- throw new Error("ReactorClient not initialized");
556
- }
557
- // delete the node in the drive document
558
- await reactorClient.execute(driveId, "main", [
559
- baseDeleteNode({ id: nodeId }),
560
- ]);
561
- // now delete the document
562
- await reactorClient.deleteDocument(nodeId);
386
+ const reactorClient = window.ph?.reactorClient;
387
+ if (!reactorClient) {
388
+ throw new Error("ReactorClient not initialized");
563
389
  }
390
+ // delete the node in the drive document
391
+ await reactorClient.execute(driveId, "main", [
392
+ baseDeleteNode({ id: nodeId }),
393
+ ]);
394
+ // now delete the document
395
+ await reactorClient.deleteDocument(nodeId);
564
396
  }
565
397
  export async function renameNode(driveId, nodeId, name) {
566
398
  const { isAllowedToCreateDocuments } = getUserPermissions();
567
399
  if (!isAllowedToCreateDocuments) {
568
400
  throw new Error("User is not allowed to rename documents");
569
401
  }
570
- const useLegacy = isLegacyWriteEnabledSync();
571
- if (useLegacy) {
572
- const reactor = window.ph?.legacyReactor;
573
- if (!reactor) {
574
- throw new Error("Legacy reactor not initialized");
575
- }
576
- const drive = await reactor.getDrive(driveId);
577
- const unsafeCastAsDrive = (await queueActions(drive, updateNode({ id: nodeId, name })));
578
- const node = unsafeCastAsDrive.state.global.nodes.find((node) => node.id === nodeId);
579
- if (!node) {
580
- throw new Error("There was an error renaming node");
581
- }
582
- return node;
402
+ const reactorClient = window.ph?.reactorClient;
403
+ if (!reactorClient) {
404
+ throw new Error("ReactorClient not initialized");
583
405
  }
584
- else {
585
- const reactorClient = window.ph?.reactorClient;
586
- if (!reactorClient) {
587
- throw new Error("ReactorClient not initialized");
588
- }
589
- // Rename the node in the drive document using updateNode action
590
- const drive = await reactorClient.execute(driveId, "main", [updateNode({ id: nodeId, name })]);
591
- const node = drive.state.global.nodes.find((n) => n.id === nodeId);
592
- if (!node) {
593
- throw new Error("There was an error renaming node");
594
- }
595
- return node;
406
+ // Rename the node in the drive document using updateNode action
407
+ const drive = await reactorClient.execute(driveId, "main", [updateNode({ id: nodeId, name })]);
408
+ const node = drive.state.global.nodes.find((n) => n.id === nodeId);
409
+ if (!node) {
410
+ throw new Error("There was an error renaming node");
596
411
  }
412
+ return node;
597
413
  }
598
414
  export async function renameDriveNode(driveId, nodeId, name) {
599
415
  const { isAllowedToCreateDocuments } = getUserPermissions();
600
416
  if (!isAllowedToCreateDocuments) {
601
417
  throw new Error("User is not allowed to rename documents");
602
418
  }
603
- const useLegacy = isLegacyWriteEnabledSync();
604
- if (useLegacy) {
605
- const reactor = window.ph?.legacyReactor;
606
- if (!reactor) {
607
- throw new Error("Legacy reactor not initialized");
608
- }
609
- const drive = await reactor.getDrive(driveId);
610
- const updatedDrive = (await queueActions(drive, updateNode({ id: nodeId, name })));
611
- return updatedDrive.state.global.nodes.find((n) => n.id === nodeId);
612
- }
613
- else {
614
- const reactorClient = window.ph?.reactorClient;
615
- if (!reactorClient) {
616
- throw new Error("ReactorClient not initialized");
617
- }
618
- await reactorClient.execute(driveId, "main", [
619
- updateNode({ id: nodeId, name }),
620
- ]);
621
- const drive = await reactorClient.get(driveId);
622
- return drive.state.global.nodes.find((n) => n.id === nodeId);
419
+ const reactorClient = window.ph?.reactorClient;
420
+ if (!reactorClient) {
421
+ throw new Error("ReactorClient not initialized");
623
422
  }
423
+ await reactorClient.execute(driveId, "main", [
424
+ updateNode({ id: nodeId, name }),
425
+ ]);
426
+ const drive = await reactorClient.get(driveId);
427
+ return drive.state.global.nodes.find((n) => n.id === nodeId);
624
428
  }
625
429
  export async function moveNode(driveId, src, target) {
626
430
  const { isAllowedToCreateDocuments } = getUserPermissions();
627
431
  if (!isAllowedToCreateDocuments) {
628
432
  throw new Error("User is not allowed to move documents");
629
433
  }
630
- const useLegacy = isLegacyWriteEnabledSync();
631
- if (useLegacy) {
632
- const reactor = window.ph?.legacyReactor;
633
- if (!reactor) {
634
- throw new Error("Legacy reactor not initialized");
635
- }
636
- const drive = await reactor.getDrive(driveId);
637
- return await queueActions(drive, baseMoveNode({ srcFolder: src.id, targetParentFolder: target?.id }));
638
- }
639
- else {
640
- const reactorClient = window.ph?.reactorClient;
641
- if (!reactorClient) {
642
- throw new Error("ReactorClient not initialized");
643
- }
644
- // Get current parent folder from source node
645
- const sourceParent = src.parentFolder ?? driveId;
646
- const targetParent = target?.id ?? driveId;
647
- return await reactorClient.moveChildren(sourceParent, targetParent, [
648
- src.id,
649
- ]);
434
+ const reactorClient = window.ph?.reactorClient;
435
+ if (!reactorClient) {
436
+ throw new Error("ReactorClient not initialized");
650
437
  }
438
+ // Get current parent folder from source node
439
+ const sourceParent = src.parentFolder ?? driveId;
440
+ const targetParent = target?.id ?? driveId;
441
+ return await reactorClient.moveChildren(sourceParent, targetParent, [src.id]);
651
442
  }
652
- function _duplicateDocument(reactor, document, newId = generateId()) {
653
- const documentModule = reactor
654
- .getDocumentModelModules()
655
- .find((module) => module.documentModel.global.id === document.header.documentType);
656
- if (!documentModule) {
657
- throw new Error(`Document model module for type ${document.header.documentType} not found`);
658
- }
443
+ async function _duplicateDocument(reactor, document, newId = generateId()) {
444
+ const documentModule = await reactor.getDocumentModelModule(document.header.documentType);
659
445
  return replayDocument(document.initialState, document.operations, documentModule.reducer, createPresignedHeader(newId, document.header.documentType));
660
446
  }
661
447
  export async function copyNode(driveId, src, target) {
662
- const reactor = window.ph?.legacyReactor;
448
+ const reactor = window.ph?.reactorClient;
663
449
  if (!reactor) {
664
450
  return;
665
451
  }
@@ -667,7 +453,7 @@ export async function copyNode(driveId, src, target) {
667
453
  if (!isAllowedToCreateDocuments) {
668
454
  throw new Error("User is not allowed to copy documents");
669
455
  }
670
- const drive = await reactor.getDrive(driveId);
456
+ const drive = await reactor.get(driveId);
671
457
  const copyNodesInput = generateNodesCopy({
672
458
  srcId: src.id,
673
459
  targetParentFolder: target?.id,
@@ -692,14 +478,14 @@ export async function copyNode(driveId, src, target) {
692
478
  });
693
479
  for (const fileNodeToCopy of fileNodesToCopy) {
694
480
  try {
695
- const document = await reactor.getDocument(fileNodeToCopy.srcId);
696
- const duplicatedDocument = _duplicateDocument(reactor, document, fileNodeToCopy.targetId);
481
+ const document = await reactor.get(fileNodeToCopy.srcId);
482
+ const duplicatedDocument = await _duplicateDocument(reactor, document, fileNodeToCopy.targetId);
697
483
  // Set the header name to match the collision-resolved node name
698
484
  const resolvedName = resolvedNamesMap.get(fileNodeToCopy.targetId);
699
485
  if (resolvedName) {
700
486
  duplicatedDocument.header.name = resolvedName;
701
487
  }
702
- await reactor.addDocument(duplicatedDocument);
488
+ await reactor.createDocumentInDrive(driveId, duplicatedDocument, target?.id);
703
489
  }
704
490
  catch (e) {
705
491
  logger.error(`Error copying document ${fileNodeToCopy.srcId}: ${String(e)}`);