infoalloggi-monorepo/apps/infoalloggi/src/pages/area-riservata/chat.tsx

91 lines
2.3 KiB
TypeScript
Raw Normal View History

import { getCookie } from "cookies-next";
2025-08-28 18:27:07 +02:00
import type { GetServerSideProps } from "next";
2025-08-04 17:45:44 +02:00
import Head from "next/head";
import { Suspense } from "react";
import { Sidebar } from "~/components/area-riservata/sidebar";
import Chat from "~/components/chat/chat";
import { LoadingPage } from "~/components/loading";
2025-08-28 18:27:07 +02:00
import { SiteHeader } from "~/components/navbar/site-header";
import type { NextPageWithLayout } from "~/pages/_app";
import { useTranslation } from "~/providers/I18nProvider";
import type { ChatsChatid } from "~/schemas/public/Chats";
2025-08-04 17:45:44 +02:00
import type { Session } from "~/server/api/trpc";
2025-08-28 18:27:07 +02:00
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
2025-08-04 17:45:44 +02:00
type UserChatProps = {
2025-08-28 18:27:07 +02:00
chatId: ChatsChatid;
session: Session;
2025-08-04 17:45:44 +02:00
};
const UserChat: NextPageWithLayout<UserChatProps> = ({
2025-08-28 18:27:07 +02:00
chatId,
session,
2025-08-04 17:45:44 +02:00
}: UserChatProps) => {
2025-08-28 18:27:07 +02:00
const { t } = useTranslation();
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return (
<>
<Head>
<title>{t.heads.area_riservata_titolo}</title>
2025-08-29 16:18:32 +02:00
<meta content={t.heads.area_riservata_description} name="description" />
2025-08-28 18:27:07 +02:00
</Head>
<div className="h-full flex-1 grow">
<div className="flex h-full w-full flex-row">
<div className="h-full">
<Sidebar
className="h-full"
defaultOpen={getCookie("sidebar_minimized") === "true"}
isAdmin={false}
/>
2025-08-28 18:27:07 +02:00
</div>
<Suspense fallback={<LoadingPage />}>
<Chat chatId={chatId} userData={session} />
</Suspense>
</div>
</div>
</>
);
2025-08-04 17:45:44 +02:00
};
UserChat.getLayout = function getLayout(page) {
2025-08-28 18:27:07 +02:00
return (
<div className="h-screen">
<div className="flex h-full flex-col">
<SiteHeader />
<div className="flex min-h-0 grow">{page}</div>
</div>
</div>
);
2025-08-04 17:45:44 +02:00
};
export const getServerSideProps = (async (context) => {
2025-08-28 18:27:07 +02:00
const access_token = context.req.cookies.access_token;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const helper = await TrpcAuthedFetchingIstance({ access_token });
if (helper) {
const chat = await helper.trpc.chat.getSessionChats.fetch();
await helper.trpc.messagesSSE.infinite.prefetchInfinite({
chatId: chat.chatid,
});
await helper.trpc.chat.getUserInfosByChat.prefetch({
chatId: chat.chatid,
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return {
props: {
chatId: chat.chatid,
session: helper.session,
trpcState: helper.trpc.dehydrate(),
},
};
}
return {
redirect: {
destination: "/login",
permanent: false,
},
};
2025-08-04 17:45:44 +02:00
}) satisfies GetServerSideProps<UserChatProps>;
export default UserChat;