mirror of
https://github.com/fosrl/pangolin.git
synced 2026-07-06 04:10:13 +00:00
add site column and filter to public resources
This commit is contained in:
@@ -113,6 +113,16 @@ const listResourcesSchema = z.object({
|
|||||||
enum: ["no_targets", "healthy", "degraded", "offline", "unknown"],
|
enum: ["no_targets", "healthy", "degraded", "offline", "unknown"],
|
||||||
description:
|
description:
|
||||||
"Filter resources based on health status of their targets. `healthy` means all targets are healthy. `degraded` means at least one target is unhealthy, but not all are unhealthy. `offline` means all targets are unhealthy. `unknown` means all targets have unknown health status. `no_targets` means the resource has no targets."
|
"Filter resources based on health status of their targets. `healthy` means all targets are healthy. `degraded` means at least one target is unhealthy, but not all are unhealthy. `offline` means all targets are unhealthy. `unknown` means all targets have unknown health status. `no_targets` means the resource has no targets."
|
||||||
|
}),
|
||||||
|
siteId: z.coerce
|
||||||
|
.number<string>()
|
||||||
|
.int()
|
||||||
|
.positive()
|
||||||
|
.optional()
|
||||||
|
.openapi({
|
||||||
|
type: "integer",
|
||||||
|
description:
|
||||||
|
"When set, only resources that have at least one target on this site are returned"
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -141,6 +151,12 @@ export type ResourceWithTargets = {
|
|||||||
healthStatus: "healthy" | "unhealthy" | "unknown" | null;
|
healthStatus: "healthy" | "unhealthy" | "unknown" | null;
|
||||||
siteName: string | null;
|
siteName: string | null;
|
||||||
}>;
|
}>;
|
||||||
|
sites: Array<{
|
||||||
|
siteId: number;
|
||||||
|
siteName: string;
|
||||||
|
siteNiceId: string;
|
||||||
|
online: boolean;
|
||||||
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Aggregate filters
|
// Aggregate filters
|
||||||
@@ -260,7 +276,8 @@ export async function listResources(
|
|||||||
query,
|
query,
|
||||||
healthStatus,
|
healthStatus,
|
||||||
sort_by,
|
sort_by,
|
||||||
order
|
order,
|
||||||
|
siteId
|
||||||
} = parsedQuery.data;
|
} = parsedQuery.data;
|
||||||
|
|
||||||
const parsedParams = listResourcesParamsSchema.safeParse(req.params);
|
const parsedParams = listResourcesParamsSchema.safeParse(req.params);
|
||||||
@@ -380,6 +397,19 @@ export async function listResources(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (siteId != null) {
|
||||||
|
const resourcesWithSite = db
|
||||||
|
.select({ resourceId: targets.resourceId })
|
||||||
|
.from(targets)
|
||||||
|
.innerJoin(sites, eq(targets.siteId, sites.siteId))
|
||||||
|
.where(
|
||||||
|
and(eq(sites.orgId, orgId), eq(sites.siteId, siteId))
|
||||||
|
);
|
||||||
|
conditions.push(
|
||||||
|
inArray(resources.resourceId, resourcesWithSite)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
let aggregateFilters: SQL<any> | undefined = sql`1 = 1`;
|
let aggregateFilters: SQL<any> | undefined = sql`1 = 1`;
|
||||||
|
|
||||||
if (typeof healthStatus !== "undefined") {
|
if (typeof healthStatus !== "undefined") {
|
||||||
@@ -444,12 +474,15 @@ export async function listResources(
|
|||||||
.select({
|
.select({
|
||||||
targetId: targets.targetId,
|
targetId: targets.targetId,
|
||||||
resourceId: targets.resourceId,
|
resourceId: targets.resourceId,
|
||||||
|
siteId: targets.siteId,
|
||||||
ip: targets.ip,
|
ip: targets.ip,
|
||||||
port: targets.port,
|
port: targets.port,
|
||||||
enabled: targets.enabled,
|
enabled: targets.enabled,
|
||||||
healthStatus: targetHealthCheck.hcHealth,
|
healthStatus: targetHealthCheck.hcHealth,
|
||||||
hcEnabled: targetHealthCheck.hcEnabled,
|
hcEnabled: targetHealthCheck.hcEnabled,
|
||||||
siteName: sites.name
|
siteName: sites.name,
|
||||||
|
siteNiceId: sites.niceId,
|
||||||
|
siteOnline: sites.online
|
||||||
})
|
})
|
||||||
.from(targets)
|
.from(targets)
|
||||||
.where(inArray(targets.resourceId, resourceIdList))
|
.where(inArray(targets.resourceId, resourceIdList))
|
||||||
@@ -481,7 +514,8 @@ export async function listResources(
|
|||||||
enabled: row.enabled,
|
enabled: row.enabled,
|
||||||
domainId: row.domainId,
|
domainId: row.domainId,
|
||||||
headerAuthId: row.headerAuthId,
|
headerAuthId: row.headerAuthId,
|
||||||
targets: []
|
targets: [],
|
||||||
|
sites: []
|
||||||
};
|
};
|
||||||
map.set(row.resourceId, entry);
|
map.set(row.resourceId, entry);
|
||||||
}
|
}
|
||||||
@@ -491,6 +525,33 @@ export async function listResources(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const entry of map.values()) {
|
||||||
|
const raw = allResourceTargets.filter(
|
||||||
|
(t) => t.resourceId === entry.resourceId
|
||||||
|
);
|
||||||
|
const siteById = new Map<
|
||||||
|
number,
|
||||||
|
{
|
||||||
|
siteId: number;
|
||||||
|
siteName: string;
|
||||||
|
siteNiceId: string;
|
||||||
|
online: boolean;
|
||||||
|
}
|
||||||
|
>();
|
||||||
|
for (const t of raw) {
|
||||||
|
if (typeof t.siteId !== "number" || siteById.has(t.siteId)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
siteById.set(t.siteId, {
|
||||||
|
siteId: t.siteId,
|
||||||
|
siteName: t.siteName ?? "",
|
||||||
|
siteNiceId: t.siteNiceId ?? "",
|
||||||
|
online: Boolean(t.siteOnline)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
entry.sites = Array.from(siteById.values());
|
||||||
|
}
|
||||||
|
|
||||||
const resourcesList: ResourceWithTargets[] = Array.from(map.values());
|
const resourcesList: ResourceWithTargets[] = Array.from(map.values());
|
||||||
|
|
||||||
return response<ListResourcesResponse>(res, {
|
return response<ListResourcesResponse>(res, {
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ import { authCookieHeader } from "@app/lib/api/cookies";
|
|||||||
import OrgProvider from "@app/providers/OrgProvider";
|
import OrgProvider from "@app/providers/OrgProvider";
|
||||||
import type { GetOrgResponse } from "@server/routers/org";
|
import type { GetOrgResponse } from "@server/routers/org";
|
||||||
import type { ListResourcesResponse } from "@server/routers/resource";
|
import type { ListResourcesResponse } from "@server/routers/resource";
|
||||||
import type { ListAllSiteResourcesByOrgResponse } from "@server/routers/siteResource";
|
import { GetSiteResponse } from "@server/routers/site/getSite";
|
||||||
|
import type ResponseT from "@server/types/Response";
|
||||||
import type { AxiosResponse } from "axios";
|
import type { AxiosResponse } from "axios";
|
||||||
import { getTranslations } from "next-intl/server";
|
import { getTranslations } from "next-intl/server";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
@@ -24,6 +25,13 @@ export interface ProxyResourcesPageProps {
|
|||||||
searchParams: Promise<Record<string, string>>;
|
searchParams: Promise<Record<string, string>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parsePositiveInt(s: string | undefined): number | undefined {
|
||||||
|
if (!s) return undefined;
|
||||||
|
const n = Number(s);
|
||||||
|
if (!Number.isInteger(n) || n <= 0) return undefined;
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
export default async function ProxyResourcesPage(
|
export default async function ProxyResourcesPage(
|
||||||
props: ProxyResourcesPageProps
|
props: ProxyResourcesPageProps
|
||||||
) {
|
) {
|
||||||
@@ -47,13 +55,31 @@ export default async function ProxyResourcesPage(
|
|||||||
pagination = responseData.pagination;
|
pagination = responseData.pagination;
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
|
|
||||||
let siteResources: ListAllSiteResourcesByOrgResponse["siteResources"] = [];
|
const siteIdParam = parsePositiveInt(searchParams.get("siteId") ?? undefined);
|
||||||
try {
|
|
||||||
const res = await internal.get<
|
let initialFilterSite: {
|
||||||
AxiosResponse<ListAllSiteResourcesByOrgResponse>
|
siteId: number;
|
||||||
>(`/org/${params.orgId}/site-resources`, await authCookieHeader());
|
name: string;
|
||||||
siteResources = res.data.data.siteResources;
|
type: string;
|
||||||
} catch (e) {}
|
} | null = null;
|
||||||
|
if (siteIdParam) {
|
||||||
|
try {
|
||||||
|
const siteRes = await internal.get(
|
||||||
|
`/site/${siteIdParam}`,
|
||||||
|
await authCookieHeader()
|
||||||
|
);
|
||||||
|
const s = (siteRes.data as ResponseT<GetSiteResponse>).data;
|
||||||
|
if (s && s.orgId === params.orgId) {
|
||||||
|
initialFilterSite = {
|
||||||
|
siteId: s.siteId,
|
||||||
|
name: s.name,
|
||||||
|
type: s.type
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// leave null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let org = null;
|
let org = null;
|
||||||
try {
|
try {
|
||||||
@@ -102,7 +128,8 @@ export default async function ProxyResourcesPage(
|
|||||||
enabled: target.enabled,
|
enabled: target.enabled,
|
||||||
healthStatus: target.healthStatus,
|
healthStatus: target.healthStatus,
|
||||||
siteName: target.siteName
|
siteName: target.siteName
|
||||||
}))
|
})),
|
||||||
|
sites: resource.sites ?? []
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
return (
|
return (
|
||||||
@@ -123,6 +150,7 @@ export default async function ProxyResourcesPage(
|
|||||||
pageIndex: pagination.page - 1,
|
pageIndex: pagination.page - 1,
|
||||||
pageSize: pagination.pageSize
|
pageSize: pagination.pageSize
|
||||||
}}
|
}}
|
||||||
|
initialFilterSite={initialFilterSite}
|
||||||
/>
|
/>
|
||||||
</OrgProvider>
|
</OrgProvider>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -48,13 +48,12 @@ import { useNavigationContext } from "@app/hooks/useNavigationContext";
|
|||||||
import { useDebouncedCallback } from "use-debounce";
|
import { useDebouncedCallback } from "use-debounce";
|
||||||
import { ColumnFilterButton } from "./ColumnFilterButton";
|
import { ColumnFilterButton } from "./ColumnFilterButton";
|
||||||
import { cn } from "@app/lib/cn";
|
import { cn } from "@app/lib/cn";
|
||||||
|
import {
|
||||||
|
ResourceSitesStatusCell,
|
||||||
|
type ResourceSiteRow
|
||||||
|
} from "@app/components/ResourceSitesStatusCell";
|
||||||
|
|
||||||
export type InternalResourceSiteRow = {
|
export type InternalResourceSiteRow = ResourceSiteRow;
|
||||||
siteId: number;
|
|
||||||
siteName: string;
|
|
||||||
siteNiceId: string;
|
|
||||||
online: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type InternalResourceRow = {
|
export type InternalResourceRow = {
|
||||||
id: number;
|
id: number;
|
||||||
@@ -119,109 +118,6 @@ function isSafeUrlForLink(href: string): boolean {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type AggregateSitesStatus = "allOnline" | "partial" | "allOffline";
|
|
||||||
|
|
||||||
function aggregateSitesStatus(
|
|
||||||
resourceSites: InternalResourceSiteRow[]
|
|
||||||
): AggregateSitesStatus {
|
|
||||||
if (resourceSites.length === 0) {
|
|
||||||
return "allOffline";
|
|
||||||
}
|
|
||||||
const onlineCount = resourceSites.filter((rs) => rs.online).length;
|
|
||||||
if (onlineCount === resourceSites.length) return "allOnline";
|
|
||||||
if (onlineCount > 0) return "partial";
|
|
||||||
return "allOffline";
|
|
||||||
}
|
|
||||||
|
|
||||||
function aggregateStatusDotClass(status: AggregateSitesStatus): string {
|
|
||||||
switch (status) {
|
|
||||||
case "allOnline":
|
|
||||||
return "bg-green-500";
|
|
||||||
case "partial":
|
|
||||||
return "bg-yellow-500";
|
|
||||||
case "allOffline":
|
|
||||||
default:
|
|
||||||
return "bg-neutral-500";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function ClientResourceSitesStatusCell({
|
|
||||||
orgId,
|
|
||||||
resourceSites
|
|
||||||
}: {
|
|
||||||
orgId: string;
|
|
||||||
resourceSites: InternalResourceSiteRow[];
|
|
||||||
}) {
|
|
||||||
const t = useTranslations();
|
|
||||||
|
|
||||||
if (resourceSites.length === 0) {
|
|
||||||
return <span>-</span>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const aggregate = aggregateSitesStatus(resourceSites);
|
|
||||||
const countLabel = t("multiSitesSelectorSitesCount", {
|
|
||||||
count: resourceSites.length
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
|
||||||
<DropdownMenu>
|
|
||||||
<DropdownMenuTrigger asChild>
|
|
||||||
<Button
|
|
||||||
variant="ghost"
|
|
||||||
size="sm"
|
|
||||||
className="flex h-8 items-center gap-2 px-0 font-normal"
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
className={cn(
|
|
||||||
"h-2 w-2 shrink-0 rounded-full",
|
|
||||||
aggregateStatusDotClass(aggregate)
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<span className="text-sm tabular-nums">{countLabel}</span>
|
|
||||||
<ChevronDown className="h-3 w-3 shrink-0" />
|
|
||||||
</Button>
|
|
||||||
</DropdownMenuTrigger>
|
|
||||||
<DropdownMenuContent align="start" className="min-w-56">
|
|
||||||
{resourceSites.map((site) => {
|
|
||||||
const isOnline = site.online;
|
|
||||||
return (
|
|
||||||
<DropdownMenuItem key={site.siteId} asChild>
|
|
||||||
<Link
|
|
||||||
href={`/${orgId}/settings/sites/${site.siteNiceId}`}
|
|
||||||
className="flex cursor-pointer items-center justify-between gap-4"
|
|
||||||
>
|
|
||||||
<div className="flex min-w-0 items-center gap-2">
|
|
||||||
<div
|
|
||||||
className={cn(
|
|
||||||
"h-2 w-2 shrink-0 rounded-full",
|
|
||||||
isOnline
|
|
||||||
? "bg-green-500"
|
|
||||||
: "bg-neutral-500"
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<span className="truncate">
|
|
||||||
{site.siteName}
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<span
|
|
||||||
className={cn(
|
|
||||||
"shrink-0 capitalize",
|
|
||||||
isOnline
|
|
||||||
? "text-green-600"
|
|
||||||
: "text-muted-foreground"
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
{isOnline ? t("online") : t("offline")}
|
|
||||||
</span>
|
|
||||||
</Link>
|
|
||||||
</DropdownMenuItem>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</DropdownMenuContent>
|
|
||||||
</DropdownMenu>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
type ClientResourcesTableProps = {
|
type ClientResourcesTableProps = {
|
||||||
internalResources: InternalResourceRow[];
|
internalResources: InternalResourceRow[];
|
||||||
orgId: string;
|
orgId: string;
|
||||||
@@ -321,9 +217,7 @@ export default function ClientResourcesTable({
|
|||||||
|
|
||||||
if (siteNames.length === 1) {
|
if (siteNames.length === 1) {
|
||||||
return (
|
return (
|
||||||
<Link
|
<Link href={`/${orgId}/settings/sites/${siteNiceIds[0]}`}>
|
||||||
href={`/${orgId}/settings/sites/${siteNiceIds[0]}`}
|
|
||||||
>
|
|
||||||
<Button variant="outline">
|
<Button variant="outline">
|
||||||
{siteNames[0]}
|
{siteNames[0]}
|
||||||
<ArrowUpRight className="ml-2 h-4 w-4" />
|
<ArrowUpRight className="ml-2 h-4 w-4" />
|
||||||
@@ -348,10 +242,7 @@ export default function ClientResourcesTable({
|
|||||||
</DropdownMenuTrigger>
|
</DropdownMenuTrigger>
|
||||||
<DropdownMenuContent align="start">
|
<DropdownMenuContent align="start">
|
||||||
{siteNames.map((siteName, idx) => (
|
{siteNames.map((siteName, idx) => (
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem key={siteNiceIds[idx]} asChild>
|
||||||
key={siteNiceIds[idx]}
|
|
||||||
asChild
|
|
||||||
>
|
|
||||||
<Link
|
<Link
|
||||||
href={`/${orgId}/settings/sites/${siteNiceIds[idx]}`}
|
href={`/${orgId}/settings/sites/${siteNiceIds[idx]}`}
|
||||||
className="flex items-center gap-2 cursor-pointer"
|
className="flex items-center gap-2 cursor-pointer"
|
||||||
@@ -470,7 +361,7 @@ export default function ClientResourcesTable({
|
|||||||
cell: ({ row }) => {
|
cell: ({ row }) => {
|
||||||
const resourceRow = row.original;
|
const resourceRow = row.original;
|
||||||
return (
|
return (
|
||||||
<ClientResourceSitesStatusCell
|
<ResourceSitesStatusCell
|
||||||
orgId={resourceRow.orgId}
|
orgId={resourceRow.orgId}
|
||||||
resourceSites={resourceRow.sites}
|
resourceSites={resourceRow.sites}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -2,6 +2,11 @@
|
|||||||
|
|
||||||
import ConfirmDeleteDialog from "@app/components/ConfirmDeleteDialog";
|
import ConfirmDeleteDialog from "@app/components/ConfirmDeleteDialog";
|
||||||
import CopyToClipboard from "@app/components/CopyToClipboard";
|
import CopyToClipboard from "@app/components/CopyToClipboard";
|
||||||
|
import {
|
||||||
|
ResourceSitesStatusCell,
|
||||||
|
type ResourceSiteRow
|
||||||
|
} from "@app/components/ResourceSitesStatusCell";
|
||||||
|
import { Badge } from "@app/components/ui/badge";
|
||||||
import { Button } from "@app/components/ui/button";
|
import { Button } from "@app/components/ui/button";
|
||||||
import { ExtendedColumnDef } from "@app/components/ui/data-table";
|
import { ExtendedColumnDef } from "@app/components/ui/data-table";
|
||||||
import {
|
import {
|
||||||
@@ -11,9 +16,16 @@ import {
|
|||||||
DropdownMenuTrigger
|
DropdownMenuTrigger
|
||||||
} from "@app/components/ui/dropdown-menu";
|
} from "@app/components/ui/dropdown-menu";
|
||||||
import { InfoPopup } from "@app/components/ui/info-popup";
|
import { InfoPopup } from "@app/components/ui/info-popup";
|
||||||
|
import {
|
||||||
|
Popover,
|
||||||
|
PopoverContent,
|
||||||
|
PopoverTrigger
|
||||||
|
} from "@app/components/ui/popover";
|
||||||
import { Switch } from "@app/components/ui/switch";
|
import { Switch } from "@app/components/ui/switch";
|
||||||
import { useEnvContext } from "@app/hooks/useEnvContext";
|
import { useEnvContext } from "@app/hooks/useEnvContext";
|
||||||
import { useNavigationContext } from "@app/hooks/useNavigationContext";
|
import { useNavigationContext } from "@app/hooks/useNavigationContext";
|
||||||
|
import { Selectedsite, SitesSelector } from "@app/components/site-selector";
|
||||||
|
import { cn } from "@app/lib/cn";
|
||||||
import { getNextSortOrder, getSortDirection } from "@app/lib/sortColumn";
|
import { getNextSortOrder, getSortDirection } from "@app/lib/sortColumn";
|
||||||
import { toast } from "@app/hooks/useToast";
|
import { toast } from "@app/hooks/useToast";
|
||||||
import { createApiClient, formatAxiosError } from "@app/lib/api";
|
import { createApiClient, formatAxiosError } from "@app/lib/api";
|
||||||
@@ -29,6 +41,7 @@ import {
|
|||||||
ChevronDown,
|
ChevronDown,
|
||||||
ChevronsUpDownIcon,
|
ChevronsUpDownIcon,
|
||||||
Clock,
|
Clock,
|
||||||
|
Funnel,
|
||||||
MoreHorizontal,
|
MoreHorizontal,
|
||||||
ShieldCheck,
|
ShieldCheck,
|
||||||
ShieldOff,
|
ShieldOff,
|
||||||
@@ -39,6 +52,7 @@ import Link from "next/link";
|
|||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import {
|
import {
|
||||||
useEffect,
|
useEffect,
|
||||||
|
useMemo,
|
||||||
useOptimistic,
|
useOptimistic,
|
||||||
useRef,
|
useRef,
|
||||||
useState,
|
useState,
|
||||||
@@ -83,6 +97,7 @@ export type ResourceRow = {
|
|||||||
targetHost?: string;
|
targetHost?: string;
|
||||||
targetPort?: number;
|
targetPort?: number;
|
||||||
targets?: TargetHealth[];
|
targets?: TargetHealth[];
|
||||||
|
sites: ResourceSiteRow[];
|
||||||
};
|
};
|
||||||
|
|
||||||
function getOverallHealthStatus(
|
function getOverallHealthStatus(
|
||||||
@@ -144,13 +159,15 @@ type ProxyResourcesTableProps = {
|
|||||||
orgId: string;
|
orgId: string;
|
||||||
pagination: PaginationState;
|
pagination: PaginationState;
|
||||||
rowCount: number;
|
rowCount: number;
|
||||||
|
initialFilterSite?: Selectedsite | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function ProxyResourcesTable({
|
export default function ProxyResourcesTable({
|
||||||
resources,
|
resources,
|
||||||
orgId,
|
orgId,
|
||||||
pagination,
|
pagination,
|
||||||
rowCount
|
rowCount,
|
||||||
|
initialFilterSite = null
|
||||||
}: ProxyResourcesTableProps) {
|
}: ProxyResourcesTableProps) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const {
|
const {
|
||||||
@@ -170,13 +187,30 @@ export default function ProxyResourcesTable({
|
|||||||
|
|
||||||
const [isRefreshing, startTransition] = useTransition();
|
const [isRefreshing, startTransition] = useTransition();
|
||||||
const [isNavigatingToAddPage, startNavigation] = useTransition();
|
const [isNavigatingToAddPage, startNavigation] = useTransition();
|
||||||
|
const [siteFilterOpen, setSiteFilterOpen] = useState(false);
|
||||||
|
|
||||||
|
const siteIdQ = searchParams.get("siteId");
|
||||||
|
const siteIdNum = siteIdQ ? parseInt(siteIdQ, 10) : NaN;
|
||||||
|
const selectedSite: Selectedsite | null = useMemo(() => {
|
||||||
|
if (!siteIdQ || !Number.isInteger(siteIdNum) || siteIdNum <= 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (initialFilterSite && initialFilterSite.siteId === siteIdNum) {
|
||||||
|
return initialFilterSite;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
siteId: siteIdNum,
|
||||||
|
name: t("standaloneHcFilterSiteIdFallback", { id: siteIdNum }),
|
||||||
|
type: "newt"
|
||||||
|
};
|
||||||
|
}, [initialFilterSite, siteIdQ, siteIdNum, t]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
router.refresh();
|
router.refresh();
|
||||||
}, 10_000);
|
}, 10_000);
|
||||||
return () => clearInterval(interval);
|
return () => clearInterval(interval);
|
||||||
}, []);
|
}, [router]);
|
||||||
|
|
||||||
const refreshData = () => {
|
const refreshData = () => {
|
||||||
startTransition(() => {
|
startTransition(() => {
|
||||||
@@ -375,6 +409,67 @@ export default function ProxyResourcesTable({
|
|||||||
return <span>{row.original.nice || "-"}</span>;
|
return <span>{row.original.nice || "-"}</span>;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: "sites",
|
||||||
|
accessorFn: (row) =>
|
||||||
|
row.sites.map((s) => s.siteName).join(", "),
|
||||||
|
friendlyName: t("sites"),
|
||||||
|
header: () => (
|
||||||
|
<Popover open={siteFilterOpen} onOpenChange={setSiteFilterOpen}>
|
||||||
|
<PopoverTrigger asChild>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="ghost"
|
||||||
|
role="combobox"
|
||||||
|
className={cn(
|
||||||
|
"justify-between text-sm h-8 px-2 w-full p-3",
|
||||||
|
!selectedSite && "text-muted-foreground"
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<div className="flex items-center gap-2 min-w-0">
|
||||||
|
{t("sites")}
|
||||||
|
<Funnel className="size-4 flex-none" />
|
||||||
|
{selectedSite && (
|
||||||
|
<Badge
|
||||||
|
className="truncate max-w-[10rem]"
|
||||||
|
variant="secondary"
|
||||||
|
>
|
||||||
|
{selectedSite.name}
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</Button>
|
||||||
|
</PopoverTrigger>
|
||||||
|
<PopoverContent
|
||||||
|
className="w-[min(20rem,var(--radix-popover-trigger-width))] p-0"
|
||||||
|
align="start"
|
||||||
|
>
|
||||||
|
<div className="border-b p-1">
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
className="h-8 w-full justify-start font-normal"
|
||||||
|
onClick={clearSiteFilter}
|
||||||
|
>
|
||||||
|
{t("standaloneHcFilterAnySite")}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<SitesSelector
|
||||||
|
orgId={orgId}
|
||||||
|
selectedSite={selectedSite}
|
||||||
|
onSelectSite={onPickSite}
|
||||||
|
/>
|
||||||
|
</PopoverContent>
|
||||||
|
</Popover>
|
||||||
|
),
|
||||||
|
cell: ({ row }) => (
|
||||||
|
<ResourceSitesStatusCell
|
||||||
|
orgId={row.original.orgId}
|
||||||
|
resourceSites={row.original.sites}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
},
|
||||||
{
|
{
|
||||||
accessorKey: "protocol",
|
accessorKey: "protocol",
|
||||||
friendlyName: t("protocol"),
|
friendlyName: t("protocol"),
|
||||||
@@ -620,6 +715,16 @@ export default function ProxyResourcesTable({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const clearSiteFilter = () => {
|
||||||
|
handleFilterChange("siteId", undefined);
|
||||||
|
setSiteFilterOpen(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onPickSite = (site: Selectedsite) => {
|
||||||
|
handleFilterChange("siteId", String(site.siteId));
|
||||||
|
setSiteFilterOpen(false);
|
||||||
|
};
|
||||||
|
|
||||||
function toggleSort(column: string) {
|
function toggleSort(column: string) {
|
||||||
const newSearch = getNextSortOrder(column, searchParams);
|
const newSearch = getNextSortOrder(column, searchParams);
|
||||||
|
|
||||||
|
|||||||
123
src/components/ResourceSitesStatusCell.tsx
Normal file
123
src/components/ResourceSitesStatusCell.tsx
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { Button } from "@app/components/ui/button";
|
||||||
|
import {
|
||||||
|
DropdownMenu,
|
||||||
|
DropdownMenuContent,
|
||||||
|
DropdownMenuItem,
|
||||||
|
DropdownMenuTrigger
|
||||||
|
} from "@app/components/ui/dropdown-menu";
|
||||||
|
import { cn } from "@app/lib/cn";
|
||||||
|
import { ChevronDown } from "lucide-react";
|
||||||
|
import { useTranslations } from "next-intl";
|
||||||
|
import Link from "next/link";
|
||||||
|
|
||||||
|
export type ResourceSiteRow = {
|
||||||
|
siteId: number;
|
||||||
|
siteName: string;
|
||||||
|
siteNiceId: string;
|
||||||
|
online: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
type AggregateSitesStatus = "allOnline" | "partial" | "allOffline";
|
||||||
|
|
||||||
|
function aggregateSitesStatus(
|
||||||
|
resourceSites: ResourceSiteRow[]
|
||||||
|
): AggregateSitesStatus {
|
||||||
|
if (resourceSites.length === 0) {
|
||||||
|
return "allOffline";
|
||||||
|
}
|
||||||
|
const onlineCount = resourceSites.filter((rs) => rs.online).length;
|
||||||
|
if (onlineCount === resourceSites.length) return "allOnline";
|
||||||
|
if (onlineCount > 0) return "partial";
|
||||||
|
return "allOffline";
|
||||||
|
}
|
||||||
|
|
||||||
|
function aggregateStatusDotClass(status: AggregateSitesStatus): string {
|
||||||
|
switch (status) {
|
||||||
|
case "allOnline":
|
||||||
|
return "bg-green-500";
|
||||||
|
case "partial":
|
||||||
|
return "bg-yellow-500";
|
||||||
|
case "allOffline":
|
||||||
|
default:
|
||||||
|
return "bg-neutral-500";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ResourceSitesStatusCell({
|
||||||
|
orgId,
|
||||||
|
resourceSites
|
||||||
|
}: {
|
||||||
|
orgId: string;
|
||||||
|
resourceSites: ResourceSiteRow[];
|
||||||
|
}) {
|
||||||
|
const t = useTranslations();
|
||||||
|
|
||||||
|
if (resourceSites.length === 0) {
|
||||||
|
return <span>-</span>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const aggregate = aggregateSitesStatus(resourceSites);
|
||||||
|
const countLabel = t("multiSitesSelectorSitesCount", {
|
||||||
|
count: resourceSites.length
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<DropdownMenu>
|
||||||
|
<DropdownMenuTrigger asChild>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
className="flex h-8 items-center gap-2 px-0 font-normal"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
"h-2 w-2 shrink-0 rounded-full",
|
||||||
|
aggregateStatusDotClass(aggregate)
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<span className="text-sm tabular-nums">{countLabel}</span>
|
||||||
|
<ChevronDown className="h-3 w-3 shrink-0" />
|
||||||
|
</Button>
|
||||||
|
</DropdownMenuTrigger>
|
||||||
|
<DropdownMenuContent align="start" className="min-w-56">
|
||||||
|
{resourceSites.map((site) => {
|
||||||
|
const isOnline = site.online;
|
||||||
|
return (
|
||||||
|
<DropdownMenuItem key={site.siteId} asChild>
|
||||||
|
<Link
|
||||||
|
href={`/${orgId}/settings/sites/${site.siteNiceId}`}
|
||||||
|
className="flex cursor-pointer items-center justify-between gap-4"
|
||||||
|
>
|
||||||
|
<div className="flex min-w-0 items-center gap-2">
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
"h-2 w-2 shrink-0 rounded-full",
|
||||||
|
isOnline
|
||||||
|
? "bg-green-500"
|
||||||
|
: "bg-neutral-500"
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<span className="truncate">
|
||||||
|
{site.siteName}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<span
|
||||||
|
className={cn(
|
||||||
|
"shrink-0 capitalize",
|
||||||
|
isOnline
|
||||||
|
? "text-green-600"
|
||||||
|
: "text-muted-foreground"
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{isOnline ? t("online") : t("offline")}
|
||||||
|
</span>
|
||||||
|
</Link>
|
||||||
|
</DropdownMenuItem>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</DropdownMenuContent>
|
||||||
|
</DropdownMenu>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user