Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

tanstack virtual list for recent messages #432

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion apps/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"@sentry/nextjs": "^7.16.0",
"@solrock/prisma": "workspace:*",
"@tabler/icons": "^1.108.0",
"@tanstack/react-query": "^4",
"@tanstack/react-virtual": "3.0.0-beta.32",
"@trpc/client": "^10",
"@trpc/next": "^10",
"@trpc/react-query": "^10",
Expand All @@ -41,7 +43,6 @@
"pusher-js": "^8.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"@tanstack/react-query": "^4",
"react-tsparticles": "^2.1.3",
"superjson": "^1.9.1",
"tabler-icons-react": "^1.52.0",
Expand Down
111 changes: 61 additions & 50 deletions apps/frontend/src/components/MediaControls.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// mmattDonk 2023
// https://mmattDonk.com

import { Button, Collapse, Container, Group, Space, Stack, Table } from '@mantine/core';
import { Button, Container, Group, Stack } from '@mantine/core';
import { showNotification } from '@mantine/notifications';
import { useVirtualizer } from '@tanstack/react-virtual';
import { signIn } from 'next-auth/react';
import { useState } from 'react';
import { Rotate } from 'tabler-icons-react';
import { useRef, useState } from 'react';
import { Refresh } from 'tabler-icons-react';
import { trpc } from '../utils/trpc';
import LoadingPage, { LoadingSpinner } from './Loading';

export default function MediaControls() {
const ttsMutation = trpc.tts.retriggerTts.useMutation();
const parentRef = useRef<HTMLDivElement>(null);

const [showTable, setShowTable] = useState(false);
const ttsMutation = trpc.tts.retriggerTts.useMutation();
const skipMutation = trpc.tts.skipTts.useMutation();

const { data: session, isLoading: isSessionLoading } = trpc.auth.getSession.useQuery();
const { data: userData, isLoading } = trpc.user.getUser.useQuery(session?.user?.name ?? '');
Expand All @@ -22,7 +24,11 @@ export default function MediaControls() {

const [skipMessage, setSkipMessage] = useState('');

const skipMutation = trpc.tts.skipTts.useMutation();
const rowVirtualizer = useVirtualizer({
count: ttsMessages?.messages?.length ?? 0,
getScrollElement: () => parentRef.current,
estimateSize: () => 50,
});

const skipTts = async (e: any) => {
e.preventDefault();
Expand Down Expand Up @@ -104,51 +110,56 @@ export default function MediaControls() {
</>
)}
</Group>
<Space h="md" />
<Button onClick={() => setShowTable((o) => !o)}>{showTable ? 'Close' : 'Open'} Recent TTS Messages</Button>
<Space h="sm" />
<Collapse in={showTable}>
{ttsMessages?.messages?.length ?? 0 > 0 ? (
<Table style={{ textAlign: 'center' }}>
<thead>
<th>Replay</th>
<th>Message</th>
<th>Created At</th>
</thead>
<tbody>
{ttsMessages?.messages?.map((message: any, i: number) => (
<tr key={i}>
<td>
<div>
<Button value={message.audioUrl} color="gray" onClickCapture={replayTts}>
<Rotate />
</Button>
</div>
</td>
<td>
<p
style={{
maxWidth: '50rem',
overflow: 'hidden',
textOverflow: 'ellipsis',
}}
>
{message.message}
</p>
</td>
<td>
<p>
{new Date(message.createdAt).toLocaleDateString()} at {new Date(message.createdAt).toLocaleTimeString()}
</p>
</td>
</tr>
{ttsMessages?.messages?.length ?? 0 > 0 ? (
<>
<h3>Recent Messages</h3>
<div
ref={parentRef}
style={{
height: '400px',
overflow: 'auto',
}}
>
<div
style={{
height: `${rowVirtualizer.getTotalSize()}px`,
width: '100%',
position: 'relative',
}}
>
{rowVirtualizer.getVirtualItems().map((virtualItem) => (
<div
key={virtualItem.key}
data-index={virtualItem.index}
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
transform: `translateY(${virtualItem.start}px)`,
}}
>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
gap: '1rem',
overflowWrap: 'break-word',
}}
>
<p>{ttsMessages?.messages[virtualItem.index].message}</p>
<Button>
<Refresh />
</Button>
</div>
</div>
))}
</tbody>
</Table>
) : (
<p>No recent TTS messages</p>
)}
</Collapse>
</div>
</div>
</>
) : (
<p>No recent TTS messages</p>
)}
</div>
);
}
Expand Down
Loading