chat.stories.js 2.3 KB

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