chat.stories.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React from 'react'
  2. import fetchMock from 'fetch-mock'
  3. import { v4 as uuid } from 'uuid'
  4. import { ContextRoot } from '../js/shared/context/root-context'
  5. import ChatPane from '../js/features/chat/components/chat-pane'
  6. import { stubMathJax } from '../../test/frontend/features/chat/components/stubs'
  7. import { setupContext } from './fixtures/context'
  8. const ONE_MINUTE = 60 * 1000
  9. const user = {
  10. id: 'fake_user',
  11. first_name: 'mortimer',
  12. email: 'fake@example.com'
  13. }
  14. const user2 = {
  15. id: 'another_fake_user',
  16. first_name: 'leopold',
  17. email: 'another_fake@example.com'
  18. }
  19. function generateMessages(count) {
  20. const messages = []
  21. let timestamp = new Date().getTime() // newest message goes first
  22. for (let i = 0; i <= count; i++) {
  23. const author = Math.random() > 0.5 ? user : user2
  24. // modify the timestamp so the previous message has 70% chances to be within 5 minutes from
  25. // the current one, for grouping purposes
  26. timestamp -= (4.3 + Math.random()) * ONE_MINUTE
  27. messages.push({
  28. id: uuid(),
  29. content: `message #${i}`,
  30. user: author,
  31. timestamp
  32. })
  33. }
  34. return messages
  35. }
  36. stubMathJax()
  37. setupContext()
  38. export const Conversation = args => <ChatPane {...args} />
  39. Conversation.parameters = {
  40. setupMocks: () => {
  41. fetchMock.restore()
  42. fetchMock.get(/messages/, generateMessages(35))
  43. fetchMock.post(/messages/, {})
  44. }
  45. }
  46. export const NoMessages = args => <ChatPane {...args} />
  47. NoMessages.parameters = {
  48. setupMocks: () => {
  49. fetchMock.restore()
  50. fetchMock.get(/messages/, [])
  51. }
  52. }
  53. export const Loading = args => <ChatPane {...args} />
  54. Loading.parameters = {
  55. setupMocks: () => {
  56. fetchMock.restore()
  57. fetchMock.get(/messages/, generateMessages(6), {
  58. delay: 1000 * 10
  59. })
  60. }
  61. }
  62. export default {
  63. title: 'Chat',
  64. component: ChatPane,
  65. argTypes: {
  66. resetUnreadMessages: { action: 'resetUnreadMessages' }
  67. },
  68. args: {
  69. resetUnreadMessages: () => {}
  70. },
  71. decorators: [
  72. (Story, { parameters: { setupMocks } }) => {
  73. if (setupMocks) setupMocks()
  74. return <Story />
  75. },
  76. Story => (
  77. <>
  78. <style>{'html, body, .chat { height: 100%; width: 100%; }'}</style>
  79. <ContextRoot ide={window._ide} settings={{}}>
  80. <Story />
  81. </ContextRoot>
  82. </>
  83. )
  84. ]
  85. }