mirror of https://github.com/langgenius/dify.git
refactor: enhance type safety for status keys in model parameter components
- update status key access in ModelParameterTrigger, Trigger, ModelSelectorTrigger, and Node components to ensure type safety - apply TypeScript's keyof operator for better type inference with derived model status and trigger status
This commit is contained in:
parent
111d82bbb4
commit
0edbc5d672
|
|
@ -78,8 +78,8 @@ const ModelParameterTrigger: FC<ModelParameterTriggerProps> = ({
|
|||
credentialState,
|
||||
)
|
||||
const iconProvider = currentProvider || providerMeta
|
||||
const statusLabelKey = DERIVED_MODEL_STATUS_BADGE_I18N[status]
|
||||
const statusTooltipKey = DERIVED_MODEL_STATUS_TOOLTIP_I18N[status]
|
||||
const statusLabelKey = DERIVED_MODEL_STATUS_BADGE_I18N[status as keyof typeof DERIVED_MODEL_STATUS_BADGE_I18N]
|
||||
const statusTooltipKey = DERIVED_MODEL_STATUS_TOOLTIP_I18N[status as keyof typeof DERIVED_MODEL_STATUS_TOOLTIP_I18N]
|
||||
const isEmpty = status === 'empty'
|
||||
const isActive = status === 'active'
|
||||
|
||||
|
|
|
|||
|
|
@ -11,19 +11,19 @@ export type DerivedModelStatus
|
|||
| 'disabled'
|
||||
| 'incompatible'
|
||||
|
||||
export const DERIVED_MODEL_STATUS_BADGE_I18N: Partial<Record<DerivedModelStatus, string>> = {
|
||||
export const DERIVED_MODEL_STATUS_BADGE_I18N = {
|
||||
'configure-required': 'modelProvider.selector.configureRequired',
|
||||
'credits-exhausted': 'modelProvider.selector.creditsExhausted',
|
||||
'api-key-unavailable': 'modelProvider.selector.apiKeyUnavailable',
|
||||
'disabled': 'modelProvider.selector.disabled',
|
||||
'incompatible': 'modelProvider.selector.incompatible',
|
||||
}
|
||||
} as const satisfies Partial<Record<DerivedModelStatus, string>>
|
||||
|
||||
export const DERIVED_MODEL_STATUS_TOOLTIP_I18N: Partial<Record<DerivedModelStatus, string>> = {
|
||||
export const DERIVED_MODEL_STATUS_TOOLTIP_I18N = {
|
||||
'credits-exhausted': 'modelProvider.selector.creditsExhaustedTip',
|
||||
'api-key-unavailable': 'modelProvider.selector.apiKeyUnavailableTip',
|
||||
'incompatible': 'modelProvider.selector.incompatibleTip',
|
||||
}
|
||||
} as const satisfies Partial<Record<DerivedModelStatus, string>>
|
||||
|
||||
export const deriveModelStatus = (
|
||||
modelId: string | undefined,
|
||||
|
|
|
|||
|
|
@ -40,10 +40,10 @@ const Trigger: FC<TriggerProps> = ({
|
|||
const currentModelProvider = modelProviders.find(p => p.provider === providerName)
|
||||
const credentialState = useCredentialPanelState(currentModelProvider)
|
||||
const status = deriveTriggerStatus(modelId, providerName, currentModelProvider, currentModel, credentialState)
|
||||
const badgeKey = TRIGGER_STATUS_BADGE_I18N[status]
|
||||
const tooltipKey = TRIGGER_STATUS_TOOLTIP_I18N[status]
|
||||
const badgeLabel = badgeKey ? t(badgeKey, { ns: 'common', defaultValue: badgeKey }) : null
|
||||
const tooltipLabel = tooltipKey ? t(tooltipKey, { ns: 'common', defaultValue: tooltipKey }) : null
|
||||
const badgeKey = TRIGGER_STATUS_BADGE_I18N[status as keyof typeof TRIGGER_STATUS_BADGE_I18N]
|
||||
const tooltipKey = TRIGGER_STATUS_TOOLTIP_I18N[status as keyof typeof TRIGGER_STATUS_TOOLTIP_I18N]
|
||||
const badgeLabel = badgeKey ? t(badgeKey, { ns: 'common' }) : null
|
||||
const tooltipLabel = tooltipKey ? t(tooltipKey, { ns: 'common' }) : null
|
||||
const isActive = status === 'active'
|
||||
const iconProvider = currentProvider || modelProviders.find(item => item.provider === providerName)
|
||||
|
||||
|
|
|
|||
|
|
@ -62,10 +62,10 @@ const ModelSelectorTrigger: FC<ModelSelectorTriggerProps> = ({
|
|||
|
||||
const isActive = status === 'active'
|
||||
const isDisabled = status !== 'active' && status !== 'empty'
|
||||
const statusI18nKey = DERIVED_MODEL_STATUS_BADGE_I18N[status]
|
||||
const tooltipI18nKey = DERIVED_MODEL_STATUS_TOOLTIP_I18N[status]
|
||||
const statusLabel = statusI18nKey ? t(statusI18nKey, { ns: 'common', defaultValue: statusI18nKey }) : null
|
||||
const tooltipLabel = tooltipI18nKey ? t(tooltipI18nKey, { ns: 'common', defaultValue: tooltipI18nKey }) : null
|
||||
const statusI18nKey = DERIVED_MODEL_STATUS_BADGE_I18N[status as keyof typeof DERIVED_MODEL_STATUS_BADGE_I18N]
|
||||
const tooltipI18nKey = DERIVED_MODEL_STATUS_TOOLTIP_I18N[status as keyof typeof DERIVED_MODEL_STATUS_TOOLTIP_I18N]
|
||||
const statusLabel = statusI18nKey ? t(statusI18nKey, { ns: 'common' }) : null
|
||||
const tooltipLabel = tooltipI18nKey ? t(tooltipI18nKey, { ns: 'common' }) : null
|
||||
const isCreditsExhausted = status === 'credits-exhausted'
|
||||
const shouldShowModelMeta = status === 'active'
|
||||
|
||||
|
|
|
|||
|
|
@ -151,7 +151,7 @@ const Node: FC<NodeProps<KnowledgeBaseNodeType>> = ({ data }) => {
|
|||
return t('detailPanel.configureModel', { ns: 'plugin' })
|
||||
|
||||
if (embeddingModelStatus !== 'active') {
|
||||
const statusI18nKey = DERIVED_MODEL_STATUS_BADGE_I18N[embeddingModelStatus]
|
||||
const statusI18nKey = DERIVED_MODEL_STATUS_BADGE_I18N[embeddingModelStatus as keyof typeof DERIVED_MODEL_STATUS_BADGE_I18N]
|
||||
if (statusI18nKey)
|
||||
return t(statusI18nKey as 'modelProvider.selector.incompatible', { ns: 'common' })
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue