-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathuseCompareReplacements.ts
More file actions
115 lines (97 loc) · 3.33 KB
/
useCompareReplacements.ts
File metadata and controls
115 lines (97 loc) · 3.33 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
import type { ModuleReplacement, ModuleReplacementMapping } from 'module-replacements'
export interface ReplacementSuggestion {
forPackage: string
replacement: ModuleReplacement
}
/**
* Replacement types that suggest "no dependency" (can be replaced with native code or inline).
*/
const NO_DEP_REPLACEMENT_TYPES = [
'native',
'simple',
'removal',
] as const satisfies ModuleReplacement['type'][]
/**
* Replacement types that are informational only.
* These suggest alternative packages exist but don't fit the "no dependency" pattern.
*/
const INFO_REPLACEMENT_TYPES = ['documented'] as const satisfies ModuleReplacement['type'][]
/**
* Composable for fetching module replacement suggestions for packages in comparison.
* Returns replacements split into "no dep" (actionable) and informational categories.
*/
export function useCompareReplacements(packageNames: MaybeRefOrGetter<string[]>) {
const packages = computed(() => toValue(packageNames))
// Cache replacement data by package name
const replacements = shallowRef(new Map<string, ModuleReplacement | null>())
const loading = shallowRef(false)
// Fetch replacements for all packages
async function fetchReplacements(names: string[]) {
if (names.length === 0) return
// Filter out packages we've already checked
const namesToCheck = names.filter(name => !replacements.value.has(name))
if (namesToCheck.length === 0) return
loading.value = true
try {
const results = await Promise.all(
namesToCheck.map(async name => {
try {
const result = await $fetch<{
mapping: ModuleReplacementMapping
replacement: ModuleReplacement
} | null>(`/api/replacements/${name}`)
return { name, replacement: result?.replacement ?? null, failed: false as const }
} catch {
return { name, failed: true as const }
}
}),
)
const newReplacements = new Map(replacements.value)
for (const result of results) {
if (result.failed) continue
newReplacements.set(result.name, result.replacement)
}
replacements.value = newReplacements
} finally {
loading.value = false
}
}
// Watch for package changes and fetch replacements
if (import.meta.client) {
watch(
packages,
newPackages => {
fetchReplacements(newPackages)
},
{ immediate: true },
)
}
// Build suggestions from replacements
const allSuggestions = computed(() => {
const result: ReplacementSuggestion[] = []
for (const pkg of packages.value) {
const replacement = replacements.value.get(pkg)
if (!replacement) continue
result.push({ forPackage: pkg, replacement })
}
return result
})
// Suggestions that prompt adding the "no dep" column (native, simple)
const noDepSuggestions = computed(() =>
allSuggestions.value.filter(s =>
(NO_DEP_REPLACEMENT_TYPES as readonly string[]).includes(s.replacement.type),
),
)
// Informational suggestions that don't prompt "no dep" (documented)
const infoSuggestions = computed(() =>
allSuggestions.value.filter(s =>
(INFO_REPLACEMENT_TYPES as readonly string[]).includes(s.replacement.type),
),
)
return {
replacements,
noDepSuggestions,
infoSuggestions,
loading: readonly(loading),
}
}