mirror of
https://github.com/andwati/dokploy.git
synced 2026-12-03 16:28:46 +00:00
refactor: resolved type errors in advance-breadcrumb.ts
This commit is contained in:
@@ -9,7 +9,7 @@ import {
|
|||||||
X,
|
X,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import { useEffect, useState } from "react";
|
import { type ComponentType, useEffect, useMemo, useState } from "react";
|
||||||
import {
|
import {
|
||||||
MariadbIcon,
|
MariadbIcon,
|
||||||
MongodbIcon,
|
MongodbIcon,
|
||||||
@@ -34,128 +34,145 @@ import {
|
|||||||
import { ScrollArea } from "@/components/ui/scroll-area";
|
import { ScrollArea } from "@/components/ui/scroll-area";
|
||||||
import { Separator } from "@/components/ui/separator";
|
import { Separator } from "@/components/ui/separator";
|
||||||
import { SidebarTrigger } from "@/components/ui/sidebar";
|
import { SidebarTrigger } from "@/components/ui/sidebar";
|
||||||
import { api } from "@/utils/api";
|
import { api, type RouterOutputs } from "@/utils/api";
|
||||||
|
|
||||||
type AdvanceBreadcrumbProps = {};
|
type ProjectItem = RouterOutputs["project"]["all"][number];
|
||||||
|
type ProjectEnvironment = ProjectItem["environments"][number];
|
||||||
|
type EnvironmentDetails = RouterOutputs["environment"]["one"];
|
||||||
|
|
||||||
const getServiceIcon = (type: ServiceType, className = "size-4") => {
|
type ServiceItem = {
|
||||||
const icons: Record<ServiceType, React.ReactNode> = {
|
|
||||||
application: <GlobeIcon className={className} />,
|
|
||||||
compose: <CircuitBoard className={className} />,
|
|
||||||
postgres: <PostgresqlIcon className={className} />,
|
|
||||||
mysql: <MysqlIcon className={className} />,
|
|
||||||
mariadb: <MariadbIcon className={className} />,
|
|
||||||
redis: <RedisIcon className={className} />,
|
|
||||||
mongo: <MongodbIcon className={className} />,
|
|
||||||
};
|
|
||||||
|
|
||||||
return icons[type];
|
|
||||||
};
|
|
||||||
|
|
||||||
interface ServiceItem {
|
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
type: ServiceType;
|
type: ServiceType;
|
||||||
appName?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface EnvironmentData {
|
|
||||||
applications?: Array<{
|
|
||||||
applicationId: string;
|
|
||||||
name: string;
|
|
||||||
appName: string;
|
|
||||||
}>;
|
|
||||||
compose?: Array<{ composeId: string; name: string; appName: string }>;
|
|
||||||
postgres?: Array<{ postgresId: string; name: string; appName: string }>;
|
|
||||||
mysql?: Array<{ mysqlId: string; name: string; appName: string }>;
|
|
||||||
mariadb?: Array<{ mariadbId: string; name: string; appName: string }>;
|
|
||||||
redis?: Array<{ redisId: string; name: string; appName: string }>;
|
|
||||||
mongo?: Array<{ mongoId: string; name: string; appName: string }>;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper function to count total services in an environment
|
|
||||||
const countEnvironmentServices = (env: EnvironmentData): number => {
|
|
||||||
return (
|
|
||||||
(env.applications?.length || 0) +
|
|
||||||
(env.compose?.length || 0) +
|
|
||||||
(env.postgres?.length || 0) +
|
|
||||||
(env.mysql?.length || 0) +
|
|
||||||
(env.mariadb?.length || 0) +
|
|
||||||
(env.redis?.length || 0) +
|
|
||||||
(env.mongo?.length || 0)
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helper function to extract services from an environment into a flat array
|
type NamedService = {
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type EnvironmentServiceCollections = {
|
||||||
|
applications: (NamedService & { applicationId: string })[];
|
||||||
|
compose: (NamedService & { composeId: string })[];
|
||||||
|
postgres: (NamedService & { postgresId: string })[];
|
||||||
|
mysql: (NamedService & { mysqlId: string })[];
|
||||||
|
mariadb: (NamedService & { mariadbId: string })[];
|
||||||
|
redis: (NamedService & { redisId: string })[];
|
||||||
|
mongo: (NamedService & { mongoId: string })[];
|
||||||
|
};
|
||||||
|
|
||||||
|
type ServiceCollections = Pick<
|
||||||
|
ProjectEnvironment,
|
||||||
|
| "applications"
|
||||||
|
| "compose"
|
||||||
|
| "postgres"
|
||||||
|
| "mysql"
|
||||||
|
| "mariadb"
|
||||||
|
| "redis"
|
||||||
|
| "mongo"
|
||||||
|
>;
|
||||||
|
|
||||||
|
const SERVICE_COLLECTION_KEYS = [
|
||||||
|
"applications",
|
||||||
|
"compose",
|
||||||
|
"postgres",
|
||||||
|
"mysql",
|
||||||
|
"mariadb",
|
||||||
|
"redis",
|
||||||
|
"mongo",
|
||||||
|
] as const satisfies ReadonlyArray<keyof ServiceCollections>;
|
||||||
|
|
||||||
|
const SERVICE_QUERY_KEYS = [
|
||||||
|
"applicationId",
|
||||||
|
"composeId",
|
||||||
|
"postgresId",
|
||||||
|
"mysqlId",
|
||||||
|
"mariadbId",
|
||||||
|
"redisId",
|
||||||
|
"mongoId",
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
const SERVICE_ICONS: Record<
|
||||||
|
ServiceType,
|
||||||
|
ComponentType<{ className?: string }>
|
||||||
|
> = {
|
||||||
|
application: GlobeIcon,
|
||||||
|
compose: CircuitBoard,
|
||||||
|
postgres: PostgresqlIcon,
|
||||||
|
mysql: MysqlIcon,
|
||||||
|
mariadb: MariadbIcon,
|
||||||
|
redis: RedisIcon,
|
||||||
|
mongo: MongodbIcon,
|
||||||
|
};
|
||||||
|
|
||||||
|
const getStringQueryParam = (value: string | string[] | undefined) =>
|
||||||
|
typeof value === "string" ? value : null;
|
||||||
|
|
||||||
|
const includesSearch = (value: string | null | undefined, search: string) =>
|
||||||
|
value?.toLowerCase().includes(search.toLowerCase()) ?? false;
|
||||||
|
|
||||||
|
const getServiceIcon = (type: ServiceType, className = "size-4") => {
|
||||||
|
const Icon = SERVICE_ICONS[type];
|
||||||
|
return <Icon className={className} />;
|
||||||
|
};
|
||||||
|
|
||||||
|
const countEnvironmentServices = (environment: ServiceCollections): number =>
|
||||||
|
SERVICE_COLLECTION_KEYS.reduce(
|
||||||
|
(total, key) => total + environment[key].length,
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
|
||||||
|
const mapServices = <T extends { name: string }>(
|
||||||
|
items: readonly T[],
|
||||||
|
getId: (item: T) => string,
|
||||||
|
type: ServiceType,
|
||||||
|
): ServiceItem[] =>
|
||||||
|
items.map((item) => ({
|
||||||
|
id: getId(item),
|
||||||
|
name: item.name,
|
||||||
|
type,
|
||||||
|
}));
|
||||||
|
|
||||||
const extractServicesFromEnvironment = (
|
const extractServicesFromEnvironment = (
|
||||||
env: EnvironmentData,
|
environment: EnvironmentDetails | null | undefined,
|
||||||
): ServiceItem[] => {
|
): ServiceItem[] => {
|
||||||
const services: ServiceItem[] = [];
|
if (!environment) return [];
|
||||||
|
|
||||||
env.applications?.forEach((app) => {
|
const servicesByType =
|
||||||
services.push({
|
environment as unknown as EnvironmentServiceCollections;
|
||||||
id: app.applicationId,
|
|
||||||
name: app.name,
|
|
||||||
type: "application",
|
|
||||||
appName: app.appName,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
env.compose?.forEach((comp) => {
|
return [
|
||||||
services.push({
|
...mapServices(
|
||||||
id: comp.composeId,
|
servicesByType.applications,
|
||||||
name: comp.name,
|
(item) => item.applicationId,
|
||||||
type: "compose",
|
"application",
|
||||||
appName: comp.appName,
|
),
|
||||||
});
|
...mapServices(servicesByType.compose, (item) => item.composeId, "compose"),
|
||||||
});
|
...mapServices(
|
||||||
|
servicesByType.postgres,
|
||||||
|
(item) => item.postgresId,
|
||||||
|
"postgres",
|
||||||
|
),
|
||||||
|
...mapServices(servicesByType.mysql, (item) => item.mysqlId, "mysql"),
|
||||||
|
...mapServices(servicesByType.mariadb, (item) => item.mariadbId, "mariadb"),
|
||||||
|
...mapServices(servicesByType.redis, (item) => item.redisId, "redis"),
|
||||||
|
...mapServices(servicesByType.mongo, (item) => item.mongoId, "mongo"),
|
||||||
|
];
|
||||||
|
};
|
||||||
|
|
||||||
env.postgres?.forEach((pg) => {
|
const getTargetEnvironmentId = (
|
||||||
services.push({
|
project: ProjectItem,
|
||||||
id: pg.postgresId,
|
selectedEnvironmentId?: string,
|
||||||
name: pg.name,
|
) => {
|
||||||
type: "postgres",
|
if (selectedEnvironmentId) return selectedEnvironmentId;
|
||||||
appName: pg.appName,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
env.mysql?.forEach((my) => {
|
const productionEnvironment = project.environments.find(
|
||||||
services.push({
|
(environment) => environment.name === "production",
|
||||||
id: my.mysqlId,
|
);
|
||||||
name: my.name,
|
|
||||||
type: "mysql",
|
|
||||||
appName: my.appName,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
env.mariadb?.forEach((maria) => {
|
return (
|
||||||
services.push({
|
productionEnvironment?.environmentId ??
|
||||||
id: maria.mariadbId,
|
project.environments[0]?.environmentId
|
||||||
name: maria.name,
|
);
|
||||||
type: "mariadb",
|
|
||||||
appName: maria.appName,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
env.redis?.forEach((red) => {
|
|
||||||
services.push({
|
|
||||||
id: red.redisId,
|
|
||||||
name: red.name,
|
|
||||||
type: "redis",
|
|
||||||
appName: red.appName,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
env.mongo?.forEach((mon) => {
|
|
||||||
services.push({
|
|
||||||
id: mon.mongoId,
|
|
||||||
name: mon.name,
|
|
||||||
type: "mongo",
|
|
||||||
appName: mon.appName,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return services;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const AdvanceBreadcrumb = () => {
|
export const AdvanceBreadcrumb = () => {
|
||||||
@@ -163,19 +180,12 @@ export const AdvanceBreadcrumb = () => {
|
|||||||
const { query } = router;
|
const { query } = router;
|
||||||
|
|
||||||
// Read IDs from URL (dynamic route segments)
|
// Read IDs from URL (dynamic route segments)
|
||||||
const projectId =
|
const projectId = getStringQueryParam(query.projectId);
|
||||||
typeof query.projectId === "string" ? query.projectId : null;
|
const environmentId = getStringQueryParam(query.environmentId);
|
||||||
const environmentId =
|
|
||||||
typeof query.environmentId === "string" ? query.environmentId : null;
|
|
||||||
const serviceId =
|
const serviceId =
|
||||||
(typeof query.applicationId === "string" ? query.applicationId : null) ??
|
SERVICE_QUERY_KEYS.map((key) => getStringQueryParam(query[key])).find(
|
||||||
(typeof query.composeId === "string" ? query.composeId : null) ??
|
(value): value is string => !!value,
|
||||||
(typeof query.postgresId === "string" ? query.postgresId : null) ??
|
) ?? null;
|
||||||
(typeof query.mysqlId === "string" ? query.mysqlId : null) ??
|
|
||||||
(typeof query.mariadbId === "string" ? query.mariadbId : null) ??
|
|
||||||
(typeof query.redisId === "string" ? query.redisId : null) ??
|
|
||||||
(typeof query.mongoId === "string" ? query.mongoId : null) ??
|
|
||||||
null;
|
|
||||||
|
|
||||||
const [projectOpen, setProjectOpen] = useState(false);
|
const [projectOpen, setProjectOpen] = useState(false);
|
||||||
const [serviceOpen, setServiceOpen] = useState(false);
|
const [serviceOpen, setServiceOpen] = useState(false);
|
||||||
@@ -221,13 +231,15 @@ export const AdvanceBreadcrumb = () => {
|
|||||||
return () => window.removeEventListener("keydown", handleKeyDown);
|
return () => window.removeEventListener("keydown", handleKeyDown);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Extract services from current environment
|
const services = useMemo(
|
||||||
const services: ServiceItem[] = currentEnvironment
|
() => extractServicesFromEnvironment(currentEnvironment),
|
||||||
? extractServicesFromEnvironment(currentEnvironment)
|
[currentEnvironment],
|
||||||
: [];
|
);
|
||||||
|
|
||||||
// Get current service
|
const currentService = useMemo(
|
||||||
const currentService = services.find((s) => s.id === serviceId);
|
() => services.find((service) => service.id === serviceId),
|
||||||
|
[serviceId, services],
|
||||||
|
);
|
||||||
|
|
||||||
// Navigate to project's default environment
|
// Navigate to project's default environment
|
||||||
const handleProjectSelect = (
|
const handleProjectSelect = (
|
||||||
@@ -235,18 +247,15 @@ export const AdvanceBreadcrumb = () => {
|
|||||||
selectedEnvironmentId?: string,
|
selectedEnvironmentId?: string,
|
||||||
) => {
|
) => {
|
||||||
const project = allProjects?.find((p) => p.projectId === selectedProjectId);
|
const project = allProjects?.find((p) => p.projectId === selectedProjectId);
|
||||||
if (project && project.environments.length > 0) {
|
if (project) {
|
||||||
// Use provided environment or find production environment or use the first one
|
const targetEnvironmentId = getTargetEnvironmentId(
|
||||||
const firstEnvironment = project.environments[0];
|
project,
|
||||||
const targetEnvId =
|
selectedEnvironmentId,
|
||||||
selectedEnvironmentId ||
|
);
|
||||||
project.environments.find((e) => e.name === "production")
|
|
||||||
?.environmentId ||
|
|
||||||
firstEnvironment?.environmentId;
|
|
||||||
|
|
||||||
if (targetEnvId) {
|
if (targetEnvironmentId) {
|
||||||
router.push(
|
router.push(
|
||||||
`/dashboard/project/${selectedProjectId}/environment/${targetEnvId}`,
|
`/dashboard/project/${selectedProjectId}/environment/${targetEnvironmentId}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -262,34 +271,37 @@ export const AdvanceBreadcrumb = () => {
|
|||||||
|
|
||||||
// Navigate to service
|
// Navigate to service
|
||||||
const handleServiceSelect = (service: ServiceItem) => {
|
const handleServiceSelect = (service: ServiceItem) => {
|
||||||
const serviceTypePath =
|
if (!environmentId) return;
|
||||||
service.type === "application" ? "application" : service.type;
|
|
||||||
router.push(
|
router.push(
|
||||||
`/dashboard/project/${projectId}/environment/${environmentId}/services/${serviceTypePath}/${service.id}`,
|
`/dashboard/project/${projectId}/environment/${environmentId}/services/${service.type}/${service.id}`,
|
||||||
);
|
);
|
||||||
setServiceOpen(false);
|
setServiceOpen(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Filter projects based on search
|
const filteredProjects = useMemo(
|
||||||
const filteredProjects =
|
() =>
|
||||||
allProjects?.filter(
|
(allProjects ?? []).filter(
|
||||||
(p) =>
|
(project) =>
|
||||||
p.name.toLowerCase().includes(projectSearch.toLowerCase()) ||
|
includesSearch(project.name, projectSearch) ||
|
||||||
p.description?.toLowerCase().includes(projectSearch.toLowerCase()),
|
includesSearch(project.description, projectSearch),
|
||||||
) || [];
|
),
|
||||||
|
[allProjects, projectSearch],
|
||||||
// Filter services based on search
|
|
||||||
const filteredServices = services.filter(
|
|
||||||
(s) =>
|
|
||||||
s.name.toLowerCase().includes(serviceSearch.toLowerCase()) ||
|
|
||||||
s.appName?.toLowerCase().includes(serviceSearch.toLowerCase()),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Filter environments based on search
|
const filteredServices = useMemo(
|
||||||
const filteredEnvironments =
|
() =>
|
||||||
projectEnvironments?.filter((env) =>
|
services.filter((service) => includesSearch(service.name, serviceSearch)),
|
||||||
env.name.toLowerCase().includes(environmentSearch.toLowerCase()),
|
[serviceSearch, services],
|
||||||
) ?? [];
|
);
|
||||||
|
|
||||||
|
const filteredEnvironments = useMemo(
|
||||||
|
() =>
|
||||||
|
(projectEnvironments ?? []).filter((environment) =>
|
||||||
|
includesSearch(environment.name, environmentSearch),
|
||||||
|
),
|
||||||
|
[environmentSearch, projectEnvironments],
|
||||||
|
);
|
||||||
|
|
||||||
// If we're just on the projects page, show simple breadcrumb
|
// If we're just on the projects page, show simple breadcrumb
|
||||||
if (!projectId) {
|
if (!projectId) {
|
||||||
|
|||||||
Reference in New Issue
Block a user