index.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import s from './index.module.css'
  2. import cn from '@/utils/classnames'
  3. import type { DataSourceNotionPage } from '@/models/common'
  4. type IconTypes = 'workspace' | 'page'
  5. type NotionIconProps = {
  6. type?: IconTypes
  7. name?: string | null
  8. className?: string
  9. src?: string | null | DataSourceNotionPage['page_icon']
  10. }
  11. const NotionIcon = ({
  12. type = 'workspace',
  13. src,
  14. name,
  15. className,
  16. }: NotionIconProps) => {
  17. if (type === 'workspace') {
  18. if (typeof src === 'string') {
  19. if (src.startsWith('https://') || src.startsWith('http://')) {
  20. return (
  21. <img
  22. alt='workspace icon'
  23. src={src}
  24. className={cn('block object-cover w-5 h-5', className)}
  25. />
  26. )
  27. }
  28. return (
  29. <div className={cn('flex items-center justify-center w-5 h-5', className)}>{src}</div>
  30. )
  31. }
  32. return (
  33. <div className={cn('flex items-center justify-center w-5 h-5 bg-gray-200 text-xs font-medium text-gray-500 rounded', className)}>{name?.[0].toLocaleUpperCase()}</div>
  34. )
  35. }
  36. if (typeof src === 'object' && src !== null) {
  37. if (src?.type === 'url') {
  38. return (
  39. <img
  40. alt='page icon'
  41. src={src.url || ''}
  42. className={cn('block object-cover w-5 h-5', className)}
  43. />
  44. )
  45. }
  46. return (
  47. <div className={cn('flex items-center justify-center w-5 h-5', className)}>{src?.emoji}</div>
  48. )
  49. }
  50. return (
  51. <div className={cn(s['default-page-icon'], className)} />
  52. )
  53. }
  54. export default NotionIcon