Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/utils/jsx/grid.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { css, keyframes } from '@emotion/css';
import { useId, useMemo } from 'react';
import { useId, useMemo, useState } from 'react';

const size = 75;
const cols = 16;
Expand All @@ -26,12 +26,7 @@ const path = (points: number[]) =>
})
.join(' L ')}`;

const grid = (id: string) => {
// Hash the id from React to generate a numeric seed for randomness.
const seed =
id.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0) %
(2 ** 31 - 1);

const grid = (seed: number) => {
// Decide which points are active and will be connected to their neighbours.
const active = Array.from({ length: cols * rows }, (_, point) => {
const col = point % cols;
Expand Down Expand Up @@ -238,12 +233,27 @@ export default ({
height?: number;
className?: string;
}) => {
const id = useId();
const { connections, requests, vias } = useMemo(() => grid(id), [id]);
const id = `grid-${useId()}`;

const [seed] = useState(() => {
// During hydration, reuse the seed already rendered into the DOM.
const fromDom =
typeof document !== 'undefined'
? document.getElementById(id)?.getAttribute('data-seed')
: null;
const parsed = Number.parseInt(fromDom ?? '', 10);
return Number.isInteger(parsed)
? parsed
: Math.floor(Math.random() * 2 ** 31 - 1);
});

const { connections, requests, vias } = useMemo(() => grid(seed), [seed]);

return (
<svg
id={id}
xmlns="http://www.w3.org/2000/svg"
data-seed={seed}
// The base viewBox would be `0 0 ${cols * size} ${rows * size}`
// But, we want to center that around whatever width and height are passed in
viewBox={`${(cols * size - width * size) / 2} ${(rows * size - height * size) / 2} ${width * size} ${height * size}`}
Expand Down
5 changes: 3 additions & 2 deletions src/utils/jsx/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ const styles = {
height: 100%;
left: 50%;
transform: translateX(-50%);
top: 0;
bottom: 0;
top: ${theme.spacing(-2)};
bottom: ${theme.spacing(-2)};
z-index: -1;
pointer-events: none;
`,
content: css`
display: flex;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/jsx/islands/swagger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ const plugin = (system: System) => ({
system.getComponent('ModelWrapper');

return (
<Wrapper>
<Wrapper key={fullPath.join('/')}>
<Summary
system={system}
badge="SCHEMA"
Expand Down