-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
129 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
app/components/ChatWindowPanel/PrefetchMessages.server.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { db } from '@/app/lib/db'; | ||
import { getServersConfig } from '@/app/lib/configParse'; | ||
import type { dbReturnAllMessages } from '@/app/lib/get-server-messages'; | ||
import PrefetchedMessagesStore from '../Misc/PrefetchedMessagesStore'; | ||
|
||
export default async function PrefetchMessages({ | ||
searchParams, | ||
children | ||
}: { | ||
searchParams: { [key: string]: string | string[] | undefined }; | ||
children: React.ReactNode; | ||
}) { | ||
const serverIndex = Number(searchParams['SelectedServer']) || 0; | ||
const allMessages = await getServerMessages(serverIndex); | ||
if ('error' in allMessages) { | ||
console.log('Error loading messages'); | ||
return ( | ||
<PrefetchedMessagesStore prefetchedMessages={[]}> | ||
{children} | ||
</PrefetchedMessagesStore> | ||
); | ||
} | ||
return ( | ||
<PrefetchedMessagesStore prefetchedMessages={allMessages}> | ||
{children} | ||
</PrefetchedMessagesStore> | ||
); | ||
} | ||
|
||
async function getServerMessages( | ||
selectedServerIndex: number, | ||
olderThan = 0, | ||
newerThan = 0 | ||
) { | ||
const config = getServersConfig(); | ||
if ('err' in config || config.global.chatLogger != true) { | ||
return { error: true }; | ||
} | ||
try { | ||
if (olderThan === 0) { | ||
const allMessages = ( | ||
await db.query( | ||
`SELECT * FROM server_messages WHERE server_id=${config.servers[selectedServerIndex].chatLoggerId} AND id > ${newerThan} ORDER BY id DESC LIMIT 30` | ||
) | ||
)[0] as dbReturnAllMessages; | ||
return allMessages.reverse(); | ||
} else { | ||
const allMessages = ( | ||
await db.query( | ||
`SELECT * FROM server_messages WHERE server_id=${config.servers[selectedServerIndex].chatLoggerId} AND id < ${olderThan} ORDER BY id DESC LIMIT 30` | ||
) | ||
)[0] as dbReturnAllMessages; | ||
return allMessages.reverse(); | ||
} | ||
} catch (err) { | ||
console.log(err); | ||
return { error: true }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use client'; | ||
import { atom, useSetAtom } from 'jotai'; | ||
import { dbReturnAllMessages } from '@/app/lib/get-server-messages'; | ||
import { useEffect } from 'react'; | ||
|
||
export const prefetchedMessagesAtom = atom<dbReturnAllMessages>( | ||
{} as dbReturnAllMessages | ||
); | ||
|
||
export const isHydrated = atom(false); | ||
|
||
export default function PrefetchedMessagesStore({ | ||
children, | ||
prefetchedMessages | ||
}: { | ||
children: React.ReactNode; | ||
prefetchedMessages: dbReturnAllMessages; | ||
}) { | ||
const setIsHydrated = useSetAtom(isHydrated); | ||
const setPrefetchedMessages = useSetAtom(prefetchedMessagesAtom); | ||
setPrefetchedMessages(prefetchedMessages); | ||
useEffect(() => { | ||
setIsHydrated(true); | ||
}); | ||
return children; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters