87 lines
2.8 kB
1
import spacepack from "@moonlight-mod/wp/spacepack_spacepack";
2
import { MoonbaseSettingsStore } from "@moonlight-mod/wp/moonbase_stores";
3
import Notices from "@moonlight-mod/wp/notices_notices";
4
import { MoonlightBranch } from "@moonlight-mod/types";
5
import React from "@moonlight-mod/wp/react";
6
import * as Components from "@moonlight-mod/wp/discord/components/common/index";
7
8
// FIXME: not indexed as importable
9
const Constants = spacepack.require("discord/Constants");
10
const UserSettingsSections = spacepack.findObjectFromKey(Constants, "DEVELOPER_OPTIONS");
11
12
const { ThemeDarkIcon } = Components;
13
14
function plural(str: string, num: number) {
15
return `${str}${num > 1 ? "s" : ""}`;
16
}
17
18
function listener() {
19
if (
20
MoonbaseSettingsStore.shouldShowNotice &&
21
MoonbaseSettingsStore.getExtensionConfigRaw("moonbase", "updateBanner", true)
22
) {
23
MoonbaseSettingsStore.removeChangeListener(listener);
24
25
const version = MoonbaseSettingsStore.newVersion;
26
const extensionUpdateCount = Object.keys(MoonbaseSettingsStore.updates).length;
27
const hasExtensionUpdates = extensionUpdateCount > 0;
28
29
let message;
30
31
if (version != null) {
32
message =
33
moonlightNode.branch === MoonlightBranch.NIGHTLY
34
? `A new version of moonlight is available`
35
: `moonlight ${version} is available`;
36
}
37
38
if (hasExtensionUpdates) {
39
let concat = false;
40
if (message == null) {
41
message = "";
42
} else {
43
concat = true;
44
message += ", and ";
45
}
46
message += `${extensionUpdateCount} ${concat ? "" : "moonlight "}${plural(
47
"extension",
48
extensionUpdateCount
49
)} can be updated`;
50
}
51
52
if (message != null) message += ".";
53
54
Notices.addNotice({
55
element: (
56
<div className="moonbase-updates-notice_text-wrapper">
57
<ThemeDarkIcon size="sm" color="currentColor" />
58
{message}
59
</div>
60
),
61
color: "moonbase-updates-notice",
62
buttons: [
63
{
64
name: "Open Moonbase",
65
onClick: () => {
66
const { open } = spacepack.findByExports("setSection", "clearSubsection")[0].exports.Z;
67
68
// settings is lazy loaded thus lazily patched
69
// FIXME: figure out a way to detect if settings has been opened
70
// alreadyjust so the transition isnt as jarring
71
open(UserSettingsSections.ACCOUNT);
72
setTimeout(() => {
73
if (MoonbaseSettingsStore.getExtensionConfigRaw<boolean>("moonbase", "sections", false)) {
74
open("moonbase-extensions");
75
} else {
76
open("moonbase", 0);
77
}
78
}, 0);
79
return true;
80
}
81
}
82
]
83
});
84
}
85
}
86
87
MoonbaseSettingsStore.addChangeListener(listener);
88