|
| 1 | +"use client"; |
| 2 | +import React, { useState, useEffect } from "react"; |
| 3 | + |
| 4 | +import { Queue } from "./../../../src"; |
| 5 | +import * as uuid from "uuid"; |
| 6 | +import { Redis } from "@upstash/redis"; |
| 7 | +import { TransitionGroup, CSSTransition } from "react-transition-group"; |
| 8 | + |
| 9 | +type MessageBody = { |
| 10 | + content: string; |
| 11 | +}; |
| 12 | + |
| 13 | +type MessageWithId = { |
| 14 | + id: string; |
| 15 | + body: MessageBody; |
| 16 | +}; |
| 17 | + |
| 18 | +const queueName = `sqs-${uuid.v4().substring(0, 18)}`; |
| 19 | + |
| 20 | +export default function Home() { |
| 21 | + const [messageInput, setMessageInput] = useState<string>( |
| 22 | + uuid.v4().substring(0, 18) |
| 23 | + ); |
| 24 | + const [queueMessages, setQueueMessages] = useState<MessageWithId[]>([]); |
| 25 | + |
| 26 | + const send = async () => { |
| 27 | + fetch("/api/send", { |
| 28 | + method: "POST", |
| 29 | + body: JSON.stringify({ message: messageInput, queueName: queueName }), |
| 30 | + }).then(async (res) => { |
| 31 | + const data = await res.json(); |
| 32 | + if (data.messageId) { |
| 33 | + setQueueMessages([ |
| 34 | + ...queueMessages, |
| 35 | + { |
| 36 | + id: data.messageId, |
| 37 | + body: { content: messageInput }, |
| 38 | + } as MessageWithId, |
| 39 | + ]); |
| 40 | + } |
| 41 | + }); |
| 42 | + }; |
| 43 | + |
| 44 | + const receive = async () => { |
| 45 | + fetch("/api/receive", { |
| 46 | + method: "POST", |
| 47 | + body: JSON.stringify({ queueName: queueName }), |
| 48 | + }).then(async (res) => { |
| 49 | + const data = await res.json(); |
| 50 | + |
| 51 | + if (data.id && data.message) { |
| 52 | + setQueueMessages(queueMessages.filter((msg) => msg.id !== data.id)); |
| 53 | + } |
| 54 | + }); |
| 55 | + }; |
| 56 | + return ( |
| 57 | + <main> |
| 58 | + <header> |
| 59 | + <h1 className="text-4xl font-bold"> |
| 60 | + Welcome to <span className="text-primary-500">@upstash/sqs</span> |
| 61 | + </h1> |
| 62 | + |
| 63 | + <p className="mt-4"> |
| 64 | + This is an example of how to use @upstash/sqs as a FIFO queue in your |
| 65 | + Next.js application. |
| 66 | + </p> |
| 67 | + |
| 68 | + <p className="mt-4"> |
| 69 | + You can create and consume messages from the queue using the buttons. |
| 70 | + </p> |
| 71 | + </header> |
| 72 | + |
| 73 | + <hr className="my-10" /> |
| 74 | + <div className="h-[55vh]"> |
| 75 | + <div className="p-4 bg-zinc-100 flex flex-col gap-2 rounded-xl w-96 mx-auto max-h-full overflow-y-scroll"> |
| 76 | + <p className="text-lg font-bold">Queue</p> |
| 77 | + <TransitionGroup className="flex flex-col gap-2 h-min"> |
| 78 | + {queueMessages.length > 0 && |
| 79 | + queueMessages.map((message, index) => { |
| 80 | + return ( |
| 81 | + <CSSTransition |
| 82 | + key={message.id} |
| 83 | + timeout={250} |
| 84 | + classNames="message" |
| 85 | + > |
| 86 | + <div className="flex flex-row items-center justify-between gap-4 w-full transform transition-all duration-500 ease-in-out"> |
| 87 | + <div className="bg-white px-2 h-full rounded-xl w-10 flex items-center justify-center text-sm"> |
| 88 | + {index} |
| 89 | + </div> |
| 90 | + |
| 91 | + <div className="bg-white p-4 rounded-xl text-slate-900 shadow-sm flex flex-col gap-2 w-80"> |
| 92 | + <p className="font-semibold text-sm"> |
| 93 | + ID:{" "} |
| 94 | + <span className="text-zinc-400 font-normal"> |
| 95 | + {message.id} |
| 96 | + </span> |
| 97 | + </p> |
| 98 | + |
| 99 | + <p className="font-semibold"> |
| 100 | + Content:{" "} |
| 101 | + <span className="text-zinc-800 font-normal"> |
| 102 | + {message.body.content} |
| 103 | + </span> |
| 104 | + </p> |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + </CSSTransition> |
| 108 | + ); |
| 109 | + })} |
| 110 | + </TransitionGroup> |
| 111 | + {queueMessages.length === 0 && <p>No messages in the queue...</p>} |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + <hr className="my-10" /> |
| 115 | + |
| 116 | + <div className="flex flex-row gap-6 items-end w-full"> |
| 117 | + <div className="flex flex-col w-full gap-2"> |
| 118 | + <div className="w-full flex flex-row justify-between gap-2"> |
| 119 | + <input |
| 120 | + type="text" |
| 121 | + className=" shadow-sm border border-zinc-400 rounded-xl px-4 py-2 w-full" |
| 122 | + placeholder="Enter message..." |
| 123 | + value={messageInput} |
| 124 | + onChange={(e) => setMessageInput(e.target.value)} |
| 125 | + /> |
| 126 | + <button |
| 127 | + className="bg-zinc-400 text-white px-4 py-2 rounded-xl hover:bg-zinc-300" |
| 128 | + onClick={() => { |
| 129 | + const randomWord = uuid.v4().substring(0, 18); |
| 130 | + setMessageInput(randomWord); |
| 131 | + }} |
| 132 | + > |
| 133 | + Random |
| 134 | + </button> |
| 135 | + </div> |
| 136 | + <div className="flex justify-center w-full gap-2"> |
| 137 | + <button |
| 138 | + className="bg-emerald-500 text-white px-4 py-2 rounded-xl hover:bg-emerald-400 transition-all ease-in-out w-full" |
| 139 | + onClick={() => { |
| 140 | + send(); |
| 141 | + }} |
| 142 | + > |
| 143 | + Produce |
| 144 | + </button> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + <button |
| 148 | + className="bg-blue-500 text-white px-4 py-2 rounded-xl hover:bg-blue-400 transition-all ease-in-out w-full h-1/2" |
| 149 | + onClick={() => { |
| 150 | + receive(); |
| 151 | + }} |
| 152 | + > |
| 153 | + Consume |
| 154 | + </button> |
| 155 | + </div> |
| 156 | + </main> |
| 157 | + ); |
| 158 | +} |
0 commit comments