|
| 1 | +import { useMutation, useQuery } from "@tanstack/react-query"; |
| 2 | +import { useSubscription } from "@trpc/tanstack-react-query"; |
1 | 3 | import { useEffect, useRef, useState } from "react"; |
2 | 4 | import type { QuestionResponse } from "server/src/live/question"; |
3 | 5 | import type { |
@@ -34,41 +36,30 @@ export function useVoterState(props: { |
34 | 36 | roomId: string; |
35 | 37 | votingKey: string; |
36 | 38 | }): VotingPageState { |
37 | | - const [state, setState] = useState<VoterState | null>(null); |
38 | 39 | const [lastVote, setLastVote] = useState<LastVote | null>(null); |
39 | 40 | const voteLock = useRef<Promise<void>>(Promise.resolve()); |
40 | 41 |
|
41 | | - const castVoteMutation = trpc.vote.castVote.useMutation(); |
| 42 | + const castVoteMutation = useMutation(trpc.vote.castVote.mutationOptions()) |
42 | 43 |
|
43 | 44 | const runSyncAsync = async (fn: () => Promise<void>) => { |
44 | | - const promise = voteLock.current.catch(() => {}).then(fn); |
| 45 | + const promise = voteLock.current.catch(() => { }).then(fn); |
45 | 46 | voteLock.current = promise; |
46 | 47 | await promise; |
47 | 48 | }; |
48 | 49 |
|
49 | | - trpc.vote.listen.useSubscription( |
50 | | - { roomId: props.roomId, votingKey: props.votingKey }, |
51 | | - { |
52 | | - onData: (data) => { |
53 | | - setState(data); |
54 | | - }, |
55 | | - onError: (err) => { |
56 | | - console.error(err); |
57 | | - }, |
58 | | - }, |
59 | | - ); |
| 50 | + const subscription = useSubscription(trpc.vote.listen.subscriptionOptions({ roomId: props.roomId, votingKey: props.votingKey })) |
60 | 51 |
|
61 | 52 | const { isInitialVoteLoading } = useFetchInitialVote( |
62 | 53 | props, |
63 | | - state, |
| 54 | + subscription.data ?? null, |
64 | 55 | setLastVote, |
65 | 56 | ); |
66 | 57 |
|
67 | | - if (!state) { |
| 58 | + if (!subscription.data) { |
68 | 59 | return VotingPageState.loading({}); |
69 | 60 | } |
70 | 61 |
|
71 | | - return VoterState.match<VotingPageState>(state, { |
| 62 | + return VoterState.match<VotingPageState>(subscription.data, { |
72 | 63 | blank: () => VotingPageState.waiting({}), |
73 | 64 | ended: () => VotingPageState.ended({}), |
74 | 65 | kicked: () => VotingPageState.kicked({}), |
@@ -131,20 +122,17 @@ function useFetchInitialVote( |
131 | 122 | InitialVoteFetchState.waitingForVoterState({}), |
132 | 123 | ); |
133 | 124 |
|
134 | | - const initialVoteQuery = trpc.vote.getMyVote.useQuery( |
135 | | - { |
136 | | - roomId: props.roomId, |
137 | | - votingKey: props.votingKey, |
138 | | - |
139 | | - // If we're not fetching, then the query is disabled anyway and this arg doesnt matter |
140 | | - questionId: InitialVoteFetchState.is.fetching(fetchState) |
141 | | - ? fetchState.questionId |
142 | | - : "", |
143 | | - }, |
144 | | - { |
145 | | - enabled: InitialVoteFetchState.is.fetching(fetchState), |
146 | | - }, |
147 | | - ); |
| 125 | + const initialVoteQuery = useQuery(trpc.vote.getMyVote.queryOptions({ |
| 126 | + roomId: props.roomId, |
| 127 | + votingKey: props.votingKey, |
| 128 | + |
| 129 | + // If we're not fetching, then the query is disabled anyway and this arg doesnt matter |
| 130 | + questionId: InitialVoteFetchState.is.fetching(fetchState) |
| 131 | + ? fetchState.questionId |
| 132 | + : "", |
| 133 | + }, { |
| 134 | + enabled: InitialVoteFetchState.is.fetching(fetchState), |
| 135 | + })) |
148 | 136 |
|
149 | 137 | useEffect(() => { |
150 | 138 | if ( |
|
0 commit comments