"use client"; import { Check, Globe, Lock } from "@openstatus/icons"; import { formatRegionCode, groupByContinent } from "@openstatus/regions"; import { Button } from "@openstatus/ui/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator, } from "@openstatus/ui/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger, } from "@openstatus/ui/components/ui/popover"; import { cn } from "@openstatus/ui/lib/utils"; import { useQuery } from "@tanstack/react-query"; import { parseAsArrayOf, parseAsString, useQueryState } from "nuqs"; import { IconCloudProvider } from "@/components/common/icon-cloud-provider"; import { Link } from "@/components/common/link"; import { BillingOverlay, BillingOverlayButton, BillingOverlayDescription, } from "@/components/content/billing-overlay"; import type { REGIONS } from "@/data/metrics.client"; import { useTRPC } from "@/lib/trpc/client"; export function CommandRegion({ regions, privateLocations, }: { regions: (typeof REGIONS)[number][]; privateLocations?: { id: number; name: string }[]; }) { const trpc = useTRPC(); const { data: workspace } = useQuery(trpc.workspace.get.queryOptions()); const [selectedRegions, setSelectedRegions] = useQueryState( "regions", parseAsArrayOf(parseAsString).withDefault([ ...regions, ...(privateLocations?.map((location) => location.id.toString()) ?? []), ]), ); const limited = workspace?.plan === "free"; return ( { const items = document.querySelectorAll( '[data-slot="command-item"][data-disabled="false"]', ); const codes: (typeof REGIONS)[number][] = []; items.forEach((item) => { const code = item.getAttribute("data-value"); if (code && code !== "select-all") { codes.push(code as (typeof REGIONS)[number]); } }); if (codes.length === selectedRegions.length) { setSelectedRegions([]); } else { setSelectedRegions(codes); } }} value="select-all" disabled={limited} > Toggle selection {Object.entries(groupByContinent).map( ([continent, continentRegions]) => { const allowedRegions = continentRegions.filter((region) => regions.includes(region.code), ); if (allowedRegions.length === 0) { return null; } return ( {allowedRegions.map((region) => ( { setSelectedRegions((prev) => prev.includes(region.code) ? prev.filter((r) => r !== region.code) : [...prev, region.code], ); }} > {region.flag} {formatRegionCode(region.code)} {region.location} ))} ); }, )} {privateLocations && privateLocations.length > 0 ? ( {privateLocations.map((location) => ( { setSelectedRegions((prev) => prev.includes(location.id.toString()) ? prev.filter((r) => r !== location.id.toString()) : [...prev, location.id.toString()], ); }} > {location.name} ))} ) : null} No region found. {limited ? ( Upgrade Filter by region is only available on paid plans. ) : null} ); }