Skip to content

Commit

Permalink
rename motd to announcements and implement new API
Browse files Browse the repository at this point in the history
  • Loading branch information
beebls committed Sep 13, 2024
1 parent 3193f71 commit 985dc66
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DialogButton, Focusable, PanelSection } from '@decky/ui';
import { useEffect, useMemo, useState } from 'react';
import { FaTimes } from 'react-icons/fa';

import { Motd, getMotd } from '../store';
import { Announcement, getLatestAnnouncement } from '../store';
import { useSetting } from '../utils/hooks/useSetting';

const SEVERITIES = {
Expand All @@ -20,50 +20,51 @@ const SEVERITIES = {
},
};

const welcomeMotd: Motd = {
id: 'welcomeMotd',
name: 'Welcome to Decky!',
date: Date.now().toString(),
description: 'We hope you enjoy using Decky! If you have any questions or feedback, please let us know.',
severity: 'Low',
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 MotdDisplay() {
const [motd, setMotd] = useState<Motd | null>(null);
export function AnnouncementsDisplay() {
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
const [hiddenMotdId, setHiddenMotdId] = useSetting('hiddenMotdId', 'showWelcome');
const [hiddenAnnouncementId, setHiddenAnnouncementId] = useSetting('hiddenAnnouncementId', 'showWelcome');

async function fetchMotd() {
const motd = await getMotd();
motd && setMotd(motd);
async function fetchAnnouncement() {
const announcement = await getLatestAnnouncement();
announcement && setAnnouncement(announcement);
}

useEffect(() => {
void fetchMotd();
void fetchAnnouncement();
}, []);

useEffect(() => {
if (hiddenMotdId === 'showWelcome') {
setMotd(welcomeMotd);
if (hiddenAnnouncementId === 'showWelcome') {
setAnnouncement(welcomeAnnouncement);
}
}, [hiddenMotdId]);
}, [hiddenAnnouncementId]);

function hideMotd() {
if (motd) {
setHiddenMotdId(motd.id);
void fetchMotd();
function hideAnnouncement() {
if (announcement) {
setHiddenAnnouncementId(announcement.id);
void fetchAnnouncement();
}
}

const hidden = useMemo(() => {
return hiddenMotdId === motd?.id;
}, [hiddenMotdId, motd]);
return hiddenAnnouncementId === announcement?.id;
}, [hiddenAnnouncementId, announcement]);

if (!motd || !motd?.name || hidden) {
if (!announcement || !announcement.title || hidden) {
return null;
}

const severity = SEVERITIES[motd?.severity || 'Low'];
// Severity is not implemented in the API currently
const severity = SEVERITIES['Low'];

return (
<PanelSection>
Expand All @@ -82,7 +83,7 @@ export function MotdDisplay() {
}}
>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<span style={{ fontWeight: 'bold' }}>{motd?.name}</span>
<span style={{ fontWeight: 'bold' }}>{announcement.title}</span>
<DialogButton
style={{
width: '1rem',
Expand All @@ -96,7 +97,7 @@ export function MotdDisplay() {
top: '.75rem',
right: '.75rem',
}}
onClick={hideMotd}
onClick={hideAnnouncement}
>
<FaTimes
style={{
Expand All @@ -105,7 +106,7 @@ export function MotdDisplay() {
/>
</DialogButton>
</div>
<span style={{ fontSize: '0.75rem', whiteSpace: 'pre-line' }}>{motd?.description}</span>
<span style={{ fontSize: '0.75rem', whiteSpace: 'pre-line' }}>{announcement.text}</span>
</Focusable>
</PanelSection>
);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/PluginView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useTranslation } from 'react-i18next';
import { FaEyeSlash } from 'react-icons/fa';

import { Plugin } from '../plugin';
import { AnnouncementsDisplay } from './AnnouncementsDisplay';
import { useDeckyState } from './DeckyState';
import { MotdDisplay } from './MotdDisplay';
import NotificationBadge from './NotificationBadge';
import { useQuickAccessVisible } from './QuickAccessVisibleState';
import TitleView from './TitleView';
Expand Down Expand Up @@ -43,7 +43,7 @@ const PluginView: FC = () => {
paddingTop: '16px',
}}
>
<MotdDisplay />
<AnnouncementsDisplay />
<PanelSection>
{pluginList
.filter((p) => p.content)
Expand Down
33 changes: 17 additions & 16 deletions frontend/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export interface PluginInstallRequest {
installType: InstallType;
}

export interface Motd {
export interface Announcement {
id: string;
name: string;
description: string;
date: string;
severity: 'High' | 'Medium' | 'Low';
title: string;
text: string;
created: string;
updated: string;
}

// name: version
Expand All @@ -55,10 +55,13 @@ export async function getStore(): Promise<Store> {
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 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) {
console.log('Could not get store, using Default.');
Expand All @@ -69,30 +72,28 @@ export async function getMotd(): Promise<Motd> {
let resolvedURL;
switch (store) {
case Store.Default:
resolvedURL = 'https://plugins.deckbrew.xyz/v1/motd';
resolvedURL = 'https://plugins.deckbrew.xyz/v1/announcements/-/current';
break;
case Store.Testing:
resolvedURL = 'https://testing.deckbrew.xyz/v1/motd';
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/motd';
resolvedURL = 'https://plugins.deckbrew.xyz/v1/announcements/-/current';
break;
}
return fetch(resolvedURL, {
const res = await fetch(resolvedURL, {
method: 'GET',
headers: {
'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(
Expand Down

0 comments on commit 985dc66

Please sign in to comment.