Line data Source code
1 : import type {
2 : LogoInfo,
3 : TemplateInfo,
4 : TemplateTypeFilter
5 : } from '@comfyorg/api-client/templateTypes'
6 :
7 : /**
8 : * Whether a template opens in App Mode rather than as a node graph.
9 : *
10 : * Reads `isApp`, which `templates/index.json` derives from the workflow's own
11 : * `extra.linearMode`. That is the author's choice, recorded in the workflow.
12 : *
13 : * This previously read `name.endsWith('.app')`, which is wrong in both
14 : * directions: `templates_all_in_one_image_edit_models.app` carries the suffix but
15 : * is a node graph, while `template_qwen_image_illustration_lora` and
16 : * `template_sugar_coated_gummy_style_qwen` are Apps without it. The suffix
17 : * records how a workflow was saved, not what it is, so no amount of renaming
18 : * would have fixed the Apps filter.
19 : */
20 1446 : export function isAppTemplate(template: TemplateInfo): boolean {
21 1446 : return template.isApp === true
22 : }
23 :
24 117 : export function filterTemplatesByType<T extends TemplateInfo>(
25 : templates: T[],
26 : type: TemplateTypeFilter
27 : ): T[] {
28 117 : if (type === 'all') return templates
29 0 : const wantApp = type === 'apps'
30 0 : return templates.filter((template) => isAppTemplate(template) === wantApp)
31 : }
32 :
33 : const iconFiles = import.meta.glob(
34 : '../../../../../packages/design-system/src/icons/*.svg',
35 : { query: '?raw' }
36 : )
37 :
38 2355 : const AVAILABLE_ICON_SLUGS = new Set(
39 : Object.keys(iconFiles).map(
40 : (path) => path.split('/').pop()?.replace('.svg', '') ?? ''
41 : )
42 : )
43 :
44 : /** Provider names whose slug does not fall out of the name itself. */
45 2355 : const PROVIDER_ICON_ALIASES: Record<string, string> = {
46 : 'black forest labs': 'bfl',
47 : google: 'gemini',
48 : lightricks: 'ltxv',
49 : stability: 'stability-ai'
50 : }
51 :
52 132 : function toSlug(provider: string): string {
53 132 : const normalized = provider.trim().toLowerCase()
54 132 : return (
55 : PROVIDER_ICON_ALIASES[normalized] ??
56 : normalized.replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '')
57 : )
58 : }
59 :
60 132 : export function getProviderIconClass(provider: string): string | null {
61 132 : const slug = toSlug(provider)
62 132 : return AVAILABLE_ICON_SLUGS.has(slug) ? `icon-mask-[comfy--${slug}]` : null
63 : }
64 :
65 : export interface ProviderBadge {
66 : provider: string
67 : iconClass: string | null
68 : logoUrl: string
69 : }
70 :
71 : export interface ProviderBadges {
72 : visible: ProviderBadge[]
73 : extraProviders: string[]
74 : }
75 :
76 : /** Badges shown before the rest collapse into a "+N" chip. */
77 2355 : const MAX_VISIBLE_PROVIDERS = 5
78 :
79 132 : export function getProviderBadges(
80 : logo: LogoInfo,
81 : getLogoUrl: (provider: string) => string
82 : ): ProviderBadges | null {
83 : const providers = Array.isArray(logo.provider)
84 : ? logo.provider
85 : : [logo.provider]
86 132 : const badges = providers
87 : .map((provider) => ({
88 : provider,
89 : iconClass: getProviderIconClass(provider),
90 : logoUrl: getLogoUrl(provider)
91 : }))
92 : .filter((badge) => badge.iconClass !== null || badge.logoUrl !== '')
93 :
94 132 : if (badges.length === 0) return null
95 :
96 132 : return {
97 : visible: badges.slice(0, MAX_VISIBLE_PROVIDERS),
98 : extraProviders: badges
99 : .slice(MAX_VISIBLE_PROVIDERS)
100 : .map((badge) => badge.provider)
101 : }
102 : }
103 :
104 : /** Tags shown before the rest collapse into a "+N" chip. */
105 2355 : const MAX_VISIBLE_TAGS = 2
106 :
107 : export interface TemplateTags {
108 : visible: string[]
109 : hidden: string[]
110 : }
111 :
112 392 : export function getTemplateTags(template: TemplateInfo): TemplateTags {
113 392 : const tags = template.tags ?? []
114 392 : return {
115 : visible: tags.slice(0, MAX_VISIBLE_TAGS),
116 : hidden: tags.slice(MAX_VISIBLE_TAGS)
117 : }
118 : }
|