summaryrefslogtreecommitdiff
path: root/web/source/settings/lib/form/check-list.tsx
blob: e44daefff49044d7de588c89ead57d1b575de851 (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
/*
	GoToSocial
	Copyright (C) GoToSocial Authors admin@gotosocial.org
	SPDX-License-Identifier: AGPL-3.0-or-later

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.

	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import {
	useRef,
	useEffect,
	useCallback,
	useMemo,
} from "react";

import type {
	Checkable,
	ChecklistInputHook,
	CreateHookNames,
	HookOpts,
} from "./types";

import {
	useChecklistReducer,
	actions,
} from "../../redux/checklist";

const _default: { [k: string]: Checkable } = {};
export default function useCheckListInput(
	/* eslint-disable no-unused-vars */
	{ name, Name }: CreateHookNames,
	{
		entries = [],
		uniqueKey = "key",
		initialValue = false,
	}: HookOpts<boolean>
): ChecklistInputHook {
	const [state, dispatch] = useChecklistReducer(entries, uniqueKey, initialValue);
	const toggleAllRef = useRef<any>(null);

	useEffect(() => {
		if (toggleAllRef.current != null) {
			let some = state.selectedEntries.size > 0;
			let all = false;
			if (some) {
				all = state.selectedEntries.size == Object.values(state.entries).length;
			}
			toggleAllRef.current.checked = all;
			toggleAllRef.current.indeterminate = some && !all;
		}
		// only needs to update when state.selectedEntries changes, not state.entries
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [state.selectedEntries]);

	const reset = useCallback(
		() => dispatch(actions.updateAll(initialValue)),
		[initialValue, dispatch]
	);

	const onChange = useCallback(
		(key: string, value: Checkable) => dispatch(actions.update({ key, value })),
		[dispatch]
	);

	const updateMultiple = useCallback(
		(entries: [key: string, value: Partial<Checkable>][]) => dispatch(actions.updateMultiple(entries)),
		[dispatch]
	);

	return useMemo(() => {
		function toggleAll(e) {
			let checked = e.target.checked;
			if (e.target.indeterminate) {
				checked = false;
			}
			dispatch(actions.updateAll(checked));
		}

		function selectedValues() {
			return Array.from((state.selectedEntries)).map((key) => ({
				...state.entries[key] // returned as new object, because reducer state is immutable
			}));
		}

		return Object.assign([
			state,
			reset,
			{ name }
		], {
			_default,
			hasChanged: () => true,
			name,
			Name: "",
			value: state.entries,
			onChange,
			selectedValues,
			reset,
			someSelected: state.selectedEntries.size > 0,
			updateMultiple,
			toggleAll: {
				ref: toggleAllRef,
				onChange: toggleAll
			}
		});
	}, [state, reset, name, onChange, updateMultiple, dispatch]);
}