Line data Source code
1 : import { sumBy } from 'es-toolkit'
2 :
3 : import type {
4 : MissingModelCandidate,
5 : MissingModelGroup,
6 : MissingModelViewModel
7 : } from '@/platform/workflowErrors/missingModel/types'
8 :
9 2356 : const UNSUPPORTED = Symbol('unsupported')
10 :
11 1233 : export function countMissingModels(groups: MissingModelGroup[]): number {
12 1233 : return sumBy(groups, (group) => group.models.length)
13 : }
14 :
15 534 : export function groupMissingModelCandidates(
16 : candidates: MissingModelCandidate[] | null | undefined,
17 : isCloud: boolean
18 : ): MissingModelGroup[] {
19 534 : if (!candidates?.length) return []
20 :
21 : type GroupKey = string | null | typeof UNSUPPORTED
22 396 : const map = new Map<
23 : GroupKey,
24 : { candidates: MissingModelCandidate[]; isAssetSupported: boolean }
25 : >()
26 :
27 396 : for (const candidate of candidates) {
28 417 : const groupKey: GroupKey =
29 : candidate.isAssetSupported || !isCloud
30 : ? candidate.directory || null
31 : : UNSUPPORTED
32 417 : const existing = map.get(groupKey)
33 417 : if (existing) {
34 21 : existing.candidates.push(candidate)
35 : } else {
36 396 : map.set(groupKey, {
37 : candidates: [candidate],
38 : isAssetSupported: candidate.isAssetSupported
39 : })
40 : }
41 : }
42 :
43 396 : return Array.from(map.entries())
44 : .sort(([dirA], [dirB]) => {
45 0 : if (dirA === UNSUPPORTED) return 1
46 0 : if (dirB === UNSUPPORTED) return -1
47 0 : if (dirA === null) return 1
48 0 : if (dirB === null) return -1
49 0 : return dirA.localeCompare(dirB)
50 : })
51 : .map(([key, { candidates: groupCandidates, isAssetSupported }]) => ({
52 : directory: typeof key === 'string' ? key : null,
53 : isAssetSupported,
54 : models: groupCandidatesByName(groupCandidates)
55 : }))
56 : }
57 :
58 396 : function groupCandidatesByName(
59 : candidates: MissingModelCandidate[]
60 : ): MissingModelViewModel[] {
61 396 : const map = new Map<string, MissingModelViewModel>()
62 396 : for (const c of candidates) {
63 417 : const existing = map.get(c.name)
64 417 : if (existing) {
65 20 : existing.replacements = existing.replacements?.filter((replacement) =>
66 : c.replacements?.includes(replacement)
67 : )
68 20 : if (c.nodeId) {
69 20 : existing.referencingNodes.push({
70 : nodeId: c.nodeId,
71 : widgetName: c.widgetName
72 : })
73 : }
74 : } else {
75 397 : map.set(c.name, {
76 : name: c.name,
77 : representative: c,
78 : replacements: c.replacements,
79 : referencingNodes: c.nodeId
80 : ? [{ nodeId: c.nodeId, widgetName: c.widgetName }]
81 : : []
82 : })
83 : }
84 : }
85 396 : return Array.from(map.values())
86 : }
|