|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { apiRoute } from "./util"; |
| 3 | +import { DebugSession, DebugSessionsPage as DebugSessionsPageType } from "./types"; |
| 4 | +import { Box, CopyToClipboard, Alert } from "@launchpad-ui/core"; |
| 5 | +import { Heading, Text, ProgressBar, Button } from "@launchpad-ui/components"; |
| 6 | +import { Icon } from "@launchpad-ui/icons"; |
| 7 | + |
| 8 | +const DebugSessionsPage = () => { |
| 9 | + const [debugSessions, setDebugSessions] = useState<DebugSession[]>([]); |
| 10 | + const [loading, setLoading] = useState<boolean>(true); |
| 11 | + const [error, setError] = useState<string | null>(null); |
| 12 | + const [totalCount, setTotalCount] = useState<number>(0); |
| 13 | + |
| 14 | + const fetchDebugSessions = async () => { |
| 15 | + try { |
| 16 | + setLoading(true); |
| 17 | + setError(null); |
| 18 | + |
| 19 | + const response = await fetch(apiRoute("/dev/debug-sessions?limit=100")); |
| 20 | + |
| 21 | + if (!response.ok) { |
| 22 | + throw new Error(`Failed to fetch debug sessions: ${response.status} ${response.statusText}`); |
| 23 | + } |
| 24 | + |
| 25 | + const data: DebugSessionsPageType = await response.json(); |
| 26 | + setDebugSessions(data.sessions); |
| 27 | + setTotalCount(data.total_count); |
| 28 | + } catch (err) { |
| 29 | + setError(err instanceof Error ? err.message : "An unknown error occurred"); |
| 30 | + } finally { |
| 31 | + setLoading(false); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + fetchDebugSessions(); |
| 37 | + }, []); |
| 38 | + |
| 39 | + const formatDate = (dateString: string) => { |
| 40 | + try { |
| 41 | + const date = new Date(dateString); |
| 42 | + return date.toLocaleString(); |
| 43 | + } catch { |
| 44 | + return dateString; |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + if (loading) { |
| 49 | + return ( |
| 50 | + <Box padding="2rem"> |
| 51 | + <Heading>Debug Sessions</Heading> |
| 52 | + <Box marginTop="1rem"> |
| 53 | + <ProgressBar isIndeterminate /> |
| 54 | + </Box> |
| 55 | + <Box marginTop="1rem"> |
| 56 | + <Text>Loading debug sessions...</Text> |
| 57 | + </Box> |
| 58 | + </Box> |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + if (error) { |
| 63 | + return ( |
| 64 | + <Box padding="2rem"> |
| 65 | + <Heading>Debug Sessions</Heading> |
| 66 | + <Box marginTop="1rem"> |
| 67 | + <Alert kind="error"> |
| 68 | + <Text>Error: {error}</Text> |
| 69 | + </Alert> |
| 70 | + </Box> |
| 71 | + <Box marginTop="1rem"> |
| 72 | + <Button onPress={fetchDebugSessions}>Retry</Button> |
| 73 | + </Box> |
| 74 | + </Box> |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + return ( |
| 79 | + <Box padding="2rem"> |
| 80 | + <Box display="flex" justifyContent="space-between" alignItems="center" marginBottom="1rem"> |
| 81 | + <Heading>Debug Sessions</Heading> |
| 82 | + <Text color="var(--lp-color-text-ui-secondary)"> |
| 83 | + {totalCount} total session{totalCount !== 1 ? 's' : ''} |
| 84 | + </Text> |
| 85 | + </Box> |
| 86 | + |
| 87 | + {debugSessions.length === 0 ? ( |
| 88 | + <Box |
| 89 | + padding="2rem" |
| 90 | + textAlign="center" |
| 91 | + backgroundColor="var(--lp-color-bg-ui-secondary)" |
| 92 | + borderRadius="4px" |
| 93 | + > |
| 94 | + <Icon name="data" size="large" /> |
| 95 | + <Box marginTop="1rem"> |
| 96 | + <Text>No debug sessions found</Text> |
| 97 | + </Box> |
| 98 | + <Box marginTop="0.5rem"> |
| 99 | + <Text color="var(--lp-color-text-ui-secondary)"> |
| 100 | + Debug sessions will appear here when events are captured |
| 101 | + </Text> |
| 102 | + </Box> |
| 103 | + </Box> |
| 104 | + ) : ( |
| 105 | + <div |
| 106 | + style={{ |
| 107 | + border: "1px solid var(--lp-color-border-ui-primary)", |
| 108 | + borderRadius: "4px", |
| 109 | + overflow: "hidden" |
| 110 | + }} |
| 111 | + > |
| 112 | + <table style={{ width: "100%", borderCollapse: "collapse" }}> |
| 113 | + <thead> |
| 114 | + <tr style={{ backgroundColor: "var(--lp-color-bg-ui-secondary)" }}> |
| 115 | + <th style={{ |
| 116 | + padding: "0.75rem", |
| 117 | + textAlign: "left", |
| 118 | + borderBottom: "1px solid var(--lp-color-border-ui-primary)", |
| 119 | + fontWeight: 600 |
| 120 | + }}> |
| 121 | + Session Key |
| 122 | + </th> |
| 123 | + <th style={{ |
| 124 | + padding: "0.75rem", |
| 125 | + textAlign: "left", |
| 126 | + borderBottom: "1px solid var(--lp-color-border-ui-primary)", |
| 127 | + fontWeight: 600 |
| 128 | + }}> |
| 129 | + Created At |
| 130 | + </th> |
| 131 | + <th style={{ |
| 132 | + padding: "0.75rem", |
| 133 | + textAlign: "right", |
| 134 | + borderBottom: "1px solid var(--lp-color-border-ui-primary)", |
| 135 | + fontWeight: 600 |
| 136 | + }}> |
| 137 | + Event Count |
| 138 | + </th> |
| 139 | + <th style={{ |
| 140 | + padding: "0.75rem", |
| 141 | + textAlign: "center", |
| 142 | + borderBottom: "1px solid var(--lp-color-border-ui-primary)", |
| 143 | + fontWeight: 600, |
| 144 | + width: "100px" |
| 145 | + }}> |
| 146 | + Actions |
| 147 | + </th> |
| 148 | + </tr> |
| 149 | + </thead> |
| 150 | + <tbody> |
| 151 | + {debugSessions.map((session, index) => ( |
| 152 | + <tr |
| 153 | + key={session.key} |
| 154 | + style={{ |
| 155 | + borderBottom: index < debugSessions.length - 1 ? "1px solid var(--lp-color-border-ui-primary)" : "none" |
| 156 | + }} |
| 157 | + > |
| 158 | + <td style={{ padding: "0.75rem" }}> |
| 159 | + <Text style={{ fontFamily: "monospace" }}> |
| 160 | + {session.key} |
| 161 | + </Text> |
| 162 | + </td> |
| 163 | + <td style={{ padding: "0.75rem" }}> |
| 164 | + <Text> |
| 165 | + {formatDate(session.written_at)} |
| 166 | + </Text> |
| 167 | + </td> |
| 168 | + <td style={{ padding: "0.75rem", textAlign: "right" }}> |
| 169 | + <Text> |
| 170 | + {session.event_count.toLocaleString()} |
| 171 | + </Text> |
| 172 | + </td> |
| 173 | + <td style={{ padding: "0.75rem", textAlign: "center" }}> |
| 174 | + <CopyToClipboard text={session.key}> |
| 175 | + <button |
| 176 | + style={{ |
| 177 | + background: "none", |
| 178 | + border: "1px solid var(--lp-color-border-ui-primary)", |
| 179 | + borderRadius: "4px", |
| 180 | + padding: "0.25rem 0.5rem", |
| 181 | + cursor: "pointer", |
| 182 | + display: "flex", |
| 183 | + alignItems: "center", |
| 184 | + gap: "0.25rem" |
| 185 | + }} |
| 186 | + title="Copy session key" |
| 187 | + > |
| 188 | + <Icon name="link" size="small" /> |
| 189 | + </button> |
| 190 | + </CopyToClipboard> |
| 191 | + </td> |
| 192 | + </tr> |
| 193 | + ))} |
| 194 | + </tbody> |
| 195 | + </table> |
| 196 | + </div> |
| 197 | + )} |
| 198 | + </Box> |
| 199 | + ); |
| 200 | +}; |
| 201 | + |
| 202 | +export default DebugSessionsPage; |
0 commit comments