Files
supabase/apps/database-new/components/AllThreadsModal/AllThreadsModal.utils.ts
Ivan Vasilov fbfe910c22 Initial work on database.new visualizer (#19027)
* Initial commit.

* Remove bunch of unneeded deps.

* Fix the providers.

* Minor fixes to make the themes work.

* Fix prettier errors.

* Add default constraint.

* Midway styling

* General styling improvements

* More styling stuff

* Bit more styling

* Bit more pizzazz

* Scaffold all threads modal

* Small style fix

* Ensure loading state persist in new page until redirect

* Fix

* Test';

* Add basic validation

* Fix

* Remove console logs

* Hide show all threads button

* chore: style the blocks

* fix: stroke width

* add some shadow

* stroke width

* Update TableNode.tsx

* Swap hide chat for hide code

* more sidebar styling

* Resolve conflicts

* Some style fixeds

* Some style fixeds

* mock profile

* update user dorpdown

* Update globals.css

* Chore/db new layouts and url params (#19142)

* chore: layouts

* Fix + clean up

---------

Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>

* Fix header

* Scaffold profile and conversations

* Some quick styling

* Update apps/database-new/.env.local.example

Co-authored-by: Greg Richardson <greg.nmr@gmail.com>

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>
Co-authored-by: Greg Richardson <greg.nmr@gmail.com>
2023-11-22 12:31:32 -03:30

66 lines
1.8 KiB
TypeScript

import { UserMessage } from '@/lib/types'
import dagre from '@dagrejs/dagre'
import { Edge, Node, Position } from 'reactflow'
import { NODE_HEIGHT, NODE_WIDTH } from './AllThreadsModal.constants'
import { MessageNodeData } from './MessageNode'
export const getGraphDataFromMessages = ({
messages,
onSelectMessage,
}: {
messages: UserMessage[]
onSelectMessage: (message: UserMessage) => void
}): { nodes: Node<MessageNodeData>[]; edges: Edge[] } => {
const nodes: Node[] = messages.map((message, idx) => {
return {
id: message.id,
type: 'message',
data: {
id: message.id,
text: message.text,
isStart: idx === 0,
isEnd: idx === messages.length - 1,
onSelectMessage: () => onSelectMessage(message),
},
position: { x: 0, y: 0 },
}
})
const edges: Edge[] = []
messages.forEach((message, idx) => {
if (idx > 0) {
edges.push({
id: `edge-${idx}`,
source: messages[idx - 1].id,
target: message.id,
type: 'smoothstep',
animated: true,
})
}
})
const dagreGraph = new dagre.graphlib.Graph()
dagreGraph.setDefaultEdgeLabel(() => ({}))
dagreGraph.setGraph({ rankdir: 'TB', ranksep: 50, nodesep: 20 })
nodes.forEach((node) =>
dagreGraph.setNode(node.id, { width: NODE_WIDTH / 2, height: NODE_HEIGHT / 2 })
)
edges.forEach((edge) => dagreGraph.setEdge(edge.source, edge.target))
dagre.layout(dagreGraph)
nodes.forEach((node) => {
const nodeWithPosition = dagreGraph.node(node.id)
node.targetPosition = Position.Top
node.sourcePosition = Position.Bottom
node.position = {
x: nodeWithPosition.x - nodeWithPosition.width / 2,
y: nodeWithPosition.y - nodeWithPosition.height / 2,
}
return node
})
return { nodes, edges }
}