dify/web/app/components/plugins/card/index.tsx

85 lines
2.3 KiB
TypeScript
Raw Normal View History

'use client'
2024-10-10 10:40:26 +08:00
import React from 'react'
import { useContext } from 'use-context-selector'
2024-10-10 10:40:26 +08:00
import { RiVerifiedBadgeLine } from '@remixicon/react'
import type { Plugin } from '../types'
2024-10-11 14:10:24 +08:00
import Icon from '../card/base/card-icon'
2024-10-10 10:40:26 +08:00
import CornerMark from './base/corner-mark'
import Title from './base/title'
import OrgInfo from './base/org-info'
import Description from './base/description'
2024-10-15 22:52:57 +08:00
import Placeholder from './base/placeholder'
2024-10-10 10:40:26 +08:00
import cn from '@/utils/classnames'
import I18n from '@/context/i18n'
2024-10-10 10:40:26 +08:00
type Props = {
className?: string
payload: Plugin
titleLeft?: React.ReactNode
2024-10-10 10:40:26 +08:00
installed?: boolean
2024-10-15 21:45:30 +08:00
hideCornerMark?: boolean
2024-10-10 10:40:26 +08:00
descriptionLineRows?: number
footer?: React.ReactNode
2024-10-15 14:56:59 +08:00
isLoading?: boolean
loadingFileName?: string
2024-10-10 10:40:26 +08:00
}
const Card = ({
className,
payload,
titleLeft,
2024-10-10 10:40:26 +08:00
installed,
2024-10-15 21:45:30 +08:00
hideCornerMark,
2024-10-10 10:40:26 +08:00
descriptionLineRows = 2,
footer,
2024-10-15 14:56:59 +08:00
isLoading = false,
loadingFileName,
2024-10-10 10:40:26 +08:00
}: Props) => {
const { locale } = useContext(I18n)
2024-10-15 14:56:59 +08:00
const { type, name, org, label, brief, icon } = payload
const getLocalizedText = (obj: Record<string, string> | undefined) =>
obj?.[locale] || obj?.['en-US'] || ''
2024-10-15 22:52:57 +08:00
const wrapClassName = cn('relative p-4 pb-3 border-[0.5px] border-components-panel-border bg-components-panel-on-panel-item-bg hover-bg-components-panel-on-panel-item-bg rounded-xl shadow-xs', className)
if (isLoading) {
return (
<Placeholder
wrapClassName={wrapClassName}
loadingFileName={loadingFileName!}
/>
)
}
2024-10-10 10:40:26 +08:00
return (
<div className={wrapClassName}>
2024-10-15 22:52:57 +08:00
{!hideCornerMark && <CornerMark text={type} />}
2024-10-10 10:40:26 +08:00
{/* Header */}
<div className="flex">
2024-10-15 22:52:57 +08:00
<Icon src={icon} installed={installed} />
2024-10-10 10:40:26 +08:00
<div className="ml-3 grow">
<div className="flex items-center h-5">
2024-10-15 22:52:57 +08:00
<Title title={getLocalizedText(label)} />
<RiVerifiedBadgeLine className="shrink-0 ml-0.5 w-4 h-4 text-text-accent" />
{titleLeft} {/* This can be version badge */}
2024-10-10 10:40:26 +08:00
</div>
<OrgInfo
className="mt-0.5"
orgName={org}
packageName={name}
/>
</div>
</div>
2024-10-15 22:52:57 +08:00
<Description
className="mt-3"
text={getLocalizedText(brief)}
descriptionLineRows={descriptionLineRows}
/>
2024-10-10 10:40:26 +08:00
{footer && <div>{footer}</div>}
</div>
)
}
export default React.memo(Card)