Skip to content

Commit 88f2cc7

Browse files
[Manager] Refactor search result types (#4154)
1 parent 7907e20 commit 88f2cc7

File tree

6 files changed

+108
-85
lines changed

6 files changed

+108
-85
lines changed

src/components/dialog/content/manager/packCard/PackCard.vue

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,15 @@ import PackBanner from '@/components/dialog/content/manager/packBanner/PackBanne
113113
import PackCardFooter from '@/components/dialog/content/manager/packCard/PackCardFooter.vue'
114114
import { usePackUpdateStatus } from '@/composables/nodePack/usePackUpdateStatus'
115115
import { useComfyManagerStore } from '@/stores/comfyManagerStore'
116-
import { IsInstallingKey } from '@/types/comfyManagerTypes'
117-
import type { components } from '@/types/comfyRegistryTypes'
116+
import {
117+
IsInstallingKey,
118+
type MergedNodePack,
119+
type RegistryPack,
120+
isMergedNodePack
121+
} from '@/types/comfyManagerTypes'
118122
119123
const { nodePack, isSelected = false } = defineProps<{
120-
nodePack: components['schemas']['Node']
124+
nodePack: MergedNodePack | RegistryPack
121125
isSelected?: boolean
122126
}>()
123127
@@ -136,9 +140,9 @@ const isDisabled = computed(
136140
137141
whenever(isInstalled, () => (isInstalling.value = false))
138142
139-
// TODO: remove type assertion once comfy_nodes is added to node (pack) info type in backend
140-
const nodesCount = computed(() => (nodePack as any).comfy_nodes?.length)
141-
143+
const nodesCount = computed(() =>
144+
isMergedNodePack(nodePack) ? nodePack.comfy_nodes?.length : undefined
145+
)
142146
const publisherName = computed(() => {
143147
if (!nodePack) return null
144148

src/components/dialog/content/manager/registrySearchBar/RegistrySearchBar.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import { computed } from 'vue'
5656
import { useI18n } from 'vue-i18n'
5757
5858
import SearchFilterDropdown from '@/components/dialog/content/manager/registrySearchBar/SearchFilterDropdown.vue'
59-
import type { NodesIndexSuggestion } from '@/services/algoliaSearchService'
59+
import type { NodesIndexSuggestion } from '@/types/algoliaTypes'
6060
import {
6161
type SearchOption,
6262
SortableAlgoliaField

src/composables/useRegistrySearch.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import type { Hit } from 'algoliasearch/dist/lite/browser'
33
import { memoize, orderBy } from 'lodash'
44
import { computed, onUnmounted, ref, watch } from 'vue'
55

6-
import {
6+
import { useAlgoliaSearchService } from '@/services/algoliaSearchService'
7+
import type {
78
AlgoliaNodePack,
8-
SearchAttribute,
9-
useAlgoliaSearchService
10-
} from '@/services/algoliaSearchService'
11-
import type { NodesIndexSuggestion } from '@/services/algoliaSearchService'
9+
NodesIndexSuggestion,
10+
SearchAttribute
11+
} from '@/types/algoliaTypes'
1212
import { SortableAlgoliaField } from '@/types/comfyManagerTypes'
1313

1414
const SEARCH_DEBOUNCE_TIME = 320

src/services/algoliaSearchService.ts

Lines changed: 10 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,25 @@
11
import QuickLRU from '@alloc/quick-lru'
22
import type {
3-
BaseSearchParamsWithoutQuery,
4-
Hit,
53
SearchQuery,
64
SearchResponse
75
} from 'algoliasearch/dist/lite/browser'
86
import { liteClient as algoliasearch } from 'algoliasearch/dist/lite/builds/browser'
97
import { omit } from 'lodash'
108

11-
import { components } from '@/types/comfyRegistryTypes'
9+
import type {
10+
AlgoliaNodePack,
11+
NodesIndexSuggestion,
12+
SearchAttribute,
13+
SearchNodePacksParams,
14+
SearchPacksResult
15+
} from '@/types/algoliaTypes'
16+
import type { components } from '@/types/comfyRegistryTypes'
1217
import { paramsToCacheKey } from '@/utils/formatUtil'
1318

14-
const DEFAULT_MAX_CACHE_SIZE = 64
15-
const DEFAULT_MIN_CHARS_FOR_SUGGESTIONS = 2
16-
17-
type SafeNestedProperty<
18-
T,
19-
K1 extends keyof T,
20-
K2 extends keyof NonNullable<T[K1]>
21-
> = T[K1] extends undefined | null ? undefined : NonNullable<T[K1]>[K2]
22-
2319
type RegistryNodePack = components['schemas']['Node']
24-
type SearchPacksResult = {
25-
nodePacks: Hit<AlgoliaNodePack>[]
26-
querySuggestions: Hit<NodesIndexSuggestion>[]
27-
}
28-
29-
export interface AlgoliaNodePack {
30-
objectID: RegistryNodePack['id']
31-
name: RegistryNodePack['name']
32-
publisher_id: SafeNestedProperty<RegistryNodePack, 'publisher', 'id'>
33-
description: RegistryNodePack['description']
34-
comfy_nodes: string[]
35-
total_install: RegistryNodePack['downloads']
36-
id: RegistryNodePack['id']
37-
create_time: string
38-
update_time: SafeNestedProperty<
39-
RegistryNodePack,
40-
'latest_version',
41-
'createdAt'
42-
>
43-
license: RegistryNodePack['license']
44-
repository_url: RegistryNodePack['repository']
45-
status: RegistryNodePack['status']
46-
latest_version: SafeNestedProperty<
47-
RegistryNodePack,
48-
'latest_version',
49-
'version'
50-
>
51-
latest_version_status: SafeNestedProperty<
52-
RegistryNodePack,
53-
'latest_version',
54-
'status'
55-
>
56-
comfy_node_extract_status: SafeNestedProperty<
57-
RegistryNodePack,
58-
'latest_version',
59-
'comfy_node_extract_status'
60-
>
61-
icon_url: RegistryNodePack['icon']
62-
}
6320

64-
export type SearchAttribute = keyof AlgoliaNodePack
21+
const DEFAULT_MAX_CACHE_SIZE = 64
22+
const DEFAULT_MIN_CHARS_FOR_SUGGESTIONS = 2
6523

6624
const RETRIEVE_ATTRIBUTES: SearchAttribute[] = [
6725
'comfy_nodes',
@@ -81,26 +39,6 @@ const RETRIEVE_ATTRIBUTES: SearchAttribute[] = [
8139
'icon_url'
8240
]
8341

84-
export interface NodesIndexSuggestion {
85-
nb_words: number
86-
nodes_index: {
87-
exact_nb_hits: number
88-
facets: {
89-
exact_matches: Record<string, number>
90-
analytics: Record<string, any>
91-
}
92-
}
93-
objectID: RegistryNodePack['id']
94-
popularity: number
95-
query: string
96-
}
97-
98-
type SearchNodePacksParams = BaseSearchParamsWithoutQuery & {
99-
pageSize: number
100-
pageNumber: number
101-
restrictSearchableAttributes: SearchAttribute[]
102-
}
103-
10442
interface AlgoliaSearchServiceOptions {
10543
/**
10644
* Maximum number of search results to store in the cache.

src/types/algoliaTypes.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import type {
2+
BaseSearchParamsWithoutQuery,
3+
Hit
4+
} from 'algoliasearch/dist/lite/browser'
5+
6+
import type { components } from '@/types/comfyRegistryTypes'
7+
8+
type SafeNestedProperty<
9+
T,
10+
K1 extends keyof T,
11+
K2 extends keyof NonNullable<T[K1]>
12+
> = T[K1] extends undefined | null ? undefined : NonNullable<T[K1]>[K2]
13+
14+
type RegistryNodePack = components['schemas']['Node']
15+
export type SearchPacksResult = {
16+
nodePacks: Hit<AlgoliaNodePack>[]
17+
querySuggestions: Hit<NodesIndexSuggestion>[]
18+
}
19+
20+
export interface AlgoliaNodePack {
21+
objectID: RegistryNodePack['id']
22+
name: RegistryNodePack['name']
23+
publisher_id: SafeNestedProperty<RegistryNodePack, 'publisher', 'id'>
24+
description: RegistryNodePack['description']
25+
comfy_nodes: string[]
26+
total_install: RegistryNodePack['downloads']
27+
id: RegistryNodePack['id']
28+
create_time: string
29+
update_time: SafeNestedProperty<
30+
RegistryNodePack,
31+
'latest_version',
32+
'createdAt'
33+
>
34+
license: RegistryNodePack['license']
35+
repository_url: RegistryNodePack['repository']
36+
status: RegistryNodePack['status']
37+
latest_version: SafeNestedProperty<
38+
RegistryNodePack,
39+
'latest_version',
40+
'version'
41+
>
42+
latest_version_status: SafeNestedProperty<
43+
RegistryNodePack,
44+
'latest_version',
45+
'status'
46+
>
47+
comfy_node_extract_status: SafeNestedProperty<
48+
RegistryNodePack,
49+
'latest_version',
50+
'comfy_node_extract_status'
51+
>
52+
icon_url: RegistryNodePack['icon']
53+
}
54+
55+
export type SearchAttribute = keyof AlgoliaNodePack
56+
export interface NodesIndexSuggestion {
57+
nb_words: number
58+
nodes_index: {
59+
exact_nb_hits: number
60+
facets: {
61+
exact_matches: Record<string, number>
62+
analytics: Record<string, any>
63+
}
64+
}
65+
objectID: RegistryNodePack['id']
66+
popularity: number
67+
query: string
68+
}
69+
70+
export type SearchNodePacksParams = BaseSearchParamsWithoutQuery & {
71+
pageSize: number
72+
pageNumber: number
73+
restrictSearchableAttributes: SearchAttribute[]
74+
}

src/types/comfyManagerTypes.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import type { InjectionKey, Ref } from 'vue'
22

33
import type { ComfyWorkflowJSON } from '@/schemas/comfyWorkflowSchema'
4+
import type { AlgoliaNodePack } from '@/types/algoliaTypes'
45
import type { components } from '@/types/comfyRegistryTypes'
56

6-
type RegistryPack = components['schemas']['Node']
77
type WorkflowNodeProperties = ComfyWorkflowJSON['nodes'][0]['properties']
8+
9+
export type RegistryPack = components['schemas']['Node']
10+
export type MergedNodePack = RegistryPack & AlgoliaNodePack
11+
export const isMergedNodePack = (
12+
nodePack: RegistryPack | AlgoliaNodePack
13+
): nodePack is MergedNodePack => 'comfy_nodes' in nodePack
14+
815
export type PackField = keyof RegistryPack | null
916

1017
export const IsInstallingKey: InjectionKey<Ref<boolean>> =

0 commit comments

Comments
 (0)