|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { orpc } from '../lib/orpc' |
| 4 | +import { useMutation, useQuery } from '@tanstack/react-query' |
| 5 | +import { match } from 'ts-pattern' |
| 6 | +import { parseFormData } from '@orpc/openapi/helpers' |
| 7 | + |
| 8 | +const CHANNEL = 'default' |
| 9 | + |
| 10 | +export function ChatRoom() { |
| 11 | + const query = useQuery(orpc.message.subscribe.streamedOptions({ |
| 12 | + input: { channel: CHANNEL }, |
| 13 | + context: { retry: Infinity }, |
| 14 | + queryFnOptions: { maxChunks: 10 }, |
| 15 | + })) |
| 16 | + |
| 17 | + const mutation = useMutation(orpc.message.publish.mutationOptions()) |
| 18 | + |
| 19 | + const statusLabel = match(query) |
| 20 | + .with({ status: 'pending' }, () => 'Joining') |
| 21 | + .with({ status: 'error' }, () => 'Error') |
| 22 | + .with({ status: 'success' }, () => 'Listening') |
| 23 | + .exhaustive() |
| 24 | + |
| 25 | + return ( |
| 26 | + <section className="module module--violet" aria-labelledby="chat-room-title"> |
| 27 | + <span className="corner tl" /> |
| 28 | + <span className="corner tr" /> |
| 29 | + <span className="corner bl" /> |
| 30 | + <span className="corner br" /> |
| 31 | + |
| 32 | + <div className="module-head"> |
| 33 | + <div> |
| 34 | + <span className="module-id">CH-03 · PUB/SUB</span> |
| 35 | + <h2 className="module-title" id="chat-room-title"> |
| 36 | + oRPC and Tanstack Query | Pub/Sub Example |
| 37 | + </h2> |
| 38 | + <p className="module-desc"> |
| 39 | + A live subscription over oRPC. Open this page in two tabs to chat across the channel. |
| 40 | + </p> |
| 41 | + </div> |
| 42 | + <span className="status-pill"> |
| 43 | + <span className="dot" /> |
| 44 | + <span className="status-text">{statusLabel}</span> |
| 45 | + </span> |
| 46 | + </div> |
| 47 | + |
| 48 | + <div className="channel-log"> |
| 49 | + {match(query) |
| 50 | + .with({ status: 'pending' }, () => <p className="channel-empty">joining...</p>) |
| 51 | + .with({ status: 'error' }, q => <p className="module-error">{String(q.error)}</p>) |
| 52 | + .with({ status: 'success' }, q => q.data.length === 0 |
| 53 | + ? ( |
| 54 | + <p className="channel-empty"> |
| 55 | + waiting for new messages..., please open in multiple tabs for chatting together |
| 56 | + </p> |
| 57 | + ) |
| 58 | + : ( |
| 59 | + <ul className="msg-list"> |
| 60 | + {q.data.map(({ message }, i) => ( |
| 61 | + <li key={i} className="msg"> |
| 62 | + <span className="msg-text">{message}</span> |
| 63 | + </li> |
| 64 | + ))} |
| 65 | + </ul> |
| 66 | + )) |
| 67 | + .exhaustive()} |
| 68 | + </div> |
| 69 | + |
| 70 | + <form |
| 71 | + className="channel-form" |
| 72 | + action={form => mutation.mutate({ ...parseFormData(form), channel: CHANNEL })} |
| 73 | + > |
| 74 | + <div className="prompt-wrap"> |
| 75 | + <span className="prompt-char">›</span> |
| 76 | + <input type="text" name="message" required minLength={1} placeholder="message..." /> |
| 77 | + </div> |
| 78 | + <button type="submit" className="btn" disabled={query.isPending || mutation.isPending}> |
| 79 | + {mutation.isPending ? 'Sending…' : 'Send'} |
| 80 | + </button> |
| 81 | + </form> |
| 82 | + </section> |
| 83 | + ) |
| 84 | +} |
0 commit comments