aboutsummaryrefslogtreecommitdiffstats
path: root/client/src/ui/Discussion.js
blob: 5fef693b37d04a0f7fa627133d5e1485393806ec (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
import { h } from 'preact';
import { useState, useEffect } from 'preact/hooks';
import Layout from '../layout';
import css from './css';
import { Links } from './Links';
import { Note } from './Note';

css`
article {
	position: absolute;
	inset: 0;

	overflow: auto;
}

article > h1 {
	display: flex;
	align-items: baseline;

	position: sticky;
	inset: 0;
	bottom: auto;
	z-index: 200;

	margin: 0;
	padding: 0.75rem 1.5rem;
	background: var(--theme-title-bg);
	color: var(--theme-title-fg);
}

article > h1 > span {
	overflow: hidden;
	white-space: nowrap;
	text-overflow: ellipsis;
}

article > h1 .back {
	font-size: 0.8em;
	color: inherit;
	opacity: 0.5;

	margin-left: 1em;
}

article > div {
	position: relative;
	margin: 2rem 2rem 6rem;
}
`;

const tmp = document.createElement('span');

export const Discussion = ({
	name, items, collapsed,
	toggleCollapsed, toggleSelected,
}) => {
	const [{ width, height, positions, links }, setState] = useState({
		width: 0,
		height: 0,
		positions: {},
		links: [],
	});

	const layout = new Layout();
	const setNodeSize = layout.addNode.bind(layout);

	useEffect(() => {
		layout.render().then(setState);
	}, [ items, collapsed ]);

	tmp.innerHTML = name;
	name = tmp.innerText;

	return (
		<article>
			<h1>
				<span>{name}</span>
				<a class="back" href="?">back</a>
			</h1>
			<div>
				<Links width={width} height={height} links={links} />
				<div>
					{Object.values(items).map((item) => {
						const isCollapsed = collapsed[item.id];
						const hidden =
							item.inReplyTo.every(p => collapsed[p.id])
							&& item.replies.every(c => collapsed[c.id])
							&& item.inReplyTo.length !== 0;
						if (hidden)
							return;

						const onlyParent = item.inReplyTo.length === 1 && items[item.inReplyTo[0].id];

						for (const reply of item.replies) {
							if (isCollapsed && collapsed[reply.id])
								continue;

							layout.addLink(item.id, reply.id);
						}

						return (
							<Note
								{...item}
								smallHeader={
									onlyParent																			 // only one parent post
									&& onlyParent.attributedTo === item.attributedTo // posted by same user
									&& onlyParent.published === item.published			 // in the same second
								}
								collapsed={isCollapsed}
								position={positions[item.id]}
								onCollapse={toggleCollapsed}
								onSelect={toggleSelected}
								onSize={setNodeSize}
							/>
				 		);
					})}
				</div>
			</div>
		</article>
	);
};