- {server.sessions?.[0]?.status === "started" ? (
+ {server.sessions?.[0]?.status === "starting" ? (
+
- {pointedProject?.id === project.id ? (
+ {pointedProject?.name === project.name ? (
diff --git a/src/components/modals/CreateSessionModal.tsx b/src/components/modals/CreateSessionModal.tsx
index 17c9b23..6f0a9b8 100644
--- a/src/components/modals/CreateSessionModal.tsx
+++ b/src/components/modals/CreateSessionModal.tsx
@@ -1,5 +1,5 @@
import { IServer } from "../../types/IServer.ts";
-import { useRef, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { IApp } from "../../types/IApp.ts";
import api from "../../utils/api.ts";
import { ISession } from "../../types/ISession.ts";
@@ -22,11 +22,13 @@ export default function CreateSessionModal({ targetServerId }: Props) {
const [phone, setPhone] = useState("");
const [email, setEmail] = useState("");
const [isSessionExists, setIsSessionExists] = useState(false);
+
const queryClient = useQueryClient();
const { data: servers } = useQuery({
queryKey: ["servers"],
queryFn: () => api.get("servers?withLastSession=true").json(),
+ refetchInterval: 1000,
});
const targetServer = targetServerId
@@ -38,6 +40,12 @@ export default function CreateSessionModal({ targetServerId }: Props) {
);
const [selectedApp, setSelectedApp] = useState(null);
+ useEffect(() => {
+ setSelectedApp(
+ selectedServer?.sessions?.[0]?.app || selectedServer?.apps?.[0] || null
+ );
+ }, [selectedServer]);
+
const { mutate: createClient } = useMutation({
mutationFn: () => {
return api
@@ -53,20 +61,27 @@ export default function CreateSessionModal({ targetServerId }: Props) {
});
const { mutate: createSession } = useMutation({
- mutationFn: (clientId: string) => {
- return api
+ mutationKey: ["create-session", selectedServer?.id],
+ mutationFn: ({
+ clientId,
+ serverId,
+ appId,
+ }: {
+ clientId: string;
+ serverId: string;
+ appId: string;
+ }) =>
+ api
.post("sessions", {
json: {
clientId,
- serverId: selectedServer?.id,
- appId: selectedApp?.id,
+ serverId,
+ appId,
},
})
- .json();
- },
+ .json(),
onMutate: () => {
queryClient.invalidateQueries({ queryKey: ["sessions"] });
- queryClient.invalidateQueries({ queryKey: ["last-started"] });
queryClient.invalidateQueries({ queryKey: ["servers"] });
setModal(null);
},
@@ -86,40 +101,43 @@ export default function CreateSessionModal({ targetServerId }: Props) {
if (!name || !phone || !selectedServer || !selectedApp) return;
- if (
- selectedServer?.sessions?.[0]?.status === "started" &&
- !isSessionExists
- ) {
+ 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;
}
- if (isSessionExists) {
- endSession(undefined, {
- onSuccess: () => {
- createClient(undefined, {
- onSuccess: (client) => {
- createSession(client.id);
- },
- onError: (error) => {
- console.log(error);
- },
- });
- },
- onError: (error) => {
- console.log("Ошибка при завершении сессии:", error);
- },
- });
- } else {
- createClient(undefined, {
- onSuccess: (client) => {
- createSession(client.id);
- },
- onError: (error) => {
- console.log(error);
- },
- });
- }
+ endSession(undefined, {
+ onSuccess: () => {
+ createClient(undefined, {
+ onSuccess: (client) => {
+ createSession({
+ clientId: client.id,
+ serverId: selectedServer.id,
+ appId: selectedApp.id,
+ });
+ },
+ onError: (error) => {
+ console.log(error);
+ },
+ });
+ },
+ onError: (error) => {
+ console.log("Ошибка при завершении сессии:", error);
+ },
+ });
}
const ref = useRef(null);
@@ -165,13 +183,20 @@ export default function CreateSessionModal({ targetServerId }: Props) {
Выберите параметры сеанса
- {selectedServer?.apps && selectedServer?.apps?.length > 0 && (
-
- )}
+ {selectedServer &&
+ selectedServer?.apps &&
+ selectedServer?.apps?.length > 0 && (
+
+ )}
{isSessionExists && (
diff --git a/src/components/modals/CurrentSessionModal.tsx b/src/components/modals/CurrentSessionModal.tsx
index 90d44b7..1d83d0a 100644
--- a/src/components/modals/CurrentSessionModal.tsx
+++ b/src/components/modals/CurrentSessionModal.tsx
@@ -1,35 +1,25 @@
import { intervalToDuration } from "date-fns";
-import { IServer } from "../../types/IServer";
import FlashIcon from "../icons/FlashIcon";
import NewButton from "../NewButton";
import ChevronRightIcon from "../icons/ChevronRightIcon";
-import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
+import { useMutation, useQueryClient } from "@tanstack/react-query";
import api from "../../utils/api";
import useModalStore from "../../stores/useModalStore";
import { ISession } from "../../types/ISession";
import { useEffect, useState } from "react";
-function CurrentSessionModal({ server }: { server: IServer }) {
+function CurrentSessionModal({ session }: { session: ISession }) {
const queryClient = useQueryClient();
const { setModal } = useModalStore();
const { mutate: endSession } = useMutation({
- mutationKey: ["sessions", server.sessions?.[0]?.id],
+ mutationKey: ["sessions", session.id],
mutationFn: () =>
- api.put(`sessions/${server.sessions?.[0]?.id}`, {
+ api.put(`sessions/${session.id}`, {
json: { status: "ending" },
}),
onMutate: () => queryClient.invalidateQueries({ queryKey: ["sessions"] }),
});
-
- const { data: currentSession } = useQuery({
- queryKey: ["sessions", server.sessions?.[0]?.id],
- queryFn: () =>
- api.get(`sessions/${server.sessions?.[0]?.id}`).json
(),
- });
-
- console.log(currentSession);
-
const [now, setNow] = useState(Date.now());
useEffect(() => {
@@ -39,19 +29,7 @@ function CurrentSessionModal({ server }: { server: IServer }) {
return () => clearInterval(interval);
}, []);
- useEffect(() => {
- if (!currentSession) return;
- const duration = intervalToDuration({
- start: currentSession.createdAt,
- end: now,
- });
- const hours = (duration.hours || 0).toString().padStart(2, "0");
- const minutes = (duration.minutes || 0).toString().padStart(2, "0");
- const seconds = (duration.seconds || 0).toString().padStart(2, "0");
- console.log(`${hours}:${minutes}:${seconds}`);
- }, [currentSession, now]);
-
- if (!currentSession) return null;
+ if (!session) return null;
return (
@@ -63,13 +41,15 @@ function CurrentSessionModal({ server }: { server: IServer }) {
-
{server.name}
+
{session.server.name}
-
+
+
+
Сеанс идёт{" "}
{(() => {
const duration = intervalToDuration({
- start: currentSession.createdAt,
+ start: session.createdAt,
end: now,
});
const hours = (duration.hours || 0)
@@ -87,7 +67,7 @@ function CurrentSessionModal({ server }: { server: IServer }) {
- {server.location}
+ {session.server.location}
@@ -98,12 +78,10 @@ function CurrentSessionModal({ server }: { server: IServer }) {
Клиент
-
- {currentSession.client.name}
-
+
{session.client.name}
- {!currentSession.client.email && (
+ {!session.client.email && (
Добавьте email
@@ -120,9 +98,7 @@ function CurrentSessionModal({ server }: { server: IServer }) {
Менеджер:
-
- {currentSession.owner.fullname}
-
+
{session.owner.fullname}