Compare commits
23 Commits
55e0370004
...
v0.21.6
| Author | SHA1 | Date | |
|---|---|---|---|
| 69d92a3fc9 | |||
| e8bf8498b8 | |||
|
|
f15a0175b8 | ||
|
|
996f1a1385 | ||
|
|
0cbc6935db | ||
|
|
6563396a83 | ||
|
|
c005090ab1 | ||
|
|
6aeb654abf | ||
|
|
3ba83ad538 | ||
|
|
fe6a36f3dc | ||
|
|
1ca98233f8 | ||
|
|
fe4d299eae | ||
|
|
b0ee67c2be | ||
|
|
b7999b02e8 | ||
|
|
4bc74f26d1 | ||
|
|
e6b7ec7401 | ||
|
|
373adcb49d | ||
|
|
8a5afee0e3 | ||
|
|
4888db7f1a | ||
|
|
4db5d49a64 | ||
|
|
bb0a5ab66d | ||
|
|
76d08df3da | ||
|
|
fac61ef678 |
16
package-lock.json
generated
16
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "acc-server-manager-web",
|
||||
"version": "0.20.0",
|
||||
"version": "0.20.2",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "acc-server-manager-web",
|
||||
"version": "0.20.0",
|
||||
"version": "0.20.2",
|
||||
"dependencies": {
|
||||
"@date-fns/utc": "^2.1.1",
|
||||
"@hookform/resolvers": "^5.2.1",
|
||||
@@ -27,7 +27,8 @@
|
||||
"react-dom": "19.1.0",
|
||||
"react-hook-form": "^7.62.0",
|
||||
"tailwind-merge": "^3.3.1",
|
||||
"tailwindcss": "^4.1.12"
|
||||
"tailwindcss": "^4.1.12",
|
||||
"zod": "^4.1.11"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/eslintrc": "^3",
|
||||
@@ -6528,6 +6529,15 @@
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/zod": {
|
||||
"version": "4.1.11",
|
||||
"resolved": "https://registry.npmjs.org/zod/-/zod-4.1.11.tgz",
|
||||
"integrity": "sha512-WPsqwxITS2tzx1bzhIKsEs19ABD5vmCVa4xBo2tq/SrV4RNZtfws1EnCWQXM6yh8bD08a1idvkB5MZSBiZsjwg==",
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/colinhacks"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "acc-server-manager-web",
|
||||
"version": "0.20.0",
|
||||
"version": "0.20.2",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev --turbopack",
|
||||
@@ -29,7 +29,8 @@
|
||||
"react-dom": "19.1.0",
|
||||
"react-hook-form": "^7.62.0",
|
||||
"tailwind-merge": "^3.3.1",
|
||||
"tailwindcss": "^4.1.12"
|
||||
"tailwindcss": "^4.1.12",
|
||||
"zod": "^4.1.11"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/eslintrc": "^3",
|
||||
|
||||
23
src/app/dashboard/layout.tsx
Normal file
23
src/app/dashboard/layout.tsx
Normal file
@@ -0,0 +1,23 @@
|
||||
import { WebSocketProvider } from '@/lib/websocket/context';
|
||||
import { SteamCMDProvider } from '@/lib/context/SteamCMDContext';
|
||||
import { ServerCreationPopupProvider } from '@/lib/context/ServerCreationPopupContext';
|
||||
import { ServerCreationPopupContainer } from '@/components/server/ServerCreationPopupContainer';
|
||||
import { requireAuth } from '@/lib/auth/server';
|
||||
|
||||
export default async function DashboardLayout({
|
||||
children
|
||||
}: Readonly<{
|
||||
children: React.ReactNode;
|
||||
}>) {
|
||||
const session = await requireAuth();
|
||||
return (
|
||||
<WebSocketProvider openToken={session.openToken!}>
|
||||
<SteamCMDProvider>
|
||||
<ServerCreationPopupProvider>
|
||||
{children}
|
||||
<ServerCreationPopupContainer />
|
||||
</ServerCreationPopupProvider>
|
||||
</SteamCMDProvider>
|
||||
</WebSocketProvider>
|
||||
);
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
import { requireAuth } from '@/lib/auth/server';
|
||||
import { getServers } from '@/lib/api/server/servers';
|
||||
import { hasPermission } from '@/lib/types';
|
||||
import { ServerCard } from '@/components/server/ServerCard';
|
||||
import { logoutAction } from '@/lib/actions/auth';
|
||||
import RefreshButton from '@/components/ui/RefreshButton';
|
||||
import { hasPermission } from '@/lib/schemas';
|
||||
import Link from 'next/link';
|
||||
import { ServerListWithActions } from '@/components/server/ServerListWithActions';
|
||||
import { SteamCMDNotification } from '@/components/ui/SteamCMDNotification';
|
||||
import LogoutButton from '@/components/ui/LogoutButton';
|
||||
|
||||
export default async function DashboardPage() {
|
||||
const session = await requireAuth();
|
||||
@@ -13,7 +13,7 @@ export default async function DashboardPage() {
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-900 text-white">
|
||||
<header className="bg-gray-800 shadow-md">
|
||||
<div className="mx-auto flex max-w-7xl items-center justify-between px-4 py-4 sm:px-6 lg:px-8">
|
||||
<div className="mx-auto flex max-w-[120rem] items-center justify-between px-4 py-4 sm:px-6 lg:px-8">
|
||||
<h1 className="text-2xl font-bold">ACC Server Manager</h1>
|
||||
<div className="flex items-center space-x-4">
|
||||
{hasPermission(session.user!, 'membership.view') && (
|
||||
@@ -38,40 +38,15 @@ export default async function DashboardPage() {
|
||||
<span className="ml-1 hidden sm:inline">Users</span>
|
||||
</Link>
|
||||
)}
|
||||
<form action={logoutAction}>
|
||||
<button type="submit" className="flex items-center text-gray-300 hover:text-white">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
className="h-6 w-6"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
stroke="currentColor"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth="2"
|
||||
d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"
|
||||
/>
|
||||
</svg>
|
||||
<span className="ml-1 hidden sm:inline">Logout</span>
|
||||
</button>
|
||||
</form>
|
||||
<LogoutButton />
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
|
||||
<div className="mb-6 flex items-center justify-between">
|
||||
<h2 className="text-xl font-semibold">Your Servers</h2>
|
||||
<RefreshButton />
|
||||
</div>
|
||||
<SteamCMDNotification />
|
||||
|
||||
<div className="grid grid-cols-1 gap-6 md:grid-cols-2 lg:grid-cols-3">
|
||||
{servers.map((server) => (
|
||||
<ServerCard key={server.id} server={server} />
|
||||
))}
|
||||
</div>
|
||||
<main className="mx-auto max-w-[120rem] px-4 py-8 sm:px-6 lg:px-8">
|
||||
<ServerListWithActions servers={servers} user={session.user!} />
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -22,13 +22,13 @@ export default async function ServerPage({ params }: ServerPageProps) {
|
||||
const [server, configurations, statistics] = await Promise.all([
|
||||
getServer(session.token!, id),
|
||||
getServerConfigurations(session.token!, id),
|
||||
getServerStatistics(session.token!, id, startDate, endDate)
|
||||
getServerStatistics(session.token!, id, { startDate, endDate })
|
||||
]);
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-900 text-white">
|
||||
<div className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
|
||||
<ServerHeader server={server} />
|
||||
<div className="mx-auto max-w-[120rem] px-4 py-8 sm:px-6 lg:px-8">
|
||||
<ServerHeader server={server} user={session.user!} />
|
||||
|
||||
<div className="mt-8">
|
||||
<ServerConfigurationTabs
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import type { Metadata } from 'next';
|
||||
import './globals.css';
|
||||
import { QueryProvider } from '@/components/providers/QueryProvider';
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'ACC Server Manager',
|
||||
@@ -14,9 +13,7 @@ export default function RootLayout({
|
||||
}>) {
|
||||
return (
|
||||
<html lang="en">
|
||||
<body className="bg-gray-900 text-white antialiased">
|
||||
<QueryProvider>{children}</QueryProvider>
|
||||
</body>
|
||||
<body className="bg-gray-900 text-white antialiased">{children}</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,65 +1,15 @@
|
||||
'use client';
|
||||
import { Suspense } from 'react';
|
||||
import LoginForm from '@/components/login/LoginForm';
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
import { loginAction, LoginResult } from '@/lib/actions/auth';
|
||||
import { useActionState } from 'react';
|
||||
|
||||
const initialState: LoginResult = {
|
||||
message: '',
|
||||
success: true
|
||||
};
|
||||
|
||||
export default function LoginPage() {
|
||||
const [state, formAction] = useActionState(loginAction, initialState);
|
||||
export default function LoginPage({
|
||||
searchParams
|
||||
}: {
|
||||
searchParams: Promise<{ expired: boolean | undefined }>;
|
||||
}) {
|
||||
return (
|
||||
<div className="flex min-h-screen items-center justify-center bg-gray-900 px-4">
|
||||
<div className="w-full max-w-md space-y-8 rounded-lg bg-gray-800 p-8 shadow-lg">
|
||||
<div className="text-center">
|
||||
<h1 className="text-3xl font-bold text-white">ACC Server Manager</h1>
|
||||
<p className="mt-2 text-gray-400">Sign in to manage your servers</p>
|
||||
</div>
|
||||
{state?.success ? null : (
|
||||
<div className="rounded-md border border-red-700 bg-red-900/50 p-3 text-sm text-red-200">
|
||||
{state?.message}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form action={formAction} className="space-y-6">
|
||||
<div>
|
||||
<label htmlFor="username" className="mb-2 block text-sm font-medium text-gray-300">
|
||||
Username
|
||||
</label>
|
||||
<input
|
||||
id="username"
|
||||
name="username"
|
||||
type="text"
|
||||
autoComplete="username"
|
||||
required
|
||||
className="form-input w-full"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label htmlFor="password" className="mb-2 block text-sm font-medium text-gray-300">
|
||||
Password
|
||||
</label>
|
||||
<input
|
||||
id="password"
|
||||
name="password"
|
||||
type="password"
|
||||
autoComplete="current-password"
|
||||
required
|
||||
className="form-input w-full"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
className="w-full rounded-md bg-blue-600 px-4 py-3 font-medium text-white transition-colors hover:bg-blue-700 focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-gray-800 focus:outline-none"
|
||||
>
|
||||
Sign in
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<Suspense fallback={<div>Loading...</div>}>
|
||||
<LoginForm searchParams={searchParams} />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import type { AssistRules } from '@/lib/types/config';
|
||||
import type { AssistRules } from '@/lib/schemas/config';
|
||||
import { updateAssistRulesAction } from '@/lib/actions/configuration';
|
||||
|
||||
interface AssistRulesEditorProps {
|
||||
@@ -9,41 +9,7 @@ interface AssistRulesEditorProps {
|
||||
config: AssistRules;
|
||||
}
|
||||
|
||||
export function AssistRulesEditor({ serverId, config }: AssistRulesEditorProps) {
|
||||
const [formData, setFormData] = useState<AssistRules>(config);
|
||||
const [restart, setRestart] = useState(true);
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
setIsSubmitting(true);
|
||||
|
||||
const formDataObj = new FormData();
|
||||
Object.entries(formData).forEach(([key, value]) => {
|
||||
formDataObj.append(key, value.toString());
|
||||
});
|
||||
if (restart) {
|
||||
formDataObj.append('restart', 'on');
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await updateAssistRulesAction(serverId, formDataObj);
|
||||
if (!result.success) {
|
||||
console.error('Failed to update assist rules:', result.message);
|
||||
}
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleInputChange = (key: keyof AssistRules, value: string | number) => {
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
[key]: typeof value === 'string' ? parseInt(value) : value
|
||||
}));
|
||||
};
|
||||
|
||||
const assistFields = [
|
||||
const assistFields = [
|
||||
{
|
||||
key: 'stabilityControlLevelMax' as keyof AssistRules,
|
||||
label: 'Stability Control Level Max',
|
||||
@@ -89,7 +55,41 @@ export function AssistRulesEditor({ serverId, config }: AssistRulesEditorProps)
|
||||
label: 'Disable Ideal Line',
|
||||
type: 'select'
|
||||
}
|
||||
];
|
||||
];
|
||||
|
||||
export function AssistRulesEditor({ serverId, config }: AssistRulesEditorProps) {
|
||||
const [formData, setFormData] = useState<AssistRules>(config);
|
||||
const [restart, setRestart] = useState(true);
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
setIsSubmitting(true);
|
||||
|
||||
const formDataObj = new FormData();
|
||||
Object.entries(formData).forEach(([key, value]) => {
|
||||
formDataObj.append(key, value.toString());
|
||||
});
|
||||
if (restart) {
|
||||
formDataObj.append('restart', 'on');
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await updateAssistRulesAction(serverId, formDataObj);
|
||||
if (!result.success) {
|
||||
console.error('Failed to update assist rules:', result.message);
|
||||
}
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleInputChange = (key: keyof AssistRules, value: string | number) => {
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
[key]: typeof value === 'string' ? parseInt(value) : value
|
||||
}));
|
||||
};
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="max-w-3xl space-y-6">
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import type { Configuration } from '@/lib/types/config';
|
||||
import type { Configuration } from '@/lib/schemas/config';
|
||||
import { updateConfigurationAction } from '@/lib/actions/configuration';
|
||||
|
||||
interface ConfigurationEditorProps {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import type { EventConfig, Session } from '@/lib/types/config';
|
||||
import type { EventConfig, Session } from '@/lib/schemas/config';
|
||||
import { updateEventConfigAction } from '@/lib/actions/configuration';
|
||||
|
||||
interface EventConfigEditorProps {
|
||||
@@ -9,6 +9,12 @@ interface EventConfigEditorProps {
|
||||
config: EventConfig;
|
||||
}
|
||||
|
||||
const sessionTypes = [
|
||||
{ value: 'P', label: 'Practice' },
|
||||
{ value: 'Q', label: 'Qualifying' },
|
||||
{ value: 'R', label: 'Race' }
|
||||
];
|
||||
|
||||
export function EventConfigEditor({ serverId, config }: EventConfigEditorProps) {
|
||||
const [formData, setFormData] = useState<EventConfig>(config);
|
||||
const [restart, setRestart] = useState(true);
|
||||
@@ -90,12 +96,6 @@ export function EventConfigEditor({ serverId, config }: EventConfigEditorProps)
|
||||
}));
|
||||
};
|
||||
|
||||
const sessionTypes = [
|
||||
{ value: 'P', label: 'Practice' },
|
||||
{ value: 'Q', label: 'Qualifying' },
|
||||
{ value: 'R', label: 'Race' }
|
||||
];
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="max-w-4xl space-y-8">
|
||||
<div className="space-y-6">
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import type { EventRules } from '@/lib/types/config';
|
||||
import type { EventRules } from '@/lib/schemas/config';
|
||||
import { updateEventRulesAction } from '@/lib/actions/configuration';
|
||||
|
||||
interface EventRulesEditorProps {
|
||||
@@ -9,6 +9,65 @@ interface EventRulesEditorProps {
|
||||
config: EventRules;
|
||||
}
|
||||
|
||||
const numberFields = [
|
||||
{
|
||||
key: 'qualifyStandingType' as keyof EventRules,
|
||||
label: 'Qualify Standing Type',
|
||||
min: -1,
|
||||
max: 1
|
||||
},
|
||||
{
|
||||
key: 'pitWindowLengthSec' as keyof EventRules,
|
||||
label: 'Pit Window Length (seconds)',
|
||||
min: -1
|
||||
},
|
||||
{
|
||||
key: 'driverStintTimeSec' as keyof EventRules,
|
||||
label: 'Driver Stint Time (seconds)',
|
||||
min: -1
|
||||
},
|
||||
{
|
||||
key: 'mandatoryPitstopCount' as keyof EventRules,
|
||||
label: 'Mandatory Pitstop Count',
|
||||
min: -1,
|
||||
max: 5
|
||||
},
|
||||
{
|
||||
key: 'maxTotalDrivingTime' as keyof EventRules,
|
||||
label: 'Max Total Driving Time (seconds)',
|
||||
min: -1
|
||||
},
|
||||
{
|
||||
key: 'tyreSetCount' as keyof EventRules,
|
||||
label: 'Tyre Set Count',
|
||||
min: 0,
|
||||
max: 50
|
||||
}
|
||||
];
|
||||
|
||||
const booleanFields = [
|
||||
{
|
||||
key: 'isRefuellingAllowedInRace' as keyof EventRules,
|
||||
label: 'Refuelling Allowed in Race'
|
||||
},
|
||||
{
|
||||
key: 'isRefuellingTimeFixed' as keyof EventRules,
|
||||
label: 'Refuelling Time Fixed'
|
||||
},
|
||||
{
|
||||
key: 'isMandatoryPitstopRefuellingRequired' as keyof EventRules,
|
||||
label: 'Mandatory Pitstop Refuelling Required'
|
||||
},
|
||||
{
|
||||
key: 'isMandatoryPitstopTyreChangeRequired' as keyof EventRules,
|
||||
label: 'Mandatory Pitstop Tyre Change Required'
|
||||
},
|
||||
{
|
||||
key: 'isMandatoryPitstopSwapDriverRequired' as keyof EventRules,
|
||||
label: 'Mandatory Pitstop Swap Driver Required'
|
||||
}
|
||||
];
|
||||
|
||||
export function EventRulesEditor({ serverId, config }: EventRulesEditorProps) {
|
||||
const [formData, setFormData] = useState<EventRules>(config);
|
||||
const [restart, setRestart] = useState(true);
|
||||
@@ -43,65 +102,6 @@ export function EventRulesEditor({ serverId, config }: EventRulesEditorProps) {
|
||||
}));
|
||||
};
|
||||
|
||||
const numberFields = [
|
||||
{
|
||||
key: 'qualifyStandingType' as keyof EventRules,
|
||||
label: 'Qualify Standing Type',
|
||||
min: -1,
|
||||
max: 1
|
||||
},
|
||||
{
|
||||
key: 'pitWindowLengthSec' as keyof EventRules,
|
||||
label: 'Pit Window Length (seconds)',
|
||||
min: -1
|
||||
},
|
||||
{
|
||||
key: 'driverStintTimeSec' as keyof EventRules,
|
||||
label: 'Driver Stint Time (seconds)',
|
||||
min: -1
|
||||
},
|
||||
{
|
||||
key: 'mandatoryPitstopCount' as keyof EventRules,
|
||||
label: 'Mandatory Pitstop Count',
|
||||
min: -1,
|
||||
max: 5
|
||||
},
|
||||
{
|
||||
key: 'maxTotalDrivingTime' as keyof EventRules,
|
||||
label: 'Max Total Driving Time (seconds)',
|
||||
min: -1
|
||||
},
|
||||
{
|
||||
key: 'tyreSetCount' as keyof EventRules,
|
||||
label: 'Tyre Set Count',
|
||||
min: 0,
|
||||
max: 50
|
||||
}
|
||||
];
|
||||
|
||||
const booleanFields = [
|
||||
{
|
||||
key: 'isRefuellingAllowedInRace' as keyof EventRules,
|
||||
label: 'Refuelling Allowed in Race'
|
||||
},
|
||||
{
|
||||
key: 'isRefuellingTimeFixed' as keyof EventRules,
|
||||
label: 'Refuelling Time Fixed'
|
||||
},
|
||||
{
|
||||
key: 'isMandatoryPitstopRefuellingRequired' as keyof EventRules,
|
||||
label: 'Mandatory Pitstop Refuelling Required'
|
||||
},
|
||||
{
|
||||
key: 'isMandatoryPitstopTyreChangeRequired' as keyof EventRules,
|
||||
label: 'Mandatory Pitstop Tyre Change Required'
|
||||
},
|
||||
{
|
||||
key: 'isMandatoryPitstopSwapDriverRequired' as keyof EventRules,
|
||||
label: 'Mandatory Pitstop Swap Driver Required'
|
||||
}
|
||||
];
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="max-w-4xl space-y-8">
|
||||
<div className="space-y-6">
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import type { ServerSettings } from '@/lib/types/config';
|
||||
import type { ServerSettings } from '@/lib/schemas/config';
|
||||
import { updateServerSettingsAction } from '@/lib/actions/configuration';
|
||||
|
||||
interface ServerSettingsEditorProps {
|
||||
@@ -9,6 +9,84 @@ interface ServerSettingsEditorProps {
|
||||
config: ServerSettings;
|
||||
}
|
||||
|
||||
const textFields = [
|
||||
{
|
||||
key: 'serverName' as keyof ServerSettings,
|
||||
label: 'Server Name',
|
||||
type: 'text'
|
||||
},
|
||||
{
|
||||
key: 'adminPassword' as keyof ServerSettings,
|
||||
label: 'Admin Password',
|
||||
type: 'password'
|
||||
},
|
||||
{
|
||||
key: 'password' as keyof ServerSettings,
|
||||
label: 'Password',
|
||||
type: 'password'
|
||||
},
|
||||
{
|
||||
key: 'spectatorPassword' as keyof ServerSettings,
|
||||
label: 'Spectator Password',
|
||||
type: 'password'
|
||||
},
|
||||
{
|
||||
key: 'centralEntryListPath' as keyof ServerSettings,
|
||||
label: 'Central Entry List Path',
|
||||
type: 'text'
|
||||
}
|
||||
];
|
||||
|
||||
const carGroups = ['FreeForAll', 'GT3', 'GT4', 'GT2', 'GTC', 'TCX'];
|
||||
|
||||
const numberFields = [
|
||||
{
|
||||
key: 'trackMedalsRequirement' as keyof ServerSettings,
|
||||
label: 'Track Medals Requirement',
|
||||
min: -1,
|
||||
max: 3
|
||||
},
|
||||
{
|
||||
key: 'safetyRatingRequirement' as keyof ServerSettings,
|
||||
label: 'Safety Rating Requirement',
|
||||
min: -1,
|
||||
max: 99
|
||||
},
|
||||
{
|
||||
key: 'racecraftRatingRequirement' as keyof ServerSettings,
|
||||
label: 'Racecraft Rating Requirement',
|
||||
min: -1,
|
||||
max: 99
|
||||
},
|
||||
{
|
||||
key: 'maxCarSlots' as keyof ServerSettings,
|
||||
label: 'Max Car Slots',
|
||||
min: 1,
|
||||
max: 30
|
||||
}
|
||||
];
|
||||
|
||||
const selectFields = [
|
||||
{
|
||||
key: 'dumpLeaderboards' as keyof ServerSettings,
|
||||
label: 'Dump Leaderboards'
|
||||
},
|
||||
{ key: 'isRaceLocked' as keyof ServerSettings, label: 'Race Locked' },
|
||||
{
|
||||
key: 'randomizeTrackWhenEmpty' as keyof ServerSettings,
|
||||
label: 'Randomize Track When Empty'
|
||||
},
|
||||
{ key: 'allowAutoDQ' as keyof ServerSettings, label: 'Allow Auto DQ' },
|
||||
{
|
||||
key: 'shortFormationLap' as keyof ServerSettings,
|
||||
label: 'Short Formation Lap'
|
||||
},
|
||||
{
|
||||
key: 'ignorePrematureDisconnects' as keyof ServerSettings,
|
||||
label: 'Ignore Premature Disconnects'
|
||||
}
|
||||
];
|
||||
|
||||
export function ServerSettingsEditor({ serverId, config }: ServerSettingsEditorProps) {
|
||||
const [formData, setFormData] = useState<ServerSettings>(config);
|
||||
const [restart, setRestart] = useState(true);
|
||||
@@ -43,84 +121,6 @@ export function ServerSettingsEditor({ serverId, config }: ServerSettingsEditorP
|
||||
}));
|
||||
};
|
||||
|
||||
const textFields = [
|
||||
{
|
||||
key: 'serverName' as keyof ServerSettings,
|
||||
label: 'Server Name',
|
||||
type: 'text'
|
||||
},
|
||||
{
|
||||
key: 'adminPassword' as keyof ServerSettings,
|
||||
label: 'Admin Password',
|
||||
type: 'password'
|
||||
},
|
||||
{
|
||||
key: 'password' as keyof ServerSettings,
|
||||
label: 'Password',
|
||||
type: 'password'
|
||||
},
|
||||
{
|
||||
key: 'spectatorPassword' as keyof ServerSettings,
|
||||
label: 'Spectator Password',
|
||||
type: 'password'
|
||||
},
|
||||
{
|
||||
key: 'centralEntryListPath' as keyof ServerSettings,
|
||||
label: 'Central Entry List Path',
|
||||
type: 'text'
|
||||
}
|
||||
];
|
||||
|
||||
const carGroups = ['FreeForAll', 'GT3', 'GT4', 'GT2', 'GTC', 'TCX'];
|
||||
|
||||
const numberFields = [
|
||||
{
|
||||
key: 'trackMedalsRequirement' as keyof ServerSettings,
|
||||
label: 'Track Medals Requirement',
|
||||
min: -1,
|
||||
max: 3
|
||||
},
|
||||
{
|
||||
key: 'safetyRatingRequirement' as keyof ServerSettings,
|
||||
label: 'Safety Rating Requirement',
|
||||
min: -1,
|
||||
max: 99
|
||||
},
|
||||
{
|
||||
key: 'racecraftRatingRequirement' as keyof ServerSettings,
|
||||
label: 'Racecraft Rating Requirement',
|
||||
min: -1,
|
||||
max: 99
|
||||
},
|
||||
{
|
||||
key: 'maxCarSlots' as keyof ServerSettings,
|
||||
label: 'Max Car Slots',
|
||||
min: 1,
|
||||
max: 30
|
||||
}
|
||||
];
|
||||
|
||||
const selectFields = [
|
||||
{
|
||||
key: 'dumpLeaderboards' as keyof ServerSettings,
|
||||
label: 'Dump Leaderboards'
|
||||
},
|
||||
{ key: 'isRaceLocked' as keyof ServerSettings, label: 'Race Locked' },
|
||||
{
|
||||
key: 'randomizeTrackWhenEmpty' as keyof ServerSettings,
|
||||
label: 'Randomize Track When Empty'
|
||||
},
|
||||
{ key: 'allowAutoDQ' as keyof ServerSettings, label: 'Allow Auto DQ' },
|
||||
{
|
||||
key: 'shortFormationLap' as keyof ServerSettings,
|
||||
label: 'Short Formation Lap'
|
||||
},
|
||||
{
|
||||
key: 'ignorePrematureDisconnects' as keyof ServerSettings,
|
||||
label: 'Ignore Premature Disconnects'
|
||||
}
|
||||
];
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="max-w-4xl space-y-8">
|
||||
<div className="space-y-6">
|
||||
|
||||
82
src/components/login/LoginForm.tsx
Normal file
82
src/components/login/LoginForm.tsx
Normal file
@@ -0,0 +1,82 @@
|
||||
'use client';
|
||||
|
||||
import { clearExpiredSessionAction, loginAction, LoginResult } from '@/lib/actions/auth';
|
||||
import { use, useActionState, useEffect } from 'react';
|
||||
|
||||
const initialState: LoginResult = {
|
||||
message: '',
|
||||
success: true
|
||||
};
|
||||
|
||||
export default function LoginForm({
|
||||
searchParams
|
||||
}: {
|
||||
searchParams: Promise<{ expired: boolean | undefined }>;
|
||||
}) {
|
||||
const params = use(searchParams);
|
||||
const expired = params.expired;
|
||||
|
||||
useEffect(() => {
|
||||
if (expired) {
|
||||
clearExpiredSessionAction();
|
||||
}
|
||||
}, [expired]);
|
||||
const [state, formAction] = useActionState(loginAction, initialState);
|
||||
return (
|
||||
<div className="flex min-h-screen items-center justify-center bg-gray-900 px-4">
|
||||
<div className="w-full max-w-md space-y-8 rounded-lg bg-gray-800 p-8 shadow-lg">
|
||||
<div className="text-center">
|
||||
<h1 className="text-3xl font-bold text-white">ACC Server Manager</h1>
|
||||
<p className="mt-2 text-gray-400">Sign in to manage your servers</p>
|
||||
</div>
|
||||
{expired && (
|
||||
<div className="rounded-md border border-yellow-700 bg-yellow-900/50 p-3 text-sm text-yellow-200">
|
||||
Your session has expired. Please sign in again.
|
||||
</div>
|
||||
)}
|
||||
{state?.success ? null : (
|
||||
<div className="rounded-md border border-red-700 bg-red-900/50 p-3 text-sm text-red-200">
|
||||
{state?.message}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form action={formAction} className="space-y-6">
|
||||
<div>
|
||||
<label htmlFor="username" className="mb-2 block text-sm font-medium text-gray-300">
|
||||
Username
|
||||
</label>
|
||||
<input
|
||||
id="username"
|
||||
name="username"
|
||||
type="text"
|
||||
autoComplete="username"
|
||||
required
|
||||
className="form-input w-full"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label htmlFor="password" className="mb-2 block text-sm font-medium text-gray-300">
|
||||
Password
|
||||
</label>
|
||||
<input
|
||||
id="password"
|
||||
name="password"
|
||||
type="password"
|
||||
autoComplete="current-password"
|
||||
required
|
||||
className="form-input w-full"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
className="w-full rounded-md bg-blue-600 px-4 py-3 font-medium text-white transition-colors hover:bg-blue-700 focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-gray-800 focus:outline-none"
|
||||
>
|
||||
Sign in
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import type { Role } from '@/lib/types';
|
||||
import type { Role } from '@/lib/schemas';
|
||||
import { createUserAction } from '@/lib/actions/membership';
|
||||
|
||||
interface CreateUserModalProps {
|
||||
@@ -32,7 +32,7 @@ export function CreateUserModal({ roles, onClose }: CreateUserModalProps) {
|
||||
const result = await createUserAction(formDataObj);
|
||||
if (result.success) {
|
||||
onClose();
|
||||
window.location.reload(); // Refresh to show new user
|
||||
window.location.reload();
|
||||
} else {
|
||||
setError(result.message);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import type { User } from '@/lib/types';
|
||||
import type { User } from '@/lib/schemas';
|
||||
import { deleteUserAction } from '@/lib/actions/membership';
|
||||
|
||||
interface DeleteUserModalProps {
|
||||
@@ -25,7 +25,7 @@ export function DeleteUserModal({ user, onClose }: DeleteUserModalProps) {
|
||||
const result = await deleteUserAction(formDataObj);
|
||||
if (result.success) {
|
||||
onClose();
|
||||
window.location.reload(); // Refresh to remove deleted user
|
||||
window.location.reload();
|
||||
} else {
|
||||
setError(result.message);
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
import { useState } from 'react';
|
||||
import Link from 'next/link';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import type { Role, User } from '@/lib/types';
|
||||
import { hasPermission } from '@/lib/types/user';
|
||||
import type { Role, User } from '@/lib/schemas';
|
||||
import { hasPermission } from '@/lib/schemas/user';
|
||||
import { CreateUserModal } from './CreateUserModal';
|
||||
import { DeleteUserModal } from './DeleteUserModal';
|
||||
|
||||
@@ -80,7 +80,7 @@ export function UserManagementTable({ initialData, roles, currentUser }: UserMan
|
||||
return (
|
||||
<>
|
||||
<header className="bg-gray-800 shadow-md">
|
||||
<div className="mx-auto flex max-w-7xl items-center justify-between px-4 py-4 sm:px-6 lg:px-8">
|
||||
<div className="mx-auto flex max-w-[120rem] items-center justify-between px-4 py-4 sm:px-6 lg:px-8">
|
||||
<div className="flex items-center space-x-4">
|
||||
<Link href="/dashboard" className="text-gray-300 hover:text-white">
|
||||
<svg
|
||||
@@ -111,7 +111,7 @@ export function UserManagementTable({ initialData, roles, currentUser }: UserMan
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
|
||||
<main className="mx-auto max-w-[120rem] px-4 py-8 sm:px-6 lg:px-8">
|
||||
{/* Filters */}
|
||||
<div className="mb-6 rounded-lg border border-gray-700 bg-gray-800 p-4">
|
||||
<h2 className="mb-3 text-lg font-semibold">Filters</h2>
|
||||
|
||||
108
src/components/server/CreateServerModal.tsx
Normal file
108
src/components/server/CreateServerModal.tsx
Normal file
@@ -0,0 +1,108 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect, useActionState, useTransition } from 'react';
|
||||
import { Modal } from '@/components/ui/Modal';
|
||||
import { LoadingSpinner } from '@/components/ui/LoadingSpinner';
|
||||
import { createServerAction, type ServerActionResult } from '@/lib/actions/server-management';
|
||||
import { useServerCreationPopup } from '@/lib/context/ServerCreationPopupContext';
|
||||
|
||||
interface CreateServerModalProps {
|
||||
isOpen: boolean;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
const initialState: ServerActionResult = { success: false, message: '', data: undefined };
|
||||
|
||||
export function CreateServerModal({ isOpen, onClose }: CreateServerModalProps) {
|
||||
const [serverName, setServerName] = useState('');
|
||||
const [submittedName, setSubmittedName] = useState('');
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [isPending, setTransition] = useTransition();
|
||||
|
||||
const [state, formAction] = useActionState(createServerAction, initialState);
|
||||
const { showPopup } = useServerCreationPopup();
|
||||
|
||||
useEffect(() => {
|
||||
if (state.success && state.data?.id) {
|
||||
showPopup(state.data.id, submittedName);
|
||||
onClose();
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
}, [state.success, state.data, showPopup, onClose, submittedName]);
|
||||
|
||||
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) =>
|
||||
setTransition(async () => {
|
||||
e.preventDefault();
|
||||
|
||||
if (!serverName.trim()) {
|
||||
return;
|
||||
}
|
||||
|
||||
setIsSubmitting(true);
|
||||
const formData = new FormData();
|
||||
formData.append('name', serverName.trim());
|
||||
formAction(formData);
|
||||
setSubmittedName(serverName.trim());
|
||||
setServerName('');
|
||||
});
|
||||
|
||||
const handleClose = () => {
|
||||
if (isSubmitting) {
|
||||
return;
|
||||
}
|
||||
onClose();
|
||||
setServerName('');
|
||||
setIsSubmitting(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<Modal isOpen={isOpen} onClose={handleClose} title="Create New Server">
|
||||
{!state.success && state.message && (
|
||||
<div className="mb-4 rounded-md bg-red-900 p-3 text-sm text-red-300">{state.message}</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="mb-6">
|
||||
<label htmlFor="server-name" className="block text-sm font-medium text-gray-300">
|
||||
Server Name
|
||||
</label>
|
||||
<input
|
||||
id="server-name"
|
||||
type="text"
|
||||
value={serverName}
|
||||
onChange={(e) => setServerName(e.target.value)}
|
||||
required
|
||||
disabled={isSubmitting || isPending}
|
||||
className="mt-1 block w-full rounded-md border border-gray-600 bg-gray-700 px-3 py-2 text-white focus:border-blue-500 focus:ring-1 focus:ring-blue-500 focus:outline-none disabled:opacity-50"
|
||||
placeholder="Enter server name..."
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-end space-x-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleClose}
|
||||
disabled={isSubmitting || isPending}
|
||||
className="rounded-md bg-gray-600 px-4 py-2 text-sm font-medium transition-colors hover:bg-gray-700 disabled:opacity-50"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isSubmitting || !serverName.trim() || isPending}
|
||||
className="flex items-center rounded-md bg-green-600 px-4 py-2 text-sm font-medium transition-colors hover:bg-green-700 disabled:opacity-50"
|
||||
>
|
||||
{isSubmitting ? (
|
||||
<>
|
||||
<LoadingSpinner className="mr-2 h-4 w-4" />
|
||||
Creating...
|
||||
</>
|
||||
) : (
|
||||
'Create Server'
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
79
src/components/server/DeleteServerModal.tsx
Normal file
79
src/components/server/DeleteServerModal.tsx
Normal file
@@ -0,0 +1,79 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useTransition } from 'react';
|
||||
import { Modal } from '@/components/ui/Modal';
|
||||
import { LoadingSpinner } from '@/components/ui/LoadingSpinner';
|
||||
import { deleteServerAction } from '@/lib/actions/server-management';
|
||||
import { Server } from '@/lib/schemas/server';
|
||||
|
||||
interface DeleteServerModalProps {
|
||||
isOpen: boolean;
|
||||
onClose: () => void;
|
||||
server: Server;
|
||||
}
|
||||
|
||||
export function DeleteServerModal({ isOpen, onClose, server }: DeleteServerModalProps) {
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [isPending, startTransition] = useTransition();
|
||||
|
||||
const handleDelete = () => {
|
||||
setError(null);
|
||||
startTransition(async () => {
|
||||
try {
|
||||
const result = await deleteServerAction(server.id);
|
||||
if (result.success) {
|
||||
onClose();
|
||||
} else {
|
||||
setError(result.message);
|
||||
}
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to delete server');
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
if (isPending) {
|
||||
return;
|
||||
}
|
||||
onClose();
|
||||
setError(null);
|
||||
};
|
||||
|
||||
return (
|
||||
<Modal isOpen={isOpen} onClose={handleClose} title="Delete Server">
|
||||
{error && <div className="mb-4 rounded-md bg-red-900 p-3 text-sm text-red-300">{error}</div>}
|
||||
|
||||
<div className="mb-6">
|
||||
<p className="text-gray-300">
|
||||
Are you sure you want to delete the server <strong>"{server.name}"</strong>?
|
||||
</p>
|
||||
<p className="mt-2 text-sm text-gray-400">This action cannot be undone.</p>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-end space-x-2">
|
||||
<button
|
||||
onClick={handleClose}
|
||||
disabled={isPending}
|
||||
className="rounded-md bg-gray-600 px-4 py-2 text-sm font-medium transition-colors hover:bg-gray-700 disabled:opacity-50"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
onClick={handleDelete}
|
||||
disabled={isPending}
|
||||
className="flex items-center rounded-md bg-red-600 px-4 py-2 text-sm font-medium transition-colors hover:bg-red-700 disabled:opacity-50"
|
||||
>
|
||||
{isPending ? (
|
||||
<>
|
||||
<LoadingSpinner size="sm" className="mr-2" />
|
||||
Deleting...
|
||||
</>
|
||||
) : (
|
||||
'Delete Server'
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
@@ -1,13 +1,46 @@
|
||||
import Link from 'next/link';
|
||||
import { Server, ServiceStatus, getStatusColor, serviceStatusToString } from '@/lib/types';
|
||||
import { startServerAction, stopServerAction, restartServerAction } from '@/lib/actions/servers';
|
||||
import { useTransition } from 'react';
|
||||
import { Server, ServiceStatus, getStatusColor, serviceStatusToString } from '@/lib/schemas';
|
||||
import {
|
||||
startServerEventAction,
|
||||
restartServerEventAction,
|
||||
stopServerEventAction
|
||||
} from '@/lib/actions/servers';
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
interface ServerCardProps {
|
||||
server: Server;
|
||||
}
|
||||
|
||||
export function ServerCard({ server }: ServerCardProps) {
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const router = useRouter();
|
||||
|
||||
const startServer = () =>
|
||||
startTransition(async () => {
|
||||
await startServerEventAction(server.id);
|
||||
router.refresh();
|
||||
});
|
||||
const restartServer = () =>
|
||||
startTransition(async () => {
|
||||
await restartServerEventAction(server.id);
|
||||
router.refresh();
|
||||
});
|
||||
const stopServer = () =>
|
||||
startTransition(async () => {
|
||||
await stopServerEventAction(server.id);
|
||||
router.refresh();
|
||||
});
|
||||
|
||||
const disabled = [
|
||||
ServiceStatus.Restarting,
|
||||
ServiceStatus.Starting,
|
||||
ServiceStatus.Stopping,
|
||||
ServiceStatus.Unknown
|
||||
].includes(server.status);
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="overflow-hidden rounded-lg border border-gray-700 bg-gray-800 shadow-lg">
|
||||
<Link href={`/dashboard/server/${server.id}`} className="block">
|
||||
<div className="p-6">
|
||||
@@ -23,6 +56,7 @@ export function ServerCard({ server }: ServerCardProps) {
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center space-x-2">
|
||||
<div className="text-gray-400 hover:text-white">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
@@ -34,6 +68,7 @@ export function ServerCard({ server }: ServerCardProps) {
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 grid grid-cols-2 gap-4 text-sm text-gray-300">
|
||||
<div>
|
||||
@@ -49,36 +84,34 @@ export function ServerCard({ server }: ServerCardProps) {
|
||||
</Link>
|
||||
|
||||
<div className="flex justify-between gap-2 bg-gray-900 px-4 py-3">
|
||||
<form action={startServerAction.bind(null, server.id)}>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={server.status === ServiceStatus.Running}
|
||||
type="button"
|
||||
onClick={startServer}
|
||||
disabled={server.status === ServiceStatus.Running || isPending || disabled}
|
||||
className="rounded bg-green-600 px-3 py-2 text-xs font-medium text-white transition-colors hover:bg-green-700 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
Start
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<form action={restartServerAction.bind(null, server.id)}>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={server.status === ServiceStatus.Stopped}
|
||||
type="button"
|
||||
onClick={restartServer}
|
||||
disabled={server.status === ServiceStatus.Stopped || isPending || disabled}
|
||||
className="rounded bg-yellow-600 px-3 py-2 text-xs font-medium text-white transition-colors hover:bg-yellow-700 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
Restart
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<form action={stopServerAction.bind(null, server.id)}>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={server.status === ServiceStatus.Stopped}
|
||||
type="button"
|
||||
onClick={stopServer}
|
||||
disabled={server.status === ServiceStatus.Stopped || isPending || disabled}
|
||||
className="rounded bg-red-600 px-3 py-2 text-xs font-medium text-white transition-colors hover:bg-red-700 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
Stop
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { Configurations, ServerTab } from '@/lib/types/config';
|
||||
import { Configurations, ServerTab } from '@/lib/schemas/config';
|
||||
import { ConfigurationEditor } from '@/components/configuration/ConfigurationEditor';
|
||||
import { AssistRulesEditor } from '@/components/configuration/AssistRulesEditor';
|
||||
import { EventConfigEditor } from '@/components/configuration/EventConfigEditor';
|
||||
@@ -8,13 +8,21 @@ import { EventRulesEditor } from '@/components/configuration/EventRulesEditor';
|
||||
import { ServerSettingsEditor } from '@/components/configuration/ServerSettingsEditor';
|
||||
import { StatisticsDashboard } from '@/components/statistics/StatisticsDashboard';
|
||||
import { useState } from 'react';
|
||||
import { StateHistoryStats } from '@/lib/types';
|
||||
import { StateHistoryStats } from '@/lib/schemas';
|
||||
|
||||
interface ServerConfigurationTabsProps {
|
||||
serverId: string;
|
||||
configurations: Configurations;
|
||||
statistics: StateHistoryStats;
|
||||
}
|
||||
const tabs = [
|
||||
{ id: ServerTab.statistics, name: 'Statistics', icon: '📊' },
|
||||
{ id: ServerTab.configuration, name: 'Configuration', icon: '⚙️' },
|
||||
{ id: ServerTab.assistRules, name: 'Assist Rules', icon: '🚗' },
|
||||
{ id: ServerTab.event, name: 'Event Config', icon: '🏁' },
|
||||
{ id: ServerTab.eventRules, name: 'Event Rules', icon: '📋' },
|
||||
{ id: ServerTab.settings, name: 'Server Settings', icon: '🔧' }
|
||||
];
|
||||
|
||||
export function ServerConfigurationTabs({
|
||||
serverId,
|
||||
@@ -22,14 +30,6 @@ export function ServerConfigurationTabs({
|
||||
statistics
|
||||
}: ServerConfigurationTabsProps) {
|
||||
const [currentTab, setCurrentTab] = useState(ServerTab.statistics);
|
||||
const tabs = [
|
||||
{ id: ServerTab.statistics, name: 'Statistics', icon: '📊' },
|
||||
{ id: ServerTab.configuration, name: 'Configuration', icon: '⚙️' },
|
||||
{ id: ServerTab.assistRules, name: 'Assist Rules', icon: '🚗' },
|
||||
{ id: ServerTab.event, name: 'Event Config', icon: '🏁' },
|
||||
{ id: ServerTab.eventRules, name: 'Event Rules', icon: '📋' },
|
||||
{ id: ServerTab.settings, name: 'Server Settings', icon: '🔧' }
|
||||
];
|
||||
|
||||
const renderTabContent = () => {
|
||||
switch (currentTab) {
|
||||
|
||||
477
src/components/server/ServerCreationPopup.tsx
Normal file
477
src/components/server/ServerCreationPopup.tsx
Normal file
@@ -0,0 +1,477 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useRef, useState, useCallback } from 'react';
|
||||
import { useWebSocket } from '@/lib/websocket/context';
|
||||
import type {
|
||||
WebSocketMessage,
|
||||
StepData,
|
||||
SteamOutputData,
|
||||
ErrorData,
|
||||
CompleteData
|
||||
} from '@/lib/schemas/websocket';
|
||||
|
||||
interface ServerCreationPopupProps {
|
||||
serverId: string;
|
||||
serverName: string;
|
||||
isOpen: boolean;
|
||||
onClose: () => void;
|
||||
onComplete?: (success: boolean, message: string) => void;
|
||||
}
|
||||
|
||||
interface ConsoleEntry {
|
||||
id: string;
|
||||
timestamp: number;
|
||||
type: 'step' | 'steam_output' | 'error' | 'complete';
|
||||
content: string;
|
||||
level: 'info' | 'success' | 'warning' | 'error';
|
||||
}
|
||||
|
||||
interface StepStatus {
|
||||
step: string;
|
||||
status: 'pending' | 'in_progress' | 'completed' | 'failed';
|
||||
message: string;
|
||||
}
|
||||
|
||||
const STEPS = [
|
||||
{ key: 'validation', label: 'Validation' },
|
||||
{ key: 'directory_creation', label: 'Directory Creation' },
|
||||
{ key: 'steam_download', label: 'Steam Download' },
|
||||
{ key: 'config_generation', label: 'Config Generation' },
|
||||
{ key: 'service_creation', label: 'Service Creation' },
|
||||
{ key: 'firewall_rules', label: 'Firewall Rules' },
|
||||
{ key: 'database_save', label: 'Database Save' },
|
||||
{ key: 'completed', label: 'Completed' }
|
||||
];
|
||||
|
||||
export function ServerCreationPopup({
|
||||
serverId,
|
||||
serverName,
|
||||
isOpen,
|
||||
onClose,
|
||||
onComplete
|
||||
}: ServerCreationPopupProps) {
|
||||
const [entries, setEntries] = useState<ConsoleEntry[]>([]);
|
||||
const [steps, setSteps] = useState<Record<string, StepStatus>>({});
|
||||
const [isCompleted, setIsCompleted] = useState(false);
|
||||
const [completionResult, setCompletionResult] = useState<{
|
||||
success: boolean;
|
||||
message: string;
|
||||
} | null>(null);
|
||||
const [isMinimized, setIsMinimized] = useState(false);
|
||||
const [isConsoleVisible, setIsConsoleVisible] = useState(true);
|
||||
|
||||
const {
|
||||
associateWithServer,
|
||||
addMessageHandler,
|
||||
removeMessageHandler,
|
||||
connectionStatus,
|
||||
reconnect
|
||||
} = useWebSocket();
|
||||
|
||||
const consoleRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const addEntry = useCallback((entry: Omit<ConsoleEntry, 'id'>) => {
|
||||
const newEntry = {
|
||||
...entry,
|
||||
id: `${Date.now()}-${Math.random()}`
|
||||
};
|
||||
setEntries((prev) => [...prev, newEntry]);
|
||||
}, []);
|
||||
|
||||
const scrollToBottom = () => {
|
||||
if (consoleRef.current && !isMinimized && isConsoleVisible) {
|
||||
consoleRef.current.scrollTop = consoleRef.current.scrollHeight;
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
scrollToBottom();
|
||||
}, [entries, isMinimized, isConsoleVisible]);
|
||||
|
||||
useEffect(() => {
|
||||
if (serverId && isOpen) {
|
||||
associateWithServer(serverId);
|
||||
}
|
||||
}, [serverId, isOpen, associateWithServer]);
|
||||
|
||||
const handleMessage = useCallback(
|
||||
(message: WebSocketMessage) => {
|
||||
if (message.server_id !== serverId) return;
|
||||
|
||||
const timestamp = message.timestamp;
|
||||
|
||||
switch (message.type) {
|
||||
case 'step': {
|
||||
const data = message.data as StepData;
|
||||
setSteps((prev) => {
|
||||
const updatedSteps = { ...prev };
|
||||
|
||||
const stepIndex = STEPS.findIndex((step) => step.key === data.step);
|
||||
if (stepIndex > 0) {
|
||||
for (let i = 0; i < stepIndex; i++) {
|
||||
const prevStepKey = STEPS[i].key;
|
||||
if (!updatedSteps[prevStepKey] || updatedSteps[prevStepKey].status === 'pending') {
|
||||
updatedSteps[prevStepKey] = {
|
||||
step: prevStepKey,
|
||||
status: 'completed',
|
||||
message: `${prevStepKey.replace('_', ' ')} completed`
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
updatedSteps[data.step] = {
|
||||
step: data.step,
|
||||
status: data.status,
|
||||
message: data.message
|
||||
};
|
||||
|
||||
return updatedSteps;
|
||||
});
|
||||
|
||||
let level: ConsoleEntry['level'] = 'info';
|
||||
if (data.status === 'completed') level = 'success';
|
||||
else if (data.status === 'failed') level = 'error';
|
||||
else if (data.status === 'in_progress') level = 'warning';
|
||||
|
||||
addEntry({
|
||||
timestamp,
|
||||
type: 'step',
|
||||
content: `[${data.step.toUpperCase()}] ${data.message}${data.error ? ` - ${data.error}` : ''}`,
|
||||
level
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
case 'steam_output': {
|
||||
const data = message.data as SteamOutputData;
|
||||
|
||||
setSteps((prev) => {
|
||||
const updatedSteps = { ...prev };
|
||||
|
||||
if (
|
||||
!updatedSteps['steam_download'] ||
|
||||
updatedSteps['steam_download'].status === 'pending'
|
||||
) {
|
||||
updatedSteps['steam_download'] = {
|
||||
step: 'steam_download',
|
||||
status: 'in_progress',
|
||||
message: 'Steam download in progress'
|
||||
};
|
||||
}
|
||||
|
||||
if (!updatedSteps['validation'] || updatedSteps['validation'].status === 'pending') {
|
||||
updatedSteps['validation'] = {
|
||||
step: 'validation',
|
||||
status: 'completed',
|
||||
message: 'Validation completed'
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
!updatedSteps['directory_creation'] ||
|
||||
updatedSteps['directory_creation'].status === 'pending'
|
||||
) {
|
||||
updatedSteps['directory_creation'] = {
|
||||
step: 'directory_creation',
|
||||
status: 'completed',
|
||||
message: 'Directory creation completed'
|
||||
};
|
||||
}
|
||||
|
||||
return updatedSteps;
|
||||
});
|
||||
|
||||
addEntry({
|
||||
timestamp,
|
||||
type: 'steam_output',
|
||||
content: data.output,
|
||||
level: data.is_error ? 'error' : 'info'
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
case 'error': {
|
||||
const data = message.data as ErrorData;
|
||||
|
||||
setSteps((prev) => {
|
||||
const updatedSteps = { ...prev };
|
||||
const currentStep = Object.values(updatedSteps).find(
|
||||
(step) => step.status === 'in_progress'
|
||||
);
|
||||
if (currentStep) {
|
||||
updatedSteps[currentStep.step] = {
|
||||
...currentStep,
|
||||
status: 'failed',
|
||||
message: `${currentStep.step.replace('_', ' ')} failed: ${data.error}`
|
||||
};
|
||||
}
|
||||
return updatedSteps;
|
||||
});
|
||||
|
||||
addEntry({
|
||||
timestamp,
|
||||
type: 'error',
|
||||
content: `ERROR: ${data.error}${data.details ? ` - ${data.details}` : ''}`,
|
||||
level: 'error'
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
case 'complete': {
|
||||
const data = message.data as CompleteData;
|
||||
setIsCompleted(true);
|
||||
setCompletionResult({ success: data.success, message: data.message });
|
||||
|
||||
addEntry({
|
||||
timestamp,
|
||||
type: 'complete',
|
||||
content: `COMPLETED: ${data.message}`,
|
||||
level: data.success ? 'success' : 'error'
|
||||
});
|
||||
|
||||
onComplete?.(data.success, data.message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
[serverId, addEntry, onComplete]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (isOpen) {
|
||||
addMessageHandler(handleMessage);
|
||||
return () => {
|
||||
removeMessageHandler(handleMessage);
|
||||
};
|
||||
}
|
||||
}, [addMessageHandler, removeMessageHandler, handleMessage, isOpen]);
|
||||
|
||||
const handleReconnect = async () => {
|
||||
try {
|
||||
await reconnect();
|
||||
} catch (error) {
|
||||
console.error('Failed to reconnect:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const getStepStatusIcon = (status: StepStatus['status']) => {
|
||||
switch (status) {
|
||||
case 'pending':
|
||||
return '⏳';
|
||||
case 'in_progress':
|
||||
return '🔄';
|
||||
case 'completed':
|
||||
return '✅';
|
||||
case 'failed':
|
||||
return '❌';
|
||||
}
|
||||
};
|
||||
|
||||
const getEntryClassName = (level: ConsoleEntry['level']) => {
|
||||
switch (level) {
|
||||
case 'success':
|
||||
return 'text-green-400';
|
||||
case 'warning':
|
||||
return 'text-yellow-400';
|
||||
case 'error':
|
||||
return 'text-red-400';
|
||||
default:
|
||||
return 'text-gray-300';
|
||||
}
|
||||
};
|
||||
|
||||
const getConnectionStatusIcon = () => {
|
||||
switch (connectionStatus) {
|
||||
case 'connected':
|
||||
return '🟢';
|
||||
case 'connecting':
|
||||
return '🟡';
|
||||
case 'disconnected':
|
||||
return '⚫';
|
||||
case 'error':
|
||||
return '🔴';
|
||||
}
|
||||
};
|
||||
|
||||
const getCurrentProgress = () => {
|
||||
const completedSteps = Object.values(steps).filter(
|
||||
(step) => step.status === 'completed'
|
||||
).length;
|
||||
const totalSteps = STEPS.length;
|
||||
return { completed: completedSteps, total: totalSteps };
|
||||
};
|
||||
|
||||
if (!isOpen) return null;
|
||||
|
||||
if (isMinimized) {
|
||||
const progress = getCurrentProgress();
|
||||
const isProgressing =
|
||||
!isCompleted && Object.values(steps).some((step) => step.status === 'in_progress');
|
||||
|
||||
return (
|
||||
<div className="fixed right-4 bottom-4 z-40">
|
||||
<button
|
||||
onClick={() => setIsMinimized(false)}
|
||||
className={`flex h-16 w-16 items-center justify-center rounded-full border-2 shadow-lg transition-all hover:scale-105 ${
|
||||
isCompleted
|
||||
? completionResult?.success
|
||||
? 'border-green-400 bg-green-600 hover:bg-green-700'
|
||||
: 'border-red-400 bg-red-600 hover:bg-red-700'
|
||||
: 'border-blue-400 bg-blue-600 hover:bg-blue-700'
|
||||
}`}
|
||||
title={`Server Creation: ${serverName} - ${progress.completed}/${progress.total} steps`}
|
||||
>
|
||||
<div className="text-center text-white">
|
||||
{isCompleted ? (
|
||||
<span className="text-2xl">{completionResult?.success ? '✅' : '❌'}</span>
|
||||
) : (
|
||||
<>
|
||||
<div className="text-xs font-bold">
|
||||
{progress.completed}/{progress.total}
|
||||
</div>
|
||||
{isProgressing && <div className="animate-pulse text-xs">⚡</div>}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{!isCompleted && (
|
||||
<svg className="absolute inset-0 h-16 w-16 -rotate-90 transform">
|
||||
<circle
|
||||
cx="32"
|
||||
cy="32"
|
||||
r="28"
|
||||
stroke="currentColor"
|
||||
strokeWidth="4"
|
||||
fill="none"
|
||||
className="text-gray-300 opacity-20"
|
||||
/>
|
||||
<circle
|
||||
cx="32"
|
||||
cy="32"
|
||||
r="28"
|
||||
stroke="currentColor"
|
||||
strokeWidth="4"
|
||||
fill="none"
|
||||
strokeDasharray={`${(progress.completed / progress.total) * 175.929} 175.929`}
|
||||
className="text-white transition-all duration-500"
|
||||
/>
|
||||
</svg>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="fixed right-4 bottom-4 z-40 max-h-[600px] w-96 rounded-lg border border-gray-700 bg-gray-800 shadow-2xl select-none">
|
||||
<div className="flex items-center justify-between border-b border-gray-700 p-4">
|
||||
<div className="flex items-center space-x-2">
|
||||
<span className="text-lg">🔧</span>
|
||||
<h3 className="truncate font-medium text-white">{serverName}</h3>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center space-x-2">
|
||||
<div className="flex items-center space-x-1">
|
||||
<span className="text-sm">{getConnectionStatusIcon()}</span>
|
||||
{(connectionStatus === 'disconnected' || connectionStatus === 'error') && (
|
||||
<button
|
||||
onClick={handleReconnect}
|
||||
className="rounded bg-blue-600 px-1 py-0.5 text-xs hover:bg-blue-700"
|
||||
title="Reconnect"
|
||||
>
|
||||
🔄
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<button
|
||||
onClick={() => setIsConsoleVisible(!isConsoleVisible)}
|
||||
className="text-sm text-gray-400 hover:text-white"
|
||||
title={isConsoleVisible ? 'Hide Console' : 'Show Console'}
|
||||
>
|
||||
📋
|
||||
</button>
|
||||
|
||||
<button
|
||||
onClick={() => setIsMinimized(true)}
|
||||
className="text-gray-400 hover:text-white"
|
||||
title="Minimize"
|
||||
>
|
||||
<svg className="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M20 12H4" />
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
<button onClick={onClose} className="text-gray-400 hover:text-white" title="Close">
|
||||
<svg className="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M6 18L18 6M6 6l12 12"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="border-b border-gray-700 p-4">
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
{STEPS.map(({ key, label }) => {
|
||||
const stepStatus = steps[key];
|
||||
return (
|
||||
<div
|
||||
key={key}
|
||||
className={`flex items-center space-x-2 rounded p-2 text-xs ${
|
||||
stepStatus?.status === 'completed'
|
||||
? 'bg-green-900/50 text-green-100'
|
||||
: stepStatus?.status === 'in_progress'
|
||||
? 'bg-yellow-900/50 text-yellow-100'
|
||||
: stepStatus?.status === 'failed'
|
||||
? 'bg-red-900/50 text-red-100'
|
||||
: 'bg-gray-700 text-gray-300'
|
||||
}`}
|
||||
>
|
||||
<span>{getStepStatusIcon(stepStatus?.status || 'pending')}</span>
|
||||
<span className="truncate">{label}</span>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{isConsoleVisible && (
|
||||
<div className="h-64 bg-black">
|
||||
<div ref={consoleRef} className="h-full space-y-1 overflow-y-auto p-3 font-mono text-xs">
|
||||
{entries.map((entry) => (
|
||||
<div key={entry.id} className={`${getEntryClassName(entry.level)} leading-tight`}>
|
||||
<span className="text-xs text-gray-500">
|
||||
{new Date(entry.timestamp * 1000).toLocaleTimeString()}
|
||||
</span>{' '}
|
||||
<span>{entry.content}</span>
|
||||
</div>
|
||||
))}
|
||||
{entries.length === 0 && (
|
||||
<div className="py-8 text-center text-gray-500">
|
||||
Waiting for server creation to begin...
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isCompleted && (
|
||||
<div
|
||||
className={`p-3 text-center text-sm ${
|
||||
completionResult?.success
|
||||
? 'bg-green-900/50 text-green-100'
|
||||
: 'bg-red-900/50 text-red-100'
|
||||
}`}
|
||||
>
|
||||
{completionResult?.message}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
36
src/components/server/ServerCreationPopupContainer.tsx
Normal file
36
src/components/server/ServerCreationPopupContainer.tsx
Normal file
@@ -0,0 +1,36 @@
|
||||
'use client';
|
||||
|
||||
import { useServerCreationPopup } from '@/lib/context/ServerCreationPopupContext';
|
||||
import { ServerCreationPopup } from './ServerCreationPopup';
|
||||
import { useSteamCMD } from '@/lib/context/SteamCMDContext';
|
||||
import { useCallback } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
export function ServerCreationPopupContainer() {
|
||||
const { popup, hidePopup } = useServerCreationPopup();
|
||||
const { dissociateServer } = useSteamCMD();
|
||||
const router = useRouter();
|
||||
const handleClose = useCallback(() => {
|
||||
hidePopup();
|
||||
if (popup) return dissociateServer(popup.serverId);
|
||||
}, [popup, dissociateServer, hidePopup]);
|
||||
if (!popup) return null;
|
||||
|
||||
const handleComplete = (success: boolean) => {
|
||||
if (success) {
|
||||
setTimeout(() => {
|
||||
router.refresh();
|
||||
}, 2000);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<ServerCreationPopup
|
||||
serverId={popup.serverId}
|
||||
serverName={popup.serverName}
|
||||
isOpen={popup.isOpen}
|
||||
onClose={handleClose}
|
||||
onComplete={handleComplete}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -1,12 +1,48 @@
|
||||
'use client';
|
||||
import Link from 'next/link';
|
||||
import { Server, getStatusColor, serviceStatusToString, ServiceStatus } from '@/lib/types/server';
|
||||
import { startServerAction, stopServerAction, restartServerAction } from '@/lib/actions/servers';
|
||||
import { Server, getStatusColor, serviceStatusToString, ServiceStatus } from '@/lib/schemas/server';
|
||||
import {
|
||||
startServerEventAction,
|
||||
restartServerEventAction,
|
||||
stopServerEventAction
|
||||
} from '@/lib/actions/servers';
|
||||
import { hasPermission, User } from '@/lib/schemas';
|
||||
import { useState, useTransition } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { DeleteServerModal } from './DeleteServerModal';
|
||||
|
||||
interface ServerHeaderProps {
|
||||
server: Server;
|
||||
user: User;
|
||||
}
|
||||
|
||||
export function ServerHeader({ server }: ServerHeaderProps) {
|
||||
export function ServerHeader({ server, user }: ServerHeaderProps) {
|
||||
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const router = useRouter();
|
||||
const canDeleteServer = hasPermission(user, 'server.delete');
|
||||
const startServer = () =>
|
||||
startTransition(async () => {
|
||||
await startServerEventAction(server.id);
|
||||
router.refresh();
|
||||
});
|
||||
const restartServer = () =>
|
||||
startTransition(async () => {
|
||||
await restartServerEventAction(server.id);
|
||||
router.refresh();
|
||||
});
|
||||
const stopServer = () =>
|
||||
startTransition(async () => {
|
||||
await stopServerEventAction(server.id);
|
||||
router.refresh();
|
||||
});
|
||||
|
||||
const disabled = [
|
||||
ServiceStatus.Restarting,
|
||||
ServiceStatus.Starting,
|
||||
ServiceStatus.Stopping,
|
||||
ServiceStatus.Unknown
|
||||
].includes(server.status);
|
||||
return (
|
||||
<div className="rounded-lg bg-gray-800 p-6">
|
||||
<div className="flex items-center justify-between">
|
||||
@@ -49,35 +85,45 @@ export function ServerHeader({ server }: ServerHeaderProps) {
|
||||
</div>
|
||||
|
||||
<div className="flex space-x-3">
|
||||
<form action={startServerAction.bind(null, server.id)}>
|
||||
{canDeleteServer && (
|
||||
<button
|
||||
type="submit"
|
||||
disabled={server.status === ServiceStatus.Running}
|
||||
type="button"
|
||||
onClick={(e) => {
|
||||
e.preventDefault();
|
||||
setIsDeleteModalOpen(true);
|
||||
}}
|
||||
disabled={disabled || isPending}
|
||||
className="mr-3 rounded bg-red-800 px-4 py-2 font-medium text-white transition-colors hover:bg-red-900 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
Remove Server
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
onClick={startServer}
|
||||
disabled={server.status === ServiceStatus.Running || disabled || isPending}
|
||||
className="rounded bg-green-600 px-4 py-2 font-medium text-white transition-colors hover:bg-green-700 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
Start
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<form action={restartServerAction.bind(null, server.id)}>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={server.status === ServiceStatus.Stopped}
|
||||
type="button"
|
||||
onClick={restartServer}
|
||||
disabled={server.status === ServiceStatus.Stopped || disabled || isPending}
|
||||
className="rounded bg-yellow-600 px-4 py-2 font-medium text-white transition-colors hover:bg-yellow-700 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
Restart
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<form action={stopServerAction.bind(null, server.id)}>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={server.status === ServiceStatus.Stopped}
|
||||
type="button"
|
||||
onClick={stopServer}
|
||||
disabled={server.status === ServiceStatus.Stopped || disabled || isPending}
|
||||
className="rounded bg-red-600 px-4 py-2 font-medium text-white transition-colors hover:bg-red-700 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
Stop
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -104,6 +150,12 @@ export function ServerHeader({ server }: ServerHeaderProps) {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DeleteServerModal
|
||||
isOpen={isDeleteModalOpen}
|
||||
onClose={() => setIsDeleteModalOpen(false)}
|
||||
server={server}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
51
src/components/server/ServerListWithActions.tsx
Normal file
51
src/components/server/ServerListWithActions.tsx
Normal file
@@ -0,0 +1,51 @@
|
||||
'use client';
|
||||
|
||||
import { useCallback, useState } from 'react';
|
||||
import { Server } from '@/lib/schemas/server';
|
||||
import { User, hasPermission } from '@/lib/schemas/user';
|
||||
import { ServerCard } from './ServerCard';
|
||||
import { CreateServerModal } from './CreateServerModal';
|
||||
import RefreshButton from '@/components/ui/RefreshButton';
|
||||
import { useSteamCMD } from '@/lib/context/SteamCMDContext';
|
||||
|
||||
interface ServerListWithActionsProps {
|
||||
servers: Server[];
|
||||
user: User;
|
||||
}
|
||||
|
||||
export function ServerListWithActions({ servers, user }: ServerListWithActionsProps) {
|
||||
const [isCreateModalOpen, setIsCreateModalOpen] = useState(false);
|
||||
const { isSteamCMDRunning } = useSteamCMD();
|
||||
|
||||
const handleOnClose = useCallback(() => setIsCreateModalOpen(false), []);
|
||||
const canCreateServer = hasPermission(user, 'server.create');
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="mb-6 flex items-center justify-between">
|
||||
<h2 className="text-xl font-semibold">Your Servers</h2>
|
||||
<div className="flex items-center space-x-4">
|
||||
{canCreateServer && (
|
||||
<button
|
||||
onClick={() => setIsCreateModalOpen(true)}
|
||||
disabled={isSteamCMDRunning}
|
||||
className="rounded-md bg-green-600 px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-green-700 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
title={isSteamCMDRunning ? 'Server creation disabled while SteamCMD is running' : ''}
|
||||
>
|
||||
Create Server
|
||||
</button>
|
||||
)}
|
||||
<RefreshButton />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 gap-6 md:grid-cols-2 lg:grid-cols-3">
|
||||
{servers.map((server) => (
|
||||
<ServerCard key={server.id} server={server} />
|
||||
))}
|
||||
</div>
|
||||
|
||||
<CreateServerModal isOpen={isCreateModalOpen} onClose={handleOnClose} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -14,16 +14,16 @@ interface SessionTypesChartProps {
|
||||
data: SessionCount[];
|
||||
}
|
||||
|
||||
export function SessionTypesChart({ data }: SessionTypesChartProps) {
|
||||
const colors = [
|
||||
const colors = [
|
||||
'#3b82f6', // blue
|
||||
'#10b981', // emerald
|
||||
'#f59e0b', // amber
|
||||
'#ef4444', // red
|
||||
'#8b5cf6', // violet
|
||||
'#06b6d4' // cyan
|
||||
];
|
||||
];
|
||||
|
||||
export function SessionTypesChart({ data }: SessionTypesChartProps) {
|
||||
const chartData = {
|
||||
labels: data.map((item) => item.name),
|
||||
datasets: [
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import type { StateHistoryStats } from '@/lib/types/statistics';
|
||||
import type { StateHistoryStats } from '@/lib/schemas/statistics';
|
||||
import { PlayerCountChart } from './PlayerCountChart';
|
||||
import { SessionTypesChart } from './SessionTypesChart';
|
||||
import { DailyActivityChart } from './DailyActivityChart';
|
||||
@@ -40,13 +40,13 @@ export function StatisticsDashboard({ stats }: StatisticsDashboardProps) {
|
||||
</div>
|
||||
|
||||
{/* Charts */}
|
||||
<div className="grid grid-cols-1 gap-6 lg:grid-cols-2">
|
||||
<div className="rounded-lg bg-gray-800 p-6">
|
||||
<div className="grid grid-cols-12 gap-4">
|
||||
<div className="col-span-9 rounded-lg bg-gray-800 p-6">
|
||||
<h3 className="mb-4 text-lg font-medium text-white">Player Count Over Time</h3>
|
||||
<PlayerCountChart data={stats.playerCountOverTime ?? []} />
|
||||
</div>
|
||||
|
||||
<div className="rounded-lg bg-gray-800 p-6">
|
||||
<div className="col-span-3 rounded-lg bg-gray-800 p-6">
|
||||
<h3 className="mb-4 text-lg font-medium text-white">Session Types</h3>
|
||||
<SessionTypesChart data={stats.sessionTypes ?? []} />
|
||||
</div>
|
||||
|
||||
@@ -8,27 +8,27 @@ interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
||||
isLoading?: boolean;
|
||||
}
|
||||
|
||||
const baseClasses =
|
||||
'inline-flex items-center justify-center font-medium rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed';
|
||||
|
||||
const variants = {
|
||||
primary: 'bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-500',
|
||||
secondary: 'bg-gray-600 text-white hover:bg-gray-700 focus:ring-gray-500',
|
||||
danger: 'bg-red-600 text-white hover:bg-red-700 focus:ring-red-500',
|
||||
ghost: 'text-gray-300 hover:text-white hover:bg-gray-700 focus:ring-gray-500'
|
||||
};
|
||||
|
||||
const sizes = {
|
||||
sm: 'px-3 py-1 text-sm',
|
||||
md: 'px-4 py-2 text-sm',
|
||||
lg: 'px-6 py-3 text-base'
|
||||
};
|
||||
|
||||
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
|
||||
(
|
||||
{ className, variant = 'primary', size = 'md', isLoading, children, disabled, ...props },
|
||||
ref
|
||||
) => {
|
||||
const baseClasses =
|
||||
'inline-flex items-center justify-center font-medium rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed';
|
||||
|
||||
const variants = {
|
||||
primary: 'bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-500',
|
||||
secondary: 'bg-gray-600 text-white hover:bg-gray-700 focus:ring-gray-500',
|
||||
danger: 'bg-red-600 text-white hover:bg-red-700 focus:ring-red-500',
|
||||
ghost: 'text-gray-300 hover:text-white hover:bg-gray-700 focus:ring-gray-500'
|
||||
};
|
||||
|
||||
const sizes = {
|
||||
sm: 'px-3 py-1 text-sm',
|
||||
md: 'px-4 py-2 text-sm',
|
||||
lg: 'px-6 py-3 text-base'
|
||||
};
|
||||
|
||||
return (
|
||||
<button
|
||||
className={cn(baseClasses, variants[variant], sizes[size], className)}
|
||||
|
||||
29
src/components/ui/LogoutButton.tsx
Normal file
29
src/components/ui/LogoutButton.tsx
Normal file
@@ -0,0 +1,29 @@
|
||||
'use client';
|
||||
|
||||
import { logoutAction } from '@/lib/actions/auth';
|
||||
import { useActionState } from 'react';
|
||||
|
||||
export default function LogoutButton() {
|
||||
const [, formAction] = useActionState(logoutAction, null);
|
||||
return (
|
||||
<form action={formAction}>
|
||||
<button type="submit" className="flex items-center text-gray-300 hover:text-white">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
className="h-6 w-6"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
stroke="currentColor"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth="2"
|
||||
d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"
|
||||
/>
|
||||
</svg>
|
||||
<span className="ml-1 hidden sm:inline">Logout</span>
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
'use client';
|
||||
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
export default function RefreshButton() {
|
||||
const router = useRouter();
|
||||
return (
|
||||
<button
|
||||
onClick={() => window.location.reload()}
|
||||
onClick={() => router.refresh()}
|
||||
className="rounded-md bg-gray-700 px-3 py-1 text-sm hover:bg-gray-600"
|
||||
>
|
||||
Refresh
|
||||
|
||||
46
src/components/ui/SteamCMDNotification.tsx
Normal file
46
src/components/ui/SteamCMDNotification.tsx
Normal file
@@ -0,0 +1,46 @@
|
||||
'use client';
|
||||
|
||||
import { useSteamCMD } from '@/lib/context/SteamCMDContext';
|
||||
|
||||
export function SteamCMDNotification() {
|
||||
const { isSteamCMDRunning, runningSteamServers } = useSteamCMD();
|
||||
|
||||
if (!isSteamCMDRunning) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const serverCount = runningSteamServers.size;
|
||||
|
||||
return (
|
||||
<div className="bg-yellow-600 border-l-4 border-yellow-400 p-4">
|
||||
<div className="flex items-center">
|
||||
<div className="flex-shrink-0">
|
||||
<svg
|
||||
className="h-5 w-5 text-yellow-50"
|
||||
viewBox="0 0 20 20"
|
||||
fill="currentColor"
|
||||
>
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
</div>
|
||||
<div className="ml-3">
|
||||
<p className="text-sm text-yellow-50">
|
||||
<strong>SteamCMD is currently running</strong> for {serverCount} server{serverCount !== 1 ? 's' : ''}.
|
||||
Server actions are temporarily disabled to prevent conflicts.
|
||||
This will automatically resolve when the download completes.
|
||||
</p>
|
||||
</div>
|
||||
<div className="ml-auto flex-shrink-0">
|
||||
<div className="flex items-center">
|
||||
<div className="animate-spin h-4 w-4 border-2 border-yellow-50 border-t-transparent rounded-full mr-2"></div>
|
||||
<span className="text-xs text-yellow-50">Downloading...</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
25
src/components/websocket/WebSocketInitializer.tsx
Normal file
25
src/components/websocket/WebSocketInitializer.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useWebSocket } from '@/lib/websocket/context';
|
||||
|
||||
interface WebSocketInitializerProps {
|
||||
openToken?: string;
|
||||
}
|
||||
|
||||
export function WebSocketInitializer({ openToken }: WebSocketInitializerProps) {
|
||||
const { connect, isConnected } = useWebSocket();
|
||||
const hasInitialized = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (openToken && !isConnected && !hasInitialized.current) {
|
||||
hasInitialized.current = true;
|
||||
connect(openToken).catch((error) => {
|
||||
console.error('Failed to connect WebSocket:', error);
|
||||
hasInitialized.current = false;
|
||||
});
|
||||
}
|
||||
}, [openToken, connect, isConnected]);
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
'use server';
|
||||
|
||||
import { redirect } from 'next/navigation';
|
||||
import { loginUser } from '@/lib/api/server/auth';
|
||||
import { loginUser, getOpenToken } from '@/lib/api/server/auth';
|
||||
import { login, logout } from '@/lib/auth/server';
|
||||
import { loginSchema, loginResponseSchema } from '../schemas';
|
||||
|
||||
export type LoginResult = {
|
||||
success: boolean;
|
||||
@@ -14,17 +15,19 @@ export async function loginAction(prevState: LoginResult, formData: FormData) {
|
||||
const username = formData.get('username') as string;
|
||||
const password = formData.get('password') as string;
|
||||
|
||||
if (!username || !password) {
|
||||
const loginData = loginSchema.safeParse({ username, password });
|
||||
if (!loginData.success) {
|
||||
return {
|
||||
success: false,
|
||||
message: 'Username and password are required'
|
||||
message: loginData.error.message
|
||||
};
|
||||
}
|
||||
|
||||
const result = await loginUser(username, password);
|
||||
const result = loginResponseSchema.safeParse(await loginUser(loginData.data));
|
||||
|
||||
if (result.token && result.user) {
|
||||
await login(result.token, result.user);
|
||||
if (result.success) {
|
||||
const openToken = await getOpenToken(result.data.token);
|
||||
await login(result.data.token, result.data.user, openToken);
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
@@ -43,5 +46,8 @@ export async function loginAction(prevState: LoginResult, formData: FormData) {
|
||||
|
||||
export async function logoutAction() {
|
||||
await logout();
|
||||
redirect('/login');
|
||||
}
|
||||
|
||||
export async function clearExpiredSessionAction() {
|
||||
await logout();
|
||||
}
|
||||
|
||||
@@ -3,14 +3,20 @@
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { requireAuth } from '@/lib/auth/server';
|
||||
import { updateServerConfiguration } from '@/lib/api/server/configuration';
|
||||
import { ConfigFile } from '@/lib/types/config';
|
||||
import {
|
||||
assistRulesSchema,
|
||||
ConfigFile,
|
||||
eventConfigSchema,
|
||||
eventRulesSchema,
|
||||
serverSettingsSchema
|
||||
} from '@/lib/schemas/config';
|
||||
import type {
|
||||
Configuration,
|
||||
AssistRules,
|
||||
EventConfig,
|
||||
EventRules,
|
||||
ServerSettings
|
||||
} from '@/lib/types/config';
|
||||
} from '@/lib/schemas/config';
|
||||
|
||||
export async function updateConfigurationAction(serverId: string, formData: FormData) {
|
||||
try {
|
||||
@@ -49,7 +55,7 @@ export async function updateAssistRulesAction(serverId: string, formData: FormDa
|
||||
const session = await requireAuth();
|
||||
const restart = formData.get('restart') === 'on';
|
||||
|
||||
const config: AssistRules = {
|
||||
const rawConfig: AssistRules = {
|
||||
stabilityControlLevelMax: parseInt(formData.get('stabilityControlLevelMax') as string),
|
||||
disableAutosteer: parseInt(formData.get('disableAutosteer') as string),
|
||||
disableAutoLights: parseInt(formData.get('disableAutoLights') as string),
|
||||
@@ -61,11 +67,16 @@ export async function updateAssistRulesAction(serverId: string, formData: FormDa
|
||||
disableIdealLine: parseInt(formData.get('disableIdealLine') as string)
|
||||
};
|
||||
|
||||
const config = assistRulesSchema.safeParse(rawConfig);
|
||||
if (!config.success) {
|
||||
return { success: false, message: config.error.message };
|
||||
}
|
||||
|
||||
await updateServerConfiguration(
|
||||
session.token!,
|
||||
serverId,
|
||||
ConfigFile.assistRules,
|
||||
config,
|
||||
config.data,
|
||||
restart
|
||||
);
|
||||
revalidatePath(`/dashboard/server/${serverId}`);
|
||||
@@ -84,7 +95,7 @@ export async function updateServerSettingsAction(serverId: string, formData: For
|
||||
const session = await requireAuth();
|
||||
const restart = formData.get('restart') === 'on';
|
||||
|
||||
const config: ServerSettings = {
|
||||
const rawConfig: ServerSettings = {
|
||||
serverName: formData.get('serverName') as string,
|
||||
adminPassword: formData.get('adminPassword') as string,
|
||||
carGroup: formData.get('carGroup') as string,
|
||||
@@ -103,8 +114,18 @@ export async function updateServerSettingsAction(serverId: string, formData: For
|
||||
formationLapType: parseInt(formData.get('formationLapType') as string),
|
||||
ignorePrematureDisconnects: parseInt(formData.get('ignorePrematureDisconnects') as string)
|
||||
};
|
||||
const config = serverSettingsSchema.safeParse(rawConfig);
|
||||
if (!config.success) {
|
||||
return { success: false, message: config.error.message };
|
||||
}
|
||||
|
||||
await updateServerConfiguration(session.token!, serverId, ConfigFile.settings, config, restart);
|
||||
await updateServerConfiguration(
|
||||
session.token!,
|
||||
serverId,
|
||||
ConfigFile.settings,
|
||||
config.data,
|
||||
restart
|
||||
);
|
||||
revalidatePath(`/dashboard/server/${serverId}`);
|
||||
|
||||
return { success: true, message: 'Server settings updated successfully' };
|
||||
@@ -124,7 +145,7 @@ export async function updateEventConfigAction(serverId: string, formData: FormDa
|
||||
const sessionsData = formData.get('sessions') as string;
|
||||
const sessions = sessionsData ? JSON.parse(sessionsData) : [];
|
||||
|
||||
const config: EventConfig = {
|
||||
const rawConfig: EventConfig = {
|
||||
track: formData.get('track') as string,
|
||||
preRaceWaitingTimeSeconds: parseInt(formData.get('preRaceWaitingTimeSeconds') as string),
|
||||
sessionOverTimeSeconds: parseInt(formData.get('sessionOverTimeSeconds') as string),
|
||||
@@ -140,8 +161,18 @@ export async function updateEventConfigAction(serverId: string, formData: FormDa
|
||||
),
|
||||
sessions
|
||||
};
|
||||
const config = eventConfigSchema.safeParse(rawConfig);
|
||||
if (!config.success) {
|
||||
return { success: false, message: config.error.message };
|
||||
}
|
||||
|
||||
await updateServerConfiguration(session.token!, serverId, ConfigFile.event, config, restart);
|
||||
await updateServerConfiguration(
|
||||
session.token!,
|
||||
serverId,
|
||||
ConfigFile.event,
|
||||
config.data,
|
||||
restart
|
||||
);
|
||||
revalidatePath(`/dashboard/server/${serverId}`);
|
||||
|
||||
return {
|
||||
@@ -161,7 +192,7 @@ export async function updateEventRulesAction(serverId: string, formData: FormDat
|
||||
const session = await requireAuth();
|
||||
const restart = formData.get('restart') === 'on';
|
||||
|
||||
const config: EventRules = {
|
||||
const rawConfig: EventRules = {
|
||||
qualifyStandingType: parseInt(formData.get('qualifyStandingType') as string),
|
||||
pitWindowLengthSec: parseInt(formData.get('pitWindowLengthSec') as string),
|
||||
driverStintTimeSec: parseInt(formData.get('driverStintTimeSec') as string),
|
||||
@@ -178,11 +209,16 @@ export async function updateEventRulesAction(serverId: string, formData: FormDat
|
||||
tyreSetCount: parseInt(formData.get('tyreSetCount') as string)
|
||||
};
|
||||
|
||||
const config = eventRulesSchema.safeParse(rawConfig);
|
||||
if (!config.success) {
|
||||
return { success: false, message: config.error.message };
|
||||
}
|
||||
|
||||
await updateServerConfiguration(
|
||||
session.token!,
|
||||
serverId,
|
||||
ConfigFile.eventRules,
|
||||
config,
|
||||
config.data,
|
||||
restart
|
||||
);
|
||||
revalidatePath(`/dashboard/server/${serverId}`);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { requireAuth } from '@/lib/auth/server';
|
||||
import { createUser, deleteUser } from '@/lib/api/server/membership';
|
||||
import { userCreateSchema } from '../schemas';
|
||||
|
||||
export async function createUserAction(formData: FormData) {
|
||||
try {
|
||||
@@ -11,7 +12,13 @@ export async function createUserAction(formData: FormData) {
|
||||
const password = formData.get('password') as string;
|
||||
const role = formData.get('role') as string;
|
||||
|
||||
await createUser(session.token!, { username, password, role });
|
||||
const rawData = { username, password, role };
|
||||
const data = userCreateSchema.safeParse(rawData);
|
||||
if (!data.success) {
|
||||
return { success: false, message: data.error.message };
|
||||
}
|
||||
|
||||
await createUser(session.token!, data.data);
|
||||
revalidatePath('/dashboard/membership');
|
||||
|
||||
return { success: true, message: 'User created successfully' };
|
||||
|
||||
64
src/lib/actions/server-management.ts
Normal file
64
src/lib/actions/server-management.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
'use server';
|
||||
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { requireAuth } from '@/lib/auth/server';
|
||||
import { createServer, deleteServer } from '@/lib/api/server/servers';
|
||||
|
||||
export type ServerActionData = {
|
||||
id: string;
|
||||
};
|
||||
|
||||
export type ServerActionResult = {
|
||||
success: boolean;
|
||||
message: string;
|
||||
data?: ServerActionData;
|
||||
};
|
||||
|
||||
export async function createServerAction(
|
||||
prevState: ServerActionResult,
|
||||
formData: FormData
|
||||
): Promise<ServerActionResult> {
|
||||
try {
|
||||
const session = await requireAuth();
|
||||
const name = formData.get('name') as string;
|
||||
|
||||
if (!name?.trim()) {
|
||||
return {
|
||||
success: false,
|
||||
message: 'Server name is required'
|
||||
};
|
||||
}
|
||||
|
||||
const server = await createServer(session.token!, name.trim());
|
||||
revalidatePath('/dashboard');
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'Server creation started',
|
||||
data: server
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : 'Failed to create server'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteServerAction(serverId: string): Promise<ServerActionResult> {
|
||||
try {
|
||||
const session = await requireAuth();
|
||||
await deleteServer(session.token!, serverId);
|
||||
revalidatePath('/dashboard');
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'Server deleted successfully'
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : 'Failed to delete server'
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
'use server';
|
||||
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { revalidatePath, revalidateTag } from 'next/cache';
|
||||
import { requireAuth } from '@/lib/auth/server';
|
||||
import { startService, stopService, restartService } from '@/lib/api/server/servers';
|
||||
|
||||
@@ -10,6 +10,7 @@ export async function startServerAction(serverId: string) {
|
||||
await startService(session.token!, serverId);
|
||||
revalidatePath('/dashboard');
|
||||
revalidatePath(`/dashboard/server/${serverId}`);
|
||||
revalidateTag('/server');
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
@@ -18,12 +19,17 @@ export async function startServerAction(serverId: string) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function startServerEventAction(serverId: string) {
|
||||
await startServerAction(serverId);
|
||||
}
|
||||
|
||||
export async function stopServerAction(serverId: string) {
|
||||
try {
|
||||
const session = await requireAuth();
|
||||
await stopService(session.token!, serverId);
|
||||
revalidatePath('/dashboard');
|
||||
revalidatePath(`/dashboard/server/${serverId}`);
|
||||
revalidateTag('/server');
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
@@ -32,12 +38,17 @@ export async function stopServerAction(serverId: string) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function stopServerEventAction(serverId: string) {
|
||||
await stopServerAction(serverId);
|
||||
}
|
||||
|
||||
export async function restartServerAction(serverId: string) {
|
||||
try {
|
||||
const session = await requireAuth();
|
||||
await restartService(session.token!, serverId);
|
||||
revalidatePath('/dashboard');
|
||||
revalidatePath(`/dashboard/server/${serverId}`);
|
||||
revalidateTag('/server');
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
@@ -45,3 +56,7 @@ export async function restartServerAction(serverId: string) {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function restartServerEventAction(serverId: string) {
|
||||
await restartServerAction(serverId);
|
||||
}
|
||||
|
||||
63
src/lib/api/client/base.ts
Normal file
63
src/lib/api/client/base.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
'use client';
|
||||
|
||||
import { SessionData } from '@/lib/session/config';
|
||||
|
||||
const BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:8080';
|
||||
|
||||
export type ClientApiResponse<T> = {
|
||||
data?: T;
|
||||
error?: string;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
const getSession = async (): Promise<SessionData | null> => {
|
||||
const response = await fetch('/api/session');
|
||||
if (response.ok) {
|
||||
return await response.json();
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
export async function fetchClientAPI<T>(
|
||||
endpoint: string,
|
||||
method: string = 'GET',
|
||||
body?: object,
|
||||
customToken?: string
|
||||
): Promise<ClientApiResponse<T>> {
|
||||
let token = customToken;
|
||||
let session: SessionData | null = null;
|
||||
|
||||
if (!token) {
|
||||
session = await getSession();
|
||||
token = session?.openToken;
|
||||
|
||||
if (!token) {
|
||||
throw new Error('No authentication token available');
|
||||
}
|
||||
}
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`
|
||||
};
|
||||
|
||||
const response = await fetch(`${BASE_URL}${endpoint}`, {
|
||||
method,
|
||||
headers,
|
||||
body: body ? JSON.stringify(body) : undefined
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
window.location.href = '/login?expired=true';
|
||||
return { error: 'unauthorized' };
|
||||
}
|
||||
throw new Error(`API Error: ${response.statusText} - ${method} - ${BASE_URL}${endpoint}`);
|
||||
}
|
||||
|
||||
if (response.headers.get('Content-Type')?.includes('application/json')) {
|
||||
return { data: await response.json() };
|
||||
}
|
||||
|
||||
return { message: await response.text() };
|
||||
}
|
||||
@@ -1,17 +1,26 @@
|
||||
import { User } from '@/lib/types';
|
||||
import {
|
||||
Login,
|
||||
LoginResponse,
|
||||
loginResponseSchema,
|
||||
loginSchema,
|
||||
loginTokenSchema,
|
||||
User,
|
||||
userSchema
|
||||
} from '@/lib/schemas';
|
||||
import { fetchServerAPI } from './base';
|
||||
|
||||
const BASE_URL = process.env.API_BASE_URL || 'http://localhost:8080';
|
||||
|
||||
const authRoute = '/auth';
|
||||
|
||||
export async function loginUser(username: string, password: string) {
|
||||
export async function loginUser(login: Login): Promise<LoginResponse> {
|
||||
const validatedLogin = loginSchema.parse(login);
|
||||
const response = await fetch(`${BASE_URL}${authRoute}/login`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ username, password })
|
||||
body: JSON.stringify(validatedLogin)
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -22,13 +31,25 @@ export async function loginUser(username: string, password: string) {
|
||||
throw new Error(`Login failed: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const { token } = await response.json();
|
||||
const rawData = await response.json();
|
||||
|
||||
const { token } = loginTokenSchema.parse(rawData);
|
||||
|
||||
const userResponse = await getCurrentUser(token);
|
||||
|
||||
return { token, user: userResponse };
|
||||
return loginResponseSchema.parse({ token, user: userResponse });
|
||||
}
|
||||
|
||||
export async function getCurrentUser(token: string) {
|
||||
return fetchServerAPI<User>(`${authRoute}/me`, token);
|
||||
export async function getCurrentUser(token: string): Promise<User> {
|
||||
const response = await fetchServerAPI<User>(`${authRoute}/me`, token);
|
||||
return userSchema.parse(response.data);
|
||||
}
|
||||
|
||||
export async function getOpenToken(token: string): Promise<string> {
|
||||
const response = await fetchServerAPI<{ token: string }>(
|
||||
`${authRoute}/open-token`,
|
||||
token,
|
||||
'POST'
|
||||
);
|
||||
return loginTokenSchema.parse(response.data).token;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
const BASE_URL = process.env.API_BASE_URL || 'http://localhost:8080';
|
||||
|
||||
type ApiResponse<T> = {
|
||||
data?: T;
|
||||
error?: string;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
export async function fetchServerAPI<T>(
|
||||
endpoint: string,
|
||||
token: string,
|
||||
method: string = 'GET',
|
||||
body?: object
|
||||
): Promise<T> {
|
||||
): Promise<ApiResponse<T>> {
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`
|
||||
@@ -14,18 +22,22 @@ export async function fetchServerAPI<T>(
|
||||
const response = await fetch(`${BASE_URL}${endpoint}`, {
|
||||
method,
|
||||
headers,
|
||||
body: body ? JSON.stringify(body) : undefined
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
next: { tags: [endpoint] }
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status == 401) {
|
||||
redirect('/login?expired=true');
|
||||
}
|
||||
throw new Error(
|
||||
`API Error: ${response.statusText} - ${method} - ${BASE_URL}${endpoint} - ${token}`
|
||||
);
|
||||
}
|
||||
|
||||
if (response.headers.get('Content-Type')?.includes('application/json')) {
|
||||
return response.json();
|
||||
return { data: await response.json() };
|
||||
}
|
||||
|
||||
return response.text() as T;
|
||||
return { message: await response.text() };
|
||||
}
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import { fetchServerAPI } from './base';
|
||||
import type {
|
||||
Configurations,
|
||||
Configuration,
|
||||
AssistRules,
|
||||
EventConfig,
|
||||
EventRules,
|
||||
ServerSettings,
|
||||
ConfigFile
|
||||
} from '@/lib/types/config';
|
||||
import {
|
||||
type Configurations,
|
||||
type Config,
|
||||
ConfigFile,
|
||||
configurationsSchema,
|
||||
configSchemaMap
|
||||
} from '@/lib/schemas/config';
|
||||
import * as z from 'zod';
|
||||
|
||||
const serverRoute = '/server';
|
||||
|
||||
@@ -15,26 +14,39 @@ export async function getServerConfigurations(
|
||||
token: string,
|
||||
serverId: string
|
||||
): Promise<Configurations> {
|
||||
return fetchServerAPI<Configurations>(`${serverRoute}/${serverId}/config`, token);
|
||||
const response = await fetchServerAPI<Configurations>(`${serverRoute}/${serverId}/config`, token);
|
||||
return configurationsSchema.parse(response.data);
|
||||
}
|
||||
|
||||
export function validateConfig(
|
||||
configType: ConfigFile,
|
||||
data: unknown
|
||||
): z.infer<(typeof configSchemaMap)[typeof configType]> {
|
||||
const schema = configSchemaMap[configType];
|
||||
return schema.parse(data);
|
||||
}
|
||||
|
||||
export async function getServerConfiguration(
|
||||
token: string,
|
||||
serverId: string,
|
||||
configType: ConfigFile
|
||||
): Promise<Configuration | AssistRules | EventConfig | EventRules | ServerSettings> {
|
||||
return fetchServerAPI(`${serverRoute}/${serverId}/config/${configType}`, token);
|
||||
): Promise<Config> {
|
||||
const response = await fetchServerAPI<Config>(
|
||||
`${serverRoute}/${serverId}/config/${configType}`,
|
||||
token
|
||||
);
|
||||
return validateConfig(configType, response.data);
|
||||
}
|
||||
|
||||
export async function updateServerConfiguration(
|
||||
token: string,
|
||||
serverId: string,
|
||||
configType: ConfigFile,
|
||||
config: Configuration | AssistRules | EventConfig | EventRules | ServerSettings,
|
||||
config: Config,
|
||||
restart = false
|
||||
): Promise<void> {
|
||||
await fetchServerAPI(`${serverRoute}/${serverId}/config/${configType}`, token, 'PUT', {
|
||||
...config,
|
||||
await fetchServerAPI(`${serverRoute}/${serverId}/config/${configType}?override=true`, token, 'PUT', {
|
||||
...validateConfig(configType, config),
|
||||
restart
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,24 +1,43 @@
|
||||
import { fetchServerAPI } from './base';
|
||||
import { Track, CarModel, CupCategory, DriverCategory, SessionType } from '@/lib/types';
|
||||
import {
|
||||
Track,
|
||||
CarModel,
|
||||
CupCategory,
|
||||
DriverCategory,
|
||||
SessionType,
|
||||
trackSchema,
|
||||
carModelSchema,
|
||||
cupCategorySchema,
|
||||
driverCategorySchema,
|
||||
sessionTypeSchema
|
||||
} from '@/lib/schemas';
|
||||
|
||||
const lookupRoute = '/lookup';
|
||||
|
||||
export async function getTracks(token: string): Promise<Track[]> {
|
||||
return fetchServerAPI(`${lookupRoute}/tracks`, token);
|
||||
const response = await fetchServerAPI<Track[]>(`${lookupRoute}/tracks`, token);
|
||||
return trackSchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
export async function getCarModels(token: string): Promise<CarModel[]> {
|
||||
return fetchServerAPI(`${lookupRoute}/car-models`, token);
|
||||
const response = await fetchServerAPI<CarModel[]>(`${lookupRoute}/car-models`, token);
|
||||
return carModelSchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
export async function getCupCategories(token: string): Promise<CupCategory[]> {
|
||||
return fetchServerAPI(`${lookupRoute}/cup-categories`, token);
|
||||
const response = await fetchServerAPI<CupCategory[]>(`${lookupRoute}/cup-categories`, token);
|
||||
return cupCategorySchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
export async function getDriverCategories(token: string): Promise<DriverCategory[]> {
|
||||
return fetchServerAPI(`${lookupRoute}/driver-categories`, token);
|
||||
const response = await fetchServerAPI<DriverCategory[]>(
|
||||
`${lookupRoute}/driver-categories`,
|
||||
token
|
||||
);
|
||||
return driverCategorySchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
export async function getSessionTypes(token: string): Promise<SessionType[]> {
|
||||
return fetchServerAPI(`${lookupRoute}/session-types`, token);
|
||||
const response = await fetchServerAPI<SessionType[]>(`${lookupRoute}/session-types`, token);
|
||||
return sessionTypeSchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { use } from 'react';
|
||||
import { fetchServerAPI } from './base';
|
||||
import { User, Role } from '@/lib/types';
|
||||
import { User, Role, userSchema, UserCreate, userCreateSchema, roleSchema } from '@/lib/schemas';
|
||||
|
||||
export interface UserListParams {
|
||||
username?: string;
|
||||
@@ -24,28 +25,37 @@ export async function getUsers(token: string, params: UserListParams = {}): Prom
|
||||
const queryString = searchParams.toString();
|
||||
const endpoint = `${membershipRoute}${queryString ? `?${queryString}` : ''}`;
|
||||
|
||||
return fetchServerAPI(endpoint, token);
|
||||
const response = await fetchServerAPI<User[]>(endpoint, token);
|
||||
return userSchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
export async function createUser(
|
||||
token: string,
|
||||
userData: { username: string; password: string; role: string }
|
||||
) {
|
||||
return fetchServerAPI(membershipRoute, token, 'POST', userData);
|
||||
export async function createUser(token: string, userData: UserCreate): Promise<void> {
|
||||
await fetchServerAPI(membershipRoute, token, 'POST', userCreateSchema.parse(userData));
|
||||
}
|
||||
|
||||
export async function getUserById(token: string, userId: string): Promise<User> {
|
||||
return fetchServerAPI(`${membershipRoute}/${userId}`, token);
|
||||
const response = await fetchServerAPI<User>(`${membershipRoute}/${userId}`, token);
|
||||
return userSchema.parse(response.data);
|
||||
}
|
||||
|
||||
export async function updateUser(token: string, userId: string, userData: Partial<User>) {
|
||||
return fetchServerAPI(`${membershipRoute}/${userId}`, token, 'PUT', userData);
|
||||
export async function updateUser(
|
||||
token: string,
|
||||
userId: string,
|
||||
userData: Partial<UserCreate>
|
||||
): Promise<void> {
|
||||
await fetchServerAPI(
|
||||
`${membershipRoute}/${userId}`,
|
||||
token,
|
||||
'PUT',
|
||||
userCreateSchema.parse(userData)
|
||||
);
|
||||
}
|
||||
|
||||
export async function deleteUser(token: string, userId: string) {
|
||||
return fetchServerAPI(`${membershipRoute}/${userId}`, token, 'DELETE');
|
||||
export async function deleteUser(token: string, userId: string): Promise<void> {
|
||||
await fetchServerAPI(`${membershipRoute}/${userId}`, token, 'DELETE');
|
||||
}
|
||||
|
||||
export async function getRoles(token: string): Promise<Role[]> {
|
||||
return fetchServerAPI(`${membershipRoute}/roles`, token);
|
||||
const response = await fetchServerAPI<Role[]>(`${membershipRoute}/roles`, token);
|
||||
return roleSchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
@@ -1,30 +1,40 @@
|
||||
import { fetchServerAPI } from './base';
|
||||
import { Server } from '@/lib/types/server';
|
||||
import { Server, serverSchema, ServiceStatus, serviceStatusSchema } from '@/lib/schemas/server';
|
||||
|
||||
const serverRoute = '/server';
|
||||
|
||||
export async function getServers(token: string): Promise<Server[]> {
|
||||
const response = await fetchServerAPI<Server[]>(serverRoute, token);
|
||||
return response;
|
||||
return serverSchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
export async function getServer(token: string, serverId: string): Promise<Server> {
|
||||
const response = await fetchServerAPI<Server>(`${serverRoute}/${serverId}`, token);
|
||||
return response;
|
||||
return serverSchema.parse(response.data);
|
||||
}
|
||||
|
||||
export async function restartService(token: string, serverId: string) {
|
||||
return fetchServerAPI(`${serverRoute}/${serverId}/service/restart`, token, 'POST');
|
||||
export async function restartService(token: string, serverId: string): Promise<void> {
|
||||
await fetchServerAPI(`${serverRoute}/${serverId}/service/restart`, token, 'POST');
|
||||
}
|
||||
|
||||
export async function startService(token: string, serverId: string) {
|
||||
return fetchServerAPI(`${serverRoute}/${serverId}/service/start`, token, 'POST');
|
||||
export async function startService(token: string, serverId: string): Promise<void> {
|
||||
await fetchServerAPI(`${serverRoute}/${serverId}/service/start`, token, 'POST');
|
||||
}
|
||||
|
||||
export async function stopService(token: string, serverId: string) {
|
||||
return fetchServerAPI(`${serverRoute}/${serverId}/service/stop`, token, 'POST');
|
||||
export async function stopService(token: string, serverId: string): Promise<void> {
|
||||
await fetchServerAPI(`${serverRoute}/${serverId}/service/stop`, token, 'POST');
|
||||
}
|
||||
|
||||
export async function getServiceStatus(token: string, serverId: string) {
|
||||
return fetchServerAPI(`${serverRoute}/${serverId}/service`, token);
|
||||
export async function getServiceStatus(token: string, serverId: string): Promise<ServiceStatus> {
|
||||
const response = await fetchServerAPI<ServiceStatus>(`${serverRoute}/${serverId}/service`, token);
|
||||
return serviceStatusSchema.parse(response.data);
|
||||
}
|
||||
|
||||
export async function createServer(token: string, name: string): Promise<Server> {
|
||||
const response = await fetchServerAPI<Server>(serverRoute, token, 'POST', { name });
|
||||
return serverSchema.parse(response.data);
|
||||
}
|
||||
|
||||
export async function deleteServer(token: string, serverId: string): Promise<void> {
|
||||
await fetchServerAPI(`${serverRoute}/${serverId}`, token, 'DELETE');
|
||||
}
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
import { fetchServerAPI } from './base';
|
||||
import type { StateHistoryStats } from '@/lib/types/statistics';
|
||||
import {
|
||||
StateHistoryStatsFilter,
|
||||
stateHistoryStatsFilterSchema,
|
||||
stateHistoryStatsSchema,
|
||||
type StateHistoryStats
|
||||
} from '@/lib/schemas/statistics';
|
||||
|
||||
const serverRoute = '/server';
|
||||
|
||||
export async function getServerStatistics(
|
||||
token: string,
|
||||
serverId: string,
|
||||
startDate: string,
|
||||
endDate: string
|
||||
filters: StateHistoryStatsFilter
|
||||
): Promise<StateHistoryStats> {
|
||||
return fetchServerAPI<StateHistoryStats>(
|
||||
const { startDate, endDate } = stateHistoryStatsFilterSchema.parse(filters);
|
||||
const response = await fetchServerAPI<StateHistoryStats>(
|
||||
`${serverRoute}/${serverId}/state-history/statistics?start_date=${startDate}&end_date=${endDate}`,
|
||||
token
|
||||
);
|
||||
return stateHistoryStatsSchema.parse(response.data);
|
||||
}
|
||||
|
||||
@@ -8,24 +8,26 @@ export async function getSession() {
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function requireAuth() {
|
||||
export async function requireAuth(skipRedirect?: boolean) {
|
||||
const session = await getSession();
|
||||
|
||||
if (!session.token || !session.user) {
|
||||
redirect('/login');
|
||||
if (!skipRedirect && (!session.token || !session.user)) {
|
||||
redirect('/login?expired=true');
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function login(token: string, user: SessionData['user']) {
|
||||
export async function login(token: string, user: SessionData['user'], openToken?: string) {
|
||||
const session = await getSession();
|
||||
session.token = token;
|
||||
session.user = user;
|
||||
session.openToken = openToken;
|
||||
await session.save();
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
'use server';
|
||||
const session = await getSession();
|
||||
session.destroy();
|
||||
}
|
||||
|
||||
61
src/lib/context/ServerCreationPopupContext.tsx
Normal file
61
src/lib/context/ServerCreationPopupContext.tsx
Normal file
@@ -0,0 +1,61 @@
|
||||
'use client';
|
||||
|
||||
import { createContext, useContext, useState, ReactNode, useCallback } from 'react';
|
||||
|
||||
interface ServerCreationPopupState {
|
||||
serverId: string;
|
||||
serverName: string;
|
||||
isOpen: boolean;
|
||||
}
|
||||
|
||||
interface ServerCreationPopupContextType {
|
||||
popup: ServerCreationPopupState | null;
|
||||
showPopup: (serverId: string, serverName: string) => void;
|
||||
hidePopup: () => void;
|
||||
isPopupOpen: boolean;
|
||||
}
|
||||
|
||||
const ServerCreationPopupContext = createContext<ServerCreationPopupContextType | null>(null);
|
||||
|
||||
export function useServerCreationPopup() {
|
||||
const context = useContext(ServerCreationPopupContext);
|
||||
if (!context) {
|
||||
throw new Error('useServerCreationPopup must be used within a ServerCreationPopupProvider');
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
interface ServerCreationPopupProviderProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export function ServerCreationPopupProvider({ children }: ServerCreationPopupProviderProps) {
|
||||
const [popup, setPopup] = useState<ServerCreationPopupState | null>(null);
|
||||
|
||||
const showPopup = useCallback((serverId: string, serverName: string) => {
|
||||
setPopup({
|
||||
serverId,
|
||||
serverName,
|
||||
isOpen: true
|
||||
});
|
||||
}, []);
|
||||
|
||||
const hidePopup = useCallback(() => {
|
||||
setPopup(null);
|
||||
}, []);
|
||||
|
||||
const isPopupOpen = popup?.isOpen || false;
|
||||
|
||||
return (
|
||||
<ServerCreationPopupContext.Provider
|
||||
value={{
|
||||
popup,
|
||||
showPopup,
|
||||
hidePopup,
|
||||
isPopupOpen
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</ServerCreationPopupContext.Provider>
|
||||
);
|
||||
}
|
||||
79
src/lib/context/SteamCMDContext.tsx
Normal file
79
src/lib/context/SteamCMDContext.tsx
Normal file
@@ -0,0 +1,79 @@
|
||||
'use client';
|
||||
|
||||
import { createContext, useContext, useState, ReactNode, useEffect, useCallback } from 'react';
|
||||
import { useWebSocket } from '@/lib/websocket/context';
|
||||
import type { WebSocketMessage, StepData } from '@/lib/schemas/websocket';
|
||||
|
||||
interface SteamCMDContextType {
|
||||
isSteamCMDRunning: boolean;
|
||||
runningSteamServers: Set<string>;
|
||||
dissociateServer: (serverId: string) => void;
|
||||
}
|
||||
|
||||
const SteamCMDContext = createContext<SteamCMDContextType | null>(null);
|
||||
|
||||
export function useSteamCMD() {
|
||||
const context = useContext(SteamCMDContext);
|
||||
if (!context) {
|
||||
throw new Error('useSteamCMD must be used within a SteamCMDProvider');
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
interface SteamCMDProviderProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export function SteamCMDProvider({ children }: SteamCMDProviderProps) {
|
||||
const [runningSteamServers, setRunningSteamServers] = useState<Set<string>>(new Set());
|
||||
const { addMessageHandler, removeMessageHandler } = useWebSocket();
|
||||
|
||||
const isSteamCMDRunning = runningSteamServers.size > 0;
|
||||
|
||||
useEffect(() => {
|
||||
const handleWebSocketMessage = (message: WebSocketMessage) => {
|
||||
if (message.type === 'step') {
|
||||
const data = message.data as StepData;
|
||||
|
||||
if (data.step === 'steam_download') {
|
||||
setRunningSteamServers((prev) => {
|
||||
const newSet = new Set(prev);
|
||||
|
||||
if (data.status === 'in_progress') {
|
||||
newSet.add(message.server_id);
|
||||
} else if (data.status === 'completed' || data.status === 'failed') {
|
||||
newSet.delete(message.server_id);
|
||||
}
|
||||
|
||||
return newSet;
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
addMessageHandler(handleWebSocketMessage);
|
||||
return () => {
|
||||
removeMessageHandler(handleWebSocketMessage);
|
||||
};
|
||||
}, [addMessageHandler, removeMessageHandler]);
|
||||
|
||||
const dissociateServer = useCallback((serverId: string) => {
|
||||
setRunningSteamServers((prev) => {
|
||||
const newSet = new Set(prev);
|
||||
newSet.delete(serverId);
|
||||
return newSet;
|
||||
});
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<SteamCMDContext.Provider
|
||||
value={{
|
||||
isSteamCMDRunning,
|
||||
runningSteamServers,
|
||||
dissociateServer
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</SteamCMDContext.Provider>
|
||||
);
|
||||
}
|
||||
130
src/lib/schemas/config.ts
Normal file
130
src/lib/schemas/config.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
export enum ConfigFile {
|
||||
configuration = 'configuration.json',
|
||||
assistRules = 'assistRules.json',
|
||||
event = 'event.json',
|
||||
eventRules = 'eventRules.json',
|
||||
settings = 'settings.json'
|
||||
}
|
||||
export const configFileSchema = z.enum(ConfigFile);
|
||||
|
||||
export enum ServerTab {
|
||||
statistics = 'statistics',
|
||||
configuration = 'configuration',
|
||||
assistRules = 'assistRules',
|
||||
event = 'event',
|
||||
eventRules = 'eventRules',
|
||||
settings = 'settings'
|
||||
}
|
||||
export const serverTabSchema = z.enum(ServerTab);
|
||||
|
||||
export const configurationSchema = z.object({
|
||||
udpPort: z.number().min(1025).max(65535),
|
||||
tcpPort: z.number().min(1025).max(65535),
|
||||
maxConnections: z.number().min(1).max(64),
|
||||
lanDiscovery: z.number().min(0).max(1),
|
||||
registerToLobby: z.number().min(0).max(2),
|
||||
configVersion: z.number().min(1).max(2).default(1).optional()
|
||||
});
|
||||
|
||||
export type Configuration = z.infer<typeof configurationSchema>;
|
||||
|
||||
export const assistRulesSchema = z.object({
|
||||
stabilityControlLevelMax: z.number().min(0).max(100).default(100),
|
||||
disableAutosteer: z.number().min(0).max(1).default(0),
|
||||
disableAutoLights: z.number().min(0).max(1).default(0),
|
||||
disableAutoWiper: z.number().min(0).max(1).default(0),
|
||||
disableAutoEngineStart: z.number().min(0).max(1).default(0),
|
||||
disableAutoPitLimiter: z.number().min(0).max(1).default(0),
|
||||
disableAutoGear: z.number().min(0).max(1).default(0),
|
||||
disableAutoClutch: z.number().min(0).max(1).default(0),
|
||||
disableIdealLine: z.number().min(0).max(1).default(0)
|
||||
});
|
||||
|
||||
export type AssistRules = z.infer<typeof assistRulesSchema>;
|
||||
|
||||
export const serverSettingsSchema = z.object({
|
||||
serverName: z.string().min(3).max(150),
|
||||
adminPassword: z.string().min(6).max(50),
|
||||
carGroup: z.string().min(1).max(50),
|
||||
trackMedalsRequirement: z.number().min(-1).max(3),
|
||||
safetyRatingRequirement: z.number().min(-1).max(99),
|
||||
racecraftRatingRequirement: z.number().min(-1).max(99),
|
||||
password: z.string().max(50).optional().or(z.literal('')),
|
||||
spectatorPassword: z.string().max(50).optional().or(z.literal('')),
|
||||
maxCarSlots: z.number().min(1).max(30),
|
||||
dumpLeaderboards: z.number().min(0).max(1).default(0),
|
||||
isRaceLocked: z.number().min(0).max(1).default(0),
|
||||
randomizeTrackWhenEmpty: z.number().min(0).max(1).default(0),
|
||||
centralEntryListPath: z.string().max(255).optional().or(z.literal('')),
|
||||
allowAutoDQ: z.number().min(0).max(1).default(0),
|
||||
shortFormationLap: z.number().min(0).max(1).default(0),
|
||||
formationLapType: z.number().min(0).max(3).default(0),
|
||||
ignorePrematureDisconnects: z.number().min(0).max(1).default(0)
|
||||
});
|
||||
|
||||
export type ServerSettings = z.infer<typeof serverSettingsSchema>;
|
||||
|
||||
export const sessionSchema = z.object({
|
||||
hourOfDay: z.number().min(1).max(24).default(14),
|
||||
dayOfWeekend: z.number().min(1).max(3).default(1),
|
||||
timeMultiplier: z.number().min(1).max(120).default(1),
|
||||
sessionType: z.string().min(1).max(20),
|
||||
sessionDurationMinutes: z.number().min(1).max(180).default(20)
|
||||
});
|
||||
|
||||
export type Session = z.infer<typeof sessionSchema>;
|
||||
|
||||
export const eventConfigSchema = z.object({
|
||||
track: z.string().min(1).max(100),
|
||||
preRaceWaitingTimeSeconds: z.number().min(0).max(600).default(30),
|
||||
sessionOverTimeSeconds: z.number().min(0).max(300).default(30),
|
||||
ambientTemp: z.number().min(0).max(50).default(24),
|
||||
cloudLevel: z.number().min(0).max(1).default(0),
|
||||
rain: z.number().min(0).max(1).default(0),
|
||||
weatherRandomness: z.number().min(0).max(7).default(0),
|
||||
postQualySeconds: z.number().min(0).max(600).default(30),
|
||||
postRaceSeconds: z.number().min(0).max(600).default(30),
|
||||
simracerWeatherConditions: z.number().min(0).max(1).default(0),
|
||||
isFixedConditionQualification: z.number().min(0).max(1).default(0),
|
||||
sessions: z.array(sessionSchema).min(1).max(10)
|
||||
});
|
||||
|
||||
export type EventConfig = z.infer<typeof eventConfigSchema>;
|
||||
|
||||
export const eventRulesSchema = z.object({
|
||||
qualifyStandingType: z.number().min(-1).max(1).default(0),
|
||||
pitWindowLengthSec: z.number().min(-1).max(3600).default(30),
|
||||
driverStintTimeSec: z.number().min(-1).max(7200).default(30),
|
||||
mandatoryPitstopCount: z.number().min(-1).max(5).default(0),
|
||||
maxTotalDrivingTime: z.number().min(-1).max(14400).default(60),
|
||||
isRefuellingAllowedInRace: z.number().min(0).max(1).default(0),
|
||||
isRefuellingTimeFixed: z.number().min(0).max(1).default(0),
|
||||
isMandatoryPitstopRefuellingRequired: z.number().min(0).max(1).default(0),
|
||||
isMandatoryPitstopTyreChangeRequired: z.number().min(0).max(1).default(0),
|
||||
isMandatoryPitstopSwapDriverRequired: z.number().min(0).max(1).default(0),
|
||||
tyreSetCount: z.number().min(0).max(50).default(0)
|
||||
});
|
||||
|
||||
export type EventRules = z.infer<typeof eventRulesSchema>;
|
||||
|
||||
export const configurationsSchema = z.object({
|
||||
configuration: configurationSchema,
|
||||
assistRules: assistRulesSchema,
|
||||
settings: serverSettingsSchema,
|
||||
event: eventConfigSchema,
|
||||
eventRules: eventRulesSchema
|
||||
});
|
||||
|
||||
export type Configurations = z.infer<typeof configurationsSchema>;
|
||||
|
||||
export const configSchemaMap = {
|
||||
[ConfigFile.configuration]: configurationSchema,
|
||||
[ConfigFile.assistRules]: assistRulesSchema,
|
||||
[ConfigFile.event]: eventConfigSchema,
|
||||
[ConfigFile.eventRules]: eventRulesSchema,
|
||||
[ConfigFile.settings]: serverSettingsSchema
|
||||
};
|
||||
|
||||
export type Config = Configuration | AssistRules | EventConfig | EventRules | ServerSettings;
|
||||
@@ -1,4 +1,3 @@
|
||||
// Re-export all types for easier imports
|
||||
export * from './server';
|
||||
export * from './user';
|
||||
export * from './config';
|
||||
33
src/lib/schemas/lookups.ts
Normal file
33
src/lib/schemas/lookups.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
export const trackSchema = z.object({
|
||||
track: z.string(),
|
||||
uniquePitBoxes: z.number().int().nonnegative(),
|
||||
privateServerSlots: z.number().int().nonnegative()
|
||||
});
|
||||
|
||||
export type Track = z.infer<typeof trackSchema>;
|
||||
|
||||
export const carModelSchema = z.object({
|
||||
value: z.number().int().nonnegative(),
|
||||
carModel: z.string()
|
||||
});
|
||||
export type CarModel = z.infer<typeof carModelSchema>;
|
||||
|
||||
export const driverCategorySchema = z.object({
|
||||
value: z.number().int().nonnegative(),
|
||||
category: z.string()
|
||||
});
|
||||
export type DriverCategory = z.infer<typeof driverCategorySchema>;
|
||||
|
||||
export const cupCategorySchema = z.object({
|
||||
value: z.number().int().nonnegative(),
|
||||
category: z.string()
|
||||
});
|
||||
export type CupCategory = z.infer<typeof cupCategorySchema>;
|
||||
|
||||
export const sessionTypeSchema = z.object({
|
||||
value: z.number().int().nonnegative(),
|
||||
sessionType: z.string()
|
||||
});
|
||||
export type SessionType = z.infer<typeof sessionTypeSchema>;
|
||||
@@ -1,3 +1,5 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
export enum ServiceStatus {
|
||||
Unknown,
|
||||
Stopped,
|
||||
@@ -6,6 +8,7 @@ export enum ServiceStatus {
|
||||
Starting,
|
||||
Running
|
||||
}
|
||||
export const serviceStatusSchema = z.enum(ServiceStatus);
|
||||
|
||||
export const serviceStatusToString = (status: ServiceStatus): string => {
|
||||
switch (status) {
|
||||
@@ -41,16 +44,20 @@ export const getStatusColor = (status: ServiceStatus): string => {
|
||||
}
|
||||
};
|
||||
|
||||
interface State {
|
||||
session: string;
|
||||
playerCount: number;
|
||||
track: string;
|
||||
maxConnections: number;
|
||||
}
|
||||
export const stateSchema = z.object({
|
||||
session: z.string(),
|
||||
playerCount: z.number().min(0),
|
||||
track: z.string(),
|
||||
maxConnections: z.number().min(0)
|
||||
});
|
||||
|
||||
export interface Server {
|
||||
id: string;
|
||||
name: string;
|
||||
status: ServiceStatus;
|
||||
state: State;
|
||||
}
|
||||
export type State = z.infer<typeof stateSchema>;
|
||||
|
||||
export const serverSchema = z.object({
|
||||
id: z.uuid(),
|
||||
name: z.string().min(1),
|
||||
status: z.enum(ServiceStatus),
|
||||
state: stateSchema.optional()
|
||||
});
|
||||
|
||||
export type Server = z.infer<typeof serverSchema>;
|
||||
58
src/lib/schemas/statistics.ts
Normal file
58
src/lib/schemas/statistics.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
export const stateHistorySchema = z.object({
|
||||
dateCreated: z.string(),
|
||||
sessionStart: z.string(),
|
||||
playerCount: z.number(),
|
||||
track: z.string(),
|
||||
sessionDurationMinutes: z.number(),
|
||||
session: z.string()
|
||||
});
|
||||
export type StateHistory = z.infer<typeof stateHistorySchema>;
|
||||
|
||||
export const sessionCountSchema = z.object({
|
||||
name: z.string(),
|
||||
count: z.number()
|
||||
});
|
||||
export type SessionCount = z.infer<typeof sessionCountSchema>;
|
||||
|
||||
export const dailyActivitySchema = z.object({
|
||||
date: z.string(),
|
||||
sessionsCount: z.number()
|
||||
});
|
||||
export type DailyActivity = z.infer<typeof dailyActivitySchema>;
|
||||
|
||||
export const playerCountPointSchema = z.object({
|
||||
timestamp: z.string(),
|
||||
count: z.number()
|
||||
});
|
||||
export type PlayerCountPoint = z.infer<typeof playerCountPointSchema>;
|
||||
|
||||
export const recentSessionSchema = z.object({
|
||||
id: z.uuid(),
|
||||
date: z.string(),
|
||||
type: z.string(),
|
||||
track: z.string(),
|
||||
duration: z.number(),
|
||||
players: z.number()
|
||||
});
|
||||
export type RecentSession = z.infer<typeof recentSessionSchema>;
|
||||
|
||||
export const stateHistoryStatsSchema = z.object({
|
||||
averagePlayers: z.number(),
|
||||
peakPlayers: z.number(),
|
||||
totalSessions: z.number(),
|
||||
totalPlaytime: z.number(),
|
||||
playerCountOverTime: z.array(playerCountPointSchema),
|
||||
sessionTypes: z.array(sessionCountSchema),
|
||||
dailyActivity: z.array(dailyActivitySchema),
|
||||
recentSessions: z.array(recentSessionSchema)
|
||||
});
|
||||
export type StateHistoryStats = z.infer<typeof stateHistoryStatsSchema>;
|
||||
|
||||
export const stateHistoryStatsFilterSchema = z.object({
|
||||
startDate: z.string().min(10, 'Start date is required'),
|
||||
endDate: z.string().min(10, 'End date is required')
|
||||
});
|
||||
|
||||
export type StateHistoryStatsFilter = z.infer<typeof stateHistoryStatsFilterSchema>;
|
||||
69
src/lib/schemas/user.ts
Normal file
69
src/lib/schemas/user.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
export const permissionSchema = z.object({
|
||||
id: z.uuid(),
|
||||
name: z.string()
|
||||
});
|
||||
|
||||
export type Permission = z.infer<typeof permissionSchema>;
|
||||
|
||||
export const roleSchema = z.object({
|
||||
id: z.uuid(),
|
||||
name: z.string(),
|
||||
permissions: z.array(permissionSchema)
|
||||
});
|
||||
|
||||
export type Role = z.infer<typeof roleSchema>;
|
||||
|
||||
export const userSchema = z.object({
|
||||
id: z.uuid(),
|
||||
username: z.string(),
|
||||
role_id: z.uuid(),
|
||||
role: roleSchema
|
||||
});
|
||||
|
||||
export type User = z.infer<typeof userSchema>;
|
||||
|
||||
export const userCreateSchema = z.object({
|
||||
username: z
|
||||
.string()
|
||||
.min(1, 'Username is required')
|
||||
.max(50, 'Username must be at most 50 characters'),
|
||||
password: z
|
||||
.string()
|
||||
.min(6, 'Password must be at least 6 characters')
|
||||
.max(100, 'Password must be at most 100 characters'),
|
||||
role: z.string().min(1, 'Role is required')
|
||||
});
|
||||
|
||||
export type UserCreate = z.infer<typeof userCreateSchema>;
|
||||
|
||||
export const loginSchema = z.object({
|
||||
username: z.string().min(1, 'Username is required'),
|
||||
password: z.string().min(1, 'Password is required')
|
||||
});
|
||||
|
||||
export type Login = z.infer<typeof loginSchema>;
|
||||
|
||||
export const loginTokenSchema = z.object({
|
||||
token: z.string().min(1)
|
||||
});
|
||||
|
||||
export type LoginTokenResponse = z.infer<typeof loginTokenSchema>;
|
||||
|
||||
export const loginResponseSchema = loginTokenSchema.extend({
|
||||
user: userSchema
|
||||
});
|
||||
|
||||
export type LoginResponse = z.infer<typeof loginResponseSchema>;
|
||||
|
||||
export function hasPermission(user: User | null, permission: string): boolean {
|
||||
if (!user || !user.role || !user.role.permissions) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (user.role.name === 'Super Admin') {
|
||||
return true;
|
||||
}
|
||||
return user.role.permissions.some((p) => p.name === permission);
|
||||
}
|
||||
87
src/lib/schemas/websocket.ts
Normal file
87
src/lib/schemas/websocket.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
// Step data schema
|
||||
export const stepDataSchema = z.object({
|
||||
step: z.enum([
|
||||
'validation',
|
||||
'directory_creation',
|
||||
'steam_download',
|
||||
'config_generation',
|
||||
'service_creation',
|
||||
'firewall_rules',
|
||||
'database_save',
|
||||
'completed'
|
||||
]),
|
||||
status: z.enum(['pending', 'in_progress', 'completed', 'failed']),
|
||||
message: z.string(),
|
||||
error: z.string()
|
||||
});
|
||||
|
||||
export type StepData = z.infer<typeof stepDataSchema>;
|
||||
|
||||
// Steam output data schema
|
||||
export const steamOutputDataSchema = z.object({
|
||||
output: z.string(),
|
||||
is_error: z.boolean()
|
||||
});
|
||||
|
||||
export type SteamOutputData = z.infer<typeof steamOutputDataSchema>;
|
||||
|
||||
// Error data schema
|
||||
export const errorDataSchema = z.object({
|
||||
error: z.string(),
|
||||
details: z.string()
|
||||
});
|
||||
|
||||
export type ErrorData = z.infer<typeof errorDataSchema>;
|
||||
|
||||
// Complete data schema
|
||||
export const completeDataSchema = z.object({
|
||||
server_id: z.string(),
|
||||
success: z.boolean(),
|
||||
message: z.string()
|
||||
});
|
||||
|
||||
export type CompleteData = z.infer<typeof completeDataSchema>;
|
||||
|
||||
// WebSocket message schema using discriminated union
|
||||
export const webSocketMessageSchema = z.discriminatedUnion('type', [
|
||||
z.object({
|
||||
type: z.literal('step'),
|
||||
server_id: z.string(),
|
||||
timestamp: z.number(),
|
||||
data: stepDataSchema
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('steam_output'),
|
||||
server_id: z.string(),
|
||||
timestamp: z.number(),
|
||||
data: steamOutputDataSchema
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('error'),
|
||||
server_id: z.string(),
|
||||
timestamp: z.number(),
|
||||
data: errorDataSchema
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('complete'),
|
||||
server_id: z.string(),
|
||||
timestamp: z.number(),
|
||||
data: completeDataSchema
|
||||
})
|
||||
]);
|
||||
|
||||
export type WebSocketMessage = z.infer<typeof webSocketMessageSchema>;
|
||||
|
||||
// Connection status schema
|
||||
export const connectionStatusSchema = z.enum(['connecting', 'connected', 'disconnected', 'error']);
|
||||
|
||||
export type ConnectionStatus = z.infer<typeof connectionStatusSchema>;
|
||||
|
||||
// Handler types (these remain as types since they're function signatures)
|
||||
export type MessageHandler = (message: WebSocketMessage) => void;
|
||||
export type ConnectionStatusHandler = (
|
||||
status: ConnectionStatus,
|
||||
error?: string
|
||||
) => void;
|
||||
@@ -1,8 +1,9 @@
|
||||
import { SessionOptions } from 'iron-session';
|
||||
import { User } from '@/lib/types';
|
||||
import { User } from '@/lib/schemas';
|
||||
|
||||
export interface SessionData {
|
||||
token?: string;
|
||||
openToken?: string;
|
||||
user?: User;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,105 +0,0 @@
|
||||
export enum ConfigFile {
|
||||
configuration = 'configuration.json',
|
||||
assistRules = 'assistRules.json',
|
||||
event = 'event.json',
|
||||
eventRules = 'eventRules.json',
|
||||
settings = 'settings.json'
|
||||
}
|
||||
|
||||
export enum ServerTab {
|
||||
statistics = 'statistics',
|
||||
configuration = 'configuration',
|
||||
assistRules = 'assistRules',
|
||||
event = 'event',
|
||||
eventRules = 'eventRules',
|
||||
settings = 'settings'
|
||||
}
|
||||
|
||||
// Configuration interfaces
|
||||
export interface Configuration {
|
||||
udpPort: number;
|
||||
tcpPort: number;
|
||||
maxConnections: number;
|
||||
lanDiscovery: number;
|
||||
registerToLobby: number;
|
||||
configVersion: number;
|
||||
}
|
||||
|
||||
export interface AssistRules {
|
||||
stabilityControlLevelMax: number;
|
||||
disableAutosteer: number;
|
||||
disableAutoLights: number;
|
||||
disableAutoWiper: number;
|
||||
disableAutoEngineStart: number;
|
||||
disableAutoPitLimiter: number;
|
||||
disableAutoGear: number;
|
||||
disableAutoClutch: number;
|
||||
disableIdealLine: number;
|
||||
}
|
||||
|
||||
export interface ServerSettings {
|
||||
serverName: string;
|
||||
adminPassword: string;
|
||||
carGroup: string;
|
||||
trackMedalsRequirement: number;
|
||||
safetyRatingRequirement: number;
|
||||
racecraftRatingRequirement: number;
|
||||
password: string;
|
||||
spectatorPassword: string;
|
||||
maxCarSlots: number;
|
||||
dumpLeaderboards: number;
|
||||
isRaceLocked: number;
|
||||
randomizeTrackWhenEmpty: number;
|
||||
centralEntryListPath: string;
|
||||
allowAutoDQ: number;
|
||||
shortFormationLap: number;
|
||||
formationLapType: number;
|
||||
ignorePrematureDisconnects: number;
|
||||
}
|
||||
|
||||
export interface Session {
|
||||
hourOfDay: number;
|
||||
dayOfWeekend: number;
|
||||
timeMultiplier: number;
|
||||
sessionType: string;
|
||||
sessionDurationMinutes: number;
|
||||
}
|
||||
|
||||
export interface EventConfig {
|
||||
track: string;
|
||||
preRaceWaitingTimeSeconds: number;
|
||||
sessionOverTimeSeconds: number;
|
||||
ambientTemp: number;
|
||||
cloudLevel: number;
|
||||
rain: number;
|
||||
weatherRandomness: number;
|
||||
postQualySeconds: number;
|
||||
postRaceSeconds: number;
|
||||
simracerWeatherConditions: number;
|
||||
isFixedConditionQualification: number;
|
||||
sessions: Session[];
|
||||
}
|
||||
|
||||
export interface EventRules {
|
||||
qualifyStandingType: number;
|
||||
pitWindowLengthSec: number;
|
||||
driverStintTimeSec: number;
|
||||
mandatoryPitstopCount: number;
|
||||
maxTotalDrivingTime: number;
|
||||
isRefuellingAllowedInRace: boolean;
|
||||
isRefuellingTimeFixed: boolean;
|
||||
isMandatoryPitstopRefuellingRequired: boolean;
|
||||
isMandatoryPitstopTyreChangeRequired: boolean;
|
||||
isMandatoryPitstopSwapDriverRequired: boolean;
|
||||
tyreSetCount: number;
|
||||
}
|
||||
|
||||
export interface Configurations {
|
||||
configuration: Configuration;
|
||||
assistRules: AssistRules;
|
||||
event: EventConfig;
|
||||
eventRules: EventRules;
|
||||
settings: ServerSettings;
|
||||
}
|
||||
|
||||
export type Config = Configuration | AssistRules | EventConfig | EventRules | ServerSettings;
|
||||
@@ -1,25 +0,0 @@
|
||||
export interface Track {
|
||||
track: string;
|
||||
uniquePitBoxes: number;
|
||||
privateServerSlots: number;
|
||||
}
|
||||
|
||||
export interface CarModel {
|
||||
value: number;
|
||||
carModel: string;
|
||||
}
|
||||
|
||||
export interface DriverCategory {
|
||||
value: number;
|
||||
category: string;
|
||||
}
|
||||
|
||||
export interface CupCategory {
|
||||
value: number;
|
||||
category: string;
|
||||
}
|
||||
|
||||
export interface SessionType {
|
||||
value: number;
|
||||
sessionType: string;
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
export interface StateHistory {
|
||||
dateCreated: string;
|
||||
sessionStart: string;
|
||||
playerCount: number;
|
||||
track: string;
|
||||
sessionDurationMinutes: number;
|
||||
session: string;
|
||||
}
|
||||
|
||||
interface SessionCount {
|
||||
name: string;
|
||||
count: number;
|
||||
}
|
||||
|
||||
interface DailyActivity {
|
||||
date: string;
|
||||
sessionsCount: number;
|
||||
}
|
||||
|
||||
interface PlayerCountPoint {
|
||||
timestamp: string;
|
||||
count: number;
|
||||
}
|
||||
|
||||
interface RecentSession {
|
||||
id: number;
|
||||
date: string;
|
||||
type: string;
|
||||
track: string;
|
||||
duration: number;
|
||||
players: number;
|
||||
}
|
||||
|
||||
export interface StateHistoryStats {
|
||||
averagePlayers: number;
|
||||
peakPlayers: number;
|
||||
totalSessions: number;
|
||||
totalPlaytime: number;
|
||||
playerCountOverTime: PlayerCountPoint[];
|
||||
sessionTypes: SessionCount[];
|
||||
dailyActivity: DailyActivity[];
|
||||
recentSessions: RecentSession[];
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
export interface Permission {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface Role {
|
||||
id: string;
|
||||
name: string;
|
||||
permissions: Permission[];
|
||||
}
|
||||
|
||||
export interface User {
|
||||
id: string;
|
||||
username: string;
|
||||
role_id: string;
|
||||
role: Role;
|
||||
}
|
||||
|
||||
export function hasPermission(user: User | null, permission: string): boolean {
|
||||
if (!user || !user.role || !user.role.permissions) {
|
||||
return false;
|
||||
}
|
||||
// Super Admins have all permissions
|
||||
if (user.role.name === 'Super Admin') {
|
||||
return true;
|
||||
}
|
||||
return user.role.permissions.some((p) => p.name === permission);
|
||||
}
|
||||
170
src/lib/websocket/client.ts
Normal file
170
src/lib/websocket/client.ts
Normal file
@@ -0,0 +1,170 @@
|
||||
import {
|
||||
webSocketMessageSchema,
|
||||
type WebSocketMessage,
|
||||
type MessageHandler,
|
||||
type ConnectionStatusHandler
|
||||
} from '@/lib/schemas/websocket';
|
||||
|
||||
export class WebSocketClient {
|
||||
private ws: WebSocket | null = null;
|
||||
private token: string;
|
||||
private messageHandlers: MessageHandler[] = [];
|
||||
private connectionStatusHandlers: ConnectionStatusHandler[] = [];
|
||||
private connectionPromise: Promise<void> | null = null;
|
||||
private reconnectAttempts = 0;
|
||||
private maxReconnectAttempts = 10;
|
||||
private reconnectDelay = 1000; // 1 second
|
||||
private maxReconnectDelay = 30000; // 30 seconds
|
||||
private reconnectTimer: NodeJS.Timeout | null = null;
|
||||
private shouldReconnect = true;
|
||||
private associatedServerId: string | null = null;
|
||||
private baseUrl: string;
|
||||
|
||||
constructor(token: string, url: string) {
|
||||
this.token = token;
|
||||
this.baseUrl = url;
|
||||
}
|
||||
|
||||
connect(): Promise<void> {
|
||||
if (this.connectionPromise) {
|
||||
return this.connectionPromise;
|
||||
}
|
||||
|
||||
this.shouldReconnect = true;
|
||||
this.notifyStatus('connecting');
|
||||
|
||||
this.connectionPromise = new Promise((resolve, reject) => {
|
||||
try {
|
||||
this.ws = new WebSocket(`${this.baseUrl}?token=${this.token}`);
|
||||
|
||||
this.ws.onopen = () => {
|
||||
console.log('WebSocket connected');
|
||||
this.reconnectAttempts = 0;
|
||||
this.reconnectDelay = 5000;
|
||||
this.notifyStatus('connected');
|
||||
|
||||
if (this.associatedServerId) {
|
||||
this.associateWithServer(this.associatedServerId);
|
||||
}
|
||||
|
||||
resolve();
|
||||
};
|
||||
|
||||
this.ws.onmessage = (event) => {
|
||||
try {
|
||||
const rawMessage: WebSocketMessage = JSON.parse(event.data);
|
||||
const message = webSocketMessageSchema.parse(rawMessage);
|
||||
this.messageHandlers.forEach((handler) => handler(message));
|
||||
} catch (error) {
|
||||
console.error('Failed to parse WebSocket message:', error);
|
||||
}
|
||||
};
|
||||
|
||||
this.ws.onclose = (event) => {
|
||||
console.log('WebSocket disconnected:', event.code, event.reason);
|
||||
this.connectionPromise = null;
|
||||
this.notifyStatus('disconnected');
|
||||
|
||||
if (this.shouldReconnect && this.reconnectAttempts < this.maxReconnectAttempts) {
|
||||
this.scheduleReconnect();
|
||||
}
|
||||
};
|
||||
|
||||
this.ws.onerror = (error) => {
|
||||
console.error('WebSocket error:', error);
|
||||
this.notifyStatus('error', 'Connection failed');
|
||||
reject(error);
|
||||
};
|
||||
} catch (error) {
|
||||
this.notifyStatus('error', error instanceof Error ? error.message : 'Unknown error');
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
|
||||
return this.connectionPromise;
|
||||
}
|
||||
|
||||
associateWithServer(serverId: string): void {
|
||||
this.associatedServerId = serverId;
|
||||
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
||||
this.ws.send(`server_id:${serverId}`);
|
||||
}
|
||||
}
|
||||
|
||||
addMessageHandler(handler: MessageHandler): void {
|
||||
this.messageHandlers.push(handler);
|
||||
}
|
||||
|
||||
removeMessageHandler(handler: MessageHandler): void {
|
||||
const index = this.messageHandlers.indexOf(handler);
|
||||
if (index > -1) {
|
||||
this.messageHandlers.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
disconnect(): void {
|
||||
this.shouldReconnect = false;
|
||||
if (this.reconnectTimer) {
|
||||
clearTimeout(this.reconnectTimer);
|
||||
this.reconnectTimer = null;
|
||||
}
|
||||
if (this.ws) {
|
||||
this.ws.close();
|
||||
this.ws = null;
|
||||
}
|
||||
this.connectionPromise = null;
|
||||
this.messageHandlers = [];
|
||||
this.connectionStatusHandlers = [];
|
||||
this.associatedServerId = null;
|
||||
}
|
||||
|
||||
isConnected(): boolean {
|
||||
return this.ws?.readyState === WebSocket.OPEN;
|
||||
}
|
||||
|
||||
addConnectionStatusHandler(handler: ConnectionStatusHandler): void {
|
||||
this.connectionStatusHandlers.push(handler);
|
||||
}
|
||||
|
||||
removeConnectionStatusHandler(handler: ConnectionStatusHandler): void {
|
||||
const index = this.connectionStatusHandlers.indexOf(handler);
|
||||
if (index > -1) {
|
||||
this.connectionStatusHandlers.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
reconnect(): Promise<void> {
|
||||
this.disconnect();
|
||||
return this.connect();
|
||||
}
|
||||
|
||||
private notifyStatus(
|
||||
status: 'connecting' | 'connected' | 'disconnected' | 'error',
|
||||
error?: string
|
||||
): void {
|
||||
this.connectionStatusHandlers.forEach((handler) => handler(status, error));
|
||||
}
|
||||
|
||||
private scheduleReconnect(): void {
|
||||
if (this.reconnectTimer) {
|
||||
clearTimeout(this.reconnectTimer);
|
||||
}
|
||||
|
||||
this.reconnectAttempts++;
|
||||
const delay = Math.min(
|
||||
this.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1),
|
||||
this.maxReconnectDelay
|
||||
);
|
||||
|
||||
console.log(
|
||||
`WebSocket reconnect attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts} in ${delay}ms`
|
||||
);
|
||||
|
||||
this.reconnectTimer = setTimeout(() => {
|
||||
this.reconnectTimer = null;
|
||||
this.connect().catch((error) => {
|
||||
console.error('Reconnection failed:', error);
|
||||
});
|
||||
}, delay);
|
||||
}
|
||||
}
|
||||
7
src/lib/websocket/config.ts
Normal file
7
src/lib/websocket/config.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export interface WebsocketOptions {
|
||||
url: string;
|
||||
}
|
||||
|
||||
export const websocketOptions: WebsocketOptions = {
|
||||
url: process.env.NEXT_PUBLIC_WEBSOCKET_URL || 'ws://localhost:3000/ws'
|
||||
};
|
||||
185
src/lib/websocket/context.tsx
Normal file
185
src/lib/websocket/context.tsx
Normal file
@@ -0,0 +1,185 @@
|
||||
'use client';
|
||||
|
||||
import {
|
||||
createContext,
|
||||
useContext,
|
||||
useEffect,
|
||||
useState,
|
||||
ReactNode,
|
||||
useCallback,
|
||||
useRef
|
||||
} from 'react';
|
||||
import { WebSocketClient } from './client';
|
||||
import { websocketOptions } from './config';
|
||||
import type { ConnectionStatusHandler, MessageHandler } from '../schemas/websocket';
|
||||
|
||||
interface WebSocketContextType {
|
||||
client: WebSocketClient | null;
|
||||
isConnected: boolean;
|
||||
connectionStatus: 'connecting' | 'connected' | 'disconnected' | 'error';
|
||||
connectionError: string | null;
|
||||
connect: (token: string) => Promise<void>;
|
||||
disconnect: () => void;
|
||||
reconnect: () => Promise<void>;
|
||||
associateWithServer: (serverId: string) => void;
|
||||
addMessageHandler: (handler: MessageHandler) => void;
|
||||
removeMessageHandler: (handler: MessageHandler) => void;
|
||||
addConnectionStatusHandler: (handler: ConnectionStatusHandler) => void;
|
||||
removeConnectionStatusHandler: (handler: ConnectionStatusHandler) => void;
|
||||
}
|
||||
|
||||
const WebSocketContext = createContext<WebSocketContextType | null>(null);
|
||||
|
||||
export function useWebSocket() {
|
||||
const context = useContext(WebSocketContext);
|
||||
if (!context) {
|
||||
throw new Error('useWebSocket must be used within a WebSocketProvider');
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
interface WebSocketProviderProps {
|
||||
children: ReactNode;
|
||||
openToken: string;
|
||||
}
|
||||
|
||||
export function WebSocketProvider({ children, openToken }: WebSocketProviderProps) {
|
||||
const [client, setClient] = useState<WebSocketClient | null>(null);
|
||||
const [isConnected, setIsConnected] = useState(false);
|
||||
const [connectionStatus, setConnectionStatus] = useState<
|
||||
'connecting' | 'connected' | 'disconnected' | 'error'
|
||||
>('disconnected');
|
||||
const [connectionError, setConnectionError] = useState<string | null>(null);
|
||||
|
||||
const connect = useCallback(
|
||||
async (token: string) => {
|
||||
if (client?.isConnected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (client) {
|
||||
client.disconnect();
|
||||
}
|
||||
|
||||
const newClient = new WebSocketClient(token, websocketOptions.url);
|
||||
|
||||
const statusHandler: ConnectionStatusHandler = (status, error) => {
|
||||
setConnectionStatus(status);
|
||||
setIsConnected(status === 'connected');
|
||||
setConnectionError(error || null);
|
||||
};
|
||||
|
||||
newClient.addConnectionStatusHandler(statusHandler);
|
||||
|
||||
try {
|
||||
await newClient.connect();
|
||||
setClient(newClient);
|
||||
} catch (error) {
|
||||
console.error('Failed to connect WebSocket:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[client]
|
||||
);
|
||||
|
||||
const disconnect = useCallback(() => {
|
||||
if (client) {
|
||||
client.disconnect();
|
||||
setClient(null);
|
||||
setIsConnected(false);
|
||||
setConnectionStatus('disconnected');
|
||||
setConnectionError(null);
|
||||
}
|
||||
}, [client]);
|
||||
|
||||
const reconnect = useCallback(async () => {
|
||||
if (client) {
|
||||
try {
|
||||
await client.reconnect();
|
||||
} catch (error) {
|
||||
console.error('Failed to reconnect WebSocket:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}, [client]);
|
||||
const hasInitialized = useRef(false);
|
||||
|
||||
const associateWithServer = useCallback(
|
||||
(serverId: string) => {
|
||||
if (openToken && !isConnected && !hasInitialized.current) {
|
||||
hasInitialized.current = true;
|
||||
connect(openToken).catch((error) => {
|
||||
console.error('Failed to connect WebSocket:', error);
|
||||
hasInitialized.current = false;
|
||||
});
|
||||
}
|
||||
if (client && isConnected) {
|
||||
client.associateWithServer(serverId);
|
||||
}
|
||||
},
|
||||
[client, isConnected, connect]
|
||||
);
|
||||
|
||||
const addMessageHandler = useCallback(
|
||||
(handler: MessageHandler) => {
|
||||
if (client) {
|
||||
client.addMessageHandler(handler);
|
||||
}
|
||||
},
|
||||
[client]
|
||||
);
|
||||
|
||||
const removeMessageHandler = useCallback(
|
||||
(handler: MessageHandler) => {
|
||||
if (client) {
|
||||
client.removeMessageHandler(handler);
|
||||
}
|
||||
},
|
||||
[client]
|
||||
);
|
||||
|
||||
const addConnectionStatusHandler = useCallback(
|
||||
(handler: ConnectionStatusHandler) => {
|
||||
if (client) {
|
||||
client.addConnectionStatusHandler(handler);
|
||||
}
|
||||
},
|
||||
[client]
|
||||
);
|
||||
|
||||
const removeConnectionStatusHandler = useCallback(
|
||||
(handler: ConnectionStatusHandler) => {
|
||||
if (client) {
|
||||
client.removeConnectionStatusHandler(handler);
|
||||
}
|
||||
},
|
||||
[client]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
disconnect();
|
||||
};
|
||||
}, [disconnect]);
|
||||
|
||||
return (
|
||||
<WebSocketContext.Provider
|
||||
value={{
|
||||
client,
|
||||
isConnected,
|
||||
connectionStatus,
|
||||
connectionError,
|
||||
connect,
|
||||
disconnect,
|
||||
reconnect,
|
||||
associateWithServer,
|
||||
addMessageHandler,
|
||||
removeMessageHandler,
|
||||
addConnectionStatusHandler,
|
||||
removeConnectionStatusHandler
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</WebSocketContext.Provider>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user