// @flow import gql from 'graphql-tag'; import { graphql } from 'react-apollo'; import queryString from 'query-string'; import { btoa } from 'b2a'; import threadInfoFragment from '../../fragments/thread/threadInfo'; import type { ThreadInfoType } from '../../fragments/thread/threadInfo'; import threadMessageConnectionFragment from '../../fragments/thread/threadMessageConnection'; import type { ThreadMessageConnectionType } from '../../fragments/thread/threadMessageConnection'; export type GetThreadMessageConnectionType = { ...$Exact, ...$Exact, }; type Variables = { id: string, last?: number, first?: number, after?: string, before?: string, }; const getVariables = ({ thread, ...props }): Variables => { // if the thread has less than 25 messages, just load all of them if (thread && thread.messageCount <= 25) { return { id: props.id, last: 25, }; } // If the user is linked to either a specific message or has pagination URL params, load those messages if (props.location && props.location.search) { const params = queryString.parse(props.location.search); if (params) { if (params.msgsafter) { return { id: props.id, after: params.msgsafter, first: 25, }; } else if (params.msgsbefore) { return { id: props.id, before: params.msgsbefore, last: 25, }; } else if (params.m) { return { id: props.id, after: params.m, first: 25, }; } } } // if it's a watercooler thread load the 25 most recent messages if (props.isWatercooler) { return { id: props.id, last: 25, }; } // In all other cases, load the first 25 return { id: props.id, first: 25, }; }; export const getThreadMessageConnectionQuery = gql` query getThreadMessages( $id: ID! $after: String $first: Int $before: String $last: Int ) { thread(id: $id) { ...threadInfo ...threadMessageConnection } } ${threadInfoFragment} ${threadMessageConnectionFragment} `; export const getThreadMessageConnectionOptions = { // $FlowFixMe options: props => { return { variables: getVariables(props), fetchPolicy: 'cache-and-network', }; }, // $FlowFixMe props: props => ({ data: { ...props.data, messageConnection: props.data.thread ? props.data.thread.messageConnection : null, }, loadNextPage: () => { let cursor; const { thread } = props.data; if ( thread && thread.messageConnection && thread.messageConnection.edges && thread.messageConnection.edges.length > 0 ) { cursor = thread.messageConnection.edges[ thread.messageConnection.edges.length - 1 ].cursor; } return props.data.fetchMore({ variables: { after: cursor, first: 25, before: undefined, last: undefined, }, updateQuery: (prev, { fetchMoreResult }) => { if (!fetchMoreResult || !fetchMoreResult.thread) return prev; return { ...prev, thread: { ...prev.thread, messageConnection: { ...prev.thread.messageConnection, pageInfo: { ...prev.thread.messageConnection.pageInfo, hasNextPage: fetchMoreResult.thread.messageConnection.pageInfo .hasNextPage, }, edges: [ ...prev.thread.messageConnection.edges, ...fetchMoreResult.thread.messageConnection.edges, ], }, }, }; }, }); }, loadPreviousPage: () => { let cursor; const { thread } = props.data; if ( thread && thread.messageConnection && thread.messageConnection.edges && thread.messageConnection.edges.length > 0 ) { cursor = thread.messageConnection.edges[0].cursor; } return props.data.fetchMore({ variables: { after: undefined, first: undefined, before: cursor, last: 25, }, updateQuery: (prev, { fetchMoreResult }) => { if (!fetchMoreResult || !fetchMoreResult.thread) return prev; const { messageConnection: { pageInfo, edges }, } = fetchMoreResult.thread; return { ...prev, thread: { ...prev.thread, messageConnection: { ...prev.thread.messageConnection, pageInfo: { ...prev.thread.messageConnection.pageInfo, hasPreviousPage: pageInfo.hasPreviousPage, }, edges: edges ? [...edges, ...prev.thread.messageConnection.edges] : prev.thread.messageConnection.edges, }, }, }; }, }); }, }), }; export default graphql( getThreadMessageConnectionQuery, getThreadMessageConnectionOptions );