LightRAG/lightrag_webui/src/hooks/useRandomGraph.tsx
clssck 77df910525 chore: add citation system and code formatting setup
Add citation.py module for document citation tracking and management.
Configure Biome and Ruff for consistent code formatting across TypeScript
and Python. Update webui with improved component organization, API client
refactoring, and enhanced user interface patterns. Add formatting configs
and dependency updates for build toolchain optimization.
2025-11-30 20:51:43 +01:00

83 lines
2.7 KiB
TypeScript

import * as Constants from '@/lib/constants'
import { randomColor } from '@/lib/utils'
import { useGraphStore } from '@/stores/graph'
import { Faker, en, faker as fak } from '@faker-js/faker'
import type Graph from 'graphology'
import { UndirectedGraph } from 'graphology'
import erdosRenyi from 'graphology-generators/random/erdos-renyi'
import { useCallback, useEffect, useState } from 'react'
import seedrandom from 'seedrandom'
export type NodeType = {
x: number
y: number
label: string
size: number
color: string
highlighted?: boolean
}
export type EdgeType = { label: string }
/**
* The goal of this file is to seed random generators if the query params 'seed' is present.
*/
const useRandomGraph = () => {
const [faker, setFaker] = useState<Faker>(fak)
useEffect(() => {
// Globally seed the Math.random
const params = new URLSearchParams(document.location.search)
const seed = params.get('seed') // is the string "Jonathan"
if (seed) {
seedrandom(seed, { global: true })
// seed faker with the random function
const f = new Faker({ locale: en })
f.seed(Math.random())
setFaker(f)
}
}, [])
const randomGraph = useCallback(() => {
useGraphStore.getState().reset()
// Create the graph
const graph = erdosRenyi(UndirectedGraph, { order: 100, probability: 0.1 })
graph.nodes().forEach((node: string) => {
// Calculate visual degree from graph edges
const visualDegree = graph.degree(node)
// Simulate db_degree being higher than visual degree (some nodes have hidden connections)
const db_degree = faker.number.int({ min: visualDegree, max: visualDegree + 8 })
graph.mergeNodeAttributes(node, {
label: faker.person.fullName(),
size: faker.number.int({ min: Constants.minNodeSize, max: Constants.maxNodeSize }),
color: randomColor(),
x: Math.random(),
y: Math.random(),
// Simulate database degree being higher than visual degree for testing
db_degree,
// for node-border
borderColor: randomColor(),
borderSize: faker.number.float({ min: 0, max: 1, multipleOf: 0.1 }),
// for node-image
pictoColor: randomColor(),
image: faker.image.urlLoremFlickr(),
})
})
// Add edge attributes
graph.edges().forEach((edge: string) => {
graph.mergeEdgeAttributes(edge, {
label: faker.lorem.words(faker.number.int({ min: 1, max: 3 })),
size: faker.number.float({ min: 1, max: 5 }),
color: randomColor(),
})
})
return graph as Graph<NodeType, EdgeType>
}, [faker])
return { faker, randomColor, randomGraph }
}
export default useRandomGraph