|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { useParams, useNavigate } from "react-router"; |
| 3 | +import { apiRoute } from "./util"; |
| 4 | +import { ApiEventsPage, EventData, convertApiEventToEventData } from "./types"; |
| 5 | +import { Box, Alert } from "@launchpad-ui/core"; |
| 6 | +import { Heading, Text, ProgressBar, Button } from "@launchpad-ui/components"; |
| 7 | +import { Icon } from "@launchpad-ui/icons"; |
| 8 | +import EventsTable from "./EventsTable"; |
| 9 | + |
| 10 | +const DebugSessionEventsPage = () => { |
| 11 | + const { debugSessionKey } = useParams<{ debugSessionKey: string }>(); |
| 12 | + const navigate = useNavigate(); |
| 13 | + const [events, setEvents] = useState<EventData[]>([]); |
| 14 | + const [loading, setLoading] = useState<boolean>(true); |
| 15 | + const [error, setError] = useState<string | null>(null); |
| 16 | + const [totalCount, setTotalCount] = useState<number>(0); |
| 17 | + |
| 18 | + const fetchEvents = async () => { |
| 19 | + if (!debugSessionKey) { |
| 20 | + setError("Debug session key is required"); |
| 21 | + setLoading(false); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + try { |
| 26 | + setLoading(true); |
| 27 | + setError(null); |
| 28 | + |
| 29 | + const response = await fetch(apiRoute(`/dev/debug-sessions/${encodeURIComponent(debugSessionKey)}/events?limit=1000`)); |
| 30 | + |
| 31 | + if (!response.ok) { |
| 32 | + throw new Error(`Failed to fetch events: ${response.status} ${response.statusText}`); |
| 33 | + } |
| 34 | + |
| 35 | + const data: ApiEventsPage = await response.json(); |
| 36 | + const convertedEvents = data.events.map(convertApiEventToEventData); |
| 37 | + setEvents(convertedEvents); |
| 38 | + setTotalCount(data.total_count); |
| 39 | + } catch (err) { |
| 40 | + setError(err instanceof Error ? err.message : "An unknown error occurred"); |
| 41 | + } finally { |
| 42 | + setLoading(false); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + fetchEvents(); |
| 48 | + }, [debugSessionKey]); |
| 49 | + |
| 50 | + const handleBackToSessions = () => { |
| 51 | + navigate("/ui/debug-sessions"); |
| 52 | + }; |
| 53 | + |
| 54 | + if (loading) { |
| 55 | + return ( |
| 56 | + <Box padding="2rem"> |
| 57 | + <Box display="flex" alignItems="center" marginBottom="1rem"> |
| 58 | + <Button onPress={handleBackToSessions} variant="minimal"> |
| 59 | + <Icon name="arrow-left" size="small" /> |
| 60 | + <Text marginLeft="0.5rem">Back to Debug Sessions</Text> |
| 61 | + </Button> |
| 62 | + </Box> |
| 63 | + <Heading>Debug Session Events</Heading> |
| 64 | + <Box marginTop="1rem"> |
| 65 | + <Text color="var(--lp-color-text-ui-secondary)"> |
| 66 | + Session: {debugSessionKey} |
| 67 | + </Text> |
| 68 | + </Box> |
| 69 | + <Box marginTop="1rem"> |
| 70 | + <ProgressBar isIndeterminate /> |
| 71 | + </Box> |
| 72 | + <Box marginTop="1rem"> |
| 73 | + <Text>Loading events...</Text> |
| 74 | + </Box> |
| 75 | + </Box> |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + if (error) { |
| 80 | + return ( |
| 81 | + <Box padding="2rem"> |
| 82 | + <Box display="flex" alignItems="center" marginBottom="1rem"> |
| 83 | + <Button onPress={handleBackToSessions} variant="minimal"> |
| 84 | + <Icon name="arrow-left" size="small" /> |
| 85 | + <Text marginLeft="0.5rem">Back to Debug Sessions</Text> |
| 86 | + </Button> |
| 87 | + </Box> |
| 88 | + <Heading>Debug Session Events</Heading> |
| 89 | + <Box marginTop="1rem"> |
| 90 | + <Text color="var(--lp-color-text-ui-secondary)"> |
| 91 | + Session: {debugSessionKey} |
| 92 | + </Text> |
| 93 | + </Box> |
| 94 | + <Box marginTop="1rem"> |
| 95 | + <Alert kind="error"> |
| 96 | + <Text>Error: {error}</Text> |
| 97 | + </Alert> |
| 98 | + </Box> |
| 99 | + <Box marginTop="1rem"> |
| 100 | + <Button onPress={fetchEvents}>Retry</Button> |
| 101 | + </Box> |
| 102 | + </Box> |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + return ( |
| 107 | + <Box padding="2rem"> |
| 108 | + <Box display="flex" alignItems="center" marginBottom="1rem"> |
| 109 | + <Button onPress={handleBackToSessions} variant="minimal"> |
| 110 | + <Icon name="arrow-left" size="small" /> |
| 111 | + <Text marginLeft="0.5rem">Back to Debug Sessions</Text> |
| 112 | + </Button> |
| 113 | + </Box> |
| 114 | + |
| 115 | + <Box display="flex" justifyContent="space-between" alignItems="center" marginBottom="1rem"> |
| 116 | + <Box> |
| 117 | + <Heading>Debug Session Events</Heading> |
| 118 | + <Box marginTop="0.5rem"> |
| 119 | + <Text color="var(--lp-color-text-ui-secondary)" style={{ fontFamily: "monospace" }}> |
| 120 | + Session: {debugSessionKey} |
| 121 | + </Text> |
| 122 | + </Box> |
| 123 | + </Box> |
| 124 | + <Text color="var(--lp-color-text-ui-secondary)"> |
| 125 | + {totalCount} total event{totalCount !== 1 ? 's' : ''} |
| 126 | + </Text> |
| 127 | + </Box> |
| 128 | + |
| 129 | + {events.length === 0 ? ( |
| 130 | + <Box |
| 131 | + padding="2rem" |
| 132 | + textAlign="center" |
| 133 | + backgroundColor="var(--lp-color-bg-ui-secondary)" |
| 134 | + borderRadius="4px" |
| 135 | + > |
| 136 | + <Icon name="data" size="large" /> |
| 137 | + <Box marginTop="1rem"> |
| 138 | + <Text>No events found for this debug session</Text> |
| 139 | + </Box> |
| 140 | + <Box marginTop="0.5rem"> |
| 141 | + <Text color="var(--lp-color-text-ui-secondary)"> |
| 142 | + Events will appear here when they are captured for this session |
| 143 | + </Text> |
| 144 | + </Box> |
| 145 | + </Box> |
| 146 | + ) : ( |
| 147 | + <EventsTable events={events} /> |
| 148 | + )} |
| 149 | + </Box> |
| 150 | + ); |
| 151 | +}; |
| 152 | + |
| 153 | +export default DebugSessionEventsPage; |
0 commit comments