"use client"; import { useEffect, useRef, useState } from "react"; import { setup, plugins, extensionPoints, activationPoints, } from "@/../../electron/core/plugin-manager/execution/index"; import { ChartPieIcon, CommandLineIcon, PlayIcon } from "@heroicons/react/24/outline"; import { MagnifyingGlassIcon } from "@heroicons/react/20/solid"; import classNames from "classnames"; import { PluginService, preferences } from "@janhq/plugin-core"; import { execute } from "../../../electron/core/plugin-manager/execution/extension-manager"; export const Preferences = () => { const [search, setSearch] = useState(""); const [activePlugins, setActivePlugins] = useState([]); const [preferenceItems, setPreferenceItems] = useState([]); const [preferenceValues, setPreferenceValues] = useState([]); const [isTestAvailable, setIsTestAvailable] = useState(false); const [fileName, setFileName] = useState(""); const experimentRef = useRef(null); const preferenceRef = useRef(null); const handleFileChange = (event: React.ChangeEvent) => { const file = event.target.files?.[0]; if (file) { setFileName(file.name); } else { setFileName(""); } }; useEffect(() => { async function setupPE() { // Enable activation point management setup({ //@ts-ignore importer: (plugin) => import(/* webpackIgnore: true */ plugin).catch((err) => { console.log(err); }), }); // Register all active plugins with their activation points await plugins.registerActive(); } const activePlugins = async () => { const plgs = await plugins.getActive(); setActivePlugins(plgs); // Activate alls setTimeout(async () => { await activationPoints.trigger("init"); if (extensionPoints.get("experimentComponent")) { const components = await Promise.all(extensionPoints.execute("experimentComponent")); if (components.length > 0) { setIsTestAvailable(true); } components.forEach((e) => { if (experimentRef.current) { // @ts-ignore experimentRef.current.appendChild(e); } }); } if (extensionPoints.get("PluginPreferences")) { const data = await Promise.all(extensionPoints.execute("PluginPreferences")); setPreferenceItems(Array.isArray(data) ? data : []); } }, 500); }; setupPE().then(() => activePlugins()); }, []); // Install a new plugin on clicking the install button const install = async (e: any) => { e.preventDefault(); //@ts-ignore const pluginFile = new FormData(e.target).get("plugin-file").path; // Send the filename of the to be installed plugin // to the main process for installation const installed = await plugins.install([pluginFile]); if (installed) window.electronAPI.relaunch(); }; // Uninstall a plugin on clicking uninstall const uninstall = async (name: string) => { // Send the filename of the to be uninstalled plugin // to the main process for removal const res = await plugins.uninstall([name]); if (res) window.electronAPI.relaunch(); }; // Update all plugins on clicking update plugins const update = async (plugin: string) => { if (typeof window !== "undefined") { // @ts-ignore await window.pluggableElectronIpc.update([plugin], true); window.electronAPI.reloadPlugins(); } // plugins.update(active.map((plg) => plg.name)); }; let timeout: any | undefined = undefined; function notifyPreferenceUpdate() { if (timeout) { clearTimeout(timeout); } timeout = setTimeout(() => execute(PluginService.OnPreferencesUpdate), 100); } useEffect(() => { if (preferenceItems) { Promise.all( preferenceItems.map((e) => preferences.get(e.pluginName, e.preferenceKey).then((k) => ({ key: e.preferenceKey, value: k })) ) ).then((data) => { setPreferenceValues(data); }); } }, [preferenceItems]); return (
{/* Separator */}
{/* Content */}
Install Plugin
Installed Plugins
{activePlugins .filter((e) => search.trim() === "" || e.name.toLowerCase().includes(search.toLowerCase())) .map((e) => (

{e.name.replaceAll("-", " ")}

Version: {e.version}

{e.description ?? "Jan's Plugin"}

))}
{activePlugins.length > 0 && isTestAvailable && (
Test Plugins
)}
Preferences
{preferenceItems?.map((e) => (
Setting:{" "} {e.preferenceName}
{e.preferenceDescription}
v.key === e.preferenceKey)[0]?.value} onChange={(event) => { preferences .set(e.pluginName, e.preferenceKey, event.target.value) .then(() => notifyPreferenceUpdate()); }} >
))}
); };