chat.stories.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import React from 'react'
  2. import { v4 as uuid } from 'uuid'
  3. import { ContextRoot } from '../js/shared/context/root-context'
  4. import ChatPane from '../js/features/chat/components/chat-pane'
  5. import { stubMathJax } from '../../test/frontend/features/chat/components/stubs'
  6. import { setupContext } from './fixtures/context'
  7. import useFetchMock from './hooks/use-fetch-mock'
  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 => {
  39. useFetchMock(fetchMock => {
  40. fetchMock.get(/messages/, generateMessages(35)).post(/messages/, {})
  41. })
  42. return <ChatPane {...args} />
  43. }
  44. export const NoMessages = args => {
  45. useFetchMock(fetchMock => {
  46. fetchMock.get(/messages/, [])
  47. })
  48. return <ChatPane {...args} />
  49. }
  50. export const Loading = args => {
  51. useFetchMock(fetchMock => {
  52. fetchMock.get(/messages/, generateMessages(6), {
  53. delay: 1000 * 10,
  54. })
  55. })
  56. return <ChatPane {...args} />
  57. }
  58. export default {
  59. title: 'Chat',
  60. component: ChatPane,
  61. argTypes: {
  62. resetUnreadMessages: { action: 'resetUnreadMessages' },
  63. },
  64. args: {
  65. resetUnreadMessages: () => {},
  66. },
  67. decorators: [
  68. Story => (
  69. <>
  70. <style>{'html, body, .chat { height: 100%; width: 100%; }'}</style>
  71. <ContextRoot ide={window._ide} settings={{}}>
  72. <Story />
  73. </ContextRoot>
  74. </>
  75. ),
  76. ],
  77. }