Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Moonbase things #192

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions packages/core-extensions/src/moonbase/webpackModules/stores.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Config, ExtensionEnvironment, ExtensionLoadSource, ExtensionSettingsAdvice } from "@moonlight-mod/types";
import { ExtensionState, MoonbaseExtension, MoonbaseNatives, RepositoryManifest, RestartAdvice } from "../types";
import {
ExtensionState,
MoonbaseExtension,
MoonbaseNatives,
RepositoryManifest,
RestartAdvice,
UpdateState
} from "../types";
import { Store } from "@moonlight-mod/wp/discord/packages/flux";
import Dispatcher from "@moonlight-mod/wp/discord/Dispatcher";
import getNatives from "../native";
Expand All @@ -25,6 +32,10 @@ class MoonbaseSettingsStore extends Store<any> {
submitting: boolean;
installing: boolean;

#updateState = UpdateState.Ready;
get updateState() {
return this.#updateState;
}
newVersion: string | null;
shouldShowNotice: boolean;

Expand Down Expand Up @@ -353,7 +364,18 @@ class MoonbaseSettingsStore extends Store<any> {
}

async updateMoonlight() {
await natives.updateMoonlight();
this.#updateState = UpdateState.Working;
this.emitChange();

await natives
.updateMoonlight()
.then(() => (this.#updateState = UpdateState.Installed))
.catch((e) => {
logger.error(e);
this.#updateState = UpdateState.Failed;
});

this.emitChange();
}

getConfigOption<K extends keyof Config>(key: K): Config[K] {
Expand Down Expand Up @@ -381,6 +403,9 @@ class MoonbaseSettingsStore extends Store<any> {
}

#computeRestartAdvice() {
// If moonlight update needs a restart, always hide advice.
if (this.#updateState === UpdateState.Installed) return RestartAdvice.NotNeeded;

const i = this.initialConfig; // Initial config, from startup
const n = this.config; // New config about to be saved

Expand Down Expand Up @@ -486,7 +511,7 @@ class MoonbaseSettingsStore extends Store<any> {
this.modified = false;
this.emitChange();

if (modifiedRepos) this.checkUpdates();
if (modifiedRepos.length !== 0) this.checkUpdates();
}

reset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function PanelLinkButton({ icon, tooltip, link }: { icon: React.ReactNode; toolt
);
}

export default function ExtensionCard({ uniqueId }: { uniqueId: number }) {
export default function ExtensionCard({ uniqueId, selectTag }: { uniqueId: number; selectTag: (tag: string) => void }) {
const { ext, enabled, busy, update, conflicting } = useStateFromStores([MoonbaseSettingsStore], () => {
return {
ext: MoonbaseSettingsStore.getExtension(uniqueId),
Expand Down Expand Up @@ -303,7 +303,7 @@ export default function ExtensionCard({ uniqueId }: { uniqueId: number }) {
rowGap: tab === ExtensionPage.Info ? "16px" : undefined
}}
>
{tab === ExtensionPage.Info && <ExtensionInfo ext={ext} />}
{tab === ExtensionPage.Info && <ExtensionInfo ext={ext} selectTag={selectTag} />}
{tab === ExtensionPage.Description && (
<Text variant="text-md/normal" className={MarkupClasses.markup} style={{ width: "100%" }}>
{MarkupUtils.parse(description ?? "*No description*", true, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import HelpMessage from "../HelpMessage";

const SearchBar = spacepack.require("discord/uikit/search/SearchBar").default;

const validTags: string[] = Object.values(ExtensionTag);

export default function ExtensionsPage() {
const { extensions, savedFilter } = useStateFromStoresObject([MoonbaseSettingsStore], () => {
return {
Expand All @@ -41,7 +43,17 @@ export default function ExtensionsPage() {
filter = filterState[0];
setFilter = filterState[1];
}

const [selectedTags, setSelectedTags] = React.useState(new Set<string>());
const selectTag = React.useCallback(
(tag: string) => {
const newState = new Set(selectedTags);
if (validTags.includes(tag)) newState.add(tag);
setSelectedTags(newState);
},
[selectedTags]
);

const sorted = Object.values(extensions).sort((a, b) => {
const aName = a.manifest.meta?.name ?? a.id;
const bName = b.manifest.meta?.name ?? b.id;
Expand Down Expand Up @@ -132,13 +144,13 @@ export default function ExtensionsPage() {
)}

{filteredWithUpdates.map((ext) => (
<ExtensionCard uniqueId={ext.uniqueId} key={ext.uniqueId} />
<ExtensionCard uniqueId={ext.uniqueId} key={ext.uniqueId} selectTag={selectTag} />
))}
{filteredWithUpdates.length > 0 && filteredWithoutUpdates.length > 0 && (
<FormDivider className="moonbase-update-divider" />
)}
{filteredWithoutUpdates.map((ext) => (
<ExtensionCard uniqueId={ext.uniqueId} key={ext.uniqueId} />
<ExtensionCard uniqueId={ext.uniqueId} key={ext.uniqueId} selectTag={selectTag} />
))}
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ function InfoSection({ title, children }: { title: string; children: React.React
function Badge({
color,
children,
style = {}
style = {},
onClick
}: {
color: string;
children: React.ReactNode;
style?: React.CSSProperties;
onClick?: () => void;
}) {
if (onClick) style.cursor ??= "pointer";
return (
<span
className="moonlight-card-badge"
Expand All @@ -69,13 +72,20 @@ function Badge({
...style
} as React.CSSProperties
}
onClick={onClick}
>
{children}
</span>
);
}

export default function ExtensionInfo({ ext }: { ext: MoonbaseExtension }) {
export default function ExtensionInfo({
ext,
selectTag
}: {
ext: MoonbaseExtension;
selectTag: (tag: string) => void;
}) {
const authors = ext.manifest?.meta?.authors;
const tags = ext.manifest?.meta?.tags;
const version = ext.manifest?.version;
Expand Down Expand Up @@ -148,7 +158,7 @@ export default function ExtensionInfo({ ext }: { ext: MoonbaseExtension }) {
}

return (
<Badge key={i} color={color} style={style}>
<Badge key={i} color={color} style={style} onClick={() => selectTag(tag)}>
{name}
</Badge>
);
Expand Down
17 changes: 5 additions & 12 deletions packages/core-extensions/src/moonbase/webpackModules/ui/update.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import {
} from "@moonlight-mod/wp/discord/components/common/index";
import MarkupClasses from "@moonlight-mod/wp/discord/modules/messages/web/Markup.css";

const logger = moonlight.getLogger("moonbase/ui/update");

const strings: Record<UpdateState, string> = {
[UpdateState.Ready]: "A new version of moonlight is available.",
[UpdateState.Working]: "Updating moonlight...",
Expand Down Expand Up @@ -67,8 +65,10 @@ function MoonlightChangelog({
}

export default function Update() {
const [state, setState] = React.useState(UpdateState.Ready);
const newVersion = useStateFromStores([MoonbaseSettingsStore], () => MoonbaseSettingsStore.newVersion);
const [newVersion, state] = useStateFromStores([MoonbaseSettingsStore], () => [
MoonbaseSettingsStore.newVersion,
MoonbaseSettingsStore.updateState
]);

if (newVersion == null) return null;

Expand Down Expand Up @@ -113,14 +113,7 @@ export default function Update() {
size={Button.Sizes.TINY}
disabled={state !== UpdateState.Ready}
onClick={() => {
setState(UpdateState.Working);

MoonbaseSettingsStore.updateMoonlight()
.then(() => setState(UpdateState.Installed))
.catch((e) => {
logger.error(e);
setState(UpdateState.Failed);
});
MoonbaseSettingsStore.updateMoonlight();
}}
>
Update
Expand Down
Loading