Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 14 additions & 16 deletions components/core/notifications.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use client";

import { useMutation, useQuery } from "@tanstack/react-query";
import { TurboWire } from "@turbowire/web";
import { useWireEvent } from "@turbowire/react";
import { Bell, X } from "lucide-react";
import { useEffect, useState } from "react";
import { useCallback, useMemo, useState } from "react";
import { toast } from "sonner";
import { realtimeSchema } from "@/data/notification";
import { type NotificationPayload, realtimeSchema } from "@/data/notification";
import { displayMutationError } from "@/lib/utils/error";
import { useTRPC } from "@/trpc/client";
import { Button } from "../ui/button";
Expand Down Expand Up @@ -40,27 +40,25 @@ export function Notifications({

const unreadCount = notifications?.filter((x) => !x.read).length;

useEffect(() => {
if (!notificationsWire) return;

const wire = new TurboWire(notificationsWire, { schema: realtimeSchema });

wire.connect();

wire.on("notification", ({ content }) => {
const onNotification = useCallback(
({ content }: NotificationPayload) => {
try {
if (!content) return;
refetchNotifications();
toast.info(content);
} catch (error) {
console.error(error);
}
});
},
[refetchNotifications],
);

const wireOptions = useMemo(
() => ({ schema: realtimeSchema, notification: onNotification }),
[onNotification],
);

return () => {
wire?.disconnect();
};
}, [notificationsWire, refetchNotifications]);
useWireEvent(notificationsWire, wireOptions);

return (
<>
Expand Down
6 changes: 6 additions & 0 deletions data/notification.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { EventPayload } from "@turbowire/serverless";
import z from "zod";

export const realtimeSchema = {
notification: z.object({ content: z.string().nullable() }),
};

export type NotificationPayload = EventPayload<
typeof realtimeSchema,
"notification"
>;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
"@trpc/client": "^11.0.2",
"@trpc/server": "^11.0.2",
"@trpc/tanstack-react-query": "^11.0.2",
"@turbowire/react": "^0.1.0",
"@turbowire/serverless": "^0.2.0",
"@turbowire/web": "^0.4.0",
"@upstash/redis": "^1.35.3",
"@upstash/search": "^0.1.5",
"@upstash/workflow": "^0.2.16",
Expand Down
Loading