index.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use client'
  2. import type { FC } from 'react'
  3. import { init } from 'emoji-mart'
  4. import data from '@emoji-mart/data'
  5. import { cva } from 'class-variance-authority'
  6. import type { AppIconType } from '@/types/app'
  7. import classNames from '@/utils/classnames'
  8. init({ data })
  9. export type AppIconProps = {
  10. size?: 'xs' | 'tiny' | 'small' | 'medium' | 'large' | 'xl' | 'xxl'
  11. rounded?: boolean
  12. iconType?: AppIconType | null
  13. icon?: string
  14. background?: string | null
  15. imageUrl?: string | null
  16. className?: string
  17. innerIcon?: React.ReactNode
  18. onClick?: () => void
  19. }
  20. const appIconVariants = cva(
  21. 'flex items-center justify-center relative text-lg rounded-lg grow-0 shrink-0 overflow-hidden leading-none',
  22. {
  23. variants: {
  24. size: {
  25. xs: 'w-4 h-4 text-xs',
  26. tiny: 'w-6 h-6 text-base',
  27. small: 'w-8 h-8 text-xl',
  28. medium: 'w-9 h-9 text-[22px]',
  29. large: 'w-10 h-10 text-[24px]',
  30. xl: 'w-12 h-12 text-[28px]',
  31. xxl: 'w-14 h-14 text-[32px]',
  32. },
  33. rounded: {
  34. true: 'rounded-full',
  35. },
  36. },
  37. defaultVariants: {
  38. size: 'medium',
  39. rounded: false,
  40. },
  41. })
  42. const AppIcon: FC<AppIconProps> = ({
  43. size = 'medium',
  44. rounded = false,
  45. iconType,
  46. icon,
  47. background,
  48. imageUrl,
  49. className,
  50. innerIcon,
  51. onClick,
  52. }) => {
  53. const isValidImageIcon = iconType === 'image' && imageUrl
  54. return <span
  55. className={classNames(appIconVariants({ size, rounded }), className)}
  56. style={{ background: isValidImageIcon ? undefined : (background || '#FFEAD5') }}
  57. onClick={onClick}
  58. >
  59. {isValidImageIcon
  60. // eslint-disable-next-line @next/next/no-img-element
  61. ? <img src={imageUrl} className="w-full h-full" alt="app icon" />
  62. : (innerIcon || ((icon && icon !== '') ? <em-emoji id={icon} /> : <em-emoji id='🤖' />))
  63. }
  64. </span>
  65. }
  66. export default AppIcon