11<script setup lang="ts">
2- import { ref , onMounted , onUnmounted , nextTick } from ' vue'
3- import { createSubscriptionClient } from ' nitro-graphql/subscribe'
42import type { SubscriptionHandle } from ' nitro-graphql/subscribe'
3+ import { createSubscriptionClient } from ' nitro-graphql/subscribe'
4+ import { nextTick , onMounted , onUnmounted , ref } from ' vue'
55
66// Types
77interface Message {
@@ -18,19 +18,20 @@ interface Channel {
1818}
1919
2020// State
21- const getOrCreateUsername = () => {
21+ function getOrCreateUsername() {
2222 if (typeof localStorage !== ' undefined' ) {
2323 const stored = localStorage .getItem (' chat-username' )
24- if (stored ) return stored
25- const newUsername = ' User' + Math .floor (Math .random () * 1000 )
24+ if (stored )
25+ return stored
26+ const newUsername = ` User${Math .floor (Math .random () * 1000 )} `
2627 localStorage .setItem (' chat-username' , newUsername )
2728 return newUsername
2829 }
29- return ' User' + Math .floor (Math .random () * 1000 )
30+ return ` User${ Math .floor (Math .random () * 1000 )} `
3031}
3132const username = ref (getOrCreateUsername ())
3233
33- const saveUsername = () => {
34+ function saveUsername() {
3435 if (typeof localStorage !== ' undefined' ) {
3536 localStorage .setItem (' chat-username' , username .value )
3637 }
@@ -99,7 +100,7 @@ function subscribeToMessages() {
99100 console .error (' Subscription error:' , error )
100101 connectionState .value = ' disconnected'
101102 },
102- { transport: transport .value }
103+ { transport: transport .value },
103104 )
104105
105106 if (subscriptionHandle ) {
@@ -109,7 +110,8 @@ function subscribeToMessages() {
109110
110111// Send message
111112async function sendMessage() {
112- if (! newMessage .value .trim ()) return
113+ if (! newMessage .value .trim ())
114+ return
113115
114116 await fetch (' /api/graphql' , {
115117 method: ' POST' ,
@@ -175,7 +177,9 @@ onUnmounted(() => {
175177 <div class =" h-screen flex flex-col bg-[#1a1a2e]" >
176178 <!-- Header -->
177179 <header class =" flex justify-between items-center px-8 py-4 bg-[#16213e] border-b border-[#0f3460]" >
178- <h1 class =" text-2xl font-bold text-[#e94560]" >GraphQL Subscriptions</h1 >
180+ <h1 class =" text-2xl font-bold text-[#e94560]" >
181+ GraphQL Subscriptions
182+ </h1 >
179183 <div class =" flex items-center gap-6" >
180184 <a
181185 href =" #/demo"
@@ -189,9 +193,9 @@ onUnmounted(() => {
189193 :class =" {
190194 'bg-green-400': connectionState === 'connected',
191195 'bg-yellow-400': connectionState === 'connecting',
192- 'bg-red-400': connectionState === 'disconnected'
196+ 'bg-red-400': connectionState === 'disconnected',
193197 }"
194- ></ span >
198+ / >
195199 {{ connectionState }} ({{ transport }})
196200 </div >
197201 </div >
@@ -200,7 +204,9 @@ onUnmounted(() => {
200204 <div class =" flex flex-1 overflow-hidden" >
201205 <!-- Sidebar -->
202206 <aside class =" w-60 bg-[#16213e] p-4 border-r border-[#0f3460] overflow-y-auto" >
203- <h3 class =" text-xs uppercase text-gray-500 mb-2" >Channels</h3 >
207+ <h3 class =" text-xs uppercase text-gray-500 mb-2" >
208+ Channels
209+ </h3 >
204210 <ul class =" space-y-1" >
205211 <li
206212 v-for =" channel in channels"
@@ -215,7 +221,9 @@ onUnmounted(() => {
215221 </li >
216222 </ul >
217223
218- <h3 class =" text-xs uppercase text-gray-500 mt-6 mb-2" >Transport</h3 >
224+ <h3 class =" text-xs uppercase text-gray-500 mt-6 mb-2" >
225+ Transport
226+ </h3 >
219227 <div class =" flex gap-2" >
220228 <button
221229 class =" flex-1 px-3 py-2 text-sm rounded border transition-colors"
@@ -237,12 +245,14 @@ onUnmounted(() => {
237245 </button >
238246 </div >
239247
240- <h3 class =" text-xs uppercase text-gray-500 mt-6 mb-2" >Username</h3 >
248+ <h3 class =" text-xs uppercase text-gray-500 mt-6 mb-2" >
249+ Username
250+ </h3 >
241251 <input
242252 v-model =" username"
243253 class =" w-full px-3 py-2 bg-[#0f3460] border border-[#0f3460] text-white rounded focus:outline-none focus:border-[#e94560]"
244254 @blur =" saveUsername"
245- / >
255+ >
246256 </aside >
247257
248258 <!-- Chat -->
@@ -265,7 +275,9 @@ onUnmounted(() => {
265275 </strong >
266276 <span class =" text-xs text-gray-600" >{{ formatTime(msg.createdAt) }}</span >
267277 </div >
268- <div class =" text-gray-300" >{{ msg.content }}</div >
278+ <div class =" text-gray-300" >
279+ {{ msg.content }}
280+ </div >
269281 </div >
270282
271283 <div v-if =" messages.length === 0" class =" text-center text-gray-600 py-8" >
@@ -282,7 +294,7 @@ onUnmounted(() => {
282294 placeholder =" Type a message..."
283295 class =" flex-1 px-4 py-3 bg-[#0f3460] border border-[#0f3460] text-white rounded-lg focus:outline-none focus:border-[#e94560]"
284296 autofocus
285- / >
297+ >
286298 <button
287299 type =" submit"
288300 class =" px-6 py-3 bg-[#e94560] text-white font-semibold rounded-lg hover:bg-[#d63850] transition-colors"
0 commit comments