|
|
|
|
|
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<ThreadInfoType>, |
|
|
...$Exact<ThreadMessageConnectionType>, |
|
|
}; |
|
|
|
|
|
type Variables = { |
|
|
id: string, |
|
|
last?: number, |
|
|
first?: number, |
|
|
after?: string, |
|
|
before?: string, |
|
|
}; |
|
|
|
|
|
const getVariables = ({ thread, ...props }): Variables => { |
|
|
|
|
|
if (thread && thread.messageCount <= 25) { |
|
|
return { |
|
|
id: props.id, |
|
|
last: 25, |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
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 (props.isWatercooler) { |
|
|
return { |
|
|
id: props.id, |
|
|
last: 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 = { |
|
|
|
|
|
options: props => { |
|
|
return { |
|
|
variables: getVariables(props), |
|
|
fetchPolicy: 'cache-and-network', |
|
|
}; |
|
|
}, |
|
|
|
|
|
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 |
|
|
); |
|
|
|