Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/modules/chat/routes/chatMessageDiscoveryLoader.server.ts
T

51 lines
1.9 KiB
TypeScript

import type { LoaderFunctionArgs } from "react-router";
import type { ChatMessageDiscoveryMode } from "@hivegrid/view-models";
import { getTranslations } from "~/locale/i18next.server";
import { loadChatMessageDiscovery } from "~/modules/chat/services/ChatMessageDiscoveryService.server";
import { loadChatContext } from "./chatAccess.server";
import type { ChatMessageDiscoveryLoaderData } from "./chatMessageDiscoveryRouteTypes";
const PAGE_LIMIT = 30;
export async function loadChatMessageDiscoveryRoute(
args: LoaderFunctionArgs,
mode: ChatMessageDiscoveryMode
): Promise<ChatMessageDiscoveryLoaderData> {
const [{ t }, context] = await Promise.all([getTranslations(args.request), loadChatContext(args)]);
const url = new URL(args.request.url);
const tenantSlug = args.params.tenant ?? "";
const pageCursor = normalizeCursor(url.searchParams.get("cursor"));
const query = mode === "search" ? normalizeQuery(url.searchParams.get("q")) : undefined;
const vm = await loadChatMessageDiscovery({
context,
tenantSlug,
mode,
...(query !== undefined ? { query } : {}),
...(pageCursor ? { beforeCursor: pageCursor } : {}),
limit: PAGE_LIMIT,
labels: {
directMessage: t("chat.channel.fallback.directMessage"),
unnamedGroup: t("chat.channel.fallback.group"),
unnamedProject: t("chat.channel.fallback.project"),
unknownUser: t("chat.participant.unknown"),
},
});
return {
title: `${t(mode === "search" ? "chat.messageDiscovery.search.title" : "chat.messageDiscovery.mentions.title")} | ${process.env.APP_NAME}`,
tenantSlug,
pageCursor,
vm,
};
}
function normalizeQuery(value: string | null): string {
return (value ?? "").trim().slice(0, 200);
}
function normalizeCursor(value: string | null): string | null {
const normalized = value?.trim() ?? "";
return normalized ? normalized.slice(0, 2_048) : null;
}