Refactor plugin manager and gRPC server (#96)

This commit is contained in:
Gregory Schier
2024-09-19 05:58:12 -07:00
committed by GitHub
parent 844d795014
commit a3b64423fd
27 changed files with 661 additions and 614 deletions

View File

@@ -1,11 +1,11 @@
import type { Plugin } from '@yaakapp/api';
import { open } from '@tauri-apps/plugin-shell';
import React from 'react';
import { useCreatePlugin } from '../../hooks/useCreatePlugin';
import { useInstallPlugin } from '../../hooks/useInstallPlugin';
import { useUninstallPlugin } from '../../hooks/useUninstallPlugin';
import { usePluginInfo } from '../../hooks/usePluginInfo';
import { usePlugins, useRefreshPlugins } from '../../hooks/usePlugins';
import { Button } from '../core/Button';
import { Checkbox } from '../core/Checkbox';
import { IconButton } from '../core/IconButton';
import { InlineCode } from '../core/InlineCode';
import { HStack } from '../core/Stacks';
@@ -15,7 +15,7 @@ import { SelectFile } from '../SelectFile';
export function SettingsPlugins() {
const [directory, setDirectory] = React.useState<string | null>(null);
const plugins = usePlugins();
const createPlugin = useCreatePlugin();
const createPlugin = useInstallPlugin();
const refreshPlugins = useRefreshPlugins();
return (
<div className="grid grid-rows-[minmax(0,1fr)_auto] h-full">
@@ -31,7 +31,6 @@ export function SettingsPlugins() {
<table className="w-full text-sm mb-auto min-w-full max-w-full divide-y divide-surface-highlight">
<thead>
<tr>
<th></th>
<th className="py-2 text-left">Plugin</th>
<th className="py-2 text-right">Version</th>
<th></th>
@@ -88,14 +87,10 @@ export function SettingsPlugins() {
function PluginInfo({ plugin }: { plugin: Plugin }) {
const pluginInfo = usePluginInfo(plugin.id);
const deletePlugin = useUninstallPlugin(plugin.id);
return (
<tr className="group">
<td className="pr-2">
<Checkbox hideLabel checked={true} title="foo" onChange={() => null} />
</td>
<td className="py-2 select-text cursor-text w-full">
<InlineCode>{pluginInfo.data?.name}</InlineCode>
</td>
<td className="py-2 select-text cursor-text w-full">{pluginInfo.data?.name}</td>
<td className="py-2 select-text cursor-text text-right">
<InlineCode>{pluginInfo.data?.version}</InlineCode>
</td>
@@ -105,6 +100,7 @@ function PluginInfo({ plugin }: { plugin: Plugin }) {
icon="trash"
title="Uninstall plugin"
className="text-text-subtlest"
onClick={() => deletePlugin.mutate()}
/>
</td>
</tr>

View File

@@ -5,12 +5,13 @@ import type {
HttpRequest,
} from '@yaakapp/api';
import { invokeCmd } from '../lib/tauri';
import { usePlugins } from './usePlugins';
import { usePluginsKey } from './usePlugins';
export function useHttpRequestActions() {
const plugins = usePlugins();
const pluginsKey = usePluginsKey();
const httpRequestActions = useQuery({
queryKey: ['http_request_actions', plugins.map((p) => p.updatedAt)],
queryKey: ['http_request_actions', pluginsKey],
refetchOnWindowFocus: false,
queryFn: async () => {
const responses = (await invokeCmd(

View File

@@ -2,11 +2,11 @@ import { useMutation } from '@tanstack/react-query';
import { trackEvent } from '../lib/analytics';
import { invokeCmd } from '../lib/tauri';
export function useCreatePlugin() {
export function useInstallPlugin() {
return useMutation<void, unknown, string>({
mutationKey: ['create_plugin'],
mutationKey: ['install_plugin'],
mutationFn: async (directory: string) => {
await invokeCmd('cmd_create_plugin', { directory });
await invokeCmd('cmd_install_plugin', { directory });
},
onSettled: () => trackEvent('plugin', 'create'),
});

View File

@@ -1,12 +1,12 @@
import { useQuery } from '@tanstack/react-query';
import type { PluginBootResponse } from '@yaakapp/api';
import type { BootResponse } from '@yaakapp/api';
import { invokeCmd } from '../lib/tauri';
export function usePluginInfo(id: string) {
return useQuery({
queryKey: ['plugin_info', id],
queryFn: async () => {
const info = (await invokeCmd('cmd_plugin_info', { id })) as PluginBootResponse;
const info = (await invokeCmd('cmd_plugin_info', { id })) as BootResponse;
return info;
},
});

View File

@@ -12,6 +12,12 @@ export function usePlugins() {
return useAtomValue(pluginsAtom);
}
export function usePluginsKey() {
return useAtomValue(pluginsAtom)
.map((p) => p.id + p.updatedAt)
.join(',');
}
/**
* Reload all plugins and refresh the list of plugins
*/

View File

@@ -1,10 +1,13 @@
import { useQuery } from '@tanstack/react-query';
import type { GetTemplateFunctionsResponse } from '@yaakapp/api';
import { invokeCmd } from '../lib/tauri';
import { usePluginsKey } from './usePlugins';
export function useTemplateFunctions() {
const pluginsKey = usePluginsKey();
const result = useQuery({
queryKey: ['template_functions'],
queryKey: ['template_functions', pluginsKey],
queryFn: async () => {
const responses = (await invokeCmd(
'cmd_template_functions',
@@ -13,6 +16,5 @@ export function useTemplateFunctions() {
},
});
const fns = result.data?.flatMap((r) => r.functions) ?? [];
return fns;
return result.data?.flatMap((r) => r.functions) ?? [];
}

View File

@@ -0,0 +1,14 @@
import { useMutation } from '@tanstack/react-query';
import type { Plugin } from '@yaakapp/api';
import { trackEvent } from '../lib/analytics';
import { invokeCmd } from '../lib/tauri';
export function useUninstallPlugin(pluginId: string) {
return useMutation<Plugin | null, string>({
mutationKey: ['uninstall_plugin'],
mutationFn: async () => {
return invokeCmd('cmd_uninstall_plugin', { pluginId });
},
onSettled: () => trackEvent('plugin', 'delete'),
});
}

View File

@@ -6,7 +6,6 @@ type TauriCmd =
| 'cmd_check_for_updates'
| 'cmd_create_cookie_jar'
| 'cmd_create_environment'
| 'cmd_create_plugin'
| 'cmd_template_tokens_to_string'
| 'cmd_create_folder'
| 'cmd_create_grpc_request'
@@ -40,6 +39,7 @@ type TauriCmd =
| 'cmd_grpc_reflect'
| 'cmd_http_request_actions'
| 'cmd_import_data'
| 'cmd_install_plugin'
| 'cmd_list_cookie_jars'
| 'cmd_list_environments'
| 'cmd_list_folders'
@@ -64,6 +64,7 @@ type TauriCmd =
| 'cmd_set_update_mode'
| 'cmd_template_functions'
| 'cmd_track_event'
| 'cmd_uninstall_plugin'
| 'cmd_update_cookie_jar'
| 'cmd_update_environment'
| 'cmd_update_folder'