aboutsummaryrefslogtreecommitdiffstats
path: root/client/src/index.js
blob: f610cd466110599190e389ab3bba7ec04dac352e (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
import 'preact/debug';
import 'core-js/stable';
import 'regenerator-runtime/runtime';
import { h, Fragment, Component, render } from 'preact';
import { GraphContainer } from './graph';
import { Menu, Discussion, Selection } from './ui';
import { UserContainer } from './user';

class SelectionContainer extends Component {
	state = {
		selection: [],
	};

	toggle = (id) => {
		const { selection } = this.state;
		if (selection.indexOf(id) < 0)
			this.setState({ selection: [ ...selection, id ] });
		else
			this.setState({ selection: selection.filter(i => i !== id) });
	}

	render() {
		return this.props.render({
			...this.state,
			toggleSelected: this.toggle,
		});
	}
}

class CollapseContainer extends Component {
	/**
	 * Contains the collapse-state for each node.
	 *
	 * - `false`: visible
	 * - `true`: implicitly hidden
	 * - `'explicit'`: explicitly hidden
	 */
	state = {};

	/**
	 * Toggle a node's collapse-state.
	 * - `false` → `'explicit'`
	 * - `true`, `'explicit'` → `false`
	 */
	toggle = (id) => {
		const { items } = this.props;

		const state = Object.assign({}, this.state);
		state[id] = state[id] ? false : 'explicit';

		this.setState(this.update(items[id], state));
	}

	/**
	 * Recursively update child node's collapse-state.
	 *
	 * If a node has parents and they are all collapsed,
	 * mark that node as implicitly hidden (`true`).
	 *
	 * Then update all of that node's children.
	 */
	update(node, state) {
		const { items } = this.props;

		if (state[node.id] !== 'explicit') {
			state[node.id] =
				node.inReplyTo.length
				&& node.inReplyTo.every(p => state[p.id]);
		}

		for (const child of node.replies)
			this.update(items[child.id], state);

		return state;
	}

	render() {
		return this.props.render({
			collapsed: this.state,
			toggleCollapsed: this.toggle,
		});
	}
}

const search = new URLSearchParams(window.location.search);
const graph = search.has('graph') ? search.get('graph') : 'lib/graph.json';

let app;
if (search.has('document') || search.has('note') || search.has('graph')) {
	const mode = search.has('document') ? "jsonld" : "mastodon";
	const url = search.get('document') || search.get('note') || search.get('graph');

	app = (
		<UserContainer>
			<GraphContainer
				url={url}
				mode={mode}
			>
				{({ loading, error, name, items }) => {
					if (loading) {
						return (
							<article>
								<div>loading...</div>
							</article>
						);
					}

					if (error) {
						return (
							<article>
								<h1>error loading</h1>
								<div>
									<p>{error.toString()}</p>
									<pre>
										<code>
											{error.stack}
										</code>
									</pre>
								</div>
							</article>
						);
					}

					return (
						<SelectionContainer render={({ selection, toggleSelected }) => (
							<>
								<CollapseContainer
									items={items}
									render={({ collapsed, toggleCollapsed }) => (
										<Discussion
											name={name}
											items={items}
											collapsed={collapsed}
											toggleCollapsed={toggleCollapsed}
											toggleSelected={toggleSelected}
										/>
									)}
								/>
								<Selection
									items={selection.map(id => items[id])}
									toggleSelected={toggleSelected}
								/>
							</>
						)} />
					);
				}}
			</GraphContainer>
		</UserContainer>
	);
} else {
	app = (
		<UserContainer>
			<Menu />
		</UserContainer>
	);
}

render(app, document.body);