-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathChatView.tsx
More file actions
168 lines (137 loc) Β· 5.48 KB
/
ChatView.tsx
File metadata and controls
168 lines (137 loc) Β· 5.48 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import React, { createContext, useContext, useEffect, useMemo, useState } from 'react';
import { ThreadProvider } from '../Threads';
import { Icon } from '../Threads/icons';
import { UnreadCountBadge } from '../Threads/UnreadCountBadge';
import { useChatContext } from '../../context';
import { useStateStore } from '../../store';
import type { PropsWithChildren } from 'react';
import type { Thread, ThreadManagerState } from 'stream-chat';
import clsx from 'clsx';
type ChatView = 'channels' | 'threads' | (string & {});
type ChatViewContextValue = {
activeChatView: ChatView;
setActiveChatView: (cv: ChatViewContextValue['activeChatView']) => void;
};
const ChatViewContext = createContext<ChatViewContextValue>({
activeChatView: 'channels',
setActiveChatView: () => undefined,
});
export const ChatView = ({ children }: PropsWithChildren) => {
const [activeChatView, setActiveChatView] =
useState<ChatViewContextValue['activeChatView']>('channels');
const { theme } = useChatContext();
const value = useMemo(() => ({ activeChatView, setActiveChatView }), [activeChatView]);
return (
<ChatViewContext.Provider value={value}>
<div className={clsx('str-chat', theme, 'str-chat__chat-view')}>{children}</div>
</ChatViewContext.Provider>
);
};
// todo: move channel list orchestrator here
const ChannelsView = ({ children }: PropsWithChildren) => {
const { activeChatView } = useContext(ChatViewContext);
if (activeChatView !== 'channels') return null;
return <div className='str-chat__chat-view__channels'>{children}</div>;
};
export type ThreadsViewContextValue = {
activeThread: Thread | undefined;
setActiveThread: (cv: ThreadsViewContextValue['activeThread']) => void;
};
const ThreadsViewContext = createContext<ThreadsViewContextValue>({
activeThread: undefined,
setActiveThread: () => undefined,
});
export const useThreadsViewContext = () => useContext(ThreadsViewContext);
const ThreadsView = ({ children }: PropsWithChildren) => {
const { activeChatView } = useContext(ChatViewContext);
const [activeThread, setActiveThread] =
useState<ThreadsViewContextValue['activeThread']>(undefined);
const value = useMemo(() => ({ activeThread, setActiveThread }), [activeThread]);
if (activeChatView !== 'threads') return null;
return (
<ThreadsViewContext.Provider value={value}>
<div className='str-chat__chat-view__threads'>{children}</div>
</ThreadsViewContext.Provider>
);
};
// thread business logic that's impossible to keep within client but encapsulated for ease of use
export const useActiveThread = ({ activeThread }: { activeThread?: Thread }) => {
useEffect(() => {
if (!activeThread) return;
const handleVisibilityChange = () => {
if (document.visibilityState === 'visible' && document.hasFocus()) {
activeThread.activate();
}
if (document.visibilityState === 'hidden' || !document.hasFocus()) {
activeThread.deactivate();
}
};
handleVisibilityChange();
window.addEventListener('focus', handleVisibilityChange);
window.addEventListener('blur', handleVisibilityChange);
return () => {
activeThread.deactivate();
window.addEventListener('blur', handleVisibilityChange);
window.removeEventListener('focus', handleVisibilityChange);
};
}, [activeThread]);
};
// ThreadList under View.Threads context, will access setting function and on item click will set activeThread
// which can be accessed for the ease of use by ThreadAdapter which forwards it to required ThreadProvider
// ThreadList can easily live without this context and click handler can be overriden, ThreadAdapter is then no longer needed
/**
* // this setup still works
* const MyCustomComponent = () => {
* const [activeThread, setActiveThread] = useState();
*
* return <>
* // simplified
* <ThreadList onItemPointerDown={setActiveThread} />
* <ThreadProvider thread={activeThread}>
* <Thread />
* </ThreadProvider>
* </>
* }
*
*/
const ThreadAdapter = ({ children }: PropsWithChildren) => {
const { activeThread } = useThreadsViewContext();
useActiveThread({ activeThread });
return <ThreadProvider thread={activeThread}>{children}</ThreadProvider>;
};
const selector = ({ unreadThreadCount }: ThreadManagerState) => ({
unreadThreadCount,
});
const ChatViewSelector = () => {
const { client } = useChatContext();
const { unreadThreadCount } = useStateStore(client.threads.state, selector);
const { activeChatView, setActiveChatView } = useContext(ChatViewContext);
return (
<div className='str-chat__chat-view__selector'>
<button
aria-selected={activeChatView === 'channels'}
className='str-chat__chat-view__selector-button'
onPointerDown={() => setActiveChatView('channels')}
role='tab'
>
<Icon.MessageBubbleEmpty />
<div className='str-chat__chat-view__selector-button-text'>Channels</div>
</button>
<button
aria-selected={activeChatView === 'threads'}
className='str-chat__chat-view__selector-button'
onPointerDown={() => setActiveChatView('threads')}
role='tab'
>
<UnreadCountBadge count={unreadThreadCount} position='top-right'>
<Icon.MessageBubble />
</UnreadCountBadge>
<div className='str-chat__chat-view__selector-button-text'>Threads</div>
</button>
</div>
);
};
ChatView.Channels = ChannelsView;
ChatView.Threads = ThreadsView;
ChatView.ThreadAdapter = ThreadAdapter;
ChatView.Selector = ChatViewSelector;