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
|
import { addNotification } from "$lib/Notification/store";
import { options } from "$lib/Notification/options";
import root from "$lib/Utility/root";
import settings from "$stores/settings";
import settingsSyncPulled from "$stores/settingsSyncPulled";
import settingsSyncTimes from "$stores/settingsSyncTimes";
import { get } from "svelte/store";
import type { CommandPaletteAction } from "./actions";
export const syncActions = (
identityId: number,
syncEnabled: boolean,
): CommandPaletteAction[] => {
if (identityId <= 0) return [];
const actions: CommandPaletteAction[] = [
{
name: "Push Settings Now",
url: "#",
preventDefault: true,
tags: ["settings", "sync", "push", "upload", "remote"],
onClick: () => {
settings.setKey("settingsSync", true);
fetch(root(`/api/configuration`), {
method: "PUT",
body: JSON.stringify(get(settings)),
})
.then((response) => {
if (!response.ok) return;
addNotification(
options({
heading: "Settings Sync",
description: "Pushed local configuration to remote",
}),
);
settingsSyncTimes.update((times) => ({
...times,
lastPush: new Date(),
}));
})
.catch((error) =>
console.error("Settings sync push failed", error),
);
},
},
{
name: "Pull Settings Now",
url: "#",
preventDefault: true,
tags: ["settings", "sync", "pull", "download", "remote"],
onClick: () => {
settings.setKey("settingsSync", true);
fetch(root(`/api/configuration?id=${identityId}`))
.then((response) => {
if (!response.ok) return;
return response.json().then((data) => {
if (!data?.configuration) {
addNotification(
options({
heading: "Settings Sync",
description: "No remote configuration found",
}),
);
return;
}
settings.set(data.configuration);
settingsSyncPulled.set(true);
settingsSyncTimes.set({
lastPull: new Date(),
lastPush: new Date(`${data.updated_at}Z`),
});
addNotification(
options({ heading: "Pulled remote configuration" }),
);
});
})
.catch((error) =>
console.error("Settings sync pull failed", error),
);
},
},
];
if (syncEnabled)
actions.push({
name: "Disable Settings Sync",
url: "#",
preventDefault: true,
tags: ["settings", "sync", "disable", "off", "stop"],
onClick: () => {
settings.setKey("settingsSync", false);
addNotification(options({ heading: "Settings sync disabled" }));
},
});
return actions;
};
|