Skip to content

Commit 94f3ae5

Browse files
committed
wip: toggle via mutation, footer placement
1 parent cefa3b2 commit 94f3ae5

File tree

5 files changed

+53
-17
lines changed

5 files changed

+53
-17
lines changed

api/resolvers/user.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,14 @@ export default {
927927

928928
await models.user.update({ where: { id: me.id }, data: { diagnostics } })
929929
return diagnostics
930+
},
931+
toggleLiveComments: async (parent, { pauseLiveComments }, { me, models }) => {
932+
if (!me) {
933+
throw new GqlAuthenticationError()
934+
}
935+
936+
await models.user.update({ where: { id: me.id }, data: { pauseLiveComments } })
937+
return pauseLiveComments
930938
}
931939
},
932940

api/typeDefs/user.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export default gql`
5757
deleteApiKey(id: ID!): User
5858
disableFreebies: Boolean
5959
setDiagnostics(diagnostics: Boolean!): Boolean
60+
toggleLiveComments(pauseLiveComments: Boolean!): Boolean!
6061
}
6162
6263
type User {

components/comments.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ import MoreFooter from './more-footer'
1010
import { FULL_COMMENTS_THRESHOLD } from '@/lib/constants'
1111
import useLiveComments from './use-live-comments'
1212
import { useCommentsNavigatorContext } from './use-comments-navigator'
13-
import ActionTooltip from './action-tooltip'
1413

15-
export function CommentsHeader ({ handleSort, pinned, bio, parentCreatedAt, commentSats, pauseLiveComments, setPauseLiveComments }) {
14+
export function CommentsHeader ({ handleSort, pinned, bio, parentCreatedAt, commentSats }) {
1615
const router = useRouter()
1716
const sort = router.query.sort || defaultCommentSort(pinned, bio, parentCreatedAt)
1817

@@ -31,17 +30,6 @@ export function CommentsHeader ({ handleSort, pinned, bio, parentCreatedAt, comm
3130
<Nav.Item className='text-muted'>
3231
{numWithUnits(commentSats)}
3332
</Nav.Item>
34-
<Nav.Item>
35-
<Nav.Link
36-
eventKey='live'
37-
className={styles.navLink}
38-
onClick={() => setPauseLiveComments(!pauseLiveComments)}
39-
>
40-
<ActionTooltip notForm placement='top' overlayText={pauseLiveComments ? 'resume live comments' : 'pause live comments'}>
41-
<div className={`${styles.newCommentDot} ${pauseLiveComments ? styles.paused : ''}`} />
42-
</ActionTooltip>
43-
</Nav.Link>
44-
</Nav.Item>
4533
<div className='ms-auto d-flex'>
4634
<Nav.Item>
4735
<Nav.Link
@@ -83,7 +71,7 @@ export default function Comments ({
8371
const router = useRouter()
8472

8573
// fetch new comments that arrived after the lastCommentAt, and update the item.comments field in cache
86-
const { pauseLiveComments, setPauseLiveComments } = useLiveComments(parentId, lastCommentAt || parentCreatedAt, router.query.sort)
74+
useLiveComments(parentId, lastCommentAt || parentCreatedAt, router.query.sort)
8775

8876
// new comments navigator, tracks new comments and provides navigation controls
8977
const { navigator } = useCommentsNavigatorContext()
@@ -106,8 +94,6 @@ export default function Comments ({
10694
query: sort === defaultCommentSort(pinned, bio, parentCreatedAt) ? undefined : { sort }
10795
}, { scroll: false })
10896
}}
109-
pauseLiveComments={pauseLiveComments}
110-
setPauseLiveComments={setPauseLiveComments}
11197
/>
11298
: null}
11399
{pins.map(item => (

components/footer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Rewards from './footer-rewards'
1616
import useDarkMode from './dark-mode'
1717
import ActionTooltip from './action-tooltip'
1818
import { useAnimationEnabled } from '@/components/animation'
19+
import { useLiveCommentsToggle } from './use-live-comments'
1920

2021
const RssPopover = (
2122
<Popover>
@@ -147,6 +148,8 @@ export default function Footer ({ links = true }) {
147148

148149
const [animationEnabled, toggleAnimation] = useAnimationEnabled()
149150

151+
const [pauseLiveComments, toggleLiveComments] = useLiveCommentsToggle()
152+
150153
const DarkModeIcon = darkMode ? Sun : Moon
151154
const LnIcon = animationEnabled ? No : Bolt
152155

@@ -164,6 +167,11 @@ export default function Footer ({ links = true }) {
164167
<ActionTooltip notForm overlayText={`${animationEnabled ? 'disable' : 'enable'} lightning animations`}>
165168
<LnIcon onClick={toggleAnimation} width={20} height={20} className='ms-2 fill-grey theme' suppressHydrationWarning />
166169
</ActionTooltip>
170+
<ActionTooltip notForm overlayText={`${pauseLiveComments ? 'disable' : 'enable'} live comments`}>
171+
<div className='nav-link p-2 d-inline-flex' style={{ cursor: 'pointer' }} onClick={toggleLiveComments} suppressHydrationWarning>
172+
live comments {pauseLiveComments ? 'on' : 'off'}
173+
</div>
174+
</ActionTooltip>
167175
</div>
168176
<div className='mb-0' style={{ fontWeight: 500 }}>
169177
<Rewards />

components/use-live-comments.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import preserveScroll from './preserve-scroll'
22
import { GET_NEW_COMMENTS } from '../fragments/comments'
33
import { useEffect, useState } from 'react'
44
import { SSR, COMMENT_DEPTH_LIMIT } from '../lib/constants'
5-
import { useQuery, useApolloClient } from '@apollo/client'
5+
import { useQuery, useApolloClient, useMutation } from '@apollo/client'
66
import { commentsViewedAfterComment } from '../lib/new-comments'
7+
import { gql } from 'graphql-tag'
78
import {
89
updateItemQuery,
910
updateCommentFragment,
@@ -127,3 +128,35 @@ export default function useLiveComments (rootId, after, sort) {
127128

128129
return { pauseLiveComments, setPauseLiveComments }
129130
}
131+
132+
export function useLiveCommentsToggle () {
133+
const { me } = useMe()
134+
const [pauseLiveComments, setPauseLiveComments] = useState(me?.privates?.pauseLiveComments)
135+
136+
const [mutate] = useMutation(
137+
gql`
138+
mutation toggleLiveComments($pauseLiveComments: Boolean!) {
139+
toggleLiveComments(pauseLiveComments: $pauseLiveComments)
140+
}`, {
141+
update (cache, { data: { toggleLiveComments } }) {
142+
cache.modify({
143+
id: `User:${me.id}`,
144+
fields: {
145+
privates: (existing) => ({
146+
...existing,
147+
pauseLiveComments: toggleLiveComments
148+
})
149+
},
150+
optimistic: true
151+
})
152+
setPauseLiveComments(toggleLiveComments)
153+
}
154+
}
155+
)
156+
157+
const toggle = () => {
158+
mutate({ variables: { pauseLiveComments: !pauseLiveComments } })
159+
}
160+
161+
return [pauseLiveComments, toggle]
162+
}

0 commit comments

Comments
 (0)