ThOneWhKnocks
ThOneWhKnocks•7d ago

The Path To Convex Virtuoso (via the Forest of Pain)

well thats the confusing part... normally the virtuoso expects to be fed batches of some number of messages... like 50 every load
14 Replies
deen
deen•7d ago
so i'm looking back at my old code and i did something kind of insane that i don't understand lol
ThOneWhKnocks
ThOneWhKnocksOP•7d ago
lmao
deen
deen•7d ago
but
ThOneWhKnocks
ThOneWhKnocksOP•7d ago
maybe i just try tanstack virtual? first i just need to get the basic scrolling working i cant even do that rn
deen
deen•7d ago
i'm gonna dump a bunch of code which may or may not help you 😂
ThOneWhKnocks
ThOneWhKnocksOP•7d ago
thanks a lot
deen
deen•7d ago
this is for reverse infinite scrolling only. once you get this part working you'll hopefully...figure out the rest but really don't worry if you can't get to the jump to message right away. its pretty hairy
ThOneWhKnocks
ThOneWhKnocksOP•7d ago
alright :))
deen
deen•7d ago
ffs it didnt let me
deen
deen•7d ago
VirtualizedMessageFeed.tsx
deen
deen•7d ago
so this is part i was embarassed to show at first
export const useMessageFeedQuery = (threadId: string, initialNumItems = 25) => {
const [roleQueryParam] = useRoleQueryParam()

const messages = usePaginatedQuery(
api.entities.threads.messages.search,
{ threadId, role: roleQueryParam || undefined },
{
initialNumItems,
},
)
const results = useMemo(() => messages.results.toReversed(), [messages.results])

const firstLoadedMessageTime = useRef(0)
if (!firstLoadedMessageTime.current && results[0]) {
firstLoadedMessageTime.current = results[0]._creationTime
}
const prependedCount = results.filter(
(message) => message._creationTime < firstLoadedMessageTime.current,
).length

if (!threadId || threadId === 'new') {
if (messages.status === 'LoadingFirstPage') {
messages.status = 'Exhausted' as any
}
}

return { ...messages, results, prependedCount }
}
export const useMessageFeedQuery = (threadId: string, initialNumItems = 25) => {
const [roleQueryParam] = useRoleQueryParam()

const messages = usePaginatedQuery(
api.entities.threads.messages.search,
{ threadId, role: roleQueryParam || undefined },
{
initialNumItems,
},
)
const results = useMemo(() => messages.results.toReversed(), [messages.results])

const firstLoadedMessageTime = useRef(0)
if (!firstLoadedMessageTime.current && results[0]) {
firstLoadedMessageTime.current = results[0]._creationTime
}
const prependedCount = results.filter(
(message) => message._creationTime < firstLoadedMessageTime.current,
).length

if (!threadId || threadId === 'new') {
if (messages.status === 'LoadingFirstPage') {
messages.status = 'Exhausted' as any
}
}

return { ...messages, results, prependedCount }
}
because i dont know why i used the current time???? oh ok. it's how i counted how many new messages were added. but... why didn't i just use the length? maybe your ai can make some sense of this 😭
ThOneWhKnocks
ThOneWhKnocksOP•7d ago
lol hopefully thanks a lot
deen
deen•7d ago
good luck
ThOneWhKnocks
ThOneWhKnocksOP•7d ago
thanks

Did you find this page helpful?