f2510dfcb5
Co-authored-by: Kandev Agent <agent@kandev.dev>
41 lines
1.4 KiB
TypeScript
41 lines
1.4 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect } from "react";
|
|
import { listPlugins } from "@/lib/api/domains/plugins-api";
|
|
import { t } from "@/lib/i18n";
|
|
import { useAppStore } from "@/components/state-provider";
|
|
|
|
/**
|
|
* Loads the plugin registry into the store on mount. Mirrors
|
|
* hooks/domains/settings/use-secrets.ts: the slice only holds
|
|
* setPlugins/setPluginsLoading/setPluginsError — the fetch + dispatch lives
|
|
* here, per the "Never fetch data directly in components" data-flow rule.
|
|
*/
|
|
export function usePlugins() {
|
|
const items = useAppStore((state) => state.plugins.items);
|
|
const loaded = useAppStore((state) => state.plugins.loaded);
|
|
const loading = useAppStore((state) => state.plugins.loading);
|
|
const error = useAppStore((state) => state.plugins.error);
|
|
const setPlugins = useAppStore((state) => state.setPlugins);
|
|
const setPluginsLoading = useAppStore((state) => state.setPluginsLoading);
|
|
const setPluginsError = useAppStore((state) => state.setPluginsError);
|
|
|
|
useEffect(() => {
|
|
if (loading) return;
|
|
setPluginsLoading(true);
|
|
listPlugins({ cache: "no-store" })
|
|
.then((response) => {
|
|
setPlugins(response);
|
|
})
|
|
.catch((err) => {
|
|
setPluginsError(err instanceof Error ? err.message : t("plugins:failedToLoadPlugins"));
|
|
})
|
|
.finally(() => {
|
|
setPluginsLoading(false);
|
|
});
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
|
|
return { items, loaded, loading, error };
|
|
}
|