|
| 1 | +import { |
| 2 | + Dialog, |
| 3 | + DialogContent, |
| 4 | + DialogDescription, |
| 5 | + DialogHeader, |
| 6 | + DialogTitle, |
| 7 | +} from "@/components/ui/dialog" |
| 8 | +import React, { useEffect } from "react"; |
| 9 | +import { AppExtendedModel } from "@/shared/model/app-extended.model"; |
| 10 | +import { getLatestAppEvents } from "./actions"; |
| 11 | +import { toast } from "sonner"; |
| 12 | +import FullLoadingSpinner from "@/components/ui/full-loading-spinnter"; |
| 13 | +import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; |
| 14 | +import { EventInfoModel } from "@/shared/model/event-info.model"; |
| 15 | +import { formatDateTime } from "@/frontend/utils/format.utils"; |
| 16 | +import { ScrollArea } from "@radix-ui/react-scroll-area"; |
| 17 | +import { cn } from "@/frontend/utils/utils"; |
| 18 | + |
| 19 | +export function AppEventsDialog({ |
| 20 | + app, |
| 21 | + children |
| 22 | +}: { |
| 23 | + app: AppExtendedModel; |
| 24 | + children: React.ReactNode; |
| 25 | +}) { |
| 26 | + |
| 27 | + const [isOpen, setIsOpen] = React.useState(false); |
| 28 | + const [events, setEvents] = React.useState<EventInfoModel[] | undefined>(undefined); |
| 29 | + |
| 30 | + const loadEvents = async () => { |
| 31 | + try { |
| 32 | + const eventsResponse = await getLatestAppEvents(app.id); |
| 33 | + if (eventsResponse.status === 'success') { |
| 34 | + setEvents(eventsResponse.data); |
| 35 | + } else { |
| 36 | + toast.error(eventsResponse.message); |
| 37 | + } |
| 38 | + } catch (error) { |
| 39 | + console.error(error); |
| 40 | + toast.error('An error occured while loading events.'); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + if (isOpen) { |
| 46 | + loadEvents(); |
| 47 | + } else { |
| 48 | + setEvents(undefined); |
| 49 | + } |
| 50 | + }, [isOpen]); |
| 51 | + |
| 52 | + return (<> |
| 53 | + <div onClick={() => setIsOpen(true)} className="cursor-pointer"> {children}</div> |
| 54 | + <Dialog open={isOpen} onOpenChange={(isO) => { |
| 55 | + setIsOpen(isO); |
| 56 | + }}> |
| 57 | + <DialogContent className="sm:max-w-[1000px]"> |
| 58 | + <DialogHeader> |
| 59 | + <DialogTitle>App Events</DialogTitle> |
| 60 | + <DialogDescription> |
| 61 | + App events occur when changes are made to the deployment. For example, when a deployment is created, updated, or restarted. |
| 62 | + Advanced users can read these events to understand what is happening in the background. Events are only available for a short period of time. |
| 63 | + </DialogDescription> |
| 64 | + </DialogHeader> |
| 65 | + <div className="space-y-4"> |
| 66 | + {!events && <FullLoadingSpinner />} |
| 67 | + {events && <> |
| 68 | + <Table> |
| 69 | + <ScrollArea className="max-h-[70vh]"> |
| 70 | + <TableCaption>{events.length} recent Events</TableCaption> |
| 71 | + <TableHeader> |
| 72 | + <TableRow> |
| 73 | + <TableHead>Time</TableHead> |
| 74 | + <TableHead>Type</TableHead> |
| 75 | + <TableHead>Action</TableHead> |
| 76 | + <TableHead>Note</TableHead> |
| 77 | + <TableHead>Pod Name</TableHead> |
| 78 | + </TableRow> |
| 79 | + </TableHeader> |
| 80 | + <TableBody> |
| 81 | + {events.map((event, index) => ( |
| 82 | + <TableRow key={(event.eventTime + '') || index}> |
| 83 | + <TableCell>{formatDateTime(event.eventTime, true)}</TableCell> |
| 84 | + <TableCell >{event.action}</TableCell> |
| 85 | + <TableCell className={cn("font-medium", event.type !== 'Normal' ? 'text-orange-500' : '')}> |
| 86 | + {event.reason} |
| 87 | + </TableCell> |
| 88 | + <TableCell >{event.note}</TableCell> |
| 89 | + <TableCell >{event.podName}</TableCell> |
| 90 | + </TableRow> |
| 91 | + ))} |
| 92 | + </TableBody> |
| 93 | + </ScrollArea> |
| 94 | + </Table> |
| 95 | + </>} |
| 96 | + </div> |
| 97 | + </DialogContent> |
| 98 | + </Dialog> |
| 99 | + </>) |
| 100 | +} |
0 commit comments