-
Notifications
You must be signed in to change notification settings - Fork 0
Tree lens: collapse path-shaped query results into a forest #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Binary file not shown.
This file contains hidden or 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
This file contains hidden or 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,138 @@ | ||
| import React, { useMemo, useState } from "react"; | ||
| import { Box, Text, useFocus, useInput } from "ink"; | ||
| import { useActions, useStateValue } from "@json-render/ink"; | ||
| import { | ||
| buildForest, | ||
| type TreeNode, | ||
| type TreeRuntimeProps, | ||
| } from "@modernrelay/notebook-core"; | ||
|
|
||
| interface ComponentCtx<P> { | ||
| props: P; | ||
| } | ||
|
|
||
| const VISIBLE_LINES = 14; | ||
|
|
||
| interface VisibleLine { | ||
| node: TreeNode; | ||
| prefix: string; | ||
| } | ||
|
|
||
| /** Flatten the expanded forest into display lines with box-drawing prefixes. */ | ||
| function visibleLines( | ||
| nodes: TreeNode[], | ||
| isOpen: (node: TreeNode) => boolean, | ||
| ancestors = "", | ||
| ): VisibleLine[] { | ||
| const out: VisibleLine[] = []; | ||
| nodes.forEach((node, index) => { | ||
| const last = index === nodes.length - 1; | ||
| const branch = node.depth === 0 ? "" : last ? "└─ " : "├─ "; | ||
| out.push({ node, prefix: ancestors + branch }); | ||
| if (node.children.length > 0 && isOpen(node)) { | ||
| const carry = node.depth === 0 ? "" : last ? " " : "│ "; | ||
| out.push(...visibleLines(node.children, isOpen, ancestors + carry)); | ||
| } | ||
| }); | ||
| return out; | ||
| } | ||
|
|
||
| /** | ||
| * Tree lens (ink) — the expanded forest as box-drawing lines. ↑/↓ move the | ||
| * cursor, ←/→ collapse/expand the focused node, Enter/space writes the | ||
| * node's key value to `select_state` (same dispatch as Table). | ||
| */ | ||
| export function Tree({ | ||
| props: p, | ||
| }: ComponentCtx<TreeRuntimeProps>): React.ReactElement { | ||
| const actions = useActions(); | ||
| const selectable = Boolean(p.select_state); | ||
| const { isFocused } = useFocus({ autoFocus: selectable }); | ||
| const selected = useStateValue<string>(p.select_state ?? "/__never__"); | ||
| const forest = useMemo(() => buildForest(p.rows, p.levels), [p.rows, p.levels]); | ||
| // Default policy + explicit user toggles (survives query re-reads; new | ||
| // paths from refreshed rows get the expand_depth default). | ||
| const defaultOpen = (node: TreeNode): boolean => | ||
| node.children.length > 0 && | ||
| (p.expand_depth === undefined || node.depth < p.expand_depth); | ||
| const [userToggled, setUserToggled] = useState<Map<string, boolean>>( | ||
| () => new Map(), | ||
| ); | ||
| const isOpen = (node: TreeNode): boolean => | ||
| userToggled.get(node.path) ?? defaultOpen(node); | ||
| const [cursor, setCursor] = useState(0); | ||
|
|
||
| const lines = visibleLines(forest, isOpen); | ||
| const total = lines.length; | ||
|
|
||
| useInput( | ||
| (input, key) => { | ||
| if (total === 0) return; | ||
| const line = lines[Math.min(cursor, total - 1)]; | ||
| if (key.upArrow) setCursor((c) => (c - 1 + total) % total); | ||
| else if (key.downArrow) setCursor((c) => (c + 1) % total); | ||
| else if (key.leftArrow && line) { | ||
| setUserToggled((prev) => new Map(prev).set(line.node.path, false)); | ||
| } else if (key.rightArrow && line && line.node.children.length > 0) { | ||
| setUserToggled((prev) => new Map(prev).set(line.node.path, true)); | ||
| } else if ((key.return || input === " ") && line && selectable) { | ||
| actions.execute({ | ||
| action: "setState", | ||
| params: { statePath: p.select_state, value: line.node.key }, | ||
| }); | ||
| } | ||
| }, | ||
| { isActive: isFocused }, | ||
| ); | ||
|
|
||
| if (total === 0) { | ||
| return <Text dimColor>{p.empty_text ?? "(no rows)"}</Text>; | ||
| } | ||
|
|
||
| const cur = Math.min(cursor, total - 1); | ||
| const start = Math.max( | ||
| 0, | ||
| Math.min(cur - Math.floor(VISIBLE_LINES / 2), total - VISIBLE_LINES), | ||
| ); | ||
| const window = lines.slice(start, start + VISIBLE_LINES); | ||
| const showCounts = p.counts !== false; | ||
|
|
||
| return ( | ||
| <Box flexDirection="column"> | ||
| {p.title && <Text bold>{p.title}</Text>} | ||
| {window.map((line, i) => { | ||
| const index = start + i; | ||
| const isCursor = isFocused && index === cur; | ||
| const isSelected = selectable && line.node.key === selected; | ||
| const hasChildren = line.node.children.length > 0; | ||
| const disclosure = hasChildren | ||
| ? isOpen(line.node) | ||
| ? "▾ " | ||
| : "▸ " | ||
| : ""; | ||
| return ( | ||
| <Box key={line.node.path}> | ||
| <Text color={isCursor ? "cyan" : isSelected ? "green" : undefined}> | ||
| {isCursor ? "▶ " : isSelected ? "● " : " "} | ||
| <Text dimColor>{line.prefix}</Text> | ||
| {disclosure} | ||
| <Text bold={line.node.depth === 0}>{line.node.label}</Text> | ||
| {showCounts && hasChildren && ( | ||
| <Text dimColor> ({line.node.children.length})</Text> | ||
| )} | ||
| </Text> | ||
| </Box> | ||
| ); | ||
| })} | ||
| {total > VISIBLE_LINES && ( | ||
| <Text dimColor> | ||
| {" "} | ||
| {cur + 1}/{total} | ||
| </Text> | ||
| )} | ||
| {isFocused && ( | ||
| <Text dimColor> ↑/↓ move · ←/→ fold{selectable ? " · Enter select" : ""}</Text> | ||
| )} | ||
| </Box> | ||
| ); | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The terminal renderer also highlights selection by
line.node.keyinstead of the tree node identity. With duplicate slugs in different branches, selecting one visible node marks all nodes with that key as selected and downstream state cannot distinguish which branch the user chose.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same rationale as the web finding — entity-level selection by design (the state pointer carries the entity key that dependent cells consume); documented in the lens description.