/
MessagesTable.tsx
218 lines (204 loc) · 8.15 KB
/
MessagesTable.tsx
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { useParams, useTelemetryProps } from 'common'
import { isEqual } from 'lodash'
import { Loader2, MegaphoneIcon } from 'lucide-react'
import Link from 'next/link'
import { Key, useEffect, useState } from 'react'
import DataGrid, { RenderRowProps, Row } from 'react-data-grid'
import { Button, IconBroadcast, IconDatabaseChanges, IconExternalLink, IconPresence, cn } from 'ui'
import ShimmerLine from 'components/ui/ShimmerLine'
import Telemetry from 'lib/telemetry'
import { useRouter } from 'next/router'
import MessageSelection from './MessageSelection'
import type { LogData } from './Messages.types'
import NoChannelEmptyState from './NoChannelEmptyState'
import { ColumnRenderer } from './RealtimeMessageColumnRenderer'
export const isErrorLog = (l: LogData) => {
return l.message === 'SYSTEM' && l.metadata?.status === 'error'
}
const NoResultAlert = ({
enabled,
hasChannelSet,
showSendMessage,
}: {
enabled: boolean
hasChannelSet: boolean
showSendMessage: () => void
}) => {
const { ref } = useParams()
return (
<div className="w-full max-w-md flex items-center flex-col">
{!hasChannelSet ? (
<NoChannelEmptyState />
) : (
<>
{enabled && <p className="text-foreground">No Realtime messages found</p>}
<p className="text-foreground-lighter">Realtime message logs will be shown here</p>
<div className="mt-4 border bg-surface-100 border-border rounded-md justify-start items-center flex flex-col w-full">
<div className="w-full px-5 py-4 items-center gap-4 inline-flex border-b">
<IconBroadcast size="xlarge" className="text-background bg-foreground rounded w-6" />
<div className="grow flex-col flex">
<p className="text-foreground">Create a Broadcast message</p>
<p className="text-foreground-lighter text-xs">Send a message in the channel</p>
</div>
<Button type="default" onClick={showSendMessage}>
Broadcast a message
</Button>
</div>
<div className="w-full px-5 py-4 items-center gap-4 inline-flex border-b">
<IconPresence size="xlarge" className="text-background bg-foreground rounded w-6" />
<div className="grow flex-col flex">
<p className="text-foreground">Join from another browser tab</p>
<p className="text-foreground-lighter text-xs">
Send messages between multiple clients
</p>
</div>
<Link href={`/project/${ref}/realtime/inspector`} target="_blank" rel="noreferrer">
<Button type="default" iconRight={<IconExternalLink />}>
Open inspector
</Button>
</Link>
</div>
<div className="w-full px-5 py-4 items-center gap-4 inline-flex border-b">
<IconDatabaseChanges
size="xlarge"
className="text-background bg-foreground rounded w-6"
/>
<div className="grow flex-col flex">
<p className="text-foreground">Listen to a table for changes</p>
<p className="text-foreground-lighter text-xs">Tables must have realtime enabled</p>
</div>
<Link href={`/project/${ref}/database/publications`} target="_blank" rel="noreferrer">
<Button type="default" iconRight={<IconExternalLink />}>
Publications settings
</Button>
</Link>
</div>
<div className="w-full px-5 py-4 items-center gap-4 inline-flex rounded-b-md bg-studio">
<div className="grow flex-col flex">
<p className="text-foreground">Not sure what to do?</p>
<p className="text-foreground-lighter text-xs">Browse our documentation</p>
</div>
<Button type="default" iconRight={<IconExternalLink />}>
<a
href="https://supabase.com/docs/guides/realtime"
target="_blank"
rel="noreferrer"
>
Documentation
</a>
</Button>
</div>
</div>
</>
)}
</div>
)
}
const RowRenderer = (key: Key, props: RenderRowProps<LogData, unknown>) => {
return <Row key={key} {...props} isRowSelected={false} selectedCellIdx={undefined} />
}
interface MessagesTableProps {
enabled: boolean
data?: LogData[]
hasChannelSet: boolean
showSendMessage: () => void
}
const MessagesTable = ({
enabled,
hasChannelSet,
data = [],
showSendMessage,
}: MessagesTableProps) => {
const [focusedLog, setFocusedLog] = useState<LogData | null>(null)
const stringData = JSON.stringify(data)
const telemetryProps = useTelemetryProps()
const router = useRouter()
useEffect(() => {
if (!data) return
const found = data.find((datum) => datum.id === focusedLog?.id)
if (!found) {
setFocusedLog(null)
}
}, [stringData])
if (!data) return null
return (
<>
<section className="flex w-full flex-col" style={{ maxHeight: 'calc(100vh - 42px - 3rem)' }}>
<ShimmerLine active={enabled} />
<div className={cn('flex h-full flex-row', enabled ? 'border-brand-400' : null)}>
<div className="flex flex-grow flex-col">
{enabled && (
<div className="w-full h-9 px-4 bg-surface-100 border-b items-center inline-flex justify-between text-foreground-light">
<div className="inline-flex gap-2.5 text-xs">
<Loader2 size="16" className="animate-spin" />
<div>Listening</div>
<div>•</div>
<div>
{data.length > 0
? data.length >= 100
? `Found a large number of messages, showing only the latest 100...`
: `Found ${data.length} messages...`
: `No message found yet...`}
</div>
</div>
<Button
type="default"
onClick={showSendMessage}
icon={<MegaphoneIcon size={14} strokeWidth={1.5} />}
>
<span>Broadcast a message</span>
</Button>
</div>
)}
<DataGrid
className="data-grid--simple-logs h-full border-b-0"
rowHeight={40}
headerRowHeight={0}
onSelectedCellChange={({ rowIdx }) => {
Telemetry.sendEvent(
{
category: 'realtime_inspector',
action: 'focused-specific-message',
label: 'realtime_inspector_results',
},
telemetryProps,
router
)
setFocusedLog(data[rowIdx])
}}
selectedRows={new Set([])}
columns={ColumnRenderer}
rowClass={(row) => {
return cn([
'font-mono tracking-tight',
isEqual(row, focusedLog)
? 'bg-scale-800 rdg-row--focused'
: ' bg-scale-200 hover:bg-scale-300 cursor-pointer',
isErrorLog(row) && '!bg-warning-300',
])
}}
rows={data}
rowKeyGetter={(row) => row.id}
renderers={{
renderRow: RowRenderer,
noRowsFallback: (
<div className="mx-auto flex h-full w-full items-center justify-center space-y-12 py-4 transition-all delay-200 duration-500">
<NoResultAlert
enabled={enabled}
hasChannelSet={hasChannelSet}
showSendMessage={showSendMessage}
/>
</div>
),
}}
/>
</div>
<div className="flex w-1/2 flex-col">
<MessageSelection onClose={() => setFocusedLog(null)} log={focusedLog} />
</div>
</div>
</section>
</>
)
}
export default MessagesTable