|
| 1 | +// @flow |
| 2 | + |
| 3 | +import React from 'react'; |
| 4 | +import graphql from 'babel-plugin-relay/macro'; |
| 5 | +import { |
| 6 | + commitMutation, |
| 7 | + createPaginationContainer, |
| 8 | + type RelayProp, |
| 9 | +} from 'react-relay'; |
| 10 | +import {ConnectionHandler} from 'relay-runtime'; |
| 11 | +import {PostBox, ReactionBar} from './Post'; |
| 12 | +import type {Comments_post} from './__generated__/Comments_post.graphql'; |
| 13 | +import LoadingSpinner from './loadingSpinner'; |
| 14 | +import MarkdownRenderer from './MarkdownRenderer'; |
| 15 | +import idx from 'idx'; |
| 16 | +import {Box, Heading, Text, TextArea, Tabs, Tab, Button, Stack} from 'grommet'; |
| 17 | +import {formatDistance, format} from 'date-fns'; |
| 18 | +import Comment from './Comment'; |
| 19 | +import {NotificationContext} from './Notifications'; |
| 20 | +import UserContext from './UserContext'; |
| 21 | +import GitHubLoginButton from './GitHubLoginButton'; |
| 22 | + |
| 23 | +type Props = { |
| 24 | + relay: RelayProp, |
| 25 | + post: Comments_post, |
| 26 | + postId: string, |
| 27 | +}; |
| 28 | + |
| 29 | +const addCommentMutation = graphql` |
| 30 | + mutation Comments_AddCommentMutation($input: GitHubAddCommentInput!) { |
| 31 | + gitHub { |
| 32 | + addComment(input: $input) { |
| 33 | + commentEdge { |
| 34 | + node { |
| 35 | + ...Comment_comment |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +`; |
| 42 | + |
| 43 | +function Comments({post, relay, postId}: Props) { |
| 44 | + const {error: notifyError} = React.useContext(NotificationContext); |
| 45 | + const {isLoggedIn, login} = React.useContext(UserContext); |
| 46 | + |
| 47 | + const [comment, setComment] = React.useState(''); |
| 48 | + const [saving, setSaving] = React.useState(false); |
| 49 | + |
| 50 | + const comments = []; |
| 51 | + for (const edge of post.comments.edges || []) { |
| 52 | + if (edge && edge.node) { |
| 53 | + comments.push(edge.node); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + const saveComment = () => { |
| 58 | + setSaving(true); |
| 59 | + commitMutation(relay.environment, { |
| 60 | + mutation: addCommentMutation, |
| 61 | + variables: { |
| 62 | + input: { |
| 63 | + body: comment, |
| 64 | + subjectId: postId, |
| 65 | + }, |
| 66 | + }, |
| 67 | + onCompleted: () => { |
| 68 | + setSaving(false); |
| 69 | + setComment(''); |
| 70 | + }, |
| 71 | + onError: err => notifyError('Error saving comment. Please try again.'), |
| 72 | + updater(store, data) { |
| 73 | + const newComment = store.get( |
| 74 | + data.gitHub.addComment.commentEdge.node.__id, |
| 75 | + ); |
| 76 | + const post = store.get(postId); |
| 77 | + const ch = ConnectionHandler; |
| 78 | + const comments = ConnectionHandler.getConnection( |
| 79 | + post, |
| 80 | + 'Comments_post_comments', |
| 81 | + ); |
| 82 | + const edge = ConnectionHandler.createEdge( |
| 83 | + store, |
| 84 | + comments, |
| 85 | + newComment, |
| 86 | + 'GitHubIssueComment', |
| 87 | + ); |
| 88 | + ConnectionHandler.insertEdgeAfter(comments, edge); |
| 89 | + }, |
| 90 | + }); |
| 91 | + }; |
| 92 | + |
| 93 | + return ( |
| 94 | + <Box id="comments"> |
| 95 | + {comments.map(comment => { |
| 96 | + return <Comment comment={comment} />; |
| 97 | + })} |
| 98 | + <PostBox> |
| 99 | + <Stack |
| 100 | + guidingChild="first" |
| 101 | + interactiveChild={isLoggedIn ? 'first' : 'last'} |
| 102 | + anchor="center"> |
| 103 | + <Box style={{opacity: isLoggedIn ? 1 : 0.3}}> |
| 104 | + <Tabs justify="start"> |
| 105 | + <Tab title={<Text size="small">Write</Text>}> |
| 106 | + <Box pad="small" height="small"> |
| 107 | + <TextArea |
| 108 | + disabled={saving} |
| 109 | + placeholder="Leave a comment (supports markdown)" |
| 110 | + value={comment} |
| 111 | + style={{height: '100%', fontWeight: 'normal'}} |
| 112 | + onChange={e => setComment(e.target.value)} |
| 113 | + /> |
| 114 | + </Box> |
| 115 | + </Tab> |
| 116 | + <Tab title={<Text size="small">Preview</Text>}> |
| 117 | + <Box pad="small" height={{min: 'small'}}> |
| 118 | + <MarkdownRenderer |
| 119 | + escapeHtml={true} |
| 120 | + source={comment.trim() ? comment : 'Nothing to preview.'} |
| 121 | + /> |
| 122 | + </Box> |
| 123 | + </Tab> |
| 124 | + </Tabs> |
| 125 | + <Box |
| 126 | + border={{ |
| 127 | + size: 'xsmall', |
| 128 | + side: 'bottom', |
| 129 | + color: 'rgba(0,0,0,0.1)', |
| 130 | + }}> |
| 131 | + <Box pad="small" align="end"> |
| 132 | + <Button |
| 133 | + fill={false} |
| 134 | + label="Comment" |
| 135 | + onClick={saveComment} |
| 136 | + disabled={saving} |
| 137 | + /> |
| 138 | + </Box> |
| 139 | + </Box> |
| 140 | + </Box> |
| 141 | + |
| 142 | + <Box style={{visibility: isLoggedIn ? 'hidden' : 'visible'}}> |
| 143 | + <GitHubLoginButton onClick={login} label="Log in with GitHub" /> |
| 144 | + </Box> |
| 145 | + </Stack> |
| 146 | + </PostBox> |
| 147 | + |
| 148 | + <Box height="small" /> |
| 149 | + </Box> |
| 150 | + ); |
| 151 | +} |
| 152 | + |
| 153 | +export default createPaginationContainer( |
| 154 | + Comments, |
| 155 | + { |
| 156 | + post: graphql` |
| 157 | + fragment Comments_post on GitHubIssue |
| 158 | + @argumentDefinitions( |
| 159 | + count: {type: "Int", defaultValue: 100} |
| 160 | + cursor: {type: "String"} |
| 161 | + ) { |
| 162 | + comments(first: $count, after: $cursor) |
| 163 | + @connection(key: "Comments_post_comments") { |
| 164 | + edges { |
| 165 | + node { |
| 166 | + ...Comment_comment |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + `, |
| 172 | + }, |
| 173 | + { |
| 174 | + direction: 'forward', |
| 175 | + getConnectionFromProps(props) { |
| 176 | + return props.comments; |
| 177 | + }, |
| 178 | + getVariables(props, {count, cursor}, fragmentVariables) { |
| 179 | + return { |
| 180 | + count: count, |
| 181 | + cursor, |
| 182 | + // XXX: fix |
| 183 | + issueNumber: props.issue.number, |
| 184 | + }; |
| 185 | + }, |
| 186 | + query: graphql` |
| 187 | + query CommentsPaginationQuery( |
| 188 | + $count: Int! |
| 189 | + $cursor: String |
| 190 | + $issueNumber: Int! |
| 191 | + ) |
| 192 | + @persistedQueryConfiguration( |
| 193 | + accessToken: {environmentVariable: "OG_GITHUB_TOKEN"} |
| 194 | + ) { |
| 195 | + gitHub { |
| 196 | + repository(name: "onegraph-changelog", owner: "onegraph") { |
| 197 | + __typename |
| 198 | + issue(number: $issueNumber) { |
| 199 | + ...Comments_post @arguments(count: $count, cursor: $cursor) |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + `, |
| 205 | + }, |
| 206 | +); |
0 commit comments