Skip to content

Commit e588093

Browse files
committed
room deletion: add manual room deletion
1 parent 4b098ba commit e588093

4 files changed

Lines changed: 31 additions & 5 deletions

File tree

src/app/api/[[...slugs]]/rooms.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Elysia from "elysia";
33
import { nanoid } from "nanoid";
44
import { authMiddleware } from "./auth";
55
import z from "zod";
6+
import { realtime } from "@/lib/realtime";
67

78
const ROOM_TTL_SECONDS = 10 * 60;
89

@@ -27,4 +28,19 @@ export const rooms = new Elysia({ prefix: "/room" })
2728
return { ttl: ttl > 0 ? ttl : 0 };
2829
},
2930
{ query: z.object({ roomId: z.string() }) },
31+
)
32+
.delete(
33+
"/",
34+
async ({ auth }) => {
35+
await realtime
36+
.channel(auth.roomId)
37+
.emit("chat.destroy", { isDestroyed: true });
38+
39+
await Promise.all([
40+
redis.del(auth.roomId),
41+
redis.del(`meta:${auth.roomId}`),
42+
redis.del(`messages:${auth.roomId}`),
43+
]);
44+
},
45+
{ query: z.object({ roomId: z.string() }) },
3046
);

src/app/api/[[...slugs]]/route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ const app = new Elysia({ prefix: "/api" }).use(rooms).use(messages);
77

88
export const GET = app.fetch;
99
export const POST = app.fetch;
10+
export const DELETE = app.fetch;
1011

1112
export type App = typeof app;

src/components/room/roomHeader.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useRoomStore } from "@/app/store/roomStore";
2+
import { useDeleteRoom } from "@/hooks/useDeleteRoom";
23
import { useGetRemainingTime } from "@/hooks/useGetRemainingTime";
34
import { useState } from "react";
45

@@ -12,6 +13,7 @@ export const RoomHeader = () => {
1213
const roomId = useRoomStore((state) => state.roomId);
1314
const { timeRemaining } = useGetRemainingTime(roomId);
1415
const [copyStatus, setCopyStatus] = useState<"copy" | "copied">("copy");
16+
const { destroyRoom } = useDeleteRoom();
1517

1618
const copyLink = () => {
1719
navigator.clipboard.writeText(`${window.location.origin}/room/${roomId}`);
@@ -22,10 +24,6 @@ export const RoomHeader = () => {
2224
}, 2000);
2325
};
2426

25-
const destroyRoom = () => {
26-
// TODO: Implement destroy room
27-
};
28-
2927
return (
3028
<header className="flex items-center justify-between border-b border-zinc-800 bg-zinc-900/30 p-4">
3129
<div className="flex items-center gap-4">
@@ -63,7 +61,7 @@ export const RoomHeader = () => {
6361
</div>
6462

6563
<button
66-
onClick={() => destroyRoom()}
64+
onClick={() => destroyRoom(roomId)}
6765
className="group flex items-center gap-2 rounded bg-zinc-800 px-3 py-1.5 text-xs font-bold text-zinc-400 transition-all hover:bg-red-600 hover:text-white disabled:opacity-50"
6866
>
6967
<span className="group-hover:animate-pulse">💣</span>

src/hooks/useDeleteRoom.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { client } from "@/lib/client";
2+
import { useMutation } from "@tanstack/react-query";
3+
4+
export const useDeleteRoom = () => {
5+
const { mutate: destroyRoom } = useMutation({
6+
mutationFn: async (roomId: string) => {
7+
await client.room.delete(null, { query: { roomId } });
8+
},
9+
});
10+
return { destroyRoom };
11+
};

0 commit comments

Comments
 (0)