fix(web): render ansi logs with wrapped lines (#1425)

This commit is contained in:
wenjie authored and GitHub committed 2026-03-12 19:12:19 +08:00
1 parent 7872bb3f0a
commit d18a319b0c
13 files changed
+595 -159

No files matched your search

@@ -0,0 +1,24 @@
import { Fragment, useMemo } from "react"
import { parseAnsiSegments, wrapLogLine } from "@/lib/ansi-log"
type AnsiLogLineProps = {
line: string
wrapColumns: number
}
export function AnsiLogLine({ line, wrapColumns }: AnsiLogLineProps) {
const segments = useMemo(() => {
return parseAnsiSegments(wrapLogLine(line, wrapColumns))
}, [line, wrapColumns])
return (
<div className="break-normal whitespace-pre-wrap">
{segments.map((segment, index) => (
<Fragment key={`${index}-${segment.text.length}`}>
<span style={segment.style}>{segment.text}</span>
</Fragment>
))}
</div>
)
}
@@ -0,0 +1,42 @@
import { IconTrash } from "@tabler/icons-react"
import { useTranslation } from "react-i18next"
import { LogsPanel } from "@/components/logs/logs-panel"
import { PageHeader } from "@/components/page-header"
import { Button } from "@/components/ui/button"
import { useGatewayLogs } from "@/hooks/use-gateway-logs"
import { useLogWrapColumns } from "@/hooks/use-log-wrap-columns"
export function LogsPage() {
const { t } = useTranslation()
const { clearLogs, clearing, logs } = useGatewayLogs()
const { contentRef, measureRef, wrapColumns } = useLogWrapColumns()
return (
<div className="flex h-full flex-col">
<PageHeader
title={t("navigation.logs")}
children={
<Button
variant="outline"
size="sm"
onClick={clearLogs}
disabled={logs.length === 0 || clearing}
>
<IconTrash className="size-4" />
{t("pages.logs.clear")}
</Button>
}
/>
<div className="flex flex-1 flex-col gap-4 overflow-hidden p-4 sm:p-8">
<LogsPanel
logs={logs}
wrapColumns={wrapColumns}
contentRef={contentRef}
measureRef={measureRef}
/>
</div>
</div>
)
}
@@ -0,0 +1,55 @@
import { type RefObject, useEffect, useRef } from "react"
import { useTranslation } from "react-i18next"
import { AnsiLogLine } from "@/components/logs/ansi-log-line"
import { ScrollArea } from "@/components/ui/scroll-area"
type LogsPanelProps = {
logs: string[]
wrapColumns: number
contentRef: RefObject<HTMLDivElement | null>
measureRef: RefObject<HTMLSpanElement | null>
}
export function LogsPanel({
logs,
wrapColumns,
contentRef,
measureRef,
}: LogsPanelProps) {
const { t } = useTranslation()
const scrollRef = useRef<HTMLDivElement>(null)
useEffect(() => {
if (scrollRef.current) {
scrollRef.current.scrollIntoView({ behavior: "smooth" })
}
}, [logs])
return (
<div className="relative flex-1 overflow-hidden rounded-lg border border-zinc-800 bg-zinc-950 text-zinc-100">
<ScrollArea className="h-full">
<div
ref={contentRef}
className="relative p-4 font-mono text-sm leading-relaxed"
>
<span
ref={measureRef}
aria-hidden
className="pointer-events-none invisible absolute font-mono text-sm"
>
0
</span>
{logs.length === 0 ? (
<div className="text-zinc-500 italic">{t("pages.logs.empty")}</div>
) : (
logs.map((log, index) => (
<AnsiLogLine key={index} line={log} wrapColumns={wrapColumns} />
))
)}
<div ref={scrollRef} />
</div>
</ScrollArea>
</div>
)
}
+4 -4
View File
@@ -35,13 +35,13 @@ export function Field({
if (layout === "setting-row") {
return (
<div className="flex flex-col gap-4 py-4 md:grid md:grid-cols-[minmax(0,1fr)_minmax(240px,320px)] md:items-center md:gap-6">
<div className="space-y-1">
<div className="max-w-full space-y-1 md:max-w-[clamp(18rem,42vw,28rem)]">
<FieldLabel>
{label}
{required && <span className="text-destructive ml-1">*</span>}
</FieldLabel>
{hint && (
<FieldDescription className="text-xs leading-normal">
<FieldDescription className="text-xs leading-normal break-words">
{hint}
</FieldDescription>
)}
@@ -141,10 +141,10 @@ export function SwitchCardField({
if (layout === "setting-row") {
return (
<div className="flex flex-col gap-4 py-4 md:grid md:grid-cols-[minmax(0,1fr)_auto] md:items-center md:gap-6">
<div className="min-w-0">
<div className="max-w-full min-w-0 md:max-w-[clamp(18rem,42vw,28rem)]">
<p className="text-sm font-medium">{label}</p>
{hint && (
<p className="text-muted-foreground mt-0.5 text-xs leading-normal">
<p className="text-muted-foreground mt-0.5 text-xs leading-normal break-words">
{hint}
</p>
)}