file-view-refresh-button.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import {
  2. type Dispatch,
  3. type SetStateAction,
  4. type ElementType,
  5. useCallback,
  6. useState,
  7. } from 'react'
  8. import { useTranslation } from 'react-i18next'
  9. import { postJSON } from '@/infrastructure/fetch-json'
  10. import { useProjectContext } from '@/shared/context/project-context'
  11. import type { BinaryFile } from '../types/binary-file'
  12. import { Nullable } from '../../../../../types/utils'
  13. import importOverleafModules from '../../../../macros/import-overleaf-module.macro'
  14. import OLButton from '@/shared/components/ol/ol-button'
  15. import { sendMB } from '@/infrastructure/event-tracking'
  16. import useIsMounted from '@/shared/hooks/use-is-mounted'
  17. import clientId from '@/utils/client-id'
  18. import { useReferencesContext } from '@/features/ide-react/context/references-context'
  19. import { useFeatureFlag } from '@/shared/context/split-test-context'
  20. type FileViewRefreshButtonProps = {
  21. setRefreshError: Dispatch<SetStateAction<Nullable<string>>>
  22. file: BinaryFile
  23. }
  24. const tprFileViewRefreshButton = importOverleafModules(
  25. 'tprFileViewRefreshButton'
  26. ) as {
  27. import: { TPRFileViewRefreshButton: ElementType }
  28. path: string
  29. }[]
  30. export default function FileViewRefreshButton({
  31. setRefreshError,
  32. file,
  33. }: FileViewRefreshButtonProps) {
  34. const { projectId } = useProjectContext()
  35. const [refreshing, setRefreshing] = useState(false)
  36. const isMountedRef = useIsMounted()
  37. const { indexAllReferences } = useReferencesContext()
  38. const clientSideReferences = useFeatureFlag('client-side-references')
  39. const refreshFile = useCallback(
  40. (isTPR: Nullable<boolean>) => {
  41. setRefreshing(true)
  42. // Replacement of the file handled by the file tree
  43. window.expectingLinkedFileRefreshedSocketFor = file.name
  44. const shouldReindexReferences = isTPR || /\.bib$/.test(file.name)
  45. const body = {
  46. shouldReindexReferences,
  47. clientId: clientId.get(),
  48. }
  49. postJSON(`/project/${projectId}/linked_file/${file.id}/refresh`, {
  50. body,
  51. })
  52. .then(() => {
  53. if (isMountedRef.current) {
  54. setRefreshing(false)
  55. }
  56. if (clientSideReferences && shouldReindexReferences) {
  57. indexAllReferences(false)
  58. }
  59. sendMB('refresh-linked-file', {
  60. provider: file.linkedFileData?.provider,
  61. })
  62. })
  63. .catch(err => {
  64. if (isMountedRef.current) {
  65. setRefreshing(false)
  66. setRefreshError(err.data?.message || err.message)
  67. }
  68. })
  69. },
  70. [
  71. file,
  72. projectId,
  73. setRefreshError,
  74. isMountedRef,
  75. indexAllReferences,
  76. clientSideReferences,
  77. ]
  78. )
  79. if (tprFileViewRefreshButton.length > 0) {
  80. return tprFileViewRefreshButton.map(
  81. ({ import: { TPRFileViewRefreshButton }, path }) => (
  82. <TPRFileViewRefreshButton
  83. key={path}
  84. file={file}
  85. refreshFile={refreshFile}
  86. refreshing={refreshing}
  87. />
  88. )
  89. )[0]
  90. } else {
  91. return (
  92. <FileViewRefreshButtonDefault
  93. refreshFile={refreshFile}
  94. refreshing={refreshing}
  95. />
  96. )
  97. }
  98. }
  99. type FileViewRefreshButtonDefaultProps = {
  100. refreshFile: (isTPR: Nullable<boolean>) => void
  101. refreshing: boolean
  102. }
  103. function FileViewRefreshButtonDefault({
  104. refreshFile,
  105. refreshing,
  106. }: FileViewRefreshButtonDefaultProps) {
  107. const { t } = useTranslation()
  108. return (
  109. <OLButton
  110. variant="primary"
  111. onClick={() => refreshFile(null)}
  112. disabled={refreshing}
  113. isLoading={refreshing}
  114. loadingLabel={t('refreshing')}
  115. >
  116. {t('refresh')}
  117. </OLButton>
  118. )
  119. }