You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

deprecation-notice.tsx 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import React, { useMemo } from 'react'
  2. import type { FC } from 'react'
  3. import Link from 'next/link'
  4. import cn from '@/utils/classnames'
  5. import { RiAlertFill } from '@remixicon/react'
  6. import { Trans } from 'react-i18next'
  7. import { snakeCase2CamelCase } from '@/utils/format'
  8. import { useMixedTranslation } from '../marketplace/hooks'
  9. type DeprecationNoticeProps = {
  10. status: 'deleted' | 'active'
  11. deprecatedReason: string
  12. alternativePluginId: string
  13. alternativePluginURL: string
  14. locale?: string
  15. className?: string
  16. innerWrapperClassName?: string
  17. iconWrapperClassName?: string
  18. textClassName?: string
  19. }
  20. const i18nPrefix = 'plugin.detailPanel.deprecation'
  21. const DeprecationNotice: FC<DeprecationNoticeProps> = ({
  22. status,
  23. deprecatedReason,
  24. alternativePluginId,
  25. alternativePluginURL,
  26. locale,
  27. className,
  28. innerWrapperClassName,
  29. iconWrapperClassName,
  30. textClassName,
  31. }) => {
  32. const { t } = useMixedTranslation(locale)
  33. const deprecatedReasonKey = useMemo(() => {
  34. if (!deprecatedReason) return ''
  35. return snakeCase2CamelCase(deprecatedReason)
  36. }, [deprecatedReason])
  37. // Check if the deprecatedReasonKey exists in i18n
  38. const hasValidDeprecatedReason = useMemo(() => {
  39. if (!deprecatedReason || !deprecatedReasonKey) return false
  40. // Define valid reason keys that exist in i18n
  41. const validReasonKeys = ['businessAdjustments', 'ownershipTransferred', 'noMaintainer']
  42. return validReasonKeys.includes(deprecatedReasonKey)
  43. }, [deprecatedReason, deprecatedReasonKey])
  44. if (status !== 'deleted')
  45. return null
  46. return (
  47. <div className={cn('w-full', className)}>
  48. <div className={cn(
  49. 'relative flex items-start gap-x-0.5 overflow-hidden rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-bg-blur p-2 shadow-xs shadow-shadow-shadow-3 backdrop-blur-[5px]',
  50. innerWrapperClassName,
  51. )}>
  52. <div className='absolute left-0 top-0 -z-10 h-full w-full bg-toast-warning-bg opacity-40' />
  53. <div className={cn('flex size-6 shrink-0 items-center justify-center', iconWrapperClassName)}>
  54. <RiAlertFill className='size-4 text-text-warning-secondary' />
  55. </div>
  56. <div className={cn('system-xs-regular grow py-1 text-text-primary', textClassName)}>
  57. {
  58. hasValidDeprecatedReason && alternativePluginId && (
  59. <Trans
  60. i18nKey={`${i18nPrefix}.fullMessage`}
  61. components={{
  62. CustomLink: (
  63. <Link
  64. href={alternativePluginURL}
  65. target='_blank'
  66. rel='noopener noreferrer'
  67. className='underline'
  68. />
  69. ),
  70. }}
  71. values={{
  72. deprecatedReason: t(`${i18nPrefix}.reason.${deprecatedReasonKey}`),
  73. alternativePluginId,
  74. }}
  75. />
  76. )
  77. }
  78. {
  79. hasValidDeprecatedReason && !alternativePluginId && (
  80. <span>
  81. {t(`${i18nPrefix}.onlyReason`, { deprecatedReason: t(`${i18nPrefix}.reason.${deprecatedReasonKey}`) })}
  82. </span>
  83. )
  84. }
  85. {
  86. !hasValidDeprecatedReason && (
  87. <span>{t(`${i18nPrefix}.noReason`)}</span>
  88. )
  89. }
  90. </div>
  91. </div>
  92. </div>
  93. )
  94. }
  95. export default React.memo(DeprecationNotice)