import { useMutation, useQuery } from "@tanstack/react-query"; import type { Plugin } from "@yaakapp-internal/models"; import { patchModel, pluginsAtom } from "@yaakapp-internal/models"; import type { PluginVersion } from "@yaakapp-internal/plugins"; import { checkPluginUpdates, installPlugin, searchPlugins } from "@yaakapp-internal/plugins"; import { Banner, HStack, Icon, InlineCode, LoadingIcon, Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow, useDebouncedValue, CountBadge, } from "@yaakapp-internal/ui"; import classNames from "classnames"; import { useAtomValue } from "jotai"; import { type ReactNode, useState } from "react"; import { useInstallPlugin } from "../../hooks/useInstallPlugin"; import { usePluginInfo, usePluginInfos } from "../../hooks/usePluginInfo"; import { usePluginsKey, useRefreshPlugins } from "../../hooks/usePlugins"; import { usePromptUninstallPlugin } from "../../hooks/useUninstallPlugin"; import { Button } from "../core/Button"; import { Checkbox } from "../core/Checkbox"; import { IconButton } from "../core/IconButton"; import { Link } from "../core/Link"; import { PlainInput } from "../core/PlainInput"; import { TabContent, Tabs } from "../core/Tabs/Tabs"; import { EmptyStateText } from "../EmptyStateText"; import { SelectFile } from "../SelectFile"; import { platform } from "@yaakapp-internal/platform"; interface SettingsPluginsProps { defaultSubtab?: string; } export function SettingsPlugins({ defaultSubtab }: SettingsPluginsProps) { const [directory, setDirectory] = useState(null); const plugins = useAtomValue(pluginsAtom); const bundledPlugins = plugins.filter((p) => p.source === "bundled"); const installedPlugins = plugins.filter((p) => p.source !== "bundled"); const createPlugin = useInstallPlugin(); const refreshPlugins = useRefreshPlugins(); return (
, }, { label: "Bundled", value: "bundled", rightSlot: , }, ]} >
setDirectory(filePath)} filePath={directory} /> {directory && ( )} refreshPlugins.mutate()} /> platform.openUrl("https://yaak.app/docs/plugin-development/plugins-quick-start") } />
); } function PluginTableRowForInstalledPlugin({ plugin }: { plugin: Plugin }) { const info = usePluginInfo(plugin.id).data; if (info == null) { return null; } return ( ); } function PluginTableRowForBundledPlugin({ plugin }: { plugin: Plugin }) { const info = usePluginInfo(plugin.id).data; if (info == null) { return null; } return ( ); } function PluginTableRowForRemotePluginVersion({ pluginVersion }: { pluginVersion: PluginVersion }) { const plugin = useAtomValue(pluginsAtom).find((p) => p.id === pluginVersion.id); const pluginInfo = usePluginInfo(plugin?.id ?? null).data; return ( ); } function PluginTableRow({ plugin, name, version, displayName, url, showCheckbox = true, showUninstall = true, }: { plugin: Plugin | null; name: string; version: string; displayName: string; url: string | null; showCheckbox?: boolean; showUninstall?: boolean; }) { const updates = usePluginUpdates(); const latestVersion = updates.data?.plugins.find((u) => u.name === name)?.version; const installPluginMutation = useMutation({ mutationKey: ["install_plugin", name], mutationFn: (name: string) => installPlugin(name, null), }); const uninstall = usePromptUninstallPlugin(plugin?.id ?? null, displayName); const refreshPlugins = useRefreshPlugins(); return ( {showCheckbox && ( { if (plugin) { await patchModel(plugin, { enabled }); refreshPlugins.mutate(); } }} /> )} {url ? ( {displayName} ) : ( displayName )} {name} {version} {latestVersion != null && ( {latestVersion} )} {plugin != null && latestVersion != null ? ( ) : plugin == null ? ( ) : null} {showUninstall && uninstall != null && ( )} ); } function PluginSearch() { const [query, setQuery] = useState(""); const debouncedQuery = useDebouncedValue(query); const results = useQuery({ queryKey: ["plugins", debouncedQuery], queryFn: async () => { const { plugins } = await searchPlugins(query); return { plugins: plugins.filter((p) => p.deprecated == null) }; }, }); return (
{results.data == null ? ( ) : (results.data.plugins ?? []).length === 0 ? ( No plugins found ) : ( Display Name Name Version {results.data.plugins.map((p) => ( ))}
)}
); } function InstalledPlugins({ plugins, className }: { plugins: Plugin[]; className?: string }) { return plugins.length === 0 ? (
Plugins extend the functionality of Yaak.
Add your first plugin to get started.
) : (
Display Name Name Version {plugins.map((p) => ( ))}
); } function DeprecatedPluginBanners() { const deprecated = usePluginInfos().filter( ({ plugin, info }) => plugin.source !== "bundled" && info.deprecated != null, ); return (
0 && "pb-3")}> {deprecated.map(({ plugin, info }) => ( {withInlineCode(info.deprecated ?? "")} ))}
); } /** Backtick spans in a package.json string, set as code. */ function withInlineCode(text: string): ReactNode { return text .split("`") .map((part, i) => (i % 2 === 1 ? {part} : part)); } export function DeprecatedPluginBanner({ children, name, plugin, }: { children: ReactNode; name: string; plugin: Plugin; }) { const uninstall = usePromptUninstallPlugin(plugin.id, name); return (
The {name} plugin is deprecated.

{children}

); } function BundledPlugins({ plugins }: { plugins: Plugin[] }) { return plugins.length === 0 ? (
No bundled plugins found.
) : ( Display Name Name Version {plugins.map((p) => ( ))}
); } function usePluginUpdates() { return useQuery({ queryKey: ["plugin_updates", usePluginsKey()], queryFn: () => checkPluginUpdates(), }); }