import { Trans, useLingui } from "@lingui/react/macro"
import { useStore } from "@nanostores/react"
import { getPagePath } from "@nanostores/router"
import {
BellIcon,
CopyIcon,
MoreHorizontalIcon,
PauseCircleIcon,
PenBoxIcon,
PlayCircleIcon,
Trash2Icon,
} from "lucide-react"
import { memo, useMemo, useRef, useState } from "react"
import { SystemDialog } from "@/components/add-system"
import { AlertDialogContent as AlertsSheetContent, alertsSheetClassName } from "@/components/alerts/alerts-sheet"
import { $router, navigate } from "@/components/router"
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog"
import { Button, buttonVariants } from "@/components/ui/button"
import { Dialog } from "@/components/ui/dialog"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { Sheet, SheetContent } from "@/components/ui/sheet"
import { isReadOnlyUser, pb } from "@/lib/api"
import { SystemStatus } from "@/lib/enums"
import { $alerts } from "@/lib/stores"
import { cn, copyToClipboard } from "@/lib/utils"
import type { SystemRecord } from "@/types"
/** Menu item that opens the alerts sheet; the bell is filled when the system has alerts configured. */
const AlertsMenuItem = ({ systemId, onSelect }: { systemId: string; onSelect: () => void }) => {
const alerts = useStore($alerts)
const hasSystemAlert = alerts[systemId]?.size > 0
return (
Alerts
)
}
async function deleteSystem({ id, name }: SystemRecord) {
await pb.collection("systems").delete(id)
// leave the dashboard of the system that was just deleted (route param may be the id or legacy name slug)
const page = $router.get()
if (page?.route === "system" && (page.params.id === id || page.params.id === name)) {
navigate(getPagePath($router, "home"))
}
}
type ActionsButtonProps = {
system: SystemRecord
variant?: "ghost" | "outline"
/** Include an item that opens the alerts sheet (used where no separate alerts button is shown). */
showAlerts?: boolean
}
export const ActionsButton = memo(({ system, variant = "ghost", showAlerts = false }: ActionsButtonProps) => {
const [deleteOpen, setDeleteOpen] = useState(false)
const [editOpen, setEditOpen] = useState(false)
const editOpened = useRef(false)
const [alertsOpen, setAlertsOpen] = useState(false)
const alertsOpened = useRef(false)
const { t } = useLingui()
const { id, status, host, name } = system
return useMemo(() => {
return (
<>
{showAlerts && (
<>
{
alertsOpened.current = true
setAlertsOpen(true)
}}
/>
>
)}
{!isReadOnlyUser() && (
{
editOpened.current = true
setEditOpen(true)
}}
>
Edit
)}
{
pb.collection("systems").update(id, {
status: status === SystemStatus.Paused ? SystemStatus.Pending : SystemStatus.Paused,
})
}}
>
{status === SystemStatus.Paused ? (
<>
Resume
>
) : (
<>
Pause
>
)}
copyToClipboard(name)}>
Copy name
copyToClipboard(host)}>
Copy host
setDeleteOpen(true)}>
Delete
{/* alerts sheet */}
{showAlerts && (
{alertsOpened.current && }
)}
{/* edit dialog */}
{/* deletion dialog */}
setDeleteOpen(open)}>
Are you sure you want to delete {name}?
This action cannot be undone. This will permanently delete all current records for {name} from the
database.
Cancel
deleteSystem(system)}
>
Continue
>
)
}, [id, status, host, name, system, t, deleteOpen, editOpen, alertsOpen, variant, showAlerts])
})