@startsimpli/integrations 0.2.101 → 0.2.103

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.
@@ -1,10 +1,11 @@
1
1
  /**
2
- * One to-do, read directly (one entity read, its project edge, and the source
3
- * documents its mentions point at) - never by scanning the list. Mark done /
2
+ * One to-do, read directly (the entity and its project edge together, then the
3
+ * project and ALL source documents together) - never by scanning the list and
4
+ * never one read per mention. Mark done /
4
5
  * reopen is optimistic and rolls back with the server's reason.
5
6
  */
6
- import type { Commitment, CommitmentStatus } from '../../corpus/types';
7
- import { toCommitment } from '../../corpus/entities';
7
+ import type { Commitment, CommitmentStatus, SourceDocument } from '../../corpus/types';
8
+ import { DEFAULT_TYPE_KEYS, toCommitment, toSourceDocument } from '../../corpus/entities';
8
9
  import type { NativeCorpusApi } from '../corpus/api';
9
10
  import { statusText } from './format';
10
11
 
@@ -37,7 +38,7 @@ export interface TrackerDetailController {
37
38
  }
38
39
 
39
40
  export interface TrackerDetailOptions {
40
- api: Pick<NativeCorpusApi, 'entities' | 'corpus' | 'relationships' | 'commitments' | 'scope'>;
41
+ api: Pick<NativeCorpusApi, 'entities' | 'relationships' | 'commitments' | 'scope'>;
41
42
  id: string;
42
43
  /** Called after a status write succeeds, so the list can refresh. */
43
44
  onChanged?: () => void;
@@ -56,23 +57,35 @@ export function createTrackerDetailController({ api, id, onChanged }: TrackerDet
56
57
  async load() {
57
58
  set({ loading: true, error: null });
58
59
  try {
59
- const item = toCommitment(await api.entities.get(id), api.scope);
60
- const edge = await api.relationships.list({ relType: 'in_focus', source: id, pageSize: 1 }).catch(() => null);
60
+ // Round 1: the to-do and its project edge are independent. Round 2: the
61
+ // project's name and ALL source documents (one `id__in` read, not one
62
+ // read per mention).
63
+ const [entity, edge] = await Promise.all([
64
+ api.entities.get(id),
65
+ api.relationships.list({ relType: 'in_focus', source: id, pageSize: 1 }).catch(() => null),
66
+ ]);
67
+ const item = toCommitment(entity, api.scope);
61
68
  const focusId = edge?.items[0]?.target;
62
- const [project, sources] = await Promise.all([
69
+ const documentIds = [...new Set(item.mentions.map((m) => m.documentId))];
70
+ const [project, documents] = await Promise.all([
63
71
  focusId ? api.entities.get(String(focusId)).then((f) => f.name || null).catch(() => null) : Promise.resolve(null),
64
- Promise.all(item.mentions.map(async (m): Promise<TrackerSource> => {
65
- const doc = await api.corpus.getDocument?.(m.documentId).catch(() => null);
66
- return {
67
- documentId: m.documentId,
68
- title: doc?.title || item.sourceLabel || 'Source',
69
- quote: m.quote,
70
- occurredAt: m.occurredAt ?? doc?.occurredAt ?? null,
71
- ...(m.section ? { section: m.section } : {}),
72
- isVoiceNote: (doc?.source ?? item.source) === 'voice',
73
- };
74
- })),
72
+ documentIds.length
73
+ ? api.entities.list({ type: DEFAULT_TYPE_KEYS.document, filters: { idIn: documentIds }, pageSize: documentIds.length })
74
+ .then((page) => new Map(page.items.map((e) => [String(e.id), toSourceDocument(e, api.scope)])))
75
+ .catch(() => new Map<string, SourceDocument>())
76
+ : Promise.resolve(new Map<string, SourceDocument>()),
75
77
  ]);
78
+ const sources = item.mentions.map((m): TrackerSource => {
79
+ const doc = documents.get(m.documentId);
80
+ return {
81
+ documentId: m.documentId,
82
+ title: doc?.title || item.sourceLabel || 'Source',
83
+ quote: m.quote,
84
+ occurredAt: m.occurredAt ?? doc?.occurredAt ?? null,
85
+ ...(m.section ? { section: m.section } : {}),
86
+ isVoiceNote: (doc?.source ?? item.source) === 'voice',
87
+ };
88
+ });
76
89
  set({ loading: false, item, projectTitle: project, sources });
77
90
  } catch (e) {
78
91
  set({ loading: false, item: null, error: (e as { status?: number })?.status === 404 ? 'This to-do is no longer available.' : msg(e) });