2024-08-19 09:16:33 +08:00
|
|
|
'use client'
|
2025-05-03 13:43:37 +08:00
|
|
|
import React, { type FC, useRef } from 'react'
|
2023-05-19 17:36:44 +08:00
|
|
|
import { init } from 'emoji-mart'
|
2024-08-19 09:16:33 +08:00
|
|
|
import data from '@emoji-mart/data'
|
2024-12-13 17:29:09 +08:00
|
|
|
import { cva } from 'class-variance-authority'
|
2024-08-19 09:16:33 +08:00
|
|
|
import type { AppIconType } from '@/types/app'
|
2024-12-13 17:29:09 +08:00
|
|
|
import classNames from '@/utils/classnames'
|
2025-05-03 13:43:37 +08:00
|
|
|
import { useHover } from 'ahooks'
|
|
|
|
import { RiEditLine } from '@remixicon/react'
|
2023-05-19 17:36:44 +08:00
|
|
|
|
|
|
|
init({ data })
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
export type AppIconProps = {
|
2024-12-13 17:29:09 +08:00
|
|
|
size?: 'xs' | 'tiny' | 'small' | 'medium' | 'large' | 'xl' | 'xxl'
|
2023-05-15 08:51:32 +08:00
|
|
|
rounded?: boolean
|
2024-08-19 09:16:33 +08:00
|
|
|
iconType?: AppIconType | null
|
2023-05-15 08:51:32 +08:00
|
|
|
icon?: string
|
2024-08-19 09:16:33 +08:00
|
|
|
background?: string | null
|
|
|
|
imageUrl?: string | null
|
2023-05-15 08:51:32 +08:00
|
|
|
className?: string
|
|
|
|
innerIcon?: React.ReactNode
|
2023-05-19 17:36:44 +08:00
|
|
|
onClick?: () => void
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
2024-12-13 17:29:09 +08:00
|
|
|
const appIconVariants = cva(
|
2025-05-03 13:43:37 +08:00
|
|
|
'flex items-center justify-center relative text-lg rounded-2xl grow-0 shrink-0 overflow-hidden leading-none border-[0.5px] border-divider-regular',
|
2024-12-13 17:29:09 +08:00
|
|
|
{
|
|
|
|
variants: {
|
|
|
|
size: {
|
|
|
|
xs: 'w-4 h-4 text-xs',
|
|
|
|
tiny: 'w-6 h-6 text-base',
|
|
|
|
small: 'w-8 h-8 text-xl',
|
|
|
|
medium: 'w-9 h-9 text-[22px]',
|
|
|
|
large: 'w-10 h-10 text-[24px]',
|
|
|
|
xl: 'w-12 h-12 text-[28px]',
|
|
|
|
xxl: 'w-14 h-14 text-[32px]',
|
|
|
|
},
|
|
|
|
rounded: {
|
|
|
|
true: 'rounded-full',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
defaultVariants: {
|
|
|
|
size: 'medium',
|
|
|
|
rounded: false,
|
|
|
|
},
|
|
|
|
})
|
2023-05-15 08:51:32 +08:00
|
|
|
const AppIcon: FC<AppIconProps> = ({
|
|
|
|
size = 'medium',
|
|
|
|
rounded = false,
|
2024-08-19 09:16:33 +08:00
|
|
|
iconType,
|
2023-05-19 17:36:44 +08:00
|
|
|
icon,
|
2023-05-15 08:51:32 +08:00
|
|
|
background,
|
2024-08-19 09:16:33 +08:00
|
|
|
imageUrl,
|
2023-05-15 08:51:32 +08:00
|
|
|
className,
|
|
|
|
innerIcon,
|
2023-05-19 17:36:44 +08:00
|
|
|
onClick,
|
2023-05-15 08:51:32 +08:00
|
|
|
}) => {
|
2024-08-19 09:16:33 +08:00
|
|
|
const isValidImageIcon = iconType === 'image' && imageUrl
|
2025-05-03 13:43:37 +08:00
|
|
|
const Icon = (icon && icon !== '') ? <em-emoji id={icon} /> : <em-emoji id='🤖' />
|
|
|
|
const wrapperRef = useRef<HTMLSpanElement>(null)
|
|
|
|
const isHovering = useHover(wrapperRef)
|
2024-08-19 09:16:33 +08:00
|
|
|
|
2025-05-03 13:43:37 +08:00
|
|
|
return (
|
|
|
|
<span
|
|
|
|
ref={wrapperRef}
|
|
|
|
className={classNames(appIconVariants({ size, rounded }), className)}
|
|
|
|
style={{ background: isValidImageIcon ? undefined : (background || '#FFEAD5') }}
|
|
|
|
onClick={onClick}
|
|
|
|
>
|
|
|
|
{
|
|
|
|
isValidImageIcon
|
|
|
|
? <img src={imageUrl} className='h-full w-full' alt='app icon' />
|
|
|
|
: (innerIcon || Icon)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
isHovering && (
|
|
|
|
<div className='absolute left-0 top-0 z-10 flex size-14 items-center justify-center rounded-2xl bg-background-overlay-alt'>
|
|
|
|
<RiEditLine className='size-6 text-text-primary-on-surface' />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</span>
|
|
|
|
)
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
|
2025-05-03 13:43:37 +08:00
|
|
|
export default React.memo(AppIcon)
|