()
| 21 | * The goal of this file is to seed random generators if the query params 'seed' is present. |
| 22 | */ |
| 23 | const useRandomGraph = () => { |
| 24 | const [faker, setFaker] = useState<Faker>(fak) |
| 25 | |
| 26 | // Seed global Math.random after commit to avoid polluting the RNG |
| 27 | // during render (StrictMode double-invoke, Concurrent Mode aborts) |
| 28 | useEffect(() => { |
| 29 | const params = new URLSearchParams(document.location.search) |
| 30 | const seed = params.get('seed') |
| 31 | if (!seed) return |
| 32 | |
| 33 | // Global side effect — intentionally in effect, not render |
| 34 | seedrandom(seed, { global: true }) |
| 35 | const f = new Faker({ locale: en }) |
| 36 | f.seed(Math.random()) |
| 37 | |
| 38 | const timer = setTimeout(() => setFaker(f), 0) |
| 39 | return () => clearTimeout(timer) |
| 40 | }, []) |
| 41 | |
| 42 | const randomGraph = useCallback(() => { |
| 43 | useGraphStore.getState().reset() |
| 44 | |
| 45 | // Create the graph |
| 46 | const graph = erdosRenyi(UndirectedGraph, { order: 100, probability: 0.1 }) |
| 47 | graph.nodes().forEach((node: string) => { |
| 48 | graph.mergeNodeAttributes(node, { |
| 49 | label: faker.person.fullName(), |
| 50 | size: faker.number.int({ min: Constants.minNodeSize, max: Constants.maxNodeSize }), |
| 51 | color: randomColor(), |
| 52 | x: Math.random(), |
| 53 | y: Math.random(), |
| 54 | // for node-border |
| 55 | borderColor: randomColor(), |
| 56 | borderSize: faker.number.float({ min: 0, max: 1, multipleOf: 0.1 }), |
| 57 | // for node-image |
| 58 | pictoColor: randomColor(), |
| 59 | image: faker.image.urlLoremFlickr() |
| 60 | }) |
| 61 | }) |
| 62 | |
| 63 | // Add edge attributes |
| 64 | graph.edges().forEach((edge: string) => { |
| 65 | graph.mergeEdgeAttributes(edge, { |
| 66 | label: faker.lorem.words(faker.number.int({ min: 1, max: 3 })), |
| 67 | size: faker.number.float({ min: 1, max: 5 }), |
| 68 | color: randomColor() |
| 69 | }) |
| 70 | }) |
| 71 | |
| 72 | return graph as Graph<NodeType, EdgeType> |
| 73 | }, [faker]) |
| 74 | |
| 75 | return { faker, randomColor, randomGraph } |
| 76 | } |
| 77 | |
| 78 | export default useRandomGraph |
no test coverage detected