mirror of
https://github.com/fosrl/pangolin.git
synced 2026-06-09 00:49:52 +00:00
Compare commits
1 Commits
backhaul
...
feat/comma
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2e9a040174 |
@@ -1354,6 +1354,29 @@
|
||||
"otpAuthBack": "Back to Password",
|
||||
"navbar": "Navigation Menu",
|
||||
"navbarDescription": "Main navigation menu for the application",
|
||||
"commandPaletteTitle": "Command palette",
|
||||
"commandPaletteDescription": "Search for pages, organizations, resources, and actions",
|
||||
"commandPaletteSearchPlaceholder": "Search pages, resources, actions...",
|
||||
"commandPaletteNoResults": "No results found.",
|
||||
"commandPaletteSearching": "Searching...",
|
||||
"commandPaletteNavigation": "Navigation",
|
||||
"commandPaletteOrganizations": "Organizations",
|
||||
"commandPaletteSites": "Sites",
|
||||
"commandPaletteResources": "Resources",
|
||||
"commandPaletteUsers": "Users",
|
||||
"commandPaletteClients": "Machine clients",
|
||||
"commandPaletteActions": "Actions",
|
||||
"commandPaletteCreateSite": "Create site",
|
||||
"commandPaletteCreateProxyResource": "Create public resource",
|
||||
"commandPaletteCreateUser": "Create user",
|
||||
"commandPaletteCreateApiKey": "Create API key",
|
||||
"commandPaletteCreateMachineClient": "Create machine client",
|
||||
"commandPaletteCreateAlertRule": "Create alert rule",
|
||||
"commandPaletteCreateIdentityProvider": "Create identity provider",
|
||||
"commandPaletteToggleTheme": "Toggle theme",
|
||||
"commandPaletteChooseOrganization": "Choose organization",
|
||||
"commandPaletteShortcutMac": "⌘K",
|
||||
"commandPaletteShortcutWindows": "Ctrl K",
|
||||
"navbarDocsLink": "Documentation",
|
||||
"otpErrorEnable": "Unable to enable 2FA",
|
||||
"otpErrorEnableDescription": "An error occurred while enabling 2FA",
|
||||
|
||||
@@ -21,9 +21,11 @@ export default async function Page(props: {
|
||||
searchParams: Promise<{
|
||||
redirect: string | undefined;
|
||||
t: string | undefined;
|
||||
orgs?: string | undefined;
|
||||
}>;
|
||||
}) {
|
||||
const params = await props.searchParams; // this is needed to prevent static optimization
|
||||
const showOrgPicker = params.orgs === "1";
|
||||
|
||||
const env = pullEnv();
|
||||
|
||||
@@ -106,7 +108,7 @@ export default async function Page(props: {
|
||||
}
|
||||
}
|
||||
|
||||
if (targetOrgId) {
|
||||
if (targetOrgId && !showOrgPicker) {
|
||||
return <RedirectToOrg targetOrgId={targetOrgId} />;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { SidebarNavSection } from "@app/app/navigation";
|
||||
import { LayoutSidebar } from "@app/components/LayoutSidebar";
|
||||
import { LayoutHeader } from "@app/components/LayoutHeader";
|
||||
import { LayoutMobileMenu } from "@app/components/LayoutMobileMenu";
|
||||
import { CommandPaletteProvider } from "@app/components/command-palette/CommandPaletteProvider";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
interface LayoutProps {
|
||||
@@ -37,51 +38,53 @@ export async function Layout({
|
||||
(sidebarStateCookie !== "expanded" && defaultSidebarCollapsed);
|
||||
|
||||
return (
|
||||
<div className="flex h-screen-safe overflow-hidden">
|
||||
{/* Desktop Sidebar */}
|
||||
{showSidebar && (
|
||||
<LayoutSidebar
|
||||
orgId={orgId}
|
||||
orgs={orgs}
|
||||
navItems={navItems}
|
||||
defaultSidebarCollapsed={initialSidebarCollapsed}
|
||||
hasCookiePreference={hasCookiePreference}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Main content area */}
|
||||
<div
|
||||
className={cn(
|
||||
"flex-1 flex flex-col h-full min-w-0 relative",
|
||||
!showSidebar && "w-full"
|
||||
)}
|
||||
>
|
||||
{/* Mobile header */}
|
||||
{showHeader && (
|
||||
<LayoutMobileMenu
|
||||
<CommandPaletteProvider orgId={orgId} orgs={orgs} navItems={navItems}>
|
||||
<div className="flex h-screen-safe overflow-hidden">
|
||||
{/* Desktop Sidebar */}
|
||||
{showSidebar && (
|
||||
<LayoutSidebar
|
||||
orgId={orgId}
|
||||
orgs={orgs}
|
||||
navItems={navItems}
|
||||
showSidebar={showSidebar}
|
||||
showTopBar={showTopBar}
|
||||
defaultSidebarCollapsed={initialSidebarCollapsed}
|
||||
hasCookiePreference={hasCookiePreference}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Desktop header */}
|
||||
{showHeader && <LayoutHeader showTopBar={showTopBar} />}
|
||||
{/* Main content area */}
|
||||
<div
|
||||
className={cn(
|
||||
"flex-1 flex flex-col h-full min-w-0 relative",
|
||||
!showSidebar && "w-full"
|
||||
)}
|
||||
>
|
||||
{/* Mobile header */}
|
||||
{showHeader && (
|
||||
<LayoutMobileMenu
|
||||
orgId={orgId}
|
||||
orgs={orgs}
|
||||
navItems={navItems}
|
||||
showSidebar={showSidebar}
|
||||
showTopBar={showTopBar}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Main content */}
|
||||
<main className="flex-1 overflow-y-auto p-3 md:p-6 w-full">
|
||||
<div
|
||||
className={cn(
|
||||
"container mx-auto max-w-12xl mb-12",
|
||||
showHeader && "md:pt-14" // Add top padding only on desktop to account for fixed header
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
</main>
|
||||
{/* Desktop header */}
|
||||
{showHeader && <LayoutHeader showTopBar={showTopBar} />}
|
||||
|
||||
{/* Main content */}
|
||||
<main className="flex-1 overflow-y-auto p-3 md:p-6 w-full">
|
||||
<div
|
||||
className={cn(
|
||||
"container mx-auto max-w-12xl mb-12",
|
||||
showHeader && "md:pt-14" // Add top padding only on desktop to account for fixed header
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</CommandPaletteProvider>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import ProfileIcon from "@app/components/ProfileIcon";
|
||||
import ThemeSwitcher from "@app/components/ThemeSwitcher";
|
||||
import { useTheme } from "next-themes";
|
||||
import BrandingLogo from "./BrandingLogo";
|
||||
import { CommandPaletteTrigger } from "@app/components/command-palette/CommandPaletteTrigger";
|
||||
import { useEnvContext } from "@app/hooks/useEnvContext";
|
||||
import { useLicenseStatusContext } from "@app/hooks/useLicenseStatusContext";
|
||||
|
||||
@@ -67,6 +68,7 @@ export function LayoutHeader({ showTopBar }: LayoutHeaderProps) {
|
||||
|
||||
{showTopBar && (
|
||||
<div className="flex items-center space-x-2">
|
||||
<CommandPaletteTrigger />
|
||||
<ThemeSwitcher />
|
||||
<ProfileIcon />
|
||||
</div>
|
||||
|
||||
@@ -13,6 +13,7 @@ import { useUserContext } from "@app/hooks/useUserContext";
|
||||
import { useTranslations } from "next-intl";
|
||||
import ProfileIcon from "@app/components/ProfileIcon";
|
||||
import ThemeSwitcher from "@app/components/ThemeSwitcher";
|
||||
import { CommandPaletteTrigger } from "@app/components/command-palette/CommandPaletteTrigger";
|
||||
import type { SidebarNavSection } from "@app/app/navigation";
|
||||
import {
|
||||
Sheet,
|
||||
@@ -121,6 +122,7 @@ export function LayoutMobileMenu({
|
||||
{showTopBar && (
|
||||
<div className="ml-auto flex items-center justify-end">
|
||||
<div className="flex items-center space-x-2">
|
||||
<CommandPaletteTrigger variant="mobile" />
|
||||
<ThemeSwitcher />
|
||||
<ProfileIcon />
|
||||
</div>
|
||||
|
||||
270
src/components/command-palette/CommandPalette.tsx
Normal file
270
src/components/command-palette/CommandPalette.tsx
Normal file
@@ -0,0 +1,270 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
CommandDialog,
|
||||
CommandEmpty,
|
||||
CommandGroup,
|
||||
CommandInput,
|
||||
CommandItem,
|
||||
CommandList,
|
||||
CommandSeparator
|
||||
} from "@app/components/ui/command";
|
||||
import type { SidebarNavSection } from "@app/app/navigation";
|
||||
import { Badge } from "@app/components/ui/badge";
|
||||
import { ListUserOrgsResponse } from "@server/routers/org";
|
||||
import { Loader2 } from "lucide-react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useCallback, useState } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { useCommandPalette } from "./commandPaletteContext";
|
||||
import { useCommandPaletteActions } from "./useCommandPaletteActions";
|
||||
import { useCommandPaletteNavigation } from "./useCommandPaletteNavigation";
|
||||
import { useCommandPaletteOrganizations } from "./useCommandPaletteOrganizations";
|
||||
import { useCommandPaletteSearch } from "./useCommandPaletteSearch";
|
||||
|
||||
type CommandPaletteProps = {
|
||||
orgId?: string;
|
||||
orgs?: ListUserOrgsResponse["orgs"];
|
||||
navItems: SidebarNavSection[];
|
||||
};
|
||||
|
||||
export function CommandPalette({ orgId, orgs, navItems }: CommandPaletteProps) {
|
||||
const t = useTranslations();
|
||||
const router = useRouter();
|
||||
const { open, setOpen } = useCommandPalette();
|
||||
const [search, setSearch] = useState("");
|
||||
|
||||
const navigationGroups = useCommandPaletteNavigation(navItems);
|
||||
const organizations = useCommandPaletteOrganizations(orgs);
|
||||
const actions = useCommandPaletteActions(orgId, orgs);
|
||||
const { shouldSearch, sites, resources, users, machineClients, isLoading } =
|
||||
useCommandPaletteSearch({
|
||||
orgId,
|
||||
query: search,
|
||||
enabled: open
|
||||
});
|
||||
|
||||
const handleOpenChange = useCallback(
|
||||
(nextOpen: boolean) => {
|
||||
setOpen(nextOpen);
|
||||
if (!nextOpen) {
|
||||
setSearch("");
|
||||
}
|
||||
},
|
||||
[setOpen]
|
||||
);
|
||||
|
||||
const runCommand = useCallback(
|
||||
(command: () => void) => {
|
||||
setOpen(false);
|
||||
setSearch("");
|
||||
command();
|
||||
},
|
||||
[setOpen]
|
||||
);
|
||||
|
||||
const hasEntityResults =
|
||||
sites.length > 0 ||
|
||||
resources.length > 0 ||
|
||||
users.length > 0 ||
|
||||
machineClients.length > 0;
|
||||
|
||||
return (
|
||||
<CommandDialog
|
||||
open={open}
|
||||
onOpenChange={handleOpenChange}
|
||||
title={t("commandPaletteTitle")}
|
||||
description={t("commandPaletteDescription")}
|
||||
>
|
||||
<CommandInput
|
||||
placeholder={t("commandPaletteSearchPlaceholder")}
|
||||
value={search}
|
||||
onValueChange={setSearch}
|
||||
/>
|
||||
<CommandList>
|
||||
<CommandEmpty>{t("commandPaletteNoResults")}</CommandEmpty>
|
||||
|
||||
{navigationGroups.map((group) => (
|
||||
<CommandGroup key={group.heading} heading={group.heading}>
|
||||
{group.items.map((item) => (
|
||||
<CommandItem
|
||||
key={item.id}
|
||||
value={`${item.title} ${group.heading}`}
|
||||
onSelect={() =>
|
||||
runCommand(() => router.push(item.href))
|
||||
}
|
||||
>
|
||||
{item.icon}
|
||||
<span>{item.title}</span>
|
||||
</CommandItem>
|
||||
))}
|
||||
</CommandGroup>
|
||||
))}
|
||||
|
||||
{organizations.length > 1 && (
|
||||
<>
|
||||
<CommandSeparator />
|
||||
<CommandGroup
|
||||
heading={t("commandPaletteOrganizations")}
|
||||
>
|
||||
{organizations.map((org) => (
|
||||
<CommandItem
|
||||
key={org.id}
|
||||
value={`${org.name} ${org.orgId}`}
|
||||
onSelect={() =>
|
||||
runCommand(() => router.push(org.href))
|
||||
}
|
||||
>
|
||||
<span className="truncate">{org.name}</span>
|
||||
<span className="text-xs text-muted-foreground font-mono truncate">
|
||||
{org.orgId}
|
||||
</span>
|
||||
{org.isPrimaryOrg && (
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="ml-auto shrink-0 text-[10px] px-1.5 py-0"
|
||||
>
|
||||
{t("primary")}
|
||||
</Badge>
|
||||
)}
|
||||
</CommandItem>
|
||||
))}
|
||||
</CommandGroup>
|
||||
</>
|
||||
)}
|
||||
|
||||
{shouldSearch && orgId && (
|
||||
<>
|
||||
<CommandSeparator />
|
||||
{isLoading && !hasEntityResults ? (
|
||||
<div className="flex items-center justify-center gap-2 py-6 text-sm text-muted-foreground">
|
||||
<Loader2 className="size-4 animate-spin" />
|
||||
{t("commandPaletteSearching")}
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
{sites.length > 0 && (
|
||||
<CommandGroup
|
||||
heading={t("commandPaletteSites")}
|
||||
>
|
||||
{sites.map((site) => (
|
||||
<CommandItem
|
||||
key={site.id}
|
||||
value={`${site.name} site`}
|
||||
onSelect={() =>
|
||||
runCommand(() =>
|
||||
router.push(site.href)
|
||||
)
|
||||
}
|
||||
>
|
||||
<span className="truncate">
|
||||
{site.name}
|
||||
</span>
|
||||
</CommandItem>
|
||||
))}
|
||||
</CommandGroup>
|
||||
)}
|
||||
{resources.length > 0 && (
|
||||
<CommandGroup
|
||||
heading={t("commandPaletteResources")}
|
||||
>
|
||||
{resources.map((resource) => (
|
||||
<CommandItem
|
||||
key={resource.id}
|
||||
value={`${resource.name} resource`}
|
||||
onSelect={() =>
|
||||
runCommand(() =>
|
||||
router.push(
|
||||
resource.href
|
||||
)
|
||||
)
|
||||
}
|
||||
>
|
||||
<span className="truncate">
|
||||
{resource.name}
|
||||
</span>
|
||||
</CommandItem>
|
||||
))}
|
||||
</CommandGroup>
|
||||
)}
|
||||
{users.length > 0 && (
|
||||
<CommandGroup
|
||||
heading={t("commandPaletteUsers")}
|
||||
>
|
||||
{users.map((user) => (
|
||||
<CommandItem
|
||||
key={user.id}
|
||||
value={`${user.name} ${user.email}`}
|
||||
onSelect={() =>
|
||||
runCommand(() =>
|
||||
router.push(user.href)
|
||||
)
|
||||
}
|
||||
>
|
||||
<div className="flex min-w-0 flex-col">
|
||||
<span className="truncate">
|
||||
{user.name}
|
||||
</span>
|
||||
<span className="truncate text-xs text-muted-foreground">
|
||||
{user.email}
|
||||
</span>
|
||||
</div>
|
||||
</CommandItem>
|
||||
))}
|
||||
</CommandGroup>
|
||||
)}
|
||||
{machineClients.length > 0 && (
|
||||
<CommandGroup
|
||||
heading={t("commandPaletteClients")}
|
||||
>
|
||||
{machineClients.map((client) => (
|
||||
<CommandItem
|
||||
key={client.id}
|
||||
value={`${client.name} client`}
|
||||
onSelect={() =>
|
||||
runCommand(() =>
|
||||
router.push(client.href)
|
||||
)
|
||||
}
|
||||
>
|
||||
<span className="truncate">
|
||||
{client.name}
|
||||
</span>
|
||||
</CommandItem>
|
||||
))}
|
||||
</CommandGroup>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
|
||||
{actions.length > 0 && (
|
||||
<>
|
||||
<CommandSeparator />
|
||||
<CommandGroup heading={t("commandPaletteActions")}>
|
||||
{actions.map((action) => (
|
||||
<CommandItem
|
||||
key={action.id}
|
||||
value={action.label}
|
||||
onSelect={() =>
|
||||
runCommand(() => {
|
||||
if (action.onSelect) {
|
||||
action.onSelect();
|
||||
} else if (action.href) {
|
||||
router.push(action.href);
|
||||
}
|
||||
})
|
||||
}
|
||||
>
|
||||
{action.icon}
|
||||
<span>{action.label}</span>
|
||||
</CommandItem>
|
||||
))}
|
||||
</CommandGroup>
|
||||
</>
|
||||
)}
|
||||
</CommandList>
|
||||
</CommandDialog>
|
||||
);
|
||||
}
|
||||
76
src/components/command-palette/CommandPaletteProvider.tsx
Normal file
76
src/components/command-palette/CommandPaletteProvider.tsx
Normal file
@@ -0,0 +1,76 @@
|
||||
"use client";
|
||||
|
||||
import type { SidebarNavSection } from "@app/app/navigation";
|
||||
import { ListUserOrgsResponse } from "@server/routers/org";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import {
|
||||
CommandPaletteContextProvider,
|
||||
type CommandPaletteContextValue
|
||||
} from "./commandPaletteContext";
|
||||
import { CommandPalette } from "./CommandPalette";
|
||||
|
||||
type CommandPaletteProviderProps = {
|
||||
children: React.ReactNode;
|
||||
orgId?: string;
|
||||
orgs?: ListUserOrgsResponse["orgs"];
|
||||
navItems: SidebarNavSection[];
|
||||
};
|
||||
|
||||
function isEditableTarget(target: EventTarget | null) {
|
||||
if (!(target instanceof HTMLElement)) return false;
|
||||
if (target.isContentEditable) return true;
|
||||
const tagName = target.tagName;
|
||||
return (
|
||||
tagName === "INPUT" || tagName === "TEXTAREA" || tagName === "SELECT"
|
||||
);
|
||||
}
|
||||
|
||||
export function CommandPaletteProvider({
|
||||
children,
|
||||
orgId,
|
||||
orgs,
|
||||
navItems
|
||||
}: CommandPaletteProviderProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
const toggle = useCallback(() => {
|
||||
setOpen((current) => !current);
|
||||
}, []);
|
||||
|
||||
const contextValue = useMemo<CommandPaletteContextValue>(
|
||||
() => ({
|
||||
open,
|
||||
setOpen,
|
||||
toggle
|
||||
}),
|
||||
[open, toggle]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
function onKeyDown(event: KeyboardEvent) {
|
||||
if (
|
||||
event.key.toLowerCase() !== "k" ||
|
||||
!(event.metaKey || event.ctrlKey)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!open && isEditableTarget(event.target)) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
toggle();
|
||||
}
|
||||
|
||||
document.addEventListener("keydown", onKeyDown);
|
||||
return () => document.removeEventListener("keydown", onKeyDown);
|
||||
}, [open, toggle]);
|
||||
|
||||
return (
|
||||
<CommandPaletteContextProvider value={contextValue}>
|
||||
{children}
|
||||
<CommandPalette orgId={orgId} orgs={orgs} navItems={navItems} />
|
||||
</CommandPaletteContextProvider>
|
||||
);
|
||||
}
|
||||
69
src/components/command-palette/CommandPaletteTrigger.tsx
Normal file
69
src/components/command-palette/CommandPaletteTrigger.tsx
Normal file
@@ -0,0 +1,69 @@
|
||||
"use client";
|
||||
|
||||
import { Button } from "@app/components/ui/button";
|
||||
import { CommandShortcut } from "@app/components/ui/command";
|
||||
import { cn } from "@app/lib/cn";
|
||||
import { Search } from "lucide-react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { useCommandPalette } from "./commandPaletteContext";
|
||||
|
||||
type CommandPaletteTriggerProps = {
|
||||
variant?: "header" | "mobile";
|
||||
className?: string;
|
||||
};
|
||||
|
||||
function useIsMac() {
|
||||
const [isMac, setIsMac] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setIsMac(/Mac|iPhone|iPod|iPad/.test(navigator.platform));
|
||||
}, []);
|
||||
|
||||
return isMac;
|
||||
}
|
||||
|
||||
export function CommandPaletteTrigger({
|
||||
variant = "header",
|
||||
className
|
||||
}: CommandPaletteTriggerProps) {
|
||||
const t = useTranslations();
|
||||
const { setOpen } = useCommandPalette();
|
||||
const isMac = useIsMac();
|
||||
|
||||
if (variant === "mobile") {
|
||||
return (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className={className}
|
||||
aria-label={t("commandPaletteTitle")}
|
||||
onClick={() => setOpen(true)}
|
||||
>
|
||||
<Search className="size-5" />
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Button
|
||||
variant="outline"
|
||||
className={cn(
|
||||
"hidden h-9 w-56 justify-start gap-2 px-3 text-muted-foreground md:flex lg:w-64",
|
||||
className
|
||||
)}
|
||||
aria-label={t("commandPaletteTitle")}
|
||||
onClick={() => setOpen(true)}
|
||||
>
|
||||
<Search className="size-4 shrink-0 opacity-50" />
|
||||
<span className="flex-1 truncate text-left text-sm font-normal">
|
||||
{t("commandPaletteSearchPlaceholder")}
|
||||
</span>
|
||||
<CommandShortcut>
|
||||
{isMac
|
||||
? t("commandPaletteShortcutMac")
|
||||
: t("commandPaletteShortcutWindows")}
|
||||
</CommandShortcut>
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
37
src/components/command-palette/commandPaletteContext.tsx
Normal file
37
src/components/command-palette/commandPaletteContext.tsx
Normal file
@@ -0,0 +1,37 @@
|
||||
"use client";
|
||||
|
||||
import React, { createContext, useContext } from "react";
|
||||
|
||||
export type CommandPaletteContextValue = {
|
||||
open: boolean;
|
||||
setOpen: (open: boolean) => void;
|
||||
toggle: () => void;
|
||||
};
|
||||
|
||||
const CommandPaletteContext = createContext<CommandPaletteContextValue | null>(
|
||||
null
|
||||
);
|
||||
|
||||
export function CommandPaletteContextProvider({
|
||||
value,
|
||||
children
|
||||
}: {
|
||||
value: CommandPaletteContextValue;
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<CommandPaletteContext.Provider value={value}>
|
||||
{children}
|
||||
</CommandPaletteContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useCommandPalette() {
|
||||
const context = useContext(CommandPaletteContext);
|
||||
if (!context) {
|
||||
throw new Error(
|
||||
"useCommandPalette must be used within CommandPaletteProvider"
|
||||
);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
161
src/components/command-palette/useCommandPaletteActions.tsx
Normal file
161
src/components/command-palette/useCommandPaletteActions.tsx
Normal file
@@ -0,0 +1,161 @@
|
||||
"use client";
|
||||
|
||||
import { useEnvContext } from "@app/hooks/useEnvContext";
|
||||
import { useUserContext } from "@app/hooks/useUserContext";
|
||||
import { build } from "@server/build";
|
||||
import {
|
||||
BellRing,
|
||||
Building2,
|
||||
Globe,
|
||||
KeyRound,
|
||||
MonitorUp,
|
||||
Plus,
|
||||
SunMoon,
|
||||
UserPlus
|
||||
} from "lucide-react";
|
||||
import { useTheme } from "next-themes";
|
||||
import { usePathname } from "next/navigation";
|
||||
import type { ReactNode } from "react";
|
||||
import { useMemo } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { ListUserOrgsResponse } from "@server/routers/org";
|
||||
|
||||
export type CommandPaletteAction = {
|
||||
id: string;
|
||||
label: string;
|
||||
icon: ReactNode;
|
||||
href?: string;
|
||||
onSelect?: () => void;
|
||||
};
|
||||
|
||||
export function useCommandPaletteActions(
|
||||
orgId?: string,
|
||||
orgs?: ListUserOrgsResponse["orgs"]
|
||||
): CommandPaletteAction[] {
|
||||
const t = useTranslations();
|
||||
const pathname = usePathname();
|
||||
const { env } = useEnvContext();
|
||||
const { user } = useUserContext();
|
||||
const { setTheme, theme } = useTheme();
|
||||
const isAdminPage = pathname?.startsWith("/admin");
|
||||
|
||||
return useMemo(() => {
|
||||
const actions: CommandPaletteAction[] = [];
|
||||
|
||||
function cycleTheme() {
|
||||
const currentTheme = theme || "system";
|
||||
if (currentTheme === "light") {
|
||||
setTheme("dark");
|
||||
} else if (currentTheme === "dark") {
|
||||
setTheme("system");
|
||||
} else {
|
||||
setTheme("light");
|
||||
}
|
||||
}
|
||||
|
||||
if (isAdminPage) {
|
||||
actions.push({
|
||||
id: "create-admin-api-key",
|
||||
label: t("commandPaletteCreateApiKey"),
|
||||
icon: <KeyRound className="size-4" />,
|
||||
href: "/admin/api-keys/create"
|
||||
});
|
||||
|
||||
if (
|
||||
build === "oss" ||
|
||||
env?.app.identityProviderMode === "global" ||
|
||||
env?.app.identityProviderMode === undefined
|
||||
) {
|
||||
actions.push({
|
||||
id: "create-admin-idp",
|
||||
label: t("commandPaletteCreateIdentityProvider"),
|
||||
icon: <Plus className="size-4" />,
|
||||
href: "/admin/idp/create"
|
||||
});
|
||||
}
|
||||
} else if (orgId) {
|
||||
actions.push({
|
||||
id: "create-site",
|
||||
label: t("commandPaletteCreateSite"),
|
||||
icon: <Plus className="size-4" />,
|
||||
href: `/${orgId}/settings/sites/create`
|
||||
});
|
||||
actions.push({
|
||||
id: "create-proxy-resource",
|
||||
label: t("commandPaletteCreateProxyResource"),
|
||||
icon: <Globe className="size-4" />,
|
||||
href: `/${orgId}/settings/resources/proxy/create`
|
||||
});
|
||||
actions.push({
|
||||
id: "create-user",
|
||||
label: t("commandPaletteCreateUser"),
|
||||
icon: <UserPlus className="size-4" />,
|
||||
href: `/${orgId}/settings/access/users/create`
|
||||
});
|
||||
actions.push({
|
||||
id: "create-api-key",
|
||||
label: t("commandPaletteCreateApiKey"),
|
||||
icon: <KeyRound className="size-4" />,
|
||||
href: `/${orgId}/settings/api-keys/create`
|
||||
});
|
||||
actions.push({
|
||||
id: "create-machine-client",
|
||||
label: t("commandPaletteCreateMachineClient"),
|
||||
icon: <MonitorUp className="size-4" />,
|
||||
href: `/${orgId}/settings/clients/machine/create`
|
||||
});
|
||||
|
||||
if (!env?.flags.disableEnterpriseFeatures) {
|
||||
actions.push({
|
||||
id: "create-alert-rule",
|
||||
label: t("commandPaletteCreateAlertRule"),
|
||||
icon: <BellRing className="size-4" />,
|
||||
href: `/${orgId}/settings/alerting/create`
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
(build === "oss" && !env?.flags.disableEnterpriseFeatures) ||
|
||||
build === "saas" ||
|
||||
env?.app.identityProviderMode === "org" ||
|
||||
(env?.app.identityProviderMode === undefined && build !== "oss")
|
||||
) {
|
||||
actions.push({
|
||||
id: "create-idp",
|
||||
label: t("commandPaletteCreateIdentityProvider"),
|
||||
icon: <Plus className="size-4" />,
|
||||
href: `/${orgId}/settings/idp/create`
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const canChooseOrganization = !isAdminPage && (orgs?.length ?? 0) > 1;
|
||||
|
||||
if (canChooseOrganization) {
|
||||
actions.push({
|
||||
id: "choose-org",
|
||||
label: t("commandPaletteChooseOrganization"),
|
||||
icon: <Building2 className="size-4" />,
|
||||
href: "/?orgs=1"
|
||||
});
|
||||
}
|
||||
|
||||
actions.push({
|
||||
id: "toggle-theme",
|
||||
label: t("commandPaletteToggleTheme"),
|
||||
icon: <SunMoon className="size-4" />,
|
||||
onSelect: cycleTheme
|
||||
});
|
||||
|
||||
if (user.serverAdmin && !isAdminPage) {
|
||||
actions.push({
|
||||
id: "go-admin",
|
||||
label: t("serverAdmin"),
|
||||
icon: <Building2 className="size-4" />,
|
||||
href: "/admin/users"
|
||||
});
|
||||
}
|
||||
|
||||
return actions;
|
||||
}, [isAdminPage, orgId, orgs, env, user.serverAdmin, theme, setTheme, t]);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
"use client";
|
||||
|
||||
import type { SidebarNavSection } from "@app/components/SidebarNav";
|
||||
import { flattenNavSections } from "@app/lib/flattenNavItems";
|
||||
import {
|
||||
hydrateNavHref,
|
||||
navHrefParamsFromRoute
|
||||
} from "@app/lib/hydrateNavHref";
|
||||
import { useParams } from "next/navigation";
|
||||
import { useMemo } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
|
||||
export type NavigationCommand = {
|
||||
id: string;
|
||||
title: string;
|
||||
href: string;
|
||||
icon?: React.ReactNode;
|
||||
sectionHeading: string;
|
||||
};
|
||||
|
||||
export type NavigationCommandGroup = {
|
||||
heading: string;
|
||||
items: NavigationCommand[];
|
||||
};
|
||||
|
||||
export function useCommandPaletteNavigation(
|
||||
navItems: SidebarNavSection[]
|
||||
): NavigationCommandGroup[] {
|
||||
const params = useParams();
|
||||
const t = useTranslations();
|
||||
|
||||
return useMemo(() => {
|
||||
const hrefParams = navHrefParamsFromRoute(params);
|
||||
const flat = flattenNavSections(navItems);
|
||||
const groups = new Map<string, NavigationCommand[]>();
|
||||
|
||||
for (const item of flat) {
|
||||
const href = hydrateNavHref(item.href, hrefParams);
|
||||
if (!href) continue;
|
||||
|
||||
const groupItems = groups.get(item.sectionHeading) ?? [];
|
||||
groupItems.push({
|
||||
id: `nav-${item.sectionHeading}-${item.title}-${href}`,
|
||||
title: t(item.title),
|
||||
href,
|
||||
icon: item.icon,
|
||||
sectionHeading: item.sectionHeading
|
||||
});
|
||||
groups.set(item.sectionHeading, groupItems);
|
||||
}
|
||||
|
||||
return Array.from(groups.entries()).map(([heading, items]) => ({
|
||||
heading: t(heading),
|
||||
items
|
||||
}));
|
||||
}, [navItems, params, t]);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
"use client";
|
||||
|
||||
import { ListUserOrgsResponse } from "@server/routers/org";
|
||||
import { usePathname } from "next/navigation";
|
||||
import { useMemo } from "react";
|
||||
|
||||
export type OrganizationCommand = {
|
||||
id: string;
|
||||
orgId: string;
|
||||
name: string;
|
||||
isPrimaryOrg?: boolean;
|
||||
href: string;
|
||||
};
|
||||
|
||||
export function useCommandPaletteOrganizations(
|
||||
orgs: ListUserOrgsResponse["orgs"] | undefined
|
||||
): OrganizationCommand[] {
|
||||
const pathname = usePathname();
|
||||
|
||||
return useMemo(() => {
|
||||
if (!orgs?.length) return [];
|
||||
|
||||
const sortedOrgs = [...orgs].sort((a, b) => {
|
||||
const aPrimary = Boolean(a.isPrimaryOrg);
|
||||
const bPrimary = Boolean(b.isPrimaryOrg);
|
||||
if (aPrimary && !bPrimary) return -1;
|
||||
if (!aPrimary && bPrimary) return 1;
|
||||
return 0;
|
||||
});
|
||||
|
||||
return sortedOrgs.map((org) => {
|
||||
const newPath = pathname.includes("/settings/")
|
||||
? pathname.replace(/^\/[^/]+/, `/${org.orgId}`)
|
||||
: `/${org.orgId}`;
|
||||
|
||||
return {
|
||||
id: `org-${org.orgId}`,
|
||||
orgId: org.orgId,
|
||||
name: org.name,
|
||||
isPrimaryOrg: org.isPrimaryOrg,
|
||||
href: newPath
|
||||
};
|
||||
});
|
||||
}, [orgs, pathname]);
|
||||
}
|
||||
142
src/components/command-palette/useCommandPaletteSearch.ts
Normal file
142
src/components/command-palette/useCommandPaletteSearch.ts
Normal file
@@ -0,0 +1,142 @@
|
||||
"use client";
|
||||
|
||||
import { orgQueries } from "@app/lib/queries";
|
||||
import { useQueries } from "@tanstack/react-query";
|
||||
import { useMemo } from "react";
|
||||
import { useDebounce } from "use-debounce";
|
||||
|
||||
const SEARCH_PER_PAGE = 5;
|
||||
const MIN_QUERY_LENGTH = 2;
|
||||
|
||||
export type SiteSearchResult = {
|
||||
id: string;
|
||||
name: string;
|
||||
href: string;
|
||||
};
|
||||
|
||||
export type ResourceSearchResult = {
|
||||
id: string;
|
||||
name: string;
|
||||
href: string;
|
||||
};
|
||||
|
||||
export type UserSearchResult = {
|
||||
id: string;
|
||||
name: string;
|
||||
email: string;
|
||||
href: string;
|
||||
};
|
||||
|
||||
export type ClientSearchResult = {
|
||||
id: string;
|
||||
name: string;
|
||||
href: string;
|
||||
};
|
||||
|
||||
export function useCommandPaletteSearch({
|
||||
orgId,
|
||||
query,
|
||||
enabled
|
||||
}: {
|
||||
orgId?: string;
|
||||
query: string;
|
||||
enabled: boolean;
|
||||
}) {
|
||||
const [debouncedQuery] = useDebounce(query, 150);
|
||||
const trimmedQuery = debouncedQuery.trim();
|
||||
const shouldSearch =
|
||||
enabled && !!orgId && trimmedQuery.length >= MIN_QUERY_LENGTH;
|
||||
|
||||
const [sitesQuery, resourcesQuery, usersQuery, clientsQuery] = useQueries({
|
||||
queries: [
|
||||
{
|
||||
...orgQueries.sites({
|
||||
orgId: orgId ?? "",
|
||||
query: trimmedQuery,
|
||||
perPage: SEARCH_PER_PAGE
|
||||
}),
|
||||
enabled: shouldSearch
|
||||
},
|
||||
{
|
||||
...orgQueries.resources({
|
||||
orgId: orgId ?? "",
|
||||
query: trimmedQuery,
|
||||
perPage: SEARCH_PER_PAGE
|
||||
}),
|
||||
enabled: shouldSearch
|
||||
},
|
||||
{
|
||||
...orgQueries.users({
|
||||
orgId: orgId ?? "",
|
||||
query: trimmedQuery,
|
||||
perPage: SEARCH_PER_PAGE
|
||||
}),
|
||||
enabled: shouldSearch
|
||||
},
|
||||
{
|
||||
...orgQueries.machineClients({
|
||||
orgId: orgId ?? "",
|
||||
query: trimmedQuery,
|
||||
perPage: SEARCH_PER_PAGE
|
||||
}),
|
||||
enabled: shouldSearch
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
const sites = useMemo((): SiteSearchResult[] => {
|
||||
if (!orgId || !sitesQuery.data) return [];
|
||||
return sitesQuery.data.map((site) => ({
|
||||
id: `site-${site.siteId}`,
|
||||
name: site.name,
|
||||
href: `/${orgId}/settings/sites/${site.niceId}`
|
||||
}));
|
||||
}, [orgId, sitesQuery.data]);
|
||||
|
||||
const resources = useMemo((): ResourceSearchResult[] => {
|
||||
if (!orgId || !resourcesQuery.data) return [];
|
||||
return resourcesQuery.data.map((resource) => ({
|
||||
id: `resource-${resource.resourceId}`,
|
||||
name: resource.name,
|
||||
href: `/${orgId}/settings/resources/proxy/${resource.niceId}`
|
||||
}));
|
||||
}, [orgId, resourcesQuery.data]);
|
||||
|
||||
const users = useMemo((): UserSearchResult[] => {
|
||||
if (!orgId || !usersQuery.data) return [];
|
||||
return usersQuery.data.map((user) => ({
|
||||
id: `user-${user.id}`,
|
||||
name: user.name ?? user.email ?? user.username ?? "",
|
||||
email: user.email ?? user.username ?? "",
|
||||
href: `/${orgId}/settings/access/users/${user.id}`
|
||||
}));
|
||||
}, [orgId, usersQuery.data]);
|
||||
|
||||
const machineClients = useMemo((): ClientSearchResult[] => {
|
||||
if (!orgId || !clientsQuery.data) return [];
|
||||
return clientsQuery.data
|
||||
.filter((client) => !client.userId)
|
||||
.map((client) => ({
|
||||
id: `client-${client.clientId}`,
|
||||
name: client.name,
|
||||
href: `/${orgId}/settings/clients/machine/${client.niceId}`
|
||||
}));
|
||||
}, [orgId, clientsQuery.data]);
|
||||
|
||||
const isLoading =
|
||||
shouldSearch &&
|
||||
(sitesQuery.isFetching ||
|
||||
resourcesQuery.isFetching ||
|
||||
usersQuery.isFetching ||
|
||||
clientsQuery.isFetching);
|
||||
|
||||
return {
|
||||
debouncedQuery: trimmedQuery,
|
||||
shouldSearch,
|
||||
sites,
|
||||
resources,
|
||||
users,
|
||||
machineClients,
|
||||
isLoading
|
||||
};
|
||||
}
|
||||
42
src/lib/flattenNavItems.ts
Normal file
42
src/lib/flattenNavItems.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { ReactNode } from "react";
|
||||
import type {
|
||||
SidebarNavItem,
|
||||
SidebarNavSection
|
||||
} from "@app/components/SidebarNav";
|
||||
|
||||
export type FlatNavItem = {
|
||||
title: string;
|
||||
href: string;
|
||||
icon?: ReactNode;
|
||||
sectionHeading: string;
|
||||
};
|
||||
|
||||
function flattenItems(
|
||||
items: SidebarNavItem[],
|
||||
sectionHeading: string,
|
||||
result: FlatNavItem[]
|
||||
) {
|
||||
for (const item of items) {
|
||||
if (item.href) {
|
||||
result.push({
|
||||
title: item.title,
|
||||
href: item.href,
|
||||
icon: item.icon,
|
||||
sectionHeading
|
||||
});
|
||||
}
|
||||
if (item.items?.length) {
|
||||
flattenItems(item.items, sectionHeading, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function flattenNavSections(
|
||||
sections: SidebarNavSection[]
|
||||
): FlatNavItem[] {
|
||||
const result: FlatNavItem[] = [];
|
||||
for (const section of sections) {
|
||||
flattenItems(section.items, section.heading, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
35
src/lib/hydrateNavHref.ts
Normal file
35
src/lib/hydrateNavHref.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
export type NavHrefParams = {
|
||||
orgId?: string;
|
||||
niceId?: string;
|
||||
resourceId?: string;
|
||||
userId?: string;
|
||||
apiKeyId?: string;
|
||||
clientId?: string;
|
||||
};
|
||||
|
||||
export function hydrateNavHref(
|
||||
val: string | undefined,
|
||||
params: NavHrefParams
|
||||
): string | undefined {
|
||||
if (!val) return undefined;
|
||||
return val
|
||||
.replace("{orgId}", params.orgId ?? "")
|
||||
.replace("{niceId}", params.niceId ?? "")
|
||||
.replace("{resourceId}", params.resourceId ?? "")
|
||||
.replace("{userId}", params.userId ?? "")
|
||||
.replace("{apiKeyId}", params.apiKeyId ?? "")
|
||||
.replace("{clientId}", params.clientId ?? "");
|
||||
}
|
||||
|
||||
export function navHrefParamsFromRoute(
|
||||
params: Record<string, string | string[] | undefined>
|
||||
): NavHrefParams {
|
||||
return {
|
||||
orgId: params.orgId as string | undefined,
|
||||
niceId: params.niceId as string | undefined,
|
||||
resourceId: params.resourceId as string | undefined,
|
||||
userId: params.userId as string | undefined,
|
||||
apiKeyId: params.apiKeyId as string | undefined,
|
||||
clientId: params.clientId as string | undefined
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user