|
| 1 | +import { |
| 2 | + Badge, |
| 3 | + Button, |
| 4 | + Dialog, |
| 5 | + DialogContent, |
| 6 | + DialogFooter, |
| 7 | + DialogHeader, |
| 8 | + DialogTitle, |
| 9 | +} from "@posthog/quill"; |
| 10 | +import { CodeBlock } from "@posthog/ui/primitives/CodeBlock"; |
| 11 | +import { openTaskInput } from "@posthog/ui/router/useOpenTask"; |
| 12 | +import { formatCapturedLogs } from "@posthog/ui/shell/logCapture"; |
| 13 | +import { serializeError, useErrorDetailsStore } from "./errorDetails"; |
| 14 | + |
| 15 | +// Keep the create-task prompt readable: the full 500-entry buffer belongs in |
| 16 | +// the downloaded bundle, not in a composer draft. |
| 17 | +const TASK_PROMPT_LOG_ENTRIES = 100; |
| 18 | + |
| 19 | +function buildBundle( |
| 20 | + title: string, |
| 21 | + occurredAt: number, |
| 22 | + prettyError: string, |
| 23 | +): string { |
| 24 | + return [ |
| 25 | + `# ${title}`, |
| 26 | + `Occurred at: ${new Date(occurredAt).toISOString()}`, |
| 27 | + "", |
| 28 | + "## Error", |
| 29 | + prettyError, |
| 30 | + "", |
| 31 | + "## Recent logs", |
| 32 | + formatCapturedLogs(), |
| 33 | + "", |
| 34 | + ].join("\n"); |
| 35 | +} |
| 36 | + |
| 37 | +// Global inspector behind every error toast's "Details" action: the full |
| 38 | +// pretty-printed payload the toast had no room for, a downloadable |
| 39 | +// error-plus-logs bundle, and (dev builds only) a one-click task prefilled |
| 40 | +// with the same context. |
| 41 | +export function ErrorDetailsDialog() { |
| 42 | + const detail = useErrorDetailsStore((s) => s.detail); |
| 43 | + const close = useErrorDetailsStore((s) => s.close); |
| 44 | + if (!detail) return null; |
| 45 | + |
| 46 | + const prettyError = serializeError(detail.error); |
| 47 | + |
| 48 | + const handleDownload = () => { |
| 49 | + const bundle = buildBundle(detail.title, detail.occurredAt, prettyError); |
| 50 | + const blob = new Blob([bundle], { type: "text/markdown" }); |
| 51 | + const url = URL.createObjectURL(blob); |
| 52 | + const anchor = document.createElement("a"); |
| 53 | + anchor.href = url; |
| 54 | + anchor.download = `posthog-code-error-${detail.occurredAt}.md`; |
| 55 | + anchor.click(); |
| 56 | + URL.revokeObjectURL(url); |
| 57 | + }; |
| 58 | + |
| 59 | + const handleCreateTask = () => { |
| 60 | + close(); |
| 61 | + openTaskInput({ |
| 62 | + initialPrompt: [ |
| 63 | + `Investigate this error from the PostHog Code app: ${detail.title}`, |
| 64 | + "", |
| 65 | + "## Error", |
| 66 | + "```", |
| 67 | + prettyError, |
| 68 | + "```", |
| 69 | + "", |
| 70 | + "## Recent logs", |
| 71 | + "```", |
| 72 | + formatCapturedLogs({ maxEntries: TASK_PROMPT_LOG_ENTRIES }), |
| 73 | + "```", |
| 74 | + ].join("\n"), |
| 75 | + }); |
| 76 | + }; |
| 77 | + |
| 78 | + return ( |
| 79 | + <Dialog open onOpenChange={(open) => !open && close()}> |
| 80 | + <DialogContent className="w-[min(720px,calc(100vw-32px))] max-w-[720px] sm:max-w-[720px]"> |
| 81 | + <DialogHeader> |
| 82 | + <DialogTitle>{detail.title}</DialogTitle> |
| 83 | + </DialogHeader> |
| 84 | + <div className="max-h-[55vh] overflow-y-auto px-1"> |
| 85 | + <CodeBlock size="1">{prettyError}</CodeBlock> |
| 86 | + </div> |
| 87 | + <DialogFooter> |
| 88 | + {import.meta.env.DEV && ( |
| 89 | + <Button |
| 90 | + variant="outline" |
| 91 | + size="sm" |
| 92 | + onClick={handleCreateTask} |
| 93 | + className="gap-2 sm:mr-auto" |
| 94 | + > |
| 95 | + Create task from error |
| 96 | + <Badge variant="warning">Dev</Badge> |
| 97 | + </Button> |
| 98 | + )} |
| 99 | + <Button variant="outline" size="sm" onClick={handleDownload}> |
| 100 | + Download error + logs |
| 101 | + </Button> |
| 102 | + <Button variant="primary" size="sm" onClick={close}> |
| 103 | + Close |
| 104 | + </Button> |
| 105 | + </DialogFooter> |
| 106 | + </DialogContent> |
| 107 | + </Dialog> |
| 108 | + ); |
| 109 | +} |
0 commit comments