forked from syhner/next-kickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseEvent.ts
53 lines (47 loc) · 1.28 KB
/
useEvent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import 'client-only';
import { useEffect, useRef } from 'react';
import { env } from '~/env.mjs';
import { events, type Events } from '~/lib/events';
/**
* @enable WebSockets
*/
// import PusherClient from 'pusher-js/worker';
export const useEvent = <T extends keyof typeof events>({
channelName,
eventName,
onEvent,
}: {
channelName: string;
eventName: T;
onEvent: (event: Events[T]) => unknown;
}) => {
const onEventRef = useRef(onEvent);
useEffect(() => {
onEventRef.current = onEvent;
}, [onEvent]);
useEffect(() => {
/**
* @enable WebSockets
*/
// const pusherClient = new PusherClient(env.NEXT_PUBLIC_PUSHER_APP_KEY, {
// cluster: env.NEXT_PUBLIC_PUSHER_CLUSTER,
// });
//
// const channel = pusherClient.subscribe(channelName);
// channel.bind(eventName, (event: unknown) => {
// const eventSchema = events[eventName];
// const parsedEventResult = eventSchema.safeParse(event);
//
// if (!parsedEventResult.success) return;
//
// onEventRef.current(events[eventName].parse(parsedEventResult.data));
// });
return () => {
/**
* @enable WebSockets
*/
// channel.unbind(eventName);
// pusherClient.unsubscribe(channelName);
};
}, [channelName, eventName]);
};