fix(dashboard): keep the log console on screen when it has no lines

With no matching lines, while loading, and while Follow waits for the
first line, the dark log console vanished and left a line of small
grey text or a bare spinner, so the page jumped and an empty result
looked like a broken view. The console frame now keeps a fixed height
in every state and says inside it what is going on.

The pod and container pickers move to the console Select and the
filter to the console Input, so the toolbar is keyboard and screen
reader friendly and a long pod name truncates instead of widening it.

Assisted-by: LLM
Signed-off-by: ZverGuy <maximbel2003@gmail.com>
This commit is contained in:
ZverGuy committed 2026-11-04 18:47:25 +07:00
1 parent f500aa955a
commit b88ad09678
6 files changed
+184 -49

No files matched your search

@@ -0,0 +1,17 @@
import { describe, it, expect } from "vitest"
import { render, screen } from "@testing-library/react"
import { EmptyState } from "./EmptyState.tsx"
describe("EmptyState", () => {
it("announces its message as a status", () => {
render(<EmptyState title="No data for this range." hint="Pick a longer range." />)
expect(screen.getByRole("status")).toHaveTextContent("No data for this range.Pick a longer range.")
})
it("marks itself busy while something is loading", () => {
render(<EmptyState busy title="Loading…" />)
expect(screen.getByRole("status")).toHaveAttribute("aria-busy", "true")
})
})
@@ -0,0 +1,25 @@
import type { ReactNode } from "react"
import { cn, Spinner } from "@cozystack/ui"
interface EmptyStateProps {
title: ReactNode
hint?: ReactNode
busy?: boolean
tone?: "light" | "dark"
className?: string
}
export function EmptyState({ title, hint, busy, tone = "light", className }: EmptyStateProps) {
const dark = tone === "dark"
return (
<div
role="status"
aria-busy={busy || undefined}
className={cn("flex h-full flex-col items-center justify-center gap-1.5 px-4 py-8 text-center", className)}
>
{busy && <Spinner className={cn("mb-1 size-5", dark && "text-slate-500")} />}
<p className={cn("text-sm", dark ? "text-slate-300" : "text-slate-600")}>{title}</p>
{hint && <p className={cn("text-xs", dark ? "text-slate-500" : "text-slate-400")}>{hint}</p>}
</div>
)
}
@@ -10,40 +10,59 @@ function sized(list: HTMLElement, scrollHeight: number) {
}
describe("LogLines", () => {
it("keeps the console frame and shows the placeholder inside it when there are no lines", () => {
render(<LogLines entries={[]} placeholder="Waiting for new log lines…" />)
const frame = screen.getByRole("region", { name: "Log lines" })
expect(frame).toHaveTextContent("Waiting for new log lines…")
expect(screen.queryByRole("list")).toBeNull()
})
it("puts the lines in the same frame once they arrive", () => {
const { rerender } = render(<LogLines entries={[]} placeholder="Waiting for new log lines…" />)
const frame = screen.getByRole("region", { name: "Log lines" })
rerender(<LogLines entries={[entry(1)]} placeholder="Waiting for new log lines…" />)
expect(screen.getByRole("region", { name: "Log lines" })).toBe(frame)
expect(frame).toContainElement(screen.getByRole("list"))
expect(frame).not.toHaveTextContent("Waiting")
})
it("keeps following the newest line while the reader is at the bottom", () => {
const { rerender } = render(<LogLines entries={[entry(1)]} />)
const { rerender } = render(<LogLines placeholder="" entries={[entry(1)]} />)
const list = screen.getByRole("list")
sized(list, 500)
rerender(<LogLines entries={[entry(1), entry(2)]} />)
rerender(<LogLines placeholder="" entries={[entry(1), entry(2)]} />)
expect(list.scrollTop).toBe(500)
})
it("leaves the position alone once the reader scrolls up", () => {
const { rerender } = render(<LogLines entries={[entry(1)]} />)
const { rerender } = render(<LogLines placeholder="" entries={[entry(1)]} />)
const list = screen.getByRole("list")
sized(list, 500)
list.scrollTop = 120
fireEvent.scroll(list)
rerender(<LogLines entries={[entry(1), entry(2)]} />)
rerender(<LogLines placeholder="" entries={[entry(1), entry(2)]} />)
expect(list.scrollTop).toBe(120)
})
it("follows again once the list is cleared for a new run", () => {
const { rerender } = render(<LogLines entries={[entry(1)]} />)
const { rerender } = render(<LogLines placeholder="" entries={[entry(1)]} />)
const first = screen.getByRole("list")
sized(first, 500)
first.scrollTop = 120
fireEvent.scroll(first)
rerender(<LogLines entries={[]} />)
rerender(<LogLines entries={[entry(2)]} />)
rerender(<LogLines placeholder="" entries={[]} />)
rerender(<LogLines placeholder="" entries={[entry(2)]} />)
const second = screen.getByRole("list")
sized(second, 300)
rerender(<LogLines entries={[entry(2), entry(3)]} />)
rerender(<LogLines placeholder="" entries={[entry(2), entry(3)]} />)
expect(second.scrollTop).toBe(300)
})
@@ -1,10 +1,10 @@
import { useEffect, useRef } from "react"
import { useEffect, useRef, type ReactNode } from "react"
import { CONTAINER_FIELD, POD_FIELD } from "../../lib/observability/logs.ts"
import type { LogEntry } from "../../lib/observability/proxy.ts"
const BOTTOM_SLACK_PX = 24
export function LogLines({ entries }: { entries: LogEntry[] }) {
export function LogLines({ entries, placeholder }: { entries: LogEntry[]; placeholder: ReactNode }) {
const ref = useRef<HTMLOListElement>(null)
const pinned = useRef(true)
@@ -13,23 +13,31 @@ export function LogLines({ entries }: { entries: LogEntry[] }) {
else if (ref.current && pinned.current) ref.current.scrollTop = ref.current.scrollHeight
}, [entries])
if (entries.length === 0) return <p className="text-sm text-slate-500">No log lines for this range and filter.</p>
return (
<ol
ref={ref}
onScroll={(e) => {
const el = e.currentTarget
pinned.current = el.scrollHeight - el.scrollTop - el.clientHeight <= BOTTOM_SLACK_PX
}}
className="max-h-[60vh] overflow-auto rounded-md bg-slate-950 p-3 font-mono text-xs leading-5 text-slate-100"
<section
aria-label="Log lines"
className="h-[60vh] min-h-72 overflow-hidden rounded-lg bg-slate-950 font-mono text-xs leading-5 text-slate-100 shadow-xs"
>
{entries.map((e, i) => (
<li key={`${e._stream_id}-${e._time}-${i}`} className="whitespace-pre-wrap break-all">
<span className="text-slate-400">{new Date(e._time).toLocaleTimeString()}</span>{" "}
<span className="text-sky-300">{`${e[POD_FIELD] ?? ""}/${e[CONTAINER_FIELD] ?? ""}`}</span>{" "}
{e._msg}
</li>
))}
</ol>
{entries.length === 0 ? (
placeholder
) : (
<ol
ref={ref}
onScroll={(e) => {
const el = e.currentTarget
pinned.current = el.scrollHeight - el.scrollTop - el.clientHeight <= BOTTOM_SLACK_PX
}}
className="h-full overflow-auto p-3"
>
{entries.map((e, i) => (
<li key={`${e._stream_id}-${e._time}-${i}`} className="whitespace-pre-wrap break-all">
<span className="text-slate-400">{new Date(e._time).toLocaleTimeString()}</span>{" "}
<span className="text-sky-300">{`${e[POD_FIELD] ?? ""}/${e[CONTAINER_FIELD] ?? ""}`}</span>{" "}
{e._msg}
</li>
))}
</ol>
)}
</section>
)
}
@@ -1,5 +1,5 @@
import { describe, it, expect, vi } from "vitest"
import { screen, waitFor } from "@testing-library/react"
import { screen, waitFor, within } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import { K8sClient } from "@cozystack/k8s-client"
import { renderWithK8sProvider } from "../../test-utils/render.tsx"
@@ -47,9 +47,9 @@ describe("LogsView", () => {
it("narrows to the picked pod with a stream filter", async () => {
const { client, raw } = clientWith(defaultHandler)
renderWithK8sProvider(<LogsView scope={app} />, { client })
await screen.findByRole("option", { name: "db-1" })
await userEvent.selectOptions(screen.getByLabelText("Pod"), "db-1")
await userEvent.click(screen.getByRole("combobox", { name: "Pod" }))
await userEvent.click(await screen.findByRole("option", { name: "db-1" }))
await waitFor(() =>
expect(
@@ -58,6 +58,50 @@ describe("LogsView", () => {
)
})
it("offers All next to every pod and starts on it", async () => {
const { client } = clientWith(defaultHandler)
renderWithK8sProvider(<LogsView scope={app} />, { client })
const pod = screen.getByRole("combobox", { name: "Pod" })
expect(pod).toHaveTextContent("All")
await userEvent.click(pod)
expect(await screen.findByRole("option", { name: "db-1" })).toBeInTheDocument()
expect(screen.getAllByRole("option").map((o) => o.textContent)).toEqual(["All", "db-0", "db-1"])
})
it("keeps the log console and says so inside it when nothing matches", async () => {
const { client } = clientWith((endpoint, params) =>
endpoint.endsWith("query") ? new Response("") : defaultHandler(endpoint, params),
)
renderWithK8sProvider(<LogsView scope={app} />, { client })
const frame = screen.getByRole("region", { name: "Log lines" })
expect(await within(frame).findByText("No log lines for this range and filter.")).toBeInTheDocument()
})
it("shows loading inside the log console", () => {
const { client } = clientWith((endpoint, params) =>
endpoint.endsWith("query") ? new Promise(() => {}) : defaultHandler(endpoint, params),
)
renderWithK8sProvider(<LogsView scope={app} />, { client })
const frame = screen.getByRole("region", { name: "Log lines" })
expect(within(frame).getByRole("status")).toHaveAttribute("aria-busy", "true")
})
it("says it is waiting while Follow has no line yet", async () => {
const { client } = clientWith(defaultHandler)
renderWithK8sProvider(<LogsView scope={app} />, { client })
await userEvent.click(screen.getByRole("button", { name: "Follow" }))
const frame = screen.getByRole("region", { name: "Log lines" })
expect(within(frame).getByText("Waiting for new log lines…")).toBeInTheDocument()
})
it("applies the LogsQL filter on submit", async () => {
const { client, raw } = clientWith(defaultHandler)
renderWithK8sProvider(<LogsView scope={app} />, { client })
@@ -1,7 +1,9 @@
import { useState } from "react"
import { useQuery } from "@tanstack/react-query"
import { useK8sClient } from "@cozystack/k8s-client"
import { Button, Spinner } from "@cozystack/ui"
import { Button } from "@cozystack/ui"
import { Input } from "../ui/input.tsx"
import { SelectItem, SelectLabel, SelectPopup, SelectRoot, SelectTrigger, SelectValue } from "../ui/select.tsx"
import {
CONTAINER_FIELD,
LOG_RANGES,
@@ -20,6 +22,7 @@ import { useLogTail } from "../../lib/observability/use-log-tail.ts"
import { ProxyNotice } from "./ProxyNotice.tsx"
import { RangePicker } from "./RangePicker.tsx"
import { LogLines } from "./LogLines.tsx"
import { EmptyState } from "./EmptyState.tsx"
const HISTORY_LIMIT = 500
const FOLLOW_CAP = 1000
@@ -30,27 +33,42 @@ function windowFor(range: LogRangeKey) {
return { start: new Date(end - LOG_RANGES[range] * 1000).toISOString(), end: new Date(end).toISOString() }
}
const ALL = ""
function Picker({ label, value, options, onChange }: { label: string; value: string; options: string[]; onChange: (v: string) => void }) {
const items = [{ value: ALL, label: "All" }, ...options.map((o) => ({ value: o, label: o }))]
return (
<label className="flex items-center gap-1.5 text-xs text-slate-600">
{label}
<select
aria-label={label}
value={value}
onChange={(e) => onChange(e.target.value)}
className="rounded-md border border-slate-200 bg-white px-2 py-1 text-xs text-slate-900"
>
<option value="">All</option>
{options.map((o) => (
<option key={o} value={o}>
{o}
</option>
<SelectRoot items={items} value={value} onValueChange={(v) => onChange(v ?? ALL)}>
<div className="flex min-w-0 items-center gap-1.5">
<SelectLabel>{label}</SelectLabel>
<SelectTrigger className="w-40" title={value || undefined}>
<SelectValue />
</SelectTrigger>
</div>
<SelectPopup>
{items.map((item) => (
<SelectItem key={item.value} value={item.value}>
{item.label}
</SelectItem>
))}
</select>
</label>
</SelectPopup>
</SelectRoot>
)
}
function placeholderFor({ follow, stopped, loading, failed }: { follow: boolean; stopped: boolean; loading: boolean; failed: boolean }) {
if (follow) {
return stopped ? (
<EmptyState tone="dark" title="Follow stopped before any line arrived." />
) : (
<EmptyState tone="dark" busy title="Waiting for new log lines…" hint="Lines written from now on appear here." />
)
}
if (loading) return <EmptyState tone="dark" busy title="Loading log lines…" />
if (failed) return <EmptyState tone="dark" title="No log lines to show." />
return <EmptyState tone="dark" title="No log lines for this range and filter." hint="Try a longer range or a broader filter." />
}
export function LogsView({ scope }: { scope: ObservabilityScope }) {
const client = useK8sClient()
const root = logsRoot(scope)
@@ -107,12 +125,12 @@ export function LogsView({ scope }: { scope: ObservabilityScope }) {
setFilter(draft)
}}
>
<input
<Input
aria-label="LogsQL filter"
value={draft}
onChange={(e) => setDraft(e.target.value)}
onValueChange={setDraft}
placeholder='LogsQL filter, e.g. error or "connection refused"'
className="w-full rounded-md border border-slate-200 px-2.5 py-1.5 font-mono text-xs"
className="font-mono"
/>
</form>
<Picker label="Pod" value={pod} options={pods.data ?? []} onChange={(v) => { setPod(v); setContainer("") }} />
@@ -129,7 +147,11 @@ export function LogsView({ scope }: { scope: ObservabilityScope }) {
</p>
)}
{error ? <ProxyNotice what="Logs" error={error} /> : null}
{!follow && history.isLoading ? <Spinner /> : <LogLines key={followRun ?? "history"} entries={entries} />}
<LogLines
key={followRun ?? "history"}
entries={entries}
placeholder={placeholderFor({ follow, stopped: tail.state === "stopped", loading: history.isLoading, failed: Boolean(error) })}
/>
</div>
)
}