file-view.stories.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import FileView from '../js/features/file-view/components/file-view'
  2. import useFetchMock from './hooks/use-fetch-mock'
  3. import { ScopeDecorator } from './decorators/scope'
  4. const bodies = {
  5. latex: `\\documentclass{article}
  6. \\begin{document}
  7. First document. This is a simple example, with no
  8. extra parameters or packages included.
  9. \\end{document}`,
  10. bibtex: `@book{latexcompanion,
  11. author = "Michel Goossens and Frank Mittelbach and Alexander Samarin",
  12. title = "The \\LaTeX\\ Companion",
  13. year = "1993",
  14. publisher = "Addison-Wesley",
  15. address = "Reading, Massachusetts"
  16. }`,
  17. text: `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.`,
  18. }
  19. const setupFetchMock = fetchMock => {
  20. return fetchMock
  21. .head('express:/project/:project_id/file/:file_id', {
  22. status: 201,
  23. headers: { 'Content-Length': 10000 },
  24. })
  25. .post('express:/project/:project_id/linked_file/:file_id/refresh', {
  26. status: 204,
  27. })
  28. .post('express:/project/:project_id/references/indexAll', {
  29. status: 204,
  30. })
  31. }
  32. const fileData = {
  33. id: 'file-id',
  34. name: 'file.tex',
  35. created: new Date().toISOString(),
  36. }
  37. export const FileFromUrl = args => {
  38. useFetchMock(fetchMock =>
  39. setupFetchMock(fetchMock).get(
  40. 'express:/project/:project_id/file/:file_id',
  41. { body: bodies.latex }
  42. )
  43. )
  44. return <FileView {...args} />
  45. }
  46. FileFromUrl.args = {
  47. file: {
  48. ...fileData,
  49. linkedFileData: {
  50. url: 'https://example.com/source-file.tex',
  51. provider: 'url',
  52. },
  53. },
  54. }
  55. export const FileFromProjectWithLinkableProjectId = args => {
  56. useFetchMock(fetchMock =>
  57. setupFetchMock(fetchMock).get(
  58. 'express:/project/:project_id/file/:file_id',
  59. { body: bodies.latex }
  60. )
  61. )
  62. return <FileView {...args} />
  63. }
  64. FileFromProjectWithLinkableProjectId.args = {
  65. file: {
  66. ...fileData,
  67. linkedFileData: {
  68. source_project_id: 'source-project-id',
  69. source_entity_path: '/source-file.tex',
  70. provider: 'project_file',
  71. },
  72. },
  73. }
  74. export const FileFromProjectWithoutLinkableProjectId = args => {
  75. useFetchMock(fetchMock =>
  76. setupFetchMock(fetchMock).get(
  77. 'express:/project/:project_id/file/:file_id',
  78. { body: bodies.latex }
  79. )
  80. )
  81. return <FileView {...args} />
  82. }
  83. FileFromProjectWithoutLinkableProjectId.args = {
  84. file: {
  85. ...fileData,
  86. linkedFileData: {
  87. v1_source_doc_id: 'v1-source-id',
  88. source_entity_path: '/source-file.tex',
  89. provider: 'project_file',
  90. },
  91. },
  92. }
  93. export const FileFromProjectOutputWithLinkableProject = args => {
  94. useFetchMock(fetchMock =>
  95. setupFetchMock(fetchMock).get(
  96. 'express:/project/:project_id/file/:file_id',
  97. { body: bodies.latex }
  98. )
  99. )
  100. return <FileView {...args} />
  101. }
  102. FileFromProjectOutputWithLinkableProject.args = {
  103. file: {
  104. ...fileData,
  105. linkedFileData: {
  106. source_project_id: 'source_project_id',
  107. source_output_file_path: '/source-file.tex',
  108. provider: 'project_output_file',
  109. },
  110. },
  111. }
  112. export const FileFromProjectOutputWithoutLinkableProjectId = args => {
  113. useFetchMock(fetchMock =>
  114. setupFetchMock(fetchMock).get(
  115. 'express:/project/:project_id/file/:file_id',
  116. { body: bodies.latex }
  117. )
  118. )
  119. return <FileView {...args} />
  120. }
  121. FileFromProjectOutputWithoutLinkableProjectId.args = {
  122. file: {
  123. ...fileData,
  124. linkedFileData: {
  125. v1_source_doc_id: 'v1-source-id',
  126. source_output_file_path: '/source-file.tex',
  127. provider: 'project_output_file',
  128. },
  129. },
  130. }
  131. export const ImageFile = args => {
  132. useFetchMock(setupFetchMock) // NOTE: can't mock img src request
  133. return <FileView {...args} />
  134. }
  135. ImageFile.storyName = 'Image File (Error)'
  136. ImageFile.args = {
  137. file: {
  138. ...fileData,
  139. id: '60097ca20454610027c442a8',
  140. name: 'file.jpg',
  141. linkedFileData: {
  142. source_project_id: 'source_project_id',
  143. source_entity_path: '/source-file.jpg',
  144. provider: 'project_file',
  145. },
  146. },
  147. }
  148. export const ThirdPartyReferenceFile = args => {
  149. useFetchMock(fetchMock =>
  150. setupFetchMock(fetchMock).get(
  151. 'express:/project/:project_id/file/:file_id',
  152. { body: bodies.bibtex }
  153. )
  154. )
  155. return <FileView {...args} />
  156. }
  157. ThirdPartyReferenceFile.args = {
  158. file: {
  159. ...fileData,
  160. name: 'references.bib',
  161. linkedFileData: {
  162. provider: 'zotero',
  163. },
  164. },
  165. }
  166. export const ThirdPartyReferenceFileWithError = args => {
  167. useFetchMock(fetchMock =>
  168. setupFetchMock(fetchMock).head(
  169. 'express:/project/:project_id/file/:file_id',
  170. { status: 500 },
  171. { overwriteRoutes: true }
  172. )
  173. )
  174. return <FileView {...args} />
  175. }
  176. ThirdPartyReferenceFileWithError.storyName =
  177. 'Third Party Reference File (Error)'
  178. ThirdPartyReferenceFileWithError.args = {
  179. file: {
  180. ...fileData,
  181. id: '500500500500500500500500',
  182. name: 'references.bib',
  183. linkedFileData: {
  184. provider: 'zotero',
  185. },
  186. },
  187. }
  188. export const TextFile = args => {
  189. useFetchMock(fetchMock =>
  190. setupFetchMock(fetchMock).get(
  191. 'express:/project/:project_id/file/:file_id',
  192. { body: bodies.text },
  193. { overwriteRoutes: true }
  194. )
  195. )
  196. return <FileView {...args} />
  197. }
  198. TextFile.args = {
  199. file: {
  200. ...fileData,
  201. linkedFileData: {
  202. source_project_id: 'source-project-id',
  203. source_entity_path: '/source-file.txt',
  204. provider: 'project_file',
  205. },
  206. name: 'file.txt',
  207. },
  208. }
  209. export const UploadedFile = args => {
  210. useFetchMock(fetchMock =>
  211. setupFetchMock(fetchMock).head(
  212. 'express:/project/:project_id/file/:file_id',
  213. { status: 500 },
  214. { overwriteRoutes: true }
  215. )
  216. )
  217. return <FileView {...args} />
  218. }
  219. UploadedFile.storyName = 'Uploaded File (Error)'
  220. UploadedFile.args = {
  221. file: {
  222. ...fileData,
  223. linkedFileData: null,
  224. name: 'file.jpg',
  225. },
  226. }
  227. export default {
  228. title: 'Editor / FileView',
  229. component: FileView,
  230. argTypes: {
  231. storeReferencesKeys: { action: 'store references keys' },
  232. },
  233. decorators: [ScopeDecorator],
  234. }