index.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* eslint-disable react/require-default-props */
  2. import { h } from 'preact'
  3. import classNames from 'classnames'
  4. import type { I18n } from '@uppy/utils/lib/Translator'
  5. import type { CompanionFile } from '@uppy/utils/lib/CompanionFile'
  6. import type { RestrictionError } from '@uppy/core/lib/Restricter'
  7. import type { Meta, Body } from '@uppy/utils/lib/UppyFile'
  8. import ItemIcon from './components/ItemIcon.tsx'
  9. import GridItem from './components/GridItem.tsx'
  10. import ListItem from './components/ListItem.tsx'
  11. type ItemProps<M extends Meta, B extends Body> = {
  12. showTitles: boolean
  13. i18n: I18n
  14. id: string
  15. title: string
  16. toggleCheckbox: (event: Event) => void
  17. recordShiftKeyPress: (event: KeyboardEvent | MouseEvent) => void
  18. handleFolderClick?: () => void
  19. restrictionError?: RestrictionError<M, B> | null
  20. isCheckboxDisabled: boolean
  21. type: 'folder' | 'file'
  22. author?: CompanionFile['author']
  23. getItemIcon: () => string
  24. isChecked: boolean
  25. isDisabled: boolean
  26. viewType: string
  27. }
  28. export default function Item<M extends Meta, B extends Body>(
  29. props: ItemProps<M, B>,
  30. ): h.JSX.Element {
  31. const { author, getItemIcon, isChecked, isDisabled, viewType } = props
  32. const itemIconString = getItemIcon()
  33. const className = classNames(
  34. 'uppy-ProviderBrowserItem',
  35. { 'uppy-ProviderBrowserItem--selected': isChecked },
  36. { 'uppy-ProviderBrowserItem--disabled': isDisabled },
  37. { 'uppy-ProviderBrowserItem--noPreview': itemIconString === 'video' },
  38. )
  39. const itemIconEl = <ItemIcon itemIconString={itemIconString} />
  40. switch (viewType) {
  41. case 'grid':
  42. return (
  43. <GridItem<M, B>
  44. // eslint-disable-next-line react/jsx-props-no-spreading
  45. {...props}
  46. className={className}
  47. itemIconEl={itemIconEl}
  48. />
  49. )
  50. case 'list':
  51. return (
  52. <ListItem<M, B>
  53. // eslint-disable-next-line react/jsx-props-no-spreading
  54. {...props}
  55. className={className}
  56. itemIconEl={itemIconEl}
  57. />
  58. )
  59. case 'unsplash':
  60. return (
  61. <GridItem<M, B>
  62. // eslint-disable-next-line react/jsx-props-no-spreading
  63. {...props}
  64. className={className}
  65. itemIconEl={itemIconEl}
  66. >
  67. <a
  68. href={`${author!.url}?utm_source=Companion&utm_medium=referral`}
  69. target="_blank"
  70. rel="noopener noreferrer"
  71. className="uppy-ProviderBrowserItem-author"
  72. tabIndex={-1}
  73. >
  74. {author!.name}
  75. </a>
  76. </GridItem>
  77. )
  78. default:
  79. throw new Error(`There is no such type ${viewType}`)
  80. }
  81. }