2021-02-17 05:05:33 -05:00
|
|
|
import { v4 as uuid } from 'uuid'
|
|
|
|
|
2021-01-14 08:58:12 -05:00
|
|
|
import { ContextRoot } from '../js/shared/context/root-context'
|
|
|
|
import ChatPane from '../js/features/chat/components/chat-pane'
|
2021-02-22 11:21:43 -05:00
|
|
|
import { stubMathJax } from '../../test/frontend/features/chat/components/stubs'
|
2021-01-21 07:22:28 -05:00
|
|
|
import { setupContext } from './fixtures/context'
|
2021-05-11 10:25:22 -04:00
|
|
|
import useFetchMock from './hooks/use-fetch-mock'
|
2021-01-14 08:58:12 -05:00
|
|
|
|
|
|
|
const ONE_MINUTE = 60 * 1000
|
|
|
|
|
|
|
|
const user = {
|
|
|
|
id: 'fake_user',
|
|
|
|
first_name: 'mortimer',
|
2021-04-27 03:52:58 -04:00
|
|
|
email: 'fake@example.com',
|
2021-01-14 08:58:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const user2 = {
|
|
|
|
id: 'another_fake_user',
|
|
|
|
first_name: 'leopold',
|
2021-04-27 03:52:58 -04:00
|
|
|
email: 'another_fake@example.com',
|
2021-01-14 08:58:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function generateMessages(count) {
|
|
|
|
const messages = []
|
|
|
|
let timestamp = new Date().getTime() // newest message goes first
|
|
|
|
for (let i = 0; i <= count; i++) {
|
|
|
|
const author = Math.random() > 0.5 ? user : user2
|
|
|
|
// modify the timestamp so the previous message has 70% chances to be within 5 minutes from
|
|
|
|
// the current one, for grouping purposes
|
|
|
|
timestamp -= (4.3 + Math.random()) * ONE_MINUTE
|
|
|
|
|
|
|
|
messages.push({
|
2021-02-17 05:05:33 -05:00
|
|
|
id: uuid(),
|
2021-01-14 08:58:12 -05:00
|
|
|
content: `message #${i}`,
|
|
|
|
user: author,
|
2021-04-27 03:52:58 -04:00
|
|
|
timestamp,
|
2021-01-14 08:58:12 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return messages
|
|
|
|
}
|
|
|
|
|
|
|
|
stubMathJax()
|
2021-01-21 07:22:28 -05:00
|
|
|
setupContext()
|
2021-01-14 08:58:12 -05:00
|
|
|
|
2021-05-11 10:25:22 -04:00
|
|
|
export const Conversation = args => {
|
|
|
|
useFetchMock(fetchMock => {
|
|
|
|
fetchMock.get(/messages/, generateMessages(35)).post(/messages/, {})
|
|
|
|
})
|
|
|
|
|
|
|
|
return <ChatPane {...args} />
|
2021-01-14 08:58:12 -05:00
|
|
|
}
|
|
|
|
|
2021-05-11 10:25:22 -04:00
|
|
|
export const NoMessages = args => {
|
|
|
|
useFetchMock(fetchMock => {
|
2021-01-14 08:58:12 -05:00
|
|
|
fetchMock.get(/messages/, [])
|
2021-05-11 10:25:22 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
return <ChatPane {...args} />
|
2021-01-14 08:58:12 -05:00
|
|
|
}
|
|
|
|
|
2021-05-11 10:25:22 -04:00
|
|
|
export const Loading = args => {
|
|
|
|
useFetchMock(fetchMock => {
|
2021-01-14 08:58:12 -05:00
|
|
|
fetchMock.get(/messages/, generateMessages(6), {
|
2021-04-27 03:52:58 -04:00
|
|
|
delay: 1000 * 10,
|
2021-01-14 08:58:12 -05:00
|
|
|
})
|
2021-05-11 10:25:22 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
return <ChatPane {...args} />
|
2021-01-14 08:58:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
title: 'Chat',
|
|
|
|
component: ChatPane,
|
|
|
|
argTypes: {
|
2021-04-27 03:52:58 -04:00
|
|
|
resetUnreadMessages: { action: 'resetUnreadMessages' },
|
2021-01-14 08:58:12 -05:00
|
|
|
},
|
|
|
|
args: {
|
2021-04-27 03:52:58 -04:00
|
|
|
resetUnreadMessages: () => {},
|
2021-01-14 08:58:12 -05:00
|
|
|
},
|
|
|
|
decorators: [
|
|
|
|
Story => (
|
|
|
|
<>
|
|
|
|
<style>{'html, body, .chat { height: 100%; width: 100%; }'}</style>
|
2021-03-04 09:22:58 -05:00
|
|
|
<ContextRoot ide={window._ide} settings={{}}>
|
2021-01-14 08:58:12 -05:00
|
|
|
<Story />
|
|
|
|
</ContextRoot>
|
|
|
|
</>
|
2021-04-27 03:52:58 -04:00
|
|
|
),
|
|
|
|
],
|
2021-01-14 08:58:12 -05:00
|
|
|
}
|