Skip to content

Commit

Permalink
feat(Pusher): add debug interest
Browse files Browse the repository at this point in the history
  • Loading branch information
simonknittel committed Jan 18, 2025
1 parent 5a7c6a0 commit f2e2113
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
5 changes: 4 additions & 1 deletion app/src/app/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export default async function AppLayout({ children }: Readonly<Props>) {
<SessionProviderContainer session={authentication.session}>
<QueryClientProviderContainer>
<TRPCReactProvider>
<BeamsProvider instanceId={env.PUSHER_BEAMS_INSTANCE_ID}>
<BeamsProvider
instanceId={env.PUSHER_BEAMS_INSTANCE_ID}
userId={authentication.session.user.id}
>
<div className="min-h-dvh bg-sinister-radial-gradient">
<MobileActionBarContainer />
<DesktopSidebarContainer />
Expand Down
6 changes: 4 additions & 2 deletions app/src/pusher/components/BeamsContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";

import type { User } from "@prisma/client";
import dynamic from "next/dynamic";
import type { Dispatch, ReactNode, SetStateAction } from "react";
import { createContext, useContext, useEffect, useMemo, useState } from "react";
Expand All @@ -18,9 +19,10 @@ const BeamsContext = createContext<BeamsContext | undefined>(undefined);
type Props = Readonly<{
children: ReactNode;
instanceId?: string;
userId: User["id"];
}>;

export const BeamsProvider = ({ children, instanceId }: Props) => {
export const BeamsProvider = ({ children, instanceId, userId }: Props) => {
const [interests, setInterests] = useState<string[] | undefined>(undefined);

useEffect(() => {
Expand Down Expand Up @@ -49,7 +51,7 @@ export const BeamsProvider = ({ children, instanceId }: Props) => {
return (
<BeamsContext.Provider value={value}>
{children}
{instanceId && <Client instanceId={instanceId} />}
{instanceId && <Client instanceId={instanceId} userId={userId} />}
</BeamsContext.Provider>
);
};
Expand Down
20 changes: 13 additions & 7 deletions app/src/pusher/components/Client.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"use client";

import type { User } from "@prisma/client";
import * as PusherPushNotifications from "@pusher/push-notifications-web";
import { useEffect } from "react";
import toast from "react-hot-toast";
import { useBeamsContext } from "./BeamsContext";

type Props = Readonly<{
instanceId: string;
userId: User["id"];
}>;

export const Client = ({ instanceId }: Props) => {
export const Client = ({ instanceId, userId }: Props) => {
const { interests } = useBeamsContext();

useEffect(() => {
Expand All @@ -24,24 +26,28 @@ export const Client = ({ instanceId }: Props) => {

if (interests.length <= 0) {
void beamsClient.stop();
console.info("[Pusher] Client stopped");
console.info("[Pusher] Client stopped.");
return;
}

void beamsClient
.start()
.then(() => {
console.info("[Pusher] Client started");
return beamsClient.setDeviceInterests(interests);
const _interests = [...interests, `debug-user-${userId}`];
console.info(
"[Pusher] Client started. Settings interests...",
_interests,
);
return beamsClient.setDeviceInterests(_interests);
})
.then(() => {
console.info("[Pusher] Device interests set");
console.info("[Pusher] Device interests set.");
});
} catch (error) {
console.error("[Pusher] Error initializing client", error);
console.error("[Pusher] Error initializing client.", error);
toast.error("Benachrichtigungen konnten nicht aktiviert werden.");
}
}, [interests, instanceId]);
}, [interests, instanceId, userId]);

return null;
};

0 comments on commit f2e2113

Please sign in to comment.