aboutsummaryrefslogtreecommitdiff
path: root/src/stores/settings.ts
blob: 5c27c516e87853c66c748a313178d4c19aea6ff4 (plain) (blame)
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
import { browser } from '$app/environment';
import { writable } from 'svelte/store';

export interface Settings {
	cacheMangaMinutes: number;
	cacheMinutes: number;
	displayUpcomingAnimeCollapsed: boolean;
	displayAnimeCollapsed: boolean;
	displayMangaCollapsed: boolean;
	displayNotStarted: boolean;
	displayUnresolved: boolean;
	calculateChaptersRoundedDown: boolean;
	displaySortedByDifference: boolean;
	displayOutboundLinksTo: 'anilist' | 'livechartme' | 'animeschedule' | 'myanimelist';
	displayPausedMedia: boolean;
	displayLimitListHeight: boolean;
	displaySocialButton: boolean;
	calculateGuessingDisabled: boolean;
	displayHoverNavigation: boolean;
	displayTitleFormat: 'english' | 'romaji' | 'native';
	calculateGuessMethod: 'median' | 'iqr_median' | 'iqr_mode' | 'mode';
	calculateDisableOutOfDateVolumeWarning: boolean;
	displayPlannedAnime: boolean;
	displayFurigana: boolean;
	displayAoButa:
		| 'kaede'
		| 'mai'
		| 'mai_2'
		| 'nodoka'
		| 'rio'
		| 'sakuta'
		| 'shouko'
		| 'tomoe'
		| 'random'
		| 'none';
	disableManga: boolean;
	disableAnime: boolean;
	disableUpcomingAnime: boolean;
	display24HourTime: boolean;
	displayCountdownRightAligned: boolean;
	displayNativeCountdown: boolean;
	displayHoverCover: boolean;
	displayDisableAnimations: boolean;
	displayDisableNotifications: boolean;
	displayCoverMode: boolean;
	displayCoverWidth: number;
	displayShortCountdown: boolean;
	displayScheduleListMode: boolean;
	displayLanguage: 'en' | 'ja';
	displayDisableLastActivityWarning: boolean;
}

const defaultSettings: Settings = {
	// Display
	displayOutboundLinksTo: 'anilist',
	displayPausedMedia: true,
	displayPlannedAnime: true,
	displayLimitListHeight: false,
	displaySocialButton: false,
	displayUnresolved: false,
	displayTitleFormat: 'english',
	displayFurigana: false,
	displayHoverNavigation: false,
	displayNotStarted: false,
	displayUpcomingAnimeCollapsed: false,
	displayAnimeCollapsed: false,
	displayMangaCollapsed: false,
	displaySortedByDifference: false,
	displayAoButa: 'mai_2',
	disableManga: false,
	disableAnime: false,
	disableUpcomingAnime: false,
	display24HourTime: false,
	displayCountdownRightAligned: false,
	displayNativeCountdown: false,
	displayHoverCover: false,
	displayDisableAnimations: false,
	displayDisableNotifications: false,
	displayCoverMode: true,
	displayCoverWidth: 100, // 116.609
	displayShortCountdown: false,
	displayScheduleListMode: false,
	displayLanguage: 'en',
	displayDisableLastActivityWarning: false,

	// Calculation
	calculateChaptersRoundedDown: true,
	calculateDisableOutOfDateVolumeWarning: false,
	calculateGuessingDisabled: true,
	calculateGuessMethod: 'iqr_mode',

	// Cache
	cacheMangaMinutes: 120,
	cacheMinutes: 30
};

const createStore = () => {
	const { subscribe, set, update } = writable<Settings>(
		JSON.parse(
			browser
				? localStorage.getItem('settings') ?? JSON.stringify(defaultSettings)
				: JSON.stringify(defaultSettings)
		)
	);
	let state: Settings;

	subscribe((value) => (state = value));

	return {
		subscribe,
		set,
		update,
		reset: () => set(defaultSettings),
		get: () => {
			const keys = Object.keys(defaultSettings);
			const settingsKeys = Object.keys(state);
			const updatedSettings = { ...state };

			for (const key of keys)
				if (!settingsKeys.includes(key))
					(updatedSettings[key as keyof Settings] as unknown) =
						defaultSettings[key as keyof Settings];

			if (browser) localStorage.setItem('settings', JSON.stringify(updatedSettings));

			return updatedSettings;
		},
		setKey: (key: keyof Settings, value: unknown) =>
			update((settings) => ({ ...settings, [key]: value }))
	};
};

const settings = createStore();

settings.subscribe((value) => {
	if (browser) localStorage.setItem('settings', JSON.stringify(value));
});

export default settings;