Compare commits

...

10 Commits

Author SHA1 Message Date
Beebles 2384f6c40e ensure nulls arent passed to sort
Builder Win / Build PluginLoader for Win (push) Has been cancelled
Builder / Build PluginLoader (push) Has been cancelled
Push Updated Plugin Stub to Template / copy-stub (push) Has been cancelled
Lint / Run linters (push) Has been cancelled
Type Check / Run type checkers (push) Has been cancelled
2024-09-13 18:46:49 -06:00
Beebles ab3b69ffed move welcome announcement to default 2024-09-13 18:34:09 -06:00
Beebles 91e4d6ca6d remove duplicates when adding announcements to array 2024-09-13 18:32:22 -06:00
Beebles 5b9613c8df change announcements to be stack 2024-09-13 18:12:56 -06:00
Beebles af9c2ecee9 change to use array of hidden announcements 2024-09-13 17:22:14 -06:00
Beebles 985dc669a1 rename motd to announcements and implement new API 2024-09-13 17:22:14 -06:00
Beebles 3193f71c20 move motd into div with padding 2024-09-13 17:22:14 -06:00
Beebles 29cfe6c66e only set motd if value returned 2024-09-13 17:22:14 -06:00
Beebles 6f8c414859 fix(motd): run prettier 2024-09-13 17:22:14 -06:00
Beebles 1f9359988b feat(motd): add motd component (untested) 2024-09-13 17:22:14 -06:00
3 changed files with 174 additions and 0 deletions
@@ -0,0 +1,123 @@
import { DialogButton, Focusable, PanelSection } from '@decky/ui';
import { useEffect, useMemo, useState } from 'react';
import { FaTimes } from 'react-icons/fa';
import { Announcement, getAnnouncements } from '../store';
import { useSetting } from '../utils/hooks/useSetting';
const SEVERITIES = {
High: {
color: '#bb1414',
text: '#fff',
},
Medium: {
color: '#bbbb14',
text: '#fff',
},
Low: {
color: '#1488bb',
text: '#fff',
},
};
const welcomeAnnouncement: Announcement = {
id: 'welcomeAnnouncement',
title: 'Welcome to Decky!',
text: 'We hope you enjoy using Decky! If you have any questions or feedback, please let us know.',
created: Date.now().toString(),
updated: Date.now().toString(),
};
export function AnnouncementsDisplay() {
const [announcements, setAnnouncements] = useState<Announcement[]>([welcomeAnnouncement]);
// showWelcome will display a welcome motd, the welcome motd has an id of "welcome" and once that is saved to hiddenMotdId, it will not show again
const [hiddenAnnouncementIds, setHiddenAnnouncementIds] = useSetting<string[]>('hiddenAnnouncementIds', []);
function addAnnouncements(newAnnouncements: Announcement[]) {
// Removes any duplicates and sorts by created date
setAnnouncements((oldAnnouncements) => {
const newArr = [...oldAnnouncements, ...newAnnouncements];
const setOfIds = new Set(newArr.map((a) => a.id));
return (
(
Array.from(setOfIds)
.map((id) => newArr.find((a) => a.id === id))
// Typescript doesn't type filter(Boolean) correctly, so I have to assert this
.filter(Boolean) as Announcement[]
).sort((a, b) => {
return new Date(b.created).getTime() - new Date(a.created).getTime();
})
);
});
}
async function fetchAnnouncement() {
const announcements = await getAnnouncements();
announcements && addAnnouncements(announcements);
}
useEffect(() => {
void fetchAnnouncement();
}, []);
const currentlyDisplayingAnnouncement: Announcement | null = useMemo(() => {
return announcements.find((announcement) => !hiddenAnnouncementIds.includes(announcement.id)) || null;
}, [announcements, hiddenAnnouncementIds]);
function hideAnnouncement(id: string) {
setHiddenAnnouncementIds([...hiddenAnnouncementIds, id]);
void fetchAnnouncement();
}
if (!currentlyDisplayingAnnouncement) {
return null;
}
// Severity is not implemented in the API currently
const severity = SEVERITIES['Low'];
return (
<PanelSection>
<Focusable
style={{
// Transparency is 20% of the color
backgroundColor: `${severity.color}33`,
color: severity.text,
borderColor: severity.color,
borderWidth: '2px',
borderStyle: 'solid',
padding: '0.7rem',
display: 'flex',
flexDirection: 'column',
position: 'relative',
}}
>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<span style={{ fontWeight: 'bold' }}>{currentlyDisplayingAnnouncement.title}</span>
<DialogButton
style={{
width: '1rem',
minWidth: '1rem',
height: '1rem',
padding: '0',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
position: 'absolute',
top: '.75rem',
right: '.75rem',
}}
onClick={() => hideAnnouncement(currentlyDisplayingAnnouncement.id)}
>
<FaTimes
style={{
height: '.75rem',
}}
/>
</DialogButton>
</div>
<span style={{ fontSize: '0.75rem', whiteSpace: 'pre-line' }}>{currentlyDisplayingAnnouncement.text}</span>
</Focusable>
</PanelSection>
);
}
+2
View File
@@ -4,6 +4,7 @@ import { useTranslation } from 'react-i18next';
import { FaEyeSlash } from 'react-icons/fa';
import { Plugin } from '../plugin';
import { AnnouncementsDisplay } from './AnnouncementsDisplay';
import { useDeckyState } from './DeckyState';
import NotificationBadge from './NotificationBadge';
import { useQuickAccessVisible } from './QuickAccessVisibleState';
@@ -42,6 +43,7 @@ const PluginView: FC = () => {
paddingTop: '16px',
}}
>
<AnnouncementsDisplay />
<PanelSection>
{pluginList
.filter((p) => p.content)
@@ -40,6 +40,14 @@ export interface PluginInstallRequest {
installType: InstallType;
}
export interface Announcement {
id: string;
title: string;
text: string;
created: string;
updated: string;
}
// name: version
export type PluginUpdateMapping = Map<string, StorePluginVersion>;
@@ -47,6 +55,47 @@ export async function getStore(): Promise<Store> {
return await getSetting<Store>('store', Store.Default);
}
export async function getAnnouncements(): Promise<Announcement[]> {
let version = await window.DeckyPluginLoader.updateVersion();
let store = await getSetting<Store | null>('store', null);
let customURL = await getSetting<string>(
'announcements-url',
'https://plugins.deckbrew.xyz/v1/announcements/-/current',
);
if (store === null) {
console.log('Could not get store, using Default.');
await setSetting('store', Store.Default);
store = Store.Default;
}
let resolvedURL;
switch (store) {
case Store.Default:
resolvedURL = 'https://plugins.deckbrew.xyz/v1/announcements/-/current';
break;
case Store.Testing:
resolvedURL = 'https://testing.deckbrew.xyz/v1/announcements/-/current';
break;
case Store.Custom:
resolvedURL = customURL;
break;
default:
console.error('Somehow you ended up without a standard URL, using the default URL.');
resolvedURL = 'https://plugins.deckbrew.xyz/v1/announcements/-/current';
break;
}
const res = await fetch(resolvedURL, {
method: 'GET',
headers: {
'X-Decky-Version': version.current,
},
});
if (res.status !== 200) return [];
const json = await res.json();
return json ?? [];
}
export async function getPluginList(
sort_by: SortOptions | null = null,
sort_direction: SortDirections | null = null,