perf(api,frontend): index hot queries, paginate chats, fix realtime

This commit is contained in:
hh committed 2026-08-06 01:52:41 +02:00
1 parent 9d767d2531
commit 525ce024bc
27 files changed
+831 -268

No files matched your search

+20 -12
View File
@@ -1,30 +1,22 @@
import { discoverPeers, searchMessages } from "$lib/api/endpoints";
import type { DiscoverItem, SearchHit } from "$lib/api/types";
import { chats } from "$lib/stores/chats.svelte";
import { discoverPeers, listChats, searchMessages } from "$lib/api/endpoints";
import type { Chat, DiscoverItem, SearchHit } from "$lib/api/types";
const DEBOUNCE_MS = 250;
const REMOTE_DEBOUNCE_MS = 700;
const MIN_LENGTH = 1;
const CHAT_LIMIT = 30;
function createSearch() {
let active = $state(false);
let query = $state("");
let messageHits = $state<SearchHit[]>([]);
let chatHits = $state<Chat[]>([]);
let peerResults = $state<DiscoverItem[]>([]);
let loading = $state(false);
let timers: ReturnType<typeof setTimeout>[] = [];
let seq = 0;
const trimmed = $derived(query.trim());
const chatHits = $derived.by(() => {
const needle = trimmed.toLowerCase();
if (needle.length < MIN_LENGTH) {
return [];
}
return chats.list.filter((chat) =>
(chat.title ?? "").toLowerCase().includes(needle)
);
});
const peerHits = $derived.by(() => {
const shown = new Set(chatHits.map((chat) => chat.chat_id));
return peerResults.filter((item) => !shown.has(item.chat_id));
@@ -47,6 +39,19 @@ function createSearch() {
}
}
async function runChats(value: string, current: number) {
try {
const found = await listChats({ limit: CHAT_LIMIT, search: value });
if (current === seq) {
chatHits = found;
}
} catch {
if (current === seq) {
chatHits = [];
}
}
}
async function runPeers(value: string, current: number, remote: boolean) {
try {
const items = await discoverPeers(value, remote);
@@ -73,6 +78,7 @@ function createSearch() {
const current = ++seq;
if (value.length < MIN_LENGTH) {
messageHits = [];
chatHits = [];
peerResults = [];
loading = false;
return;
@@ -81,6 +87,7 @@ function createSearch() {
timers.push(
setTimeout(() => {
runMessages(value, current).catch(() => undefined);
runChats(value, current).catch(() => undefined);
runPeers(value, current, false).catch(() => undefined);
}, DEBOUNCE_MS),
setTimeout(() => {
@@ -122,6 +129,7 @@ function createSearch() {
active = false;
query = "";
messageHits = [];
chatHits = [];
peerResults = [];
loading = false;
seq++;