Skip to content

Commit ff67b40

Browse files
authored
Inline identicon (#2145)
* copy identicon into the repo * render svg directly * clean it up
1 parent f1e8f2e commit ff67b40

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

app/ui/lib/Identicon.tsx

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,35 @@
55
*
66
* Copyright Oxide Computer Company
77
*/
8+
import md5 from 'md5'
89
import { useMemo } from 'react'
910

10-
import { generateIdenticon, md5 } from '@oxide/identicon'
11+
type Rectangle = { x: number; y: number; isOn: boolean }
12+
13+
const getPixels = (s: string) => {
14+
const hash = md5(s)
15+
const buffer: Rectangle[] = []
16+
17+
for (let i = 0; i < 18; i++) {
18+
const isOn = hash.charCodeAt(i) % 2 === 0
19+
20+
if (i < 3) {
21+
// Start with the two central columns
22+
buffer.push({ x: 2, y: i, isOn })
23+
buffer.push({ x: 3, y: i, isOn })
24+
} else if (i < 6) {
25+
// Move out to the columns one from the edge
26+
buffer.push({ x: 1, y: i - 3, isOn })
27+
buffer.push({ x: 4, y: i - 3, isOn })
28+
} else if (i < 9) {
29+
// Fill the outside columns
30+
buffer.push({ x: 0, y: i - 6, isOn })
31+
buffer.push({ x: 5, y: i - 6, isOn })
32+
}
33+
}
34+
35+
return buffer
36+
}
1137

1238
type IdenticonProps = {
1339
/** string used to generate the graphic */
@@ -16,6 +42,19 @@ type IdenticonProps = {
1642
}
1743

1844
export function Identicon({ name, className }: IdenticonProps) {
19-
const content = useMemo(() => generateIdenticon(md5(name)), [name])
20-
return <div className={className} dangerouslySetInnerHTML={{ __html: content }} />
45+
const pixels = useMemo(() => getPixels(name), [name])
46+
return (
47+
<div className={className}>
48+
<svg xmlns="http://www.w3.org/2000/svg" width="28" height="28">
49+
<g fill="currentColor">
50+
{pixels.map((pixel) => {
51+
if (!pixel.isOn) return null
52+
const x = pixel.x * 3 + 2 * pixel.x
53+
const y = pixel.y * 8 + 2 * pixel.y
54+
return <rect key={`${x}|${y}`} x={x} y={y} width="3" height="8" />
55+
})}
56+
</g>
57+
</svg>
58+
</div>
59+
)
2160
}

package-lock.json

Lines changed: 14 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"@floating-ui/react": "^0.26.11",
3434
"@headlessui/react": "^1.7.18",
3535
"@oxide/design-system": "^1.2.10",
36-
"@oxide/identicon": "0.0.4",
3736
"@radix-ui/react-accordion": "^1.1.2",
3837
"@radix-ui/react-dialog": "^1.0.5",
3938
"@radix-ui/react-dropdown-menu": "^2.0.6",
@@ -48,6 +47,7 @@
4847
"filesize": "^10.1.1",
4948
"lodash.throttle": "^4.1.1",
5049
"match-sorter": "^6.3.4",
50+
"md5": "^2.3.0",
5151
"mousetrap": "^1.6.5",
5252
"p-map": "^7.0.2",
5353
"p-retry": "^6.2.0",
@@ -81,6 +81,7 @@
8181
"@testing-library/jest-dom": "^6.4.2",
8282
"@testing-library/react": "^14.2.2",
8383
"@types/lodash.throttle": "^4.1.9",
84+
"@types/md5": "^2.3.5",
8485
"@types/mousetrap": "^1.6.15",
8586
"@types/react": "^18.2.74",
8687
"@types/react-dom": "^18.2.24",

0 commit comments

Comments
 (0)