- Added API routes for fetching connected users, keywords, missions, and stations. - Created a new QueryProvider component for managing query states and socket events. - Introduced connection stores for dispatch and pilot, managing socket connections and states. - Updated Prisma schema for connected aircraft model. - Enhanced UI with toast notifications for status updates and chat interactions. - Implemented query functions for fetching connected users and keywords with error handling.
89 lines
2.5 KiB
TypeScript
89 lines
2.5 KiB
TypeScript
import { PersonIcon } from "@radix-ui/react-icons";
|
|
import { PrismaClient, User } from "@repo/db";
|
|
import { AdminForm, ConnectionHistory, ProfileForm } from "./_components/forms";
|
|
import { Error } from "../../../../_components/Error";
|
|
|
|
const Page = async ({ params }: { params: { id: string } }) => {
|
|
const prisma = new PrismaClient();
|
|
const { id } = await params;
|
|
|
|
const user: User | null = await prisma.user.findUnique({
|
|
where: {
|
|
id: id,
|
|
},
|
|
});
|
|
|
|
const dispoSessions = await prisma.connectedDispatcher.findMany({
|
|
where: {
|
|
userId: user?.id,
|
|
logoutTime: {
|
|
not: null,
|
|
},
|
|
},
|
|
select: {
|
|
loginTime: true,
|
|
logoutTime: true,
|
|
},
|
|
});
|
|
const totalDispoTime = dispoSessions.reduce((acc, session) => {
|
|
const logoffTime = new Date(session.logoutTime!).getTime();
|
|
const logonTime = new Date(session.loginTime).getTime();
|
|
return acc + (logoffTime - logonTime);
|
|
}, 0);
|
|
|
|
const dispoTime = {
|
|
hours: Math.floor(totalDispoTime / (1000 * 60 * 60)),
|
|
minutes: Math.floor((totalDispoTime % (1000 * 60 * 60)) / (1000 * 60)),
|
|
lastLogin: dispoSessions[dispoSessions.length - 1]?.loginTime,
|
|
};
|
|
|
|
const pilotSessions = await prisma.connectedAircraft.findMany({
|
|
where: {
|
|
userId: user?.id,
|
|
logoutTime: {
|
|
not: null,
|
|
},
|
|
},
|
|
select: {
|
|
loginTime: true,
|
|
logoutTime: true,
|
|
Station: true,
|
|
},
|
|
});
|
|
|
|
const totalPilotTime = pilotSessions.reduce((acc, session) => {
|
|
const logoffTime = new Date(session.logoutTime!).getTime();
|
|
const logonTime = new Date(session.loginTime).getTime();
|
|
return acc + (logoffTime - logonTime);
|
|
}, 0);
|
|
|
|
const pilotTime = {
|
|
hours: Math.floor(totalPilotTime / (1000 * 60 * 60)),
|
|
minutes: Math.floor((totalPilotTime % (1000 * 60 * 60)) / (1000 * 60)),
|
|
lastLogin: pilotSessions[pilotSessions.length - 1]?.loginTime,
|
|
};
|
|
|
|
if (!user) return <Error statusCode={404} title="User not found" />;
|
|
return (
|
|
<div className="grid grid-cols-6 gap-4">
|
|
<div className="col-span-full">
|
|
<p className="text-2xl font-semibold text-left flex items-center gap-2">
|
|
<PersonIcon className="w-5 h-5" />
|
|
{user?.firstname} {user?.lastname} #{user?.publicId}
|
|
</p>
|
|
</div>
|
|
<div className="card bg-base-200 shadow-xl mb-4 col-span-6 xl:col-span-3">
|
|
<ProfileForm user={user} />
|
|
</div>
|
|
<div className="card bg-base-200 shadow-xl mb-4 col-span-6 xl:col-span-3">
|
|
<AdminForm user={user} dispoTime={dispoTime} pilotTime={pilotTime} />
|
|
</div>
|
|
<div className="card bg-base-200 shadow-xl mb-4 col-span-6 xl:col-span-6">
|
|
<ConnectionHistory user={user} />
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default Page;
|