file-tree.stories.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { rootFolderBase } from './fixtures/file-tree-base'
  2. import { rootFolderLimit } from './fixtures/file-tree-limit'
  3. import FileTreeRoot from '../js/features/file-tree/components/file-tree-root'
  4. import FileTreeError from '../js/features/file-tree/components/file-tree-error'
  5. import useFetchMock from './hooks/use-fetch-mock'
  6. import { ScopeDecorator } from './decorators/scope'
  7. import { useScope } from './hooks/use-scope'
  8. import { useIdeContext } from '@/shared/context/ide-context'
  9. const MOCK_DELAY = 2000
  10. const DEFAULT_PROJECT = {
  11. _id: '123abc',
  12. name: 'Some Project',
  13. rootDocId: '5e74f1a7ce17ae0041dfd056',
  14. rootFolder: rootFolderBase,
  15. }
  16. function defaultSetupMocks(fetchMock, socket) {
  17. fetchMock
  18. .post(
  19. /\/project\/\w+\/(file|doc|folder)\/\w+\/rename/,
  20. (path, req) => {
  21. const body = JSON.parse(req.body)
  22. const entityId = path.match(/([^/]+)\/rename$/)[1]
  23. socket.emitToClient('reciveEntityRename', entityId, body.name)
  24. return 204
  25. },
  26. {
  27. delay: MOCK_DELAY,
  28. }
  29. )
  30. .post(
  31. /\/project\/\w+\/folder/,
  32. (_path, req) => {
  33. const body = JSON.parse(req.body)
  34. const newFolder = {
  35. folders: [],
  36. fileRefs: [],
  37. docs: [],
  38. _id: Math.random().toString(16).replace(/0\./, 'random-test-id-'),
  39. name: body.name,
  40. }
  41. socket.emitToClient('reciveNewFolder', body.parent_folder_id, newFolder)
  42. return newFolder
  43. },
  44. {
  45. delay: MOCK_DELAY,
  46. }
  47. )
  48. .delete(
  49. /\/project\/\w+\/(file|doc|folder)\/\w+/,
  50. path => {
  51. const entityId = path.match(/[^/]+$/)[0]
  52. socket.emitToClient('removeEntity', entityId)
  53. return 204
  54. },
  55. {
  56. delay: MOCK_DELAY,
  57. }
  58. )
  59. .post(/\/project\/\w+\/(file|doc|folder)\/\w+\/move/, (path, req) => {
  60. const body = JSON.parse(req.body)
  61. const entityId = path.match(/([^/]+)\/move/)[1]
  62. socket.emitToClient('reciveEntityMove', entityId, body.folder_id)
  63. return 204
  64. })
  65. }
  66. export const FullTree = args => {
  67. const { socket } = useIdeContext()
  68. useFetchMock(fetchMock => defaultSetupMocks(fetchMock, socket))
  69. useScope({
  70. project: DEFAULT_PROJECT,
  71. permissionsLevel: 'owner',
  72. })
  73. return <FileTreeRoot {...args} />
  74. }
  75. export const ReadOnly = args => {
  76. useScope({
  77. project: DEFAULT_PROJECT,
  78. permissionsLevel: 'readOnly',
  79. })
  80. return <FileTreeRoot {...args} />
  81. }
  82. export const Disconnected = args => {
  83. useScope({
  84. project: DEFAULT_PROJECT,
  85. permissionsLevel: 'owner',
  86. })
  87. return <FileTreeRoot {...args} />
  88. }
  89. Disconnected.args = { isConnected: false }
  90. export const NetworkErrors = args => {
  91. useFetchMock(fetchMock => {
  92. fetchMock
  93. .post(/\/project\/\w+\/folder/, 500, {
  94. delay: MOCK_DELAY,
  95. })
  96. .post(/\/project\/\w+\/(file|doc|folder)\/\w+\/rename/, 500, {
  97. delay: MOCK_DELAY,
  98. })
  99. .post(/\/project\/\w+\/(file|doc|folder)\/\w+\/move/, 500, {
  100. delay: MOCK_DELAY,
  101. })
  102. .delete(/\/project\/\w+\/(file|doc|folder)\/\w+/, 500, {
  103. delay: MOCK_DELAY,
  104. })
  105. })
  106. useScope({
  107. project: DEFAULT_PROJECT,
  108. permissionsLevel: 'owner',
  109. })
  110. return <FileTreeRoot {...args} />
  111. }
  112. export const FallbackError = args => {
  113. useScope({
  114. project: DEFAULT_PROJECT,
  115. })
  116. return <FileTreeError {...args} />
  117. }
  118. export const FilesLimit = args => {
  119. const { socket } = useIdeContext()
  120. useFetchMock(fetchMock => defaultSetupMocks(fetchMock, socket))
  121. useScope({
  122. project: {
  123. ...DEFAULT_PROJECT,
  124. rootFolder: rootFolderLimit,
  125. },
  126. permissionsLevel: 'owner',
  127. })
  128. return <FileTreeRoot {...args} />
  129. }
  130. export default {
  131. title: 'Editor / File Tree',
  132. component: FileTreeRoot,
  133. args: {
  134. setStartedFreeTrial: () => {
  135. console.log('started free trial')
  136. },
  137. refProviders: {},
  138. setRefProviderEnabled: provider => {
  139. console.log(`ref provider ${provider} enabled`)
  140. },
  141. isConnected: true,
  142. },
  143. argTypes: {
  144. onInit: { action: 'onInit' },
  145. onSelect: { action: 'onSelect' },
  146. },
  147. decorators: [
  148. ScopeDecorator,
  149. Story => (
  150. <>
  151. <style>{'html, body, .file-tree { height: 100%; width: 100%; }'}</style>
  152. <div className="editor-sidebar full-size">
  153. <div className="file-tree">
  154. <Story />
  155. </div>
  156. </div>
  157. </>
  158. ),
  159. ],
  160. }