summaryrefslogtreecommitdiff
path: root/src/client/theme-store.ts
blob: 5e440efbf9841759470fce16148fef902b50400d (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
import { api } from '@/os';
import { $i } from '@/account';
import { ColdDeviceStorage } from './store';
import { Theme } from './scripts/theme';

const lsCacheKey = $i ? `themes:${$i.id}` : '';

export function getThemes(): Theme[] {
	return JSON.parse(localStorage.getItem(lsCacheKey) || '[]');
}

export async function fetchThemes(): Promise<void> {
	if ($i == null) return;

	try {
		const themes = await api('i/registry/get', { scope: ['client'], key: 'themes' });
		localStorage.setItem(lsCacheKey, JSON.stringify(themes));
	} catch (e) {
		if (e.code === 'NO_SUCH_KEY') return;
		throw e;
	}
}

export async function addTheme(theme: Theme): Promise<void> {
	await fetchThemes();
	const themes = getThemes().concat(theme);
	await api('i/registry/set', { scope: ['client'], key: 'themes', value: themes });
	localStorage.setItem(lsCacheKey, JSON.stringify(themes));
}

export async function removeTheme(theme: Theme): Promise<void> {
	const themes = getThemes().filter(t => t.id != theme.id);
	await api('i/registry/set', { scope: ['client'], key: 'themes', value: themes });
	localStorage.setItem(lsCacheKey, JSON.stringify(themes));
}

// TODO: そのうち消す
if (ColdDeviceStorage.get('themes').length > 0) {
	const lsThemes = ColdDeviceStorage.get('themes');
	let registryThemes;
	try {
		registryThemes = await api('i/registry/get', { scope: ['client'], key: 'themes' });
	} catch (e) {
		if (e.code === 'NO_SUCH_KEY') {
			registryThemes = [];
		} else {
			throw e;
		}
	}
	const themes = [] as Theme[];
	for (const theme of lsThemes) {
		if (themes.some(x => x.id === theme.id)) continue;
		themes.push(theme);
	}
	for (const theme of registryThemes) {
		if (themes.some(x => x.id === theme.id)) continue;
		themes.push(theme);
	}
	await api('i/registry/set', { scope: ['client'], key: 'themes', value: themes });
	localStorage.setItem(lsCacheKey, JSON.stringify(themes));
	ColdDeviceStorage.set('themes', []);
}