Files
dify/web/app/components/plugins/utils.ts
CodingOnStar c167ee199c feat: implement dynamic plugin card icon URL generation
Added a utility function to generate plugin card icon URLs based on the plugin's source and workspace context. Updated the Card component to utilize this function for determining the correct icon source. Enhanced unit tests to verify the correct URL generation for both marketplace and package icons.
2026-03-12 14:58:16 +08:00

43 lines
1.1 KiB
TypeScript

import type {
TagKey,
} from './constants'
import type { Plugin } from './types'
import { API_PREFIX, MARKETPLACE_API_PREFIX } from '@/config'
import {
categoryKeys,
tagKeys,
} from './constants'
export const getValidTagKeys = (tags: TagKey[]) => {
return tags.filter(tag => tagKeys.includes(tag))
}
export const getValidCategoryKeys = (category?: string) => {
return categoryKeys.find(key => key === category)
}
const hasUrlProtocol = (value: string) => /^[a-z][a-z\d+.-]*:/i.test(value)
export const getPluginCardIconUrl = (
plugin: Pick<Plugin, 'from' | 'name' | 'org' | 'type'>,
icon: string | undefined,
tenantId: string,
) => {
if (!icon)
return ''
if (hasUrlProtocol(icon) || icon.startsWith('/'))
return icon
if (plugin.from === 'marketplace') {
const basePath = plugin.type === 'bundle' ? 'bundles' : 'plugins'
return `${MARKETPLACE_API_PREFIX}/${basePath}/${plugin.org}/${plugin.name}/icon`
}
if (!tenantId)
return icon
return `${API_PREFIX}/workspaces/current/plugin/icon?tenant_id=${tenantId}&filename=${icon}`
}