-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
769 additions
and
252 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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
"compilerOptions": { | ||
"declaration": false, | ||
"module": "CommonJS", | ||
"noEmit": false, | ||
"sourceMap": false | ||
} | ||
} |
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
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,100 @@ | ||
import { Stream } from '@yeger/streams' | ||
import { graphStratify, sugiyama } from 'd3-dag' | ||
import { type Edge, type Node } from 'reactflow' | ||
|
||
import type { TurboEdge, TurboGraph, TurboNode } from './data' | ||
|
||
export type FlowGraph = ReturnType<typeof convertGraph> | ||
|
||
export interface SizeConfig { | ||
width: number | ||
height: number | ||
horizontalSpacing: number | ||
verticalSpacing: number | ||
} | ||
|
||
export function convertGraph(graph: TurboGraph) { | ||
const hierarchy = createHierarchy(graph) | ||
const longestLine = getLongestLineLength(graph) | ||
const sizeConfig = createSizeConfig(longestLine) | ||
return createFlowGraph(hierarchy, graph.edges, sizeConfig) | ||
} | ||
|
||
function createHierarchy(graph: TurboGraph) { | ||
const stratify = graphStratify() | ||
return stratify([ | ||
...graph.nodes.map((node) => ({ | ||
...node, | ||
id: node.id, | ||
parentIds: graph.edges | ||
.filter((edge) => edge.target === node.id) | ||
.map(({ source }) => source), | ||
})), | ||
]) | ||
} | ||
|
||
function getLongestLineLength({ nodes }: TurboGraph) { | ||
return Math.max( | ||
...Stream.from(nodes).flatMap(({ task, workspace }) => [ | ||
task.length, | ||
workspace.length, | ||
]), | ||
) | ||
} | ||
|
||
function createSizeConfig(longestLine: number): SizeConfig { | ||
return { | ||
width: longestLine * 10, | ||
height: 64, | ||
horizontalSpacing: 128, | ||
verticalSpacing: 128, | ||
} | ||
} | ||
|
||
export interface FlowNode extends TurboNode { | ||
isOrigin: boolean | ||
isTerminal: boolean | ||
} | ||
|
||
function createFlowGraph( | ||
hierarchy: ReturnType<typeof createHierarchy>, | ||
turboEdges: TurboEdge[], | ||
sizeConfig: SizeConfig, | ||
) { | ||
const { width, height, horizontalSpacing, verticalSpacing } = sizeConfig | ||
const layout = sugiyama().nodeSize([ | ||
width + horizontalSpacing, | ||
height + verticalSpacing, | ||
]) | ||
layout(hierarchy) | ||
const nodes = Stream.from(hierarchy.nodes()) | ||
.map<Node<FlowNode>>( | ||
(node) => | ||
({ | ||
id: node.data.id, | ||
data: { | ||
...node.data, | ||
isTerminal: node.nchildren() === 0, | ||
isOrigin: node.nparents() === 0, | ||
}, | ||
position: { x: node.x, y: node.y }, | ||
type: 'task', | ||
draggable: false, | ||
selectable: false, | ||
connectable: false, | ||
deletable: false, | ||
focusable: false, | ||
}) as const, | ||
) | ||
.toArray() | ||
const edges = turboEdges.map<Edge<TurboEdge>>((edge) => ({ | ||
id: `edge-${edge.source}-${edge.target}`, | ||
source: edge.source, | ||
target: edge.target, | ||
animated: true, | ||
deletable: false, | ||
focusable: false, | ||
updatable: false, | ||
})) | ||
return { nodes, edges, sizeConfig } | ||
} |
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
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 @@ | ||
'use client' | ||
|
||
import { scaleOrdinal } from 'd3-scale' | ||
import { schemeSet3 } from 'd3-scale-chromatic' | ||
import { useMemo } from 'react' | ||
import { Background, Controls, Handle, Position, ReactFlow } from 'reactflow' | ||
|
||
import 'reactflow/dist/style.css' | ||
import type { FlowGraph, FlowNode } from './converter' | ||
|
||
export interface Props { | ||
graph: FlowGraph | ||
uniqueTasks: Set<string> | ||
} | ||
|
||
interface TaskProps { | ||
data: FlowNode | ||
} | ||
|
||
// TODO: Omit source and target handles if not needed | ||
export function Flow({ graph, uniqueTasks }: Props) { | ||
const { sizeConfig } = graph | ||
const nodeTypes = useMemo(() => { | ||
const getColor = scaleOrdinal(schemeSet3).domain(uniqueTasks) | ||
return { | ||
task: function Task({ data }: TaskProps) { | ||
const { task, workspace, isTerminal, isOrigin } = data | ||
return ( | ||
<div className="flex flex-col"> | ||
{isOrigin ? null : ( | ||
<Handle | ||
type="target" | ||
position={Position.Top} | ||
isConnectable={false} | ||
/> | ||
)} | ||
<div | ||
className="flex flex-col rounded border-2 bg-neutral-50 p-2 outline outline-2 outline-neutral-500" | ||
style={{ | ||
width: `${sizeConfig.width}px`, | ||
height: `${sizeConfig.height}px`, | ||
borderColor: getColor(task), | ||
}} | ||
> | ||
<div className="font-bold">{task}</div> | ||
<div className="text-right text-neutral-600">{workspace}</div> | ||
</div> | ||
{isTerminal ? null : ( | ||
<Handle | ||
type="source" | ||
position={Position.Bottom} | ||
isConnectable={false} | ||
/> | ||
)} | ||
</div> | ||
) | ||
}, | ||
} | ||
}, [sizeConfig, uniqueTasks]) | ||
return ( | ||
<div style={{ width: '100%', height: '100%' }}> | ||
<ReactFlow | ||
nodes={graph.nodes} | ||
edges={graph.edges} | ||
nodeTypes={nodeTypes} | ||
minZoom={0.1} | ||
> | ||
<Background /> | ||
<Controls showInteractive={false} /> | ||
</ReactFlow> | ||
</div> | ||
) | ||
} |
File renamed without changes.
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,112 @@ | ||
'use client' | ||
|
||
import { | ||
GraphController, | ||
Markers, | ||
PositionInitializers, | ||
defineGraph, | ||
defineGraphConfig, | ||
defineLink, | ||
defineNodeWithDefaults, | ||
} from 'd3-graph-controller' | ||
import { scaleOrdinal } from 'd3-scale' | ||
import { schemeSet3 } from 'd3-scale-chromatic' | ||
import { useEffect, useMemo, useRef } from 'react' | ||
import 'd3-graph-controller/default.css' | ||
|
||
import type { TurboGraph } from './data' | ||
|
||
export interface Props { | ||
graph: TurboGraph | ||
} | ||
|
||
export function Graph({ graph }: Props) { | ||
const graphRef = useRef<HTMLDivElement>(null) | ||
|
||
const colors = useMemo(() => { | ||
const tasks = [ | ||
...new Set(graph?.nodes.map(({ task }) => task) ?? []), | ||
].sort() | ||
return scaleOrdinal(schemeSet3).domain(tasks) | ||
}, [graph]) | ||
|
||
const graphController = useMemo(() => { | ||
const container = graphRef.current | ||
if (!container || !graph) { | ||
return undefined | ||
} | ||
const nodes = graph.nodes.map((node) => | ||
defineNodeWithDefaults({ | ||
id: node.id, | ||
type: node.task, | ||
color: colors(node.task), | ||
label: { text: node.workspace, color: 'black', fontSize: '0.875rem' }, | ||
}), | ||
) | ||
const links = graph.edges.map((edge) => { | ||
const source = nodes.find((node) => node.id === edge.source)! | ||
const target = nodes.find((node) => node.id === edge.target)! | ||
return defineLink({ | ||
source, | ||
target, | ||
color: '#aaa', | ||
label: false, | ||
}) | ||
}) | ||
|
||
return new GraphController( | ||
container, | ||
defineGraph({ nodes, links }), | ||
defineGraphConfig({ | ||
autoResize: true, | ||
hooks: { | ||
afterZoom(scale: number, xOffset: number, yOffset: number) { | ||
container.style.setProperty('--offset-x', `${xOffset}px`) | ||
container.style.setProperty('--offset-y', `${yOffset}px`) | ||
container.style.setProperty('--dot-size', `${scale}rem`) | ||
}, | ||
}, | ||
marker: Markers.Arrow(4), | ||
positionInitializer: | ||
nodes.length > 1 | ||
? PositionInitializers.Randomized | ||
: PositionInitializers.Centered, | ||
simulation: { | ||
forces: { | ||
link: { length: 200 }, | ||
charge: { | ||
strength: 200, | ||
}, | ||
collision: { | ||
radiusMultiplier: 10, | ||
strength: 300, | ||
}, | ||
}, | ||
}, | ||
zoom: { | ||
min: 0.3, | ||
max: 2, | ||
}, | ||
}), | ||
) | ||
}, [colors, graphRef, graph]) | ||
|
||
useEffect(() => { | ||
return () => { | ||
graphController?.shutdown() | ||
} | ||
}, [graphController]) | ||
|
||
// const tasks = graphController?.nodeTypes.sort() ?? [] | ||
|
||
return ( | ||
<div className="relative flex-1"> | ||
{!graphController ? ( | ||
<div className="absolute inset-0 flex h-full w-full items-center justify-center"> | ||
<span className="text-gray-700">Loading</span> | ||
</div> | ||
) : null} | ||
<div ref={graphRef} className="bg-dotted h-full w-full bg-gray-50 " /> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use client' | ||
|
||
import { useRouter } from 'next/navigation' | ||
import type { ChangeEventHandler } from 'react' | ||
|
||
export function TaskInput() { | ||
const router = useRouter() | ||
const onChange: ChangeEventHandler<HTMLInputElement> = (event) => | ||
router.push(`/?tasks=${event.target.value}`) | ||
return <input type="text" onChange={onChange} /> | ||
} |
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,15 @@ | ||
import './globals.css' | ||
|
||
export default function RootLayout({ | ||
// Layouts must accept a children prop. | ||
// This will be populated with nested layouts or pages | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
return ( | ||
<html lang="en" className="h-full w-full"> | ||
<body className="m-0 h-full w-full">{children}</body> | ||
</html> | ||
) | ||
} |
Oops, something went wrong.