aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/Note.js
blob: 34c15d78d6514f6a007065d66f03a2813f1cee52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { h } from 'preact';
import { useRef, useLayoutEffect } from 'preact/hooks';
import ColorHash from 'color-hash';
import { formatRelative } from 'date-fns';
import cn from 'classnames';
import css from './css';

const now = new Date();
const Time = ({ time }) => {
	const dt = new Date(time);
	return formatRelative(dt, now);
};

css`
.flex-space {
	flex: 1;
	margin: 0;
}
`;
const Space = () => <div class="flex-space" />;

css`
section > header {
	display: flex;
	flex-direction: row;

	height: 1.75rem;

	color: var(--theme-header-fg);
	background: var(--theme-header-bg);

	cursor: pointer;
	transition: opacity 0.3s;
}

section > header:hover {
	opacity: 0.5;
}

section > header.small {
	height: 0.5em;
}

section > header > * {
	margin: 0 0.5em;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	line-height: 1.75rem;
}

section > header > img {
	height: 1.5rem;
	width: 1.5rem;
	flex: 0 0 1.5rem;
	margin: 0.125rem;
	border-radius: 0.2rem;
}

section > header > .user {
	flex: 0 0.5 auto;
}

section > header > .time {
	flex: 0 1 auto;
	font-size: 0.8em;
	color: #363636;
}
`;
const Header = ({ user, published, onClick }) => {
	const username = user.name || user.preferredUsername;

	return (
		<header onClick={onClick}>
			<img src={new URL(user.icon.url, user.id)} />
			<span className="user">{username}</span>
			<Space />
			<span className="time"><Time time={published} /></span>
		</header>
	);
};

css`
section {
	position: absolute;
	overflow: hidden;

	width: 22em;
	color: var(--theme-note-fg);
	background: var(--theme-note-bg);
	box-shadow: rgba(0,0,0, 0.7) 2px 4px 5px;

	border-radius: 0.4em;
}

section.type-Tombstone {
	color: var(--theme-note-bg);
	background: var(--theme-note-fg);
}

section > main {
	padding: 1em;
	font-family: serif;
}

section.tombstone > main {
	font-family: inherit;
}

section.hidden {
	display: none;
}

section.collapsed {
	width: auto;
}
section.collapsed > main {
	display: none;
}
`;

const colors = new ColorHash({ lightness: 0.8 });
export const Note = ({
	id, type, attributedTo, published, content,
	smallHeader = false, collapsed = false, position,
	onHide, onSize,
}) => {
	const backgroundColor = attributedTo.color || colors.hex(attributedTo.id);

	const root = useRef(null);
	useLayoutEffect(() => {
		onSize(id, root.current.offsetWidth, root.current.offsetHeight);
	});

	const onClickHeader = (e) => {
		e.preventDefault();
		onHide(id);
	};

	return (
		<section
			style={{
				'--theme-header-bg': backgroundColor,
				'left': position && `${position[0] - root.current.offsetWidth / 2}px`,
				'top': position && `${position[1] - root.current.offsetHeight / 2}px`,
				'visibility': position ? 'visible' : 'hidden',
			}}
			class={cn(`type-${type}`, collapsed && 'collapsed')}
			data-id={id}
			ref={root}
		>
			{smallHeader
				? <header className="small" onClick={onClickHeader} />
				: <Header onClick={onClickHeader} user={attributedTo} published={published} />
			}
			<main>
				{content}
			</main>
		</section>
	);
};