From f6144f9634482d6bd0ed88a31495c6c6c88add96 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=81lvaro=20Cuesta?=
 <1827495+alvaro-cuesta@users.noreply.github.com>
Date: Thu, 2 Jan 2025 20:38:40 +0100
Subject: feat: sync with local plugin status in store (#733)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* fix: useDeckyState proper type and safety

* refactor: plugin list

Avoids unneeded re-renders. See https://react.dev/learn/you-might-not-need-an-effect#caching-expensive-calculations

* feat: sync with local plugin status in store

Adds some QoL changes to the plugin store browser:

- Add ✓ icon to currently installed plugin version in version selector
- Change install button label depending on the install type that the
  button would trigger
- Adds icon to install button for clarity

The goal is to make it clear to the user what the current state of the
installed plugin is, and what would be the impact of installing the
selected version.

Resolves #360

* lint: prettier

* fix: add missing translations

* refactor: safer translation strings on install

Prefer using `t(...)` instead of `TranslationHelper` since it ensures
that the translation keys are not missing in the locale files when
running the `extractext` task.

By adding comments with `t(...)` calls, `i18next-parser` will generate
the strings as if they were present as literals in the code (see
https://github.com/i18next/i18next-parser#caveats).

This does _not_ suppress the warnings (since `i18next-parser` does not
have access to TS types, so it cannot infer template literals) but it at
least makes it less likely that a translation will be missed by mistake,
have typos, etc.
---
 frontend/src/components/PluginView.tsx | 44 ++++++++++++++++------------------
 1 file changed, 20 insertions(+), 24 deletions(-)

(limited to 'frontend/src/components/PluginView.tsx')

diff --git a/frontend/src/components/PluginView.tsx b/frontend/src/components/PluginView.tsx
index 19afbca5..1d39972e 100644
--- a/frontend/src/components/PluginView.tsx
+++ b/frontend/src/components/PluginView.tsx
@@ -1,27 +1,26 @@
 import { ButtonItem, ErrorBoundary, Focusable, PanelSection, PanelSectionRow } from '@decky/ui';
-import { FC, useEffect, useState } from 'react';
+import { FC, useMemo } from 'react';
 import { useTranslation } from 'react-i18next';
 import { FaEyeSlash } from 'react-icons/fa';
 
-import { Plugin } from '../plugin';
 import { useDeckyState } from './DeckyState';
 import NotificationBadge from './NotificationBadge';
 import { useQuickAccessVisible } from './QuickAccessVisibleState';
 import TitleView from './TitleView';
 
 const PluginView: FC = () => {
-  const { hiddenPlugins } = useDeckyState();
-  const { plugins, updates, activePlugin, pluginOrder, setActivePlugin, closeActivePlugin } = useDeckyState();
+  const { plugins, hiddenPlugins, updates, activePlugin, pluginOrder, setActivePlugin, closeActivePlugin } =
+    useDeckyState();
   const visible = useQuickAccessVisible();
   const { t } = useTranslation();
 
-  const [pluginList, setPluginList] = useState<Plugin[]>(
-    plugins.sort((a, b) => pluginOrder.indexOf(a.name) - pluginOrder.indexOf(b.name)),
-  );
-
-  useEffect(() => {
-    setPluginList(plugins.sort((a, b) => pluginOrder.indexOf(a.name) - pluginOrder.indexOf(b.name)));
+  const pluginList = useMemo(() => {
     console.log('updating PluginView after changes');
+
+    return [...plugins]
+      .sort((a, b) => pluginOrder.indexOf(a.name) - pluginOrder.indexOf(b.name))
+      .filter((p) => p.content)
+      .filter(({ name }) => !hiddenPlugins.includes(name));
   }, [plugins, pluginOrder]);
 
   if (activePlugin) {
@@ -43,20 +42,17 @@ const PluginView: FC = () => {
         }}
       >
         <PanelSection>
-          {pluginList
-            .filter((p) => p.content)
-            .filter(({ name }) => !hiddenPlugins.includes(name))
-            .map(({ name, icon }) => (
-              <PanelSectionRow key={name}>
-                <ButtonItem layout="below" onClick={() => setActivePlugin(name)}>
-                  <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
-                    {icon}
-                    <div>{name}</div>
-                    <NotificationBadge show={updates?.has(name)} style={{ top: '-5px', right: '-5px' }} />
-                  </div>
-                </ButtonItem>
-              </PanelSectionRow>
-            ))}
+          {pluginList.map(({ name, icon }) => (
+            <PanelSectionRow key={name}>
+              <ButtonItem layout="below" onClick={() => setActivePlugin(name)}>
+                <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
+                  {icon}
+                  <div>{name}</div>
+                  <NotificationBadge show={updates?.has(name)} style={{ top: '-5px', right: '-5px' }} />
+                </div>
+              </ButtonItem>
+            </PanelSectionRow>
+          ))}
           {hiddenPlugins.length > 0 && (
             <div style={{ display: 'flex', alignItems: 'center', gap: '10px', fontSize: '0.8rem', marginTop: '10px' }}>
               <FaEyeSlash />
-- 
cgit v1.2.3

