-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: basic virtualization for console
- Loading branch information
1 parent
6387c2a
commit 1e253f2
Showing
4 changed files
with
170 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Show, Accessor, JSXElement, For, createEffect, on } from "solid-js"; | ||
import { createVirtualizer } from "@tanstack/solid-virtual"; | ||
|
||
function scrollEnd(ref?: HTMLElement, smooth?: boolean) { | ||
ref?.scroll({ | ||
top: ref.scrollHeight, | ||
/** | ||
* @todo | ||
* make it smooth when autoscroll is turned BACK on. | ||
*/ | ||
behavior: smooth ? "smooth" : "instant", | ||
}); | ||
} | ||
|
||
type AutoScrollPaneProps<DataType> = { | ||
shouldAutoScroll: Accessor<boolean>; | ||
dataStream: DataType[]; | ||
fallback: JSXElement; | ||
displayOptions: Record<string, unknown>; | ||
displayComponent: (props: { | ||
event: DataType; | ||
[key: string]: unknown; | ||
}) => JSXElement; | ||
}; | ||
|
||
export function AutoScrollPane<DataType>(props: AutoScrollPaneProps<DataType>) { | ||
let logPanel: HTMLDivElement | undefined; | ||
|
||
const virtualizer = createVirtualizer({ | ||
count: props.dataStream.length, | ||
getScrollElement: () => logPanel ?? null, | ||
estimateSize: () => 30, | ||
overscan: 10, | ||
}); | ||
|
||
function updateVirtualizer(newCount: number) { | ||
virtualizer.setOptions({ | ||
...virtualizer.options, | ||
count: newCount, | ||
}); | ||
virtualizer.measure(); | ||
} | ||
|
||
createEffect( | ||
on( | ||
() => props.dataStream, | ||
() => { | ||
updateVirtualizer(props.dataStream.length); | ||
if (props.shouldAutoScroll()) { | ||
scrollEnd(logPanel); | ||
} | ||
} | ||
) | ||
); | ||
|
||
return ( | ||
<div | ||
ref={(e) => (logPanel = e)} | ||
class="overflow-auto h-[calc(100%-var(--toolbar-height))] relative" | ||
> | ||
<Show when={props.dataStream.length === 0 || !props.dataStream}> | ||
{props.fallback} | ||
</Show> | ||
<ul class="" style={{ height: `${virtualizer.getTotalSize()}px` }}> | ||
<For each={virtualizer.getVirtualItems()}> | ||
{(virtualRow, index) => { | ||
return ( | ||
<li | ||
data-id={virtualRow.index} | ||
style={{ | ||
height: `${virtualRow.size}px`, | ||
transform: `translateY(${ | ||
virtualRow.start - index() * virtualRow.size | ||
}px)`, | ||
}} | ||
> | ||
<props.displayComponent | ||
event={props.dataStream[virtualRow.index]} | ||
{...props.displayOptions} | ||
/> | ||
</li> | ||
); | ||
}} | ||
</For> | ||
</ul> | ||
</div> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Show, untrack } from "solid-js"; | ||
import { getLogMetadata } from "~/lib/console/get-log-metadata"; | ||
import { timestampToDate, formatTimestamp } from "~/lib/formatters"; | ||
import { getLevelClasses } from "~/lib/console/get-level-classes"; | ||
import { useMonitor } from "~/context/monitor-provider"; | ||
import { processFieldValue } from "~/lib/span/process-field-value"; | ||
import type { LogEvent } from "~/lib/proto/logs"; | ||
import clsx from "clsx"; | ||
import { getFileNameFromPath } from "~/lib/console/get-file-name-from-path"; | ||
|
||
export function LogEventEntry(props: { | ||
event: LogEvent; | ||
showTimestamp?: boolean; | ||
}) { | ||
const { monitorData } = useMonitor(); | ||
const logEvent = untrack(() => props.event); | ||
const { message, at } = logEvent; | ||
if (!at) return null; | ||
|
||
const metadata = getLogMetadata(monitorData, logEvent); | ||
const timeDate = timestampToDate(at); | ||
const levelStyle = getLevelClasses(metadata?.level); | ||
|
||
let target = metadata?.target; | ||
if (target === "log") { | ||
const field = logEvent.fields.find((field) => field.name === "log.target"); | ||
if (field) { | ||
target = processFieldValue(field.value); | ||
} | ||
} | ||
|
||
return ( | ||
<div | ||
class={clsx( | ||
"p-1 font-mono text-sm items-center flex gap-4 odd:bg-slate-900 group", | ||
levelStyle ? levelStyle : "border-b-gray-800 text-white" | ||
)} | ||
> | ||
<Show when={props.showTimestamp}> | ||
<time | ||
dateTime={timeDate.toISOString()} | ||
class={clsx( | ||
levelStyle | ||
? levelStyle | ||
: "text-slate-400 group-hover:text-slate-100", | ||
"font-mono text-xs transition-colors" | ||
)} | ||
> | ||
{formatTimestamp(timeDate)} | ||
</time> | ||
</Show> | ||
<span class="group-hover:text-white text-slate-300 transition-colors"> | ||
{message} | ||
</span> | ||
<span class="ml-auto flex gap-2 items-center text-xs"> | ||
<Show when={target}> | ||
{(logTarget) => ( | ||
<span class="text-slate-400 group-hover:text-slate-100 transition-colors"> | ||
{logTarget()} | ||
</span> | ||
)} | ||
</Show> | ||
<Show when={metadata?.location?.file}> | ||
{(filePath) => ( | ||
<> | ||
{getFileNameFromPath(filePath())}:{metadata?.location?.line ?? ""} | ||
</> | ||
)} | ||
</Show> | ||
</span> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters