anproto personal data server
1import { h } from './lib/h.js'
2import { apds } from './apds.js'
3
4export const profile = async () => {
5 const div = h('div')
6
7 const avatarImg = await apds.visual(await apds.pubkey())
8
9 const existingImage = await apds.get('image')
10
11 if (existingImage) { avatarImg.src = await apds.get(existingImage)}
12
13 avatarImg.style = 'height: 30px; width: 30px; float: left; margin-right: 5px; object-fit: cover;'
14
15 avatarImg.onclick = () => {uploader.click()}
16
17 const uploader = h('input', { type: 'file', style: 'display: none;'})
18
19 uploader.addEventListener('change', (e) => {
20 const file = e.target.files[0]
21 const reader = new FileReader()
22
23 reader.onload = (e) => {
24 const canvas = document.createElement("canvas")
25 const ctx = canvas.getContext("2d")
26 const img = new Image()
27
28 img.onload = async () => {
29 const size = 256
30 if (img.width > size || img.height > size) {
31 const width = img.width
32 const height = img.height
33 let cropWidth
34 let cropHeight
35
36 if (width > height) {
37 cropWidth = size
38 cropHeight = cropWidth * (height / width)
39 } else {
40 cropHeight = size
41 cropWidth = cropHeight * (width / height)
42 }
43
44 canvas.width = cropWidth
45 canvas.height = cropHeight
46 ctx.drawImage(img, 0, 0, width, height, 0, 0, cropWidth, cropHeight)
47 const croppedImage = canvas.toDataURL()
48 avatarImg.src = croppedImage
49 const hash = await apds.make(croppedImage)
50 await apds.put('image', hash)
51 } else {
52 const croppedImage = canvas.toDataURL()
53 avatarImg.src = img.src
54 const hash = await apds.make(img.src)
55 await apds.put('image', hash)
56 }
57 }
58 img.src = e.target.result
59 }
60 reader.readAsDataURL(file)
61 })
62
63
64 div.appendChild(uploader)
65
66 div.appendChild(avatarImg)
67
68 div.appendChild(h('div', [await apds.pubkey()]))
69
70 const name = await apds.get('name')
71
72 const namer = h('input', {
73 placeholder: name || 'Name yourself'
74 })
75
76
77 const namerDiv = h('div', [
78 namer,
79 h('button', {onclick: async () => {
80 if (namer.value) {
81 namer.placeholder = namer.value
82 await apds.put('name', namer.value)
83 namer.value = ''
84 }
85 }}, ['Save'])
86 ])
87
88 div.appendChild(namerDiv)
89 return div
90}