summaryrefslogtreecommitdiff
path: root/frontend/src/components/MotdDisplay.tsx
blob: 3c651b1159bf51aa6cc7675f9bea7aa3bd4b9f3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import {useEffect, useMemo, useState} from 'react'
import { getMotd, Motd } from '../store';
import { useSetting } from '../utils/hooks/useSetting';
import { DialogButton, Focusable, PanelSection } from '@decky/ui';
import { FaTimes } from 'react-icons/fa';

const SEVERITIES = {
  High: {
    color: "#bb1414",
    text: "#fff",
  },
  Medium: {
    color: "#bbbb14",
    text: "#fff",
  },
  Low: {
    color: "#1488bb",
    text: "#fff",
  },
};

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",
}

export function MotdDisplay() {
  const [motd, setMotd] = useState<Motd | 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")

  async function fetchMotd() {
    const motd = await getMotd();
    setMotd(motd);
  }

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

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

  function hideMotd() {
    if (motd) {
      setHiddenMotdId(motd.id);
      void fetchMotd();
    }
  }

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

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

  const severity = SEVERITIES[motd?.severity || "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" }}>{motd?.name}</span>
          <DialogButton
            style={{
              width: "1rem",
              minWidth: "1rem",
              height: "1rem",
              padding: "0",
              display: "flex",
              alignItems: "center",
              justifyContent: "center",
              position: "absolute",
              top: ".75rem",
              right: ".75rem",
            }}
            onClick={hideMotd}
          >
            <FaTimes
              style={{
                height: ".75rem",
              }}
            />
          </DialogButton>
        </div>
        <span style={{ fontSize: "0.75rem", whiteSpace: "pre-line" }}>{motd?.description}</span>
      </Focusable>
    </PanelSection>
  )

}