refactor: rename apps to appsToServers in Server type and update CreateSessionModal to reflect changes in session handling and state management

This commit is contained in:
2025-06-23 18:13:00 +05:00
parent 3f11fcd829
commit 4c2369ce88
2 changed files with 68 additions and 64 deletions
+67 -63
View File
@@ -24,7 +24,7 @@ export default function CreateSessionModal({ targetServerId, client }: Props) {
const [name, setName] = useState<string | null>(client?.name || null);
const [phone, setPhone] = useState<string | null>(client?.phone || null);
const [email, setEmail] = useState<string | null>(client?.email || null);
// const [isSessionExists, setIsSessionExists] = useState(false);
const [isSessionExists, setIsSessionExists] = useState(false);
const queryClient = useQueryClient();
@@ -46,7 +46,7 @@ export default function CreateSessionModal({ targetServerId, client }: Props) {
useEffect(() => {
setSelectedApp(
selectedServer?.sessions?.[0]?.app ||
selectedServer?.apps?.[0].app ||
selectedServer?.appsToServers?.[0]?.app ||
null
);
}, [selectedServer]);
@@ -114,70 +114,74 @@ export default function CreateSessionModal({ targetServerId, client }: Props) {
},
});
// const { mutate: endSession } = useMutation({
// mutationKey: ["end-session", selectedServer?.sessions?.[0]?.id],
// mutationFn: () =>
// api.put(`sessions/${selectedServer?.sessions?.[0]?.id}`, {
// json: { status: "ending" },
// }),
// onMutate: () => queryClient.invalidateQueries({ queryKey: ["sessions"] }),
// });
const { mutate: endSession } = useMutation({
mutationKey: ["end-session", selectedServer?.sessions?.[0]?.id],
mutationFn: () =>
api.put(`sessions/${selectedServer?.sessions?.[0]?.id}`, {
json: { status: "ending" },
}),
onMutate: () => queryClient.invalidateQueries({ queryKey: ["sessions"] }),
});
async function handleClickCreateSession(e: React.FormEvent<HTMLFormElement>) {
e.preventDefault();
if (!name || !phone || !selectedServer || !selectedApp) return;
// if (selectedServer?.sessions?.[0]?.status !== "started") {
createClient(undefined, {
onSuccess: (client) => {
createSession({
clientId: client.id,
serverId: selectedServer.id,
appId: selectedApp.id,
});
if (selectedServer?.sessions?.[0]?.status !== "started") {
createClient(undefined, {
onSuccess: (client) => {
createSession({
clientId: client.id,
serverId: selectedServer.id,
appId: selectedApp.id,
});
},
});
return;
}
if (!isSessionExists) {
setIsSessionExists(true);
return;
}
endSession(undefined, {
onError: (error) => {
console.log("Ошибка при завершении сессии:", error);
},
});
// return;
// }
// if (!isSessionExists) {
// setIsSessionExists(true);
// return;
// }
// endSession(undefined, {
// onError: (error) => {
// console.log("Ошибка при завершении сессии:", error);
// },
// });
}
// useEffect(() => {
// if (
// selectedServer &&
// servers?.find((server) => server.id === selectedServer?.id)?.sessions?.[0]
// ?.status === "ended" &&
// selectedApp
// // && isSessionExists
// )
// createClient(undefined, {
// onSuccess: (client) => {
// createSession({
// clientId: client.id,
// serverId: selectedServer?.id,
// appId: selectedApp.id,
// });
// },
// });
// }, [
// selectedApp,
// servers,
// createClient,
// createSession,
// // isSessionExists,
// selectedServer,
// ]);
useEffect(() => {
if (
selectedServer &&
servers?.find((server) => server.id === selectedServer?.id)?.sessions?.[0]
?.status === "ended" &&
selectedApp &&
isSessionExists
)
createClient(undefined, {
onSuccess: (client) => {
createSession({
clientId: client.id,
serverId: selectedServer?.id,
appId: selectedApp.id,
});
},
});
}, [
selectedApp,
servers,
createClient,
createSession,
isSessionExists,
selectedServer,
]);
useEffect(() => {
console.log(selectedServer);
}, [selectedServer]);
const ref = useRef<HTMLFormElement>(null);
@@ -244,22 +248,22 @@ export default function CreateSessionModal({ targetServerId, client }: Props) {
<div className="flex flex-col gap-y-[0.833vw]">
<p className="title-s font-medium">Выберите параметры сеанса</p>
{selectedServer &&
selectedServer?.apps &&
selectedServer?.apps?.length > 0 && (
selectedServer?.appsToServers &&
selectedServer.appsToServers?.length > 0 && (
<ProjectSelector
activeProject={
selectedServer?.sessions?.[0]?.status === "started"
? selectedApp
: null
}
projects={selectedServer?.apps.map(({ app }) => app)}
projects={selectedServer?.appsToServers.map(({ app }) => app)}
selectedProject={selectedApp}
setSelectedProject={setSelectedApp}
/>
)}
</div>
{/* {isSessionExists && ( */}
{/* <div className="absolute inset-0 top-[11.806vw] bg-[#FFFFFF] flex flex-col gap-[1.111vw] items-center justify-center h-[31.458vw]">
{isSessionExists && (
<div className="absolute inset-0 top-[11.806vw] bg-[#FFFFFF] flex flex-col gap-[1.111vw] items-center justify-center h-[31.458vw]">
<img
src="/images/ghost.png"
alt="ghost error"
@@ -272,8 +276,8 @@ export default function CreateSessionModal({ targetServerId, client }: Props) {
При запуске нового текущий будет завершен.`}
</p>
</div>
</div> */}
{/* )} */}
</div>
)}
<div className="flex-1 flex flex-col justify-end">
<Button
type="submit"
+1 -1
View File
@@ -8,7 +8,7 @@ export interface Server {
description: string;
companyId: string;
sessions?: Session[];
apps?: { app: App }[];
appsToServers?: { app: App }[];
status: "online" | "offline";
ipAddress: string;
}