|
1 |
| -import { |
2 |
| - ApolloClient, |
3 |
| - ApolloProvider, |
4 |
| - gql, |
5 |
| - HttpLink, |
6 |
| - InMemoryCache, |
7 |
| - split, |
8 |
| - useSubscription |
9 |
| -} from '@apollo/client'; |
10 |
| -import AJPrimary from '../components/AJPrimary'; |
11 |
| -import { SubscriptionClient } from 'subscriptions-transport-ws'; |
12 |
| -import { WebSocketLink } from '@apollo/client/link/ws'; |
13 |
| -import { getMainDefinition } from '@apollo/client/utilities'; |
14 |
| -import { useEffect, useRef, useState } from 'react'; |
15 |
| - |
16 |
| -const httpLink = new HttpLink({ |
17 |
| - uri: 'https://codingcat-twitch.onrender.com/graphql' |
18 |
| -}); |
19 |
| - |
20 |
| -const wsLink = new WebSocketLink( |
21 |
| - new SubscriptionClient('wss:codingcat-twitch.onrender.com/graphql') |
22 |
| -); |
23 |
| - |
24 |
| -// The split function takes three parameters: |
25 |
| -// |
26 |
| -// * A function that's called for each operation to execute |
27 |
| -// * The Link to use for an operation if the function returns a "truthy" value |
28 |
| -// * The Link to use for an operation if the function returns a "falsy" value |
29 |
| -const splitLink = split( |
30 |
| - ({ query }) => { |
31 |
| - const definition = getMainDefinition(query); |
32 |
| - return definition.kind === 'OperationDefinition' && definition.operation === 'subscription'; |
33 |
| - }, |
34 |
| - wsLink, |
35 |
| - httpLink |
36 |
| -); |
37 |
| - |
38 |
| -const client = new ApolloClient({ |
39 |
| - link: splitLink, |
40 |
| - cache: new InMemoryCache() |
41 |
| -}); |
42 |
| - |
43 |
| -const MESSAGES_SUBSCRIPTION = gql` |
44 |
| - subscription getMessages($username: String!) { |
45 |
| - message(channel: $username) { |
46 |
| - id |
47 |
| - message |
48 |
| - author { |
49 |
| - username |
50 |
| - roles |
51 |
| - } |
52 |
| - emotes { |
53 |
| - id |
54 |
| - name |
55 |
| - locations |
56 |
| - images { |
57 |
| - small |
58 |
| - medium |
59 |
| - large |
60 |
| - } |
61 |
| - } |
62 |
| - time |
63 |
| - } |
64 |
| - } |
65 |
| -`; |
| 1 | +import { createSocketStudioClient, SocketStudioProvider } from '@socket-studio/preact'; |
| 2 | +import { useState } from 'react'; |
| 3 | +import AJPrimary from './AJPrimary'; |
| 4 | +import { Chat } from './Chat'; |
66 | 5 |
|
67 | 6 | export default function Home() {
|
68 |
| - const { data, loading } = useSubscription(MESSAGES_SUBSCRIPTION, { |
69 |
| - client, |
70 |
| - variables: { username: 'codingcatdev' } |
71 |
| - }); |
72 |
| - const bottomRef = useRef<HTMLDivElement>(null); |
73 |
| - const [chat, setChat] = useState<any>([]); |
74 |
| - |
75 |
| - useEffect(() => { |
76 |
| - if (data && data?.message?.id) { |
77 |
| - if (!chat.includes(data?.message?.id)) { |
78 |
| - setChat((c: any) => [...c, data.message]); |
79 |
| - } |
80 |
| - } |
81 |
| - bottomRef.current?.scrollIntoView({ behavior: 'smooth' }); |
82 |
| - bottomRef.current?.scrollIntoView({ behavior: 'smooth' }); |
83 |
| - }, [data]); |
| 7 | + const client = createSocketStudioClient('https://codingcat-twitch.onrender.com/graphql'); |
| 8 | + const [isBrowser] = useState(typeof window !== 'undefined'); |
84 | 9 |
|
85 | 10 | return (
|
86 |
| - <ApolloProvider client={client}> |
| 11 | + <SocketStudioProvider client={client}> |
87 | 12 | <main className="grid grid-rows-[1fr_8px_144px] h-screen overflow-hidden">
|
88 | 13 | <div className="overlay-top h-full w-full" />
|
89 | 14 | <div className="w-full bg-purple-900 h-2" />
|
90 | 15 | <div className="grid grid-cols-[400px_minmax(500px,_1fr)_140px] w-full bg-gradient-to-r to-purple-700 via-purple-500 from-pink-500">
|
91 |
| - <div className="flex flex-grow"> |
92 |
| - <div className="messages flex flex-col overflow-y-scroll max-w-xl p-1 relative"> |
93 |
| - {loading ? ( |
94 |
| - <div className="rounded-xl p-1 bg-purple-500 text-purple-50 px-1"> |
95 |
| - No Messages Yet... |
96 |
| - </div> |
97 |
| - ) : ( |
98 |
| - <> |
99 |
| - {chat.map((c: any) => ( |
100 |
| - <div key={c.id} className="flex gap-1"> |
101 |
| - <div className="rounded bg-purple-500 text-purple-50 px-1"> |
102 |
| - {c?.author?.username} |
103 |
| - </div> |
104 |
| - <div className="text-purple-50">{c.message}</div> |
105 |
| - </div> |
106 |
| - ))} |
107 |
| - <div ref={bottomRef} className="mt-6" /> |
108 |
| - </> |
109 |
| - )} |
110 |
| - </div> |
111 |
| - </div> |
| 16 | + <Chat /> |
112 | 17 | <div></div>
|
113 | 18 | <div className="w-36 h-36 p-2">
|
114 | 19 | <AJPrimary className="block w-32 h-32" />
|
115 | 20 | </div>
|
116 | 21 | </div>
|
117 | 22 | </main>
|
118 |
| - </ApolloProvider> |
| 23 | + </SocketStudioProvider> |
119 | 24 | );
|
120 | 25 | }
|
0 commit comments