Skip to content

Commit

Permalink
feat: handle selected alert via query params (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo authored Nov 13, 2024
1 parent 796362c commit df24ec1
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 66 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"lucide-react": "^0.454.0",
"next": "15.0.2",
"next-themes": "^0.4.1",
"nuqs": "^2.1.2",
"react": "19.0.0-rc-02c0e824-20241028",
"react-dom": "19.0.0-rc-02c0e824-20241028",
"tailwind-merge": "^2.5.4",
Expand Down
30 changes: 30 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 17 additions & 14 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import localFont from "next/font/local";
import "./globals.css";
import { ConfigProvider } from "@/contexts/config";
import { ThemeProvider } from "next-themes";
import { NuqsAdapter } from 'nuqs/adapters/next/app'
import { AppLayout } from "@/components/layout/app-layout";
import { AlertsProvider } from "@/contexts/alerts";

Expand Down Expand Up @@ -34,20 +35,22 @@ export default async function RootLayout({
<body
className={`${geistSans.variable} ${geistMono.variable} font-sans antialiased`}
>
<ThemeProvider
attribute="class"
defaultTheme="system"
enableSystem
disableTransitionOnChange
>
<ConfigProvider>
<AlertsProvider>
<AppLayout>
{children}
</AppLayout>
</AlertsProvider>
</ConfigProvider>
</ThemeProvider>
<NuqsAdapter>
<ThemeProvider
attribute="class"
defaultTheme="system"
enableSystem
disableTransitionOnChange
>
<ConfigProvider>
<AlertsProvider>
<AppLayout>
{children}
</AppLayout>
</AlertsProvider>
</ConfigProvider>
</ThemeProvider>
</NuqsAdapter>
</body>
</html>
);
Expand Down
8 changes: 2 additions & 6 deletions src/components/alerts/alert-groups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@ import { Group } from "./types"

type AlertGroupsProps = {
alertGroups: Group[]
setSelectedAlert: (alert: Alert | null) => void
}

export function AlertGroups(props: AlertGroupsProps) {
const { alertGroups, setSelectedAlert } = props
const { alertGroups } = props

return (
<div>
{alertGroups.map((alertGroup) => (
<AlertGroup
key={alertGroup.name}
alertGroup={alertGroup}
setSelectedAlert={setSelectedAlert}
/>
))}
</div>
Expand All @@ -35,11 +33,10 @@ export function AlertGroups(props: AlertGroupsProps) {

type AlertGroupProps = {
alertGroup: Group
setSelectedAlert: (alert: Alert | null) => void
}

function AlertGroup(props: AlertGroupProps) {
const { alertGroup, setSelectedAlert } = props
const { alertGroup } = props
const { name, alerts } = alertGroup
const [open, setOpen] = useState(true)

Expand All @@ -58,7 +55,6 @@ function AlertGroup(props: AlertGroupProps) {
{alerts.map((alert: Alert) => (
<li
key={alert.fingerprint}
onClick={() => setSelectedAlert(alert)}
className={cn({
'animate-highlight': new Date(alert.startsAt) > new Date(Date.now() - 30 * 60 * 1000)
})}
Expand Down
14 changes: 10 additions & 4 deletions src/components/alerts/alert-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client'

import { useMemo, useState } from "react"
import {
Drawer,
Expand All @@ -20,6 +22,7 @@ import { Alert } from "@/types/alertmanager"
import { Check, ClipboardCopy, Square, SquareArrowOutUpRight } from "lucide-react"
import { cn } from "@/lib/utils"
import { AlertSeverity } from "./alert-severity"
import { useQueryState } from "nuqs"

function isURL(string: string): boolean {
try {
Expand All @@ -32,16 +35,19 @@ function isURL(string: string): boolean {

type Props = {
alert: Alert | null
setSelectedAlert: (alert: Alert | null) => void
close: () => void
}

export function AlertModal(props: Props) {
const { alert, close } = props
const { alert } = props
const { summary } = alert?.annotations || {}
const [selectedAlertId, setSelectedAlertId] = useQueryState('alert', { defaultValue: '' })

const close = () => {
setSelectedAlertId(null)
}

return (
<Sheet open={!!alert} onOpenChange={close}>
<Sheet open={!!selectedAlertId} onOpenChange={close}>
{/* <SheetTrigger>Open</SheetTrigger> */}
<SheetContent className="w-screen">
<div className="flex flex-col h-screen">
Expand Down
15 changes: 13 additions & 2 deletions src/components/alerts/alert-row.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Link from "next/link"
import { useRouter } from "next/navigation"
import { Alert } from "@/types/alertmanager"
import { formatDate } from "@/lib/date"
import { stringToColor } from "./utils"
Expand All @@ -21,18 +23,27 @@ type Props = {
alert: Alert
}

function paramsWithAlert(id: string) {
const params = new URLSearchParams(window.location.search)
params.set('alert', id)
return params.toString()
}

export function AlertRow(props: Props) {
const { alert } = props

return (
<div className="flex gap-2 xl:gap-4 items-center px-6 h-[45px] border-b group cursor-pointer">
<Link
href={`?${paramsWithAlert(alert.fingerprint)}`}
className="flex gap-2 xl:gap-4 items-center px-6 h-[45px] border-b group cursor-pointer"
>
<AlertSeverity alert={alert} />
<AlertTitle alert={alert} />
<AlertSummary alert={alert} />
<div className="grow" />
<AlertLabels alert={alert} />
<AlertTime alert={alert} />
</div>
</Link>
)
}

Expand Down
Loading

0 comments on commit df24ec1

Please sign in to comment.