|
| 1 | +import { BackgroundWrapper } from "@components/BackgroundWrapper"; |
| 2 | +import { Box, Flex, Spinner, Text } from "@radix-ui/themes"; |
| 3 | +import { useTRPC } from "@renderer/trpc/client"; |
| 4 | +import { useSubscription } from "@trpc/tanstack-react-query"; |
| 5 | +import { useEffect, useRef, useState } from "react"; |
| 6 | + |
| 7 | +interface ProvisioningViewProps { |
| 8 | + taskId: string; |
| 9 | +} |
| 10 | + |
| 11 | +// biome-ignore lint/suspicious/noControlCharactersInRegex: ESC is required to strip ANSI sequences |
| 12 | +const ANSI_RE = /\x1b\[[0-9;]*[A-Za-z]/g; |
| 13 | + |
| 14 | +function stripAnsi(text: string): string { |
| 15 | + return text.replace(ANSI_RE, ""); |
| 16 | +} |
| 17 | + |
| 18 | +function processOutput(lines: string[], chunk: string): string[] { |
| 19 | + const next = [...lines]; |
| 20 | + const parts = chunk.split("\n"); |
| 21 | + |
| 22 | + for (let i = 0; i < parts.length; i++) { |
| 23 | + const part = parts[i]; |
| 24 | + const crSegments = part.split("\r"); |
| 25 | + const lastSegment = crSegments[crSegments.length - 1]; |
| 26 | + |
| 27 | + if (i === 0 && next.length > 0) { |
| 28 | + if (crSegments.length > 1) { |
| 29 | + next[next.length - 1] = lastSegment; |
| 30 | + } else { |
| 31 | + next[next.length - 1] += lastSegment; |
| 32 | + } |
| 33 | + } else { |
| 34 | + next.push(lastSegment); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return next; |
| 39 | +} |
| 40 | + |
| 41 | +export function ProvisioningView({ taskId }: ProvisioningViewProps) { |
| 42 | + const trpc = useTRPC(); |
| 43 | + const [lines, setLines] = useState<string[]>([]); |
| 44 | + const scrollRef = useRef<HTMLPreElement>(null); |
| 45 | + |
| 46 | + useSubscription( |
| 47 | + trpc.provisioning.onOutput.subscriptionOptions(undefined, { |
| 48 | + onData: (data) => { |
| 49 | + if (data.taskId !== taskId) return; |
| 50 | + setLines((prev) => processOutput(prev, stripAnsi(data.data))); |
| 51 | + }, |
| 52 | + }), |
| 53 | + ); |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + const el = scrollRef.current; |
| 57 | + if (el) { |
| 58 | + el.scrollTop = el.scrollHeight; |
| 59 | + } |
| 60 | + }, []); |
| 61 | + |
| 62 | + return ( |
| 63 | + <BackgroundWrapper> |
| 64 | + <Flex direction="column" height="100%" p="3" gap="2"> |
| 65 | + <Flex align="center" gap="2"> |
| 66 | + <Spinner size="1" /> |
| 67 | + <Text size="1" weight="medium"> |
| 68 | + Setting up worktree... |
| 69 | + </Text> |
| 70 | + </Flex> |
| 71 | + <Box |
| 72 | + style={{ |
| 73 | + flex: 1, |
| 74 | + minHeight: 0, |
| 75 | + borderRadius: "var(--radius-2)", |
| 76 | + background: "var(--color-surface)", |
| 77 | + border: "1px solid var(--gray-a5)", |
| 78 | + }} |
| 79 | + > |
| 80 | + <pre |
| 81 | + ref={scrollRef} |
| 82 | + style={{ |
| 83 | + margin: 0, |
| 84 | + padding: "var(--space-2)", |
| 85 | + height: "100%", |
| 86 | + overflow: "auto", |
| 87 | + fontSize: "var(--font-size-1)", |
| 88 | + lineHeight: "var(--line-height-2)", |
| 89 | + fontFamily: "var(--code-font-family)", |
| 90 | + whiteSpace: "pre-wrap", |
| 91 | + wordBreak: "break-all", |
| 92 | + color: "var(--gray-12)", |
| 93 | + }} |
| 94 | + > |
| 95 | + {lines.join("\n")} |
| 96 | + </pre> |
| 97 | + </Box> |
| 98 | + </Flex> |
| 99 | + </BackgroundWrapper> |
| 100 | + ); |
| 101 | +} |
0 commit comments