mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-24 20:58:46 +00:00
refactor: share log container selection
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { resolveContainerSelection } from "@/components/dashboard/application/logs/container-selection";
|
import { resolveContainerSelection } from "@/components/dashboard/docker/logs/utils";
|
||||||
|
|
||||||
const containers = [
|
const containers = [
|
||||||
{ containerId: "first-container" },
|
{ containerId: "first-container" },
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
interface ContainerOption {
|
|
||||||
containerId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const resolveContainerSelection = (
|
|
||||||
currentContainerId: string | undefined,
|
|
||||||
containers: readonly ContainerOption[] | undefined,
|
|
||||||
) => {
|
|
||||||
if (!containers) {
|
|
||||||
return currentContainerId;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
currentContainerId &&
|
|
||||||
containers.some(({ containerId }) => containerId === currentContainerId)
|
|
||||||
) {
|
|
||||||
return currentContainerId;
|
|
||||||
}
|
|
||||||
|
|
||||||
return containers[0]?.containerId;
|
|
||||||
};
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Loader2 } from "lucide-react";
|
import { Loader2 } from "lucide-react";
|
||||||
import dynamic from "next/dynamic";
|
import dynamic from "next/dynamic";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
import { resolveContainerSelection } from "@/components/dashboard/docker/logs/utils";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
import {
|
import {
|
||||||
Card,
|
Card,
|
||||||
@@ -21,7 +22,6 @@ import {
|
|||||||
} from "@/components/ui/select";
|
} from "@/components/ui/select";
|
||||||
import { Switch } from "@/components/ui/switch";
|
import { Switch } from "@/components/ui/switch";
|
||||||
import { api } from "@/utils/api";
|
import { api } from "@/utils/api";
|
||||||
import { resolveContainerSelection } from "./container-selection";
|
|
||||||
export const DockerLogs = dynamic(
|
export const DockerLogs = dynamic(
|
||||||
() =>
|
() =>
|
||||||
import("@/components/dashboard/docker/logs/docker-logs-id").then(
|
import("@/components/dashboard/docker/logs/docker-logs-id").then(
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { Loader2 } from "lucide-react";
|
|||||||
import dynamic from "next/dynamic";
|
import dynamic from "next/dynamic";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { badgeStateColor } from "@/components/dashboard/application/logs/show";
|
import { badgeStateColor } from "@/components/dashboard/application/logs/show";
|
||||||
|
import { resolveContainerSelection } from "@/components/dashboard/docker/logs/utils";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
import {
|
import {
|
||||||
Card,
|
Card,
|
||||||
@@ -70,22 +71,13 @@ export const ShowDockerLogsStack = ({
|
|||||||
);
|
);
|
||||||
|
|
||||||
const containers = data?.filter((container) => container.containerId);
|
const containers = data?.filter((container) => container.containerId);
|
||||||
|
const availableContainers = option === "native" ? containers : services;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const currentContainers = option === "native" ? containers : services;
|
setContainerId((currentContainerId) =>
|
||||||
|
resolveContainerSelection(currentContainerId, availableContainers),
|
||||||
if (currentContainers) {
|
);
|
||||||
const nextContainerId = currentContainers.some(
|
}, [availableContainers]);
|
||||||
(container) => container.containerId === containerId,
|
|
||||||
)
|
|
||||||
? containerId
|
|
||||||
: currentContainers[0]?.containerId;
|
|
||||||
|
|
||||||
if (nextContainerId !== containerId) {
|
|
||||||
setContainerId(nextContainerId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, [option, services, containers, containerId]);
|
|
||||||
|
|
||||||
const isLoading = option === "native" ? containersLoading : servicesLoading;
|
const isLoading = option === "native" ? containersLoading : servicesLoading;
|
||||||
const containersLength =
|
const containersLength =
|
||||||
|
|||||||
@@ -7,6 +7,28 @@ export interface LogLine {
|
|||||||
message: string;
|
message: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ContainerOption {
|
||||||
|
containerId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const resolveContainerSelection = (
|
||||||
|
currentContainerId: string | undefined,
|
||||||
|
containers: readonly ContainerOption[] | undefined,
|
||||||
|
) => {
|
||||||
|
if (!containers) {
|
||||||
|
return currentContainerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
currentContainerId &&
|
||||||
|
containers.some(({ containerId }) => containerId === currentContainerId)
|
||||||
|
) {
|
||||||
|
return currentContainerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
return containers[0]?.containerId;
|
||||||
|
};
|
||||||
|
|
||||||
interface LogStyle {
|
interface LogStyle {
|
||||||
type: LogType;
|
type: LogType;
|
||||||
variant: LogVariant;
|
variant: LogVariant;
|
||||||
|
|||||||
Reference in New Issue
Block a user