Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

fix(admin): refresh plugins sidebar when a plugin is installed or removed

Gabe Kangas1b75b4841107
diff --git a/web/components/admin/MainLayout.tsx b/web/components/admin/MainLayout.tsx
index 3dbd1318a8..7e1e006f51 100644
--- a/web/components/admin/MainLayout.tsx
+++ b/web/components/admin/MainLayout.tsx
@@ -8,8 +8,8 @@ import { Layout, Menu, Alert, Button, Space, Tooltip, Badge } from 'antd';
import { useTranslation } from 'next-export-i18n';
import classNames from 'classnames';
import dynamic from 'next/dynamic';
-import { fetchData, PLUGINS_LIST, upgradeVersionAvailable } from '../../utils/apis';
-import { Plugin } from '../../interfaces/plugin';
+import { upgradeVersionAvailable } from '../../utils/apis';
+import { PluginsContext } from '../../utils/plugins-context';
import { PluginIcon } from './plugins/PluginIcon';
import { Localization } from '../../types/localization';
import { parseSecondsToDurationString } from '../../utils/format';
@@ -125,24 +125,10 @@ export const MainLayout: FC<MainLayoutProps> = ({ children }) => {
}, [versionNumber]);
// Plugins with declared admin pages drive the sidebar's Plugins
- // submenu. Fetched once on mount; the admin would refresh the page to
- // see the submenu reflect newly enabled/disabled plugins.
- const [plugins, setPlugins] = useState<Plugin[]>([]);
- useEffect(() => {
- let cancelled = false;
- fetchData(PLUGINS_LIST)
- .then(result => {
- if (cancelled) return;
- setPlugins(Array.isArray(result) ? result : []);
- })
- .catch(() => {
- // Sidebar still works without the submenu — the top-level Plugins
- // link to the overview page remains. Silent on error.
- });
- return () => {
- cancelled = true;
- };
- }, []);
+ // submenu. Read from the shared PluginsContext so an install or
+ // uninstall on the Plugins page updates the submenu live, without the
+ // admin needing to refresh the whole page.
+ const { plugins } = useContext(PluginsContext);
useEffect(() => {
setCurrentStreamTitle(instanceDetails.streamTitle);
diff --git a/web/components/layouts/AdminLayout.tsx b/web/components/layouts/AdminLayout.tsx
index ea2e6dae60..f62c1d5e87 100644
--- a/web/components/layouts/AdminLayout.tsx
+++ b/web/components/layouts/AdminLayout.tsx
@@ -2,6 +2,7 @@
import { FC, ReactElement } from 'react';
import ServerStatusProvider from '../../utils/server-status-context';
import AlertMessageProvider from '../../utils/alert-message-context';
+import PluginsProvider from '../../utils/plugins-context';
import { MainLayout } from '../admin/MainLayout';
/*
@@ -30,7 +31,9 @@ export const AdminLayout: FC<AdminLayoutProps> = ({ page }) => (
<ServerStatusProvider>
<AlertMessageProvider>
- <MainLayout>{page}</MainLayout>
+ <PluginsProvider>
+ <MainLayout>{page}</MainLayout>
+ </PluginsProvider>
</AlertMessageProvider>
</ServerStatusProvider>
</>
diff --git a/web/pages/admin/plugins.tsx b/web/pages/admin/plugins.tsx
index 9bd50e8391..3c66e94332 100644
--- a/web/pages/admin/plugins.tsx
+++ b/web/pages/admin/plugins.tsx
@@ -1,4 +1,4 @@
-import React, { ReactElement, useCallback, useEffect, useMemo, useState } from 'react';
+import React, { ReactElement, useCallback, useContext, useEffect, useMemo, useState } from 'react';
import { Alert, Button, message, Space, Tabs, Typography, Upload } from 'antd';
import type { UploadProps } from 'antd';
import dynamic from 'next/dynamic';
@@ -11,10 +11,10 @@ import {
PLUGIN_REGISTRY_INSTALL,
PLUGIN_REGISTRY_LIST,
PLUGIN_UPLOAD,
- PLUGINS_LIST,
pluginActionUrl,
} from '../../utils/apis';
import { Plugin } from '../../interfaces/plugin';
+import { PluginsContext } from '../../utils/plugins-context';
import { PluginsList } from '../../components/admin/plugins/PluginsList';
import { BrowseRegistry, RegistryPlugin } from '../../components/admin/plugins/BrowseRegistry';
import { InstallConfirmModal } from '../../components/admin/plugins/InstallConfirmModal';
@@ -29,8 +29,17 @@ const UploadOutlined = dynamic(() => import('@ant-design/icons/UploadOutlined'),
const Plugins = () => {
const { t } = useTranslation();
const router = useRouter();
- const [plugins, setPlugins] = useState<Plugin[]>([]);
- const [loading, setLoading] = useState(true);
+ // Installed plugins come from the shared context so the sidebar and
+ // this page stay in sync: any install/uninstall here calls loadPlugins
+ // (the context reload) and the sidebar's submenu updates with it.
+ const {
+ plugins,
+ loading,
+ error: pluginsLoadError,
+ reload: loadPlugins,
+ } = useContext(PluginsContext);
+ // Local error for action failures (toggle/reload/uninstall/install).
+ // Load failures surface via pluginsLoadError from the context.
const [error, setError] = useState<string | null>(null);
// Names of plugins whose enable/disable POST is in flight. Drives the per-row
// spinner on the Switch so the admin sees the action is happening (the
@@ -59,18 +68,6 @@ const Plugins = () => {
// both the loading and the empty-but-reachable cases.
const [registryError, setRegistryError] = useState<string | null>(null);
- const loadPlugins = useCallback(async () => {
- try {
- const result = await fetchData(PLUGINS_LIST);
- setPlugins(Array.isArray(result) ? result : []);
- setError(null);
- } catch (e) {
- setError(e instanceof Error ? e.message : String(e));
- } finally {
- setLoading(false);
- }
- }, []);
-
// Registry fetch is independent of the installed-list fetch: a
// misconfigured or unreachable registry shouldn't block the
// Installed tab. On failure we set registryError so the Browse tab
@@ -90,10 +87,11 @@ const Plugins = () => {
}
}, []);
+ // The installed list loads via PluginsContext on mount; this page only
+ // needs to kick off the registry fetch.
useEffect(() => {
- loadPlugins();
loadRegistry();
- }, [loadPlugins, loadRegistry]);
+ }, [loadRegistry]);
// Map of installed plugin slug -> currently-installed version.
// Browse cards use this to decide whether to render "Install" /
@@ -323,13 +321,13 @@ const Plugins = () => {
<Title>{t(Localization.Admin.Plugins.pageTitle)}</Title>
<Paragraph>{t(Localization.Admin.Plugins.pageDescription)}</Paragraph>
- {error && (
+ {(error || pluginsLoadError) && (
<Alert
type="error"
showIcon
closable
message={t(Localization.Admin.Plugins.errorTitle)}
- description={error}
+ description={error || pluginsLoadError}
onClose={() => setError(null)}
className={s.errorAlert}
/>
diff --git a/web/utils/plugins-context.tsx b/web/utils/plugins-context.tsx
new file mode 100644
index 0000000000..0091139e9b
--- /dev/null
+++ b/web/utils/plugins-context.tsx
@@ -0,0 +1,68 @@
+import React, {
+ createContext,
+ FC,
+ ReactElement,
+ useCallback,
+ useEffect,
+ useMemo,
+ useState,
+} from 'react';
+import { fetchData, PLUGINS_LIST } from './apis';
+import { Plugin } from '../interfaces/plugin';
+
+// Shared installed-plugins state for the admin. The sidebar's Plugins
+// submenu and the Plugins page both read from this single source, so an
+// install/uninstall/enable from the page can call reload() and have the
+// sidebar pick up the change without a full-page refresh.
+export type PluginsContextValue = {
+ plugins: Plugin[];
+ // True until the first fetch settles. The Plugins page drives its
+ // table's loading state from this.
+ loading: boolean;
+ // Set when the most recent fetch failed; null when it succeeded.
+ error: string | null;
+ // Re-fetches the installed plugin list. Awaitable so callers can
+ // sequence UI (e.g. open a confirmation modal) after the refresh.
+ reload: () => Promise<void>;
+};
+
+export const PluginsContext = createContext<PluginsContextValue>({
+ plugins: [],
+ loading: true,
+ error: null,
+ reload: async () => {},
+});
+
+export type PluginsProviderProps = {
+ children: ReactElement;
+};
+
+const PluginsProvider: FC<PluginsProviderProps> = ({ children }) => {
+ const [plugins, setPlugins] = useState<Plugin[]>([]);
+ const [loading, setLoading] = useState(true);
+ const [error, setError] = useState<string | null>(null);
+
+ const reload = useCallback(async () => {
+ try {
+ const result = await fetchData(PLUGINS_LIST);
+ setPlugins(Array.isArray(result) ? result : []);
+ setError(null);
+ } catch (e) {
+ setError(e instanceof Error ? e.message : String(e));
+ } finally {
+ setLoading(false);
+ }
+ }, []);
+
+ useEffect(() => {
+ reload();
+ }, [reload]);
+
+ const value = useMemo(
+ () => ({ plugins, loading, error, reload }),
+ [plugins, loading, error, reload],
+ );
+ return <PluginsContext.Provider value={value}>{children}</PluginsContext.Provider>;
+};
+
+export default PluginsProvider;