rename motd to announcements and implement new API

This commit is contained in:
Beebles
2024-08-31 16:41:58 -06:00
parent edf6b54db4
commit 1a231bf03e
3 changed files with 48 additions and 46 deletions
@@ -2,7 +2,7 @@ import { DialogButton, Focusable, PanelSection } from '@decky/ui';
import { useEffect, useMemo, useState } from 'react'; import { useEffect, useMemo, useState } from 'react';
import { FaTimes } from 'react-icons/fa'; import { FaTimes } from 'react-icons/fa';
import { Motd, getMotd } from '../store'; import { Announcement, getLatestAnnouncement } from '../store';
import { useSetting } from '../utils/hooks/useSetting'; import { useSetting } from '../utils/hooks/useSetting';
const SEVERITIES = { const SEVERITIES = {
@@ -20,50 +20,51 @@ const SEVERITIES = {
}, },
}; };
const welcomeMotd: Motd = { const welcomeAnnouncement: Announcement = {
id: 'welcomeMotd', id: 'welcomeAnnouncement',
name: 'Welcome to Decky!', title: 'Welcome to Decky!',
date: Date.now().toString(), text: 'We hope you enjoy using Decky! If you have any questions or feedback, please let us know.',
description: 'We hope you enjoy using Decky! If you have any questions or feedback, please let us know.', created: Date.now().toString(),
severity: 'Low', updated: Date.now().toString(),
}; };
export function MotdDisplay() { export function AnnouncementsDisplay() {
const [motd, setMotd] = useState<Motd | null>(null); const [announcement, setAnnouncement] = useState<Announcement | null>(null);
// 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 // 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 [hiddenMotdId, setHiddenMotdId] = useSetting('hiddenMotdId', 'showWelcome'); const [hiddenAnnouncementId, setHiddenAnnouncementId] = useSetting('hiddenAnnouncementId', 'showWelcome');
async function fetchMotd() { async function fetchAnnouncement() {
const motd = await getMotd(); const announcement = await getLatestAnnouncement();
motd && setMotd(motd); announcement && setAnnouncement(announcement);
} }
useEffect(() => { useEffect(() => {
void fetchMotd(); void fetchAnnouncement();
}, []); }, []);
useEffect(() => { useEffect(() => {
if (hiddenMotdId === 'showWelcome') { if (hiddenAnnouncementId === 'showWelcome') {
setMotd(welcomeMotd); setAnnouncement(welcomeAnnouncement);
} }
}, [hiddenMotdId]); }, [hiddenAnnouncementId]);
function hideMotd() { function hideAnnouncement() {
if (motd) { if (announcement) {
setHiddenMotdId(motd.id); setHiddenAnnouncementId(announcement.id);
void fetchMotd(); void fetchAnnouncement();
} }
} }
const hidden = useMemo(() => { const hidden = useMemo(() => {
return hiddenMotdId === motd?.id; return hiddenAnnouncementId === announcement?.id;
}, [hiddenMotdId, motd]); }, [hiddenAnnouncementId, announcement]);
if (!motd || !motd?.name || hidden) { if (!announcement || !announcement.title || hidden) {
return null; return null;
} }
const severity = SEVERITIES[motd?.severity || 'Low']; // Severity is not implemented in the API currently
const severity = SEVERITIES['Low'];
return ( return (
<PanelSection> <PanelSection>
@@ -82,7 +83,7 @@ export function MotdDisplay() {
}} }}
> >
<div style={{ display: 'flex', justifyContent: 'space-between' }}> <div style={{ display: 'flex', justifyContent: 'space-between' }}>
<span style={{ fontWeight: 'bold' }}>{motd?.name}</span> <span style={{ fontWeight: 'bold' }}>{announcement.title}</span>
<DialogButton <DialogButton
style={{ style={{
width: '1rem', width: '1rem',
@@ -96,7 +97,7 @@ export function MotdDisplay() {
top: '.75rem', top: '.75rem',
right: '.75rem', right: '.75rem',
}} }}
onClick={hideMotd} onClick={hideAnnouncement}
> >
<FaTimes <FaTimes
style={{ style={{
@@ -105,7 +106,7 @@ export function MotdDisplay() {
/> />
</DialogButton> </DialogButton>
</div> </div>
<span style={{ fontSize: '0.75rem', whiteSpace: 'pre-line' }}>{motd?.description}</span> <span style={{ fontSize: '0.75rem', whiteSpace: 'pre-line' }}>{announcement.text}</span>
</Focusable> </Focusable>
</PanelSection> </PanelSection>
); );
+2 -2
View File
@@ -3,8 +3,8 @@ import { FC, useMemo } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { FaEyeSlash } from 'react-icons/fa'; import { FaEyeSlash } from 'react-icons/fa';
import { AnnouncementsDisplay } from './AnnouncementsDisplay';
import { useDeckyState } from './DeckyState'; import { useDeckyState } from './DeckyState';
import { MotdDisplay } from './MotdDisplay';
import NotificationBadge from './NotificationBadge'; import NotificationBadge from './NotificationBadge';
import { useQuickAccessVisible } from './QuickAccessVisibleState'; import { useQuickAccessVisible } from './QuickAccessVisibleState';
import TitleView from './TitleView'; import TitleView from './TitleView';
@@ -42,7 +42,7 @@ const PluginView: FC = () => {
paddingTop: '16px', paddingTop: '16px',
}} }}
> >
<MotdDisplay /> <AnnouncementsDisplay />
<PanelSection> <PanelSection>
{pluginList.map(({ name, icon }) => ( {pluginList.map(({ name, icon }) => (
<PanelSectionRow key={name}> <PanelSectionRow key={name}>
+17 -16
View File
@@ -42,12 +42,12 @@ export interface PluginInstallRequest {
installType: InstallType; installType: InstallType;
} }
export interface Motd { export interface Announcement {
id: string; id: string;
name: string; title: string;
description: string; text: string;
date: string; created: string;
severity: 'High' | 'Medium' | 'Low'; updated: string;
} }
// name: version // name: version
@@ -57,10 +57,13 @@ export async function getStore(): Promise<Store> {
return await getSetting<Store>('store', Store.Default); return await getSetting<Store>('store', Store.Default);
} }
export async function getMotd(): Promise<Motd> { export async function getLatestAnnouncement(): Promise<Announcement | null> {
let version = await window.DeckyPluginLoader.updateVersion(); let version = await window.DeckyPluginLoader.updateVersion();
let store = await getSetting<Store | null>('store', null); let store = await getSetting<Store | null>('store', null);
let customURL = await getSetting<string>('motd-url', 'https://plugins.deckbrew.xyz/v1/motd'); let customURL = await getSetting<string>(
'announcements-url',
'https://plugins.deckbrew.xyz/v1/announcements/-/current',
);
if (store === null) { if (store === null) {
console.log('Could not get store, using Default.'); console.log('Could not get store, using Default.');
@@ -71,30 +74,28 @@ export async function getMotd(): Promise<Motd> {
let resolvedURL; let resolvedURL;
switch (store) { switch (store) {
case Store.Default: case Store.Default:
resolvedURL = 'https://plugins.deckbrew.xyz/v1/motd'; resolvedURL = 'https://plugins.deckbrew.xyz/v1/announcements/-/current';
break; break;
case Store.Testing: case Store.Testing:
resolvedURL = 'https://testing.deckbrew.xyz/v1/motd'; resolvedURL = 'https://testing.deckbrew.xyz/v1/announcements/-/current';
break; break;
case Store.Custom: case Store.Custom:
resolvedURL = customURL; resolvedURL = customURL;
break; break;
default: default:
console.error('Somehow you ended up without a standard URL, using the default URL.'); console.error('Somehow you ended up without a standard URL, using the default URL.');
resolvedURL = 'https://plugins.deckbrew.xyz/v1/motd'; resolvedURL = 'https://plugins.deckbrew.xyz/v1/announcements/-/current';
break; break;
} }
return fetch(resolvedURL, { const res = await fetch(resolvedURL, {
method: 'GET', method: 'GET',
headers: { headers: {
'X-Decky-Version': version.current, 'X-Decky-Version': version.current,
}, },
}).then((r) => {
if (r.status === 200) {
return r.json();
}
return null;
}); });
if (res.status !== 200) return null;
const json = await res.json();
return json?.[0] ?? null;
} }
export async function getPluginList( export async function getPluginList(