figure-modal-source-picker.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { FC } from 'react'
  2. import {
  3. FigureModalSource,
  4. useFigureModalContext,
  5. } from './figure-modal-context'
  6. import Icon from '../../../../shared/components/icon'
  7. import { useTranslation } from 'react-i18next'
  8. import getMeta from '@/utils/meta'
  9. import MaterialIcon from '@/shared/components/material-icon'
  10. import BootstrapVersionSwitcher from '@/features/ui/components/bootstrap-5/bootstrap-version-switcher'
  11. import { bsVersion } from '@/features/utils/bootstrap-5'
  12. import { usePermissionsContext } from '@/features/ide-react/context/permissions-context'
  13. export const FigureModalSourcePicker: FC = () => {
  14. const { t } = useTranslation()
  15. const {
  16. hasLinkedProjectFileFeature,
  17. hasLinkedProjectOutputFileFeature,
  18. hasLinkUrlFeature,
  19. } = getMeta('ol-ExposedSettings')
  20. const { write } = usePermissionsContext()
  21. return (
  22. <div className="figure-modal-source-button-grid">
  23. {write && (
  24. <FigureModalSourceButton
  25. type={FigureModalSource.FILE_UPLOAD}
  26. title={t('replace_from_computer')}
  27. icon={bsVersion({ bs3: 'upload', bs5: 'upload' })}
  28. />
  29. )}
  30. <FigureModalSourceButton
  31. type={FigureModalSource.FILE_TREE}
  32. title={t('replace_from_project_files')}
  33. icon={bsVersion({ bs3: 'archive', bs5: 'inbox' })}
  34. />
  35. {write &&
  36. (hasLinkedProjectFileFeature || hasLinkedProjectOutputFileFeature) && (
  37. <FigureModalSourceButton
  38. type={FigureModalSource.OTHER_PROJECT}
  39. title={t('replace_from_another_project')}
  40. icon={bsVersion({ bs3: 'folder-open', bs5: 'folder_open' })}
  41. />
  42. )}
  43. {write && hasLinkUrlFeature && (
  44. <FigureModalSourceButton
  45. type={FigureModalSource.FROM_URL}
  46. title={t('replace_from_url')}
  47. icon={bsVersion({ bs3: 'globe', bs5: 'public' })}
  48. />
  49. )}
  50. </div>
  51. )
  52. }
  53. const FigureModalSourceButton: FC<{
  54. type: FigureModalSource
  55. title: string
  56. icon: string
  57. }> = ({ type, title, icon }) => {
  58. const { dispatch } = useFigureModalContext()
  59. return (
  60. <button
  61. type="button"
  62. className="figure-modal-source-button"
  63. onClick={() => {
  64. dispatch({ source: type, sourcePickerShown: false, getPath: undefined })
  65. }}
  66. >
  67. <BootstrapVersionSwitcher
  68. bs3={
  69. <Icon type={icon} className="figure-modal-source-button-icon" fw />
  70. }
  71. bs5={
  72. <MaterialIcon
  73. type={icon}
  74. className="figure-modal-source-button-icon"
  75. />
  76. }
  77. />
  78. <span className="figure-modal-source-button-title">{title}</span>
  79. <BootstrapVersionSwitcher
  80. bs3={<Icon type="chevron-right" fw />}
  81. bs5={<MaterialIcon type="chevron_right" />}
  82. />
  83. </button>
  84. )
  85. }