summaryrefslogtreecommitdiff
path: root/packages/frontend/src/scripts/following-feed-utils.ts
blob: 39f17949d60e987c5e92ff0bc7cc8d6b9f55bc45 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { computed, Ref, WritableComputedRef } from 'vue';
import { defaultStore } from '@/store.js';
import { deepMerge } from '@/scripts/merge.js';
import { PageHeaderItem } from '@/types/page-header.js';
import { i18n } from '@/i18n.js';
import { popupMenu } from '@/os.js';
import { MenuItem } from '@/types/menu.js';

export const followingTab = 'following' as const;
export const mutualsTab = 'mutuals' as const;
export const followersTab = 'followers' as const;
export const followingFeedTabs = [followingTab, mutualsTab, followersTab] as const;
export type FollowingFeedTab = typeof followingFeedTabs[number];

export function followingTabName(tab: FollowingFeedTab): string;
export function followingTabName(tab: FollowingFeedTab | null | undefined): null;
export function followingTabName(tab: FollowingFeedTab | null | undefined): string | null {
	if (tab === followingTab) return i18n.ts.following;
	if (tab === followersTab) return i18n.ts.followers;
	if (tab === mutualsTab) return i18n.ts.mutuals;
	return null;
}

export function followingTabIcon(tab: FollowingFeedTab | null | undefined): string {
	if (tab === followersTab) return 'ph-user ph-bold ph-lg';
	if (tab === mutualsTab) return 'ph-user-switch ph-bold ph-lg';
	return 'ph-user-check ph-bold ph-lg';
}

export type FollowingFeedModel = {
	[Key in keyof FollowingFeedState]: WritableComputedRef<FollowingFeedState[Key]>;
}

export interface FollowingFeedState {
	withNonPublic: boolean,
	withQuotes: boolean,
	withBots: boolean,
	withReplies: boolean,
	onlyFiles: boolean,
	userList: FollowingFeedTab,
	remoteWarningDismissed: boolean,
}

export const defaultFollowingFeedState: FollowingFeedState = {
	withNonPublic: false,
	withQuotes: false,
	withBots: true,
	withReplies: false,
	onlyFiles: false,
	userList: followingTab,
	remoteWarningDismissed: false,
};

interface StorageInterface<T extends Partial<FollowingFeedState> = Partial<FollowingFeedState>> {
	readonly state: Partial<T>;
	readonly reactiveState: Ref<Partial<T>>;
	save(updated: T): void;
}

export function createHeaderItem(storage?: Ref<StorageInterface>): PageHeaderItem {
	const menu = createOptionsMenu(storage);
	return {
		icon: 'ti ti-dots',
		text: i18n.ts.options,
		handler: ev => popupMenu(menu, ev.currentTarget ?? ev.target),
	};
}

export function createOptionsMenu(storage?: Ref<StorageInterface>): MenuItem[] {
	const {
		userList,
		withNonPublic,
		withQuotes,
		withBots,
		withReplies,
		onlyFiles,
	} = createModel(storage);

	return [
		{
			type: 'switch',
			text: i18n.ts.showNonPublicNotes,
			ref: withNonPublic,
			disabled: computed(() => userList.value === followersTab),
		},
		{
			type: 'switch',
			text: i18n.ts.showQuotes,
			ref: withQuotes,
		},
		{
			type: 'switch',
			text: i18n.ts.showBots,
			ref: withBots,
		},
		{
			type: 'switch',
			text: i18n.ts.showReplies,
			ref: withReplies,
			disabled: onlyFiles,
		},
		{
			type: 'divider',
		},
		{
			type: 'switch',
			text: i18n.ts.fileAttachedOnly,
			ref: onlyFiles,
			disabled: withReplies,
		},
	];
}

export function createModel(storage?: Ref<StorageInterface>): FollowingFeedModel {
	// eslint-disable-next-line no-param-reassign
	storage ??= createDefaultStorage();

	// Based on timeline.saveTlFilter()
	const saveFollowingFilter = <K extends keyof FollowingFeedState>(key: K, value: FollowingFeedState[K]) => {
		const state = deepMerge(storage.value.state, defaultFollowingFeedState);
		const out = deepMerge({ [key]: value }, state);
		storage.value.save(out);
	};

	const userList: WritableComputedRef<FollowingFeedTab> = computed({
		get: () => storage.value.reactiveState.value.userList ?? defaultFollowingFeedState.userList,
		set: value => saveFollowingFilter('userList', value),
	});
	const withNonPublic: WritableComputedRef<boolean> = computed({
		get: () => {
			if (userList.value === 'followers') return false;
			return storage.value.reactiveState.value.withNonPublic ?? defaultFollowingFeedState.withNonPublic;
		},
		set: value => saveFollowingFilter('withNonPublic', value),
	});
	const withQuotes: WritableComputedRef<boolean> = computed({
		get: () => storage.value.reactiveState.value.withQuotes ?? defaultFollowingFeedState.withQuotes,
		set: value => saveFollowingFilter('withQuotes', value),
	});
	const withBots: WritableComputedRef<boolean> = computed({
		get: () => storage.value.reactiveState.value.withBots ?? defaultFollowingFeedState.withBots,
		set: value => saveFollowingFilter('withBots', value),
	});
	const withReplies: WritableComputedRef<boolean> = computed({
		get: () => storage.value.reactiveState.value.withReplies ?? defaultFollowingFeedState.withReplies,
		set: value => saveFollowingFilter('withReplies', value),
	});
	const onlyFiles: WritableComputedRef<boolean> = computed({
		get: () => storage.value.reactiveState.value.onlyFiles ?? defaultFollowingFeedState.onlyFiles,
		set: value => saveFollowingFilter('onlyFiles', value),
	});
	const remoteWarningDismissed: WritableComputedRef<boolean> = computed({
		get: () => storage.value.reactiveState.value.remoteWarningDismissed ?? defaultFollowingFeedState.remoteWarningDismissed,
		set: value => saveFollowingFilter('remoteWarningDismissed', value),
	});

	return {
		userList,
		withNonPublic,
		withQuotes,
		withBots,
		withReplies,
		onlyFiles,
		remoteWarningDismissed,
	};
}

function createDefaultStorage() {
	return computed(() => ({
		state: defaultStore.state.followingFeed,
		reactiveState: defaultStore.reactiveState.followingFeed,
		save(updated: typeof defaultStore.state.followingFeed) {
			return defaultStore.set('followingFeed', updated);
		},
	}));
}