mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-11 03:26:58 +02:00
Refactor new actions apis
This commit is contained in:
50
src-web/hooks/useFolderActions.ts
Normal file
50
src-web/hooks/useFolderActions.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import type { Folder } from '@yaakapp-internal/models';
|
||||
import type {
|
||||
CallFolderActionRequest,
|
||||
GetFolderActionsResponse,
|
||||
FolderAction,
|
||||
} from '@yaakapp-internal/plugins';
|
||||
import { useMemo } from 'react';
|
||||
import { invokeCmd } from '../lib/tauri';
|
||||
import { usePluginsKey } from './usePlugins';
|
||||
|
||||
export type CallableFolderAction = Pick<FolderAction, 'label' | 'icon'> & {
|
||||
call: (folder: Folder) => Promise<void>;
|
||||
};
|
||||
|
||||
export function useFolderActions() {
|
||||
const pluginsKey = usePluginsKey();
|
||||
|
||||
const actionsResult = useQuery<CallableFolderAction[]>({
|
||||
queryKey: ['folder_actions', pluginsKey],
|
||||
queryFn: () => getFolderActions(),
|
||||
});
|
||||
|
||||
// biome-ignore lint/correctness/useExhaustiveDependencies: none
|
||||
const actions = useMemo(() => {
|
||||
return actionsResult.data ?? [];
|
||||
}, [JSON.stringify(actionsResult.data)]);
|
||||
|
||||
return actions;
|
||||
}
|
||||
|
||||
export async function getFolderActions() {
|
||||
const responses = await invokeCmd<GetFolderActionsResponse[]>('cmd_folder_actions');
|
||||
const actions = responses.flatMap((r) =>
|
||||
r.actions.map((a, i) => ({
|
||||
label: a.label,
|
||||
icon: a.icon,
|
||||
call: async (folder: Folder) => {
|
||||
const payload: CallFolderActionRequest = {
|
||||
index: i,
|
||||
pluginRefId: r.pluginRefId,
|
||||
args: { folder },
|
||||
};
|
||||
await invokeCmd('cmd_call_folder_action', { req: payload });
|
||||
},
|
||||
})),
|
||||
);
|
||||
|
||||
return actions;
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import type { Folder, Workspace } from '@yaakapp-internal/models';
|
||||
import type {
|
||||
CallHttpCollectionActionRequest,
|
||||
GetHttpCollectionActionsResponse,
|
||||
HttpCollectionAction,
|
||||
} from '@yaakapp-internal/plugins';
|
||||
import { useMemo } from 'react';
|
||||
import { invokeCmd } from '../lib/tauri';
|
||||
import { usePluginsKey } from './usePlugins';
|
||||
|
||||
export type CallableHttpCollectionAction = Pick<HttpCollectionAction, 'label' | 'icon'> & {
|
||||
call: (model: Folder | Workspace) => Promise<void>;
|
||||
};
|
||||
|
||||
export function useHttpCollectionActions() {
|
||||
const pluginsKey = usePluginsKey();
|
||||
|
||||
const actionsResult = useQuery<CallableHttpCollectionAction[]>({
|
||||
queryKey: ['http_collection_actions', pluginsKey],
|
||||
queryFn: () => getHttpCollectionActions(),
|
||||
});
|
||||
|
||||
// biome-ignore lint/correctness/useExhaustiveDependencies: none
|
||||
const actions = useMemo(() => {
|
||||
return actionsResult.data ?? [];
|
||||
}, [JSON.stringify(actionsResult.data)]);
|
||||
|
||||
return actions;
|
||||
}
|
||||
|
||||
export async function getHttpCollectionActions() {
|
||||
const responses = await invokeCmd<GetHttpCollectionActionsResponse[]>('cmd_http_collection_actions');
|
||||
const actions = responses.flatMap((r) =>
|
||||
r.actions.map((a, i) => ({
|
||||
label: a.label,
|
||||
icon: a.icon,
|
||||
call: async (model: Folder | Workspace) => {
|
||||
const payload: CallHttpCollectionActionRequest = {
|
||||
index: i,
|
||||
pluginRefId: r.pluginRefId,
|
||||
args: (model as any).model === 'folder' ? { folder: model as Folder } : { workspace: model as Workspace },
|
||||
} as any;
|
||||
await invokeCmd('cmd_call_http_collection_action', { req: payload });
|
||||
},
|
||||
})),
|
||||
);
|
||||
|
||||
return actions;
|
||||
}
|
||||
50
src-web/hooks/useWorkspaceActions.ts
Normal file
50
src-web/hooks/useWorkspaceActions.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import type { Workspace } from '@yaakapp-internal/models';
|
||||
import type {
|
||||
CallWorkspaceActionRequest,
|
||||
GetWorkspaceActionsResponse,
|
||||
WorkspaceAction,
|
||||
} from '@yaakapp-internal/plugins';
|
||||
import { useMemo } from 'react';
|
||||
import { invokeCmd } from '../lib/tauri';
|
||||
import { usePluginsKey } from './usePlugins';
|
||||
|
||||
export type CallableWorkspaceAction = Pick<WorkspaceAction, 'label' | 'icon'> & {
|
||||
call: (workspace: Workspace) => Promise<void>;
|
||||
};
|
||||
|
||||
export function useWorkspaceActions() {
|
||||
const pluginsKey = usePluginsKey();
|
||||
|
||||
const actionsResult = useQuery<CallableWorkspaceAction[]>({
|
||||
queryKey: ['workspace_actions', pluginsKey],
|
||||
queryFn: () => getWorkspaceActions(),
|
||||
});
|
||||
|
||||
// biome-ignore lint/correctness/useExhaustiveDependencies: none
|
||||
const actions = useMemo(() => {
|
||||
return actionsResult.data ?? [];
|
||||
}, [JSON.stringify(actionsResult.data)]);
|
||||
|
||||
return actions;
|
||||
}
|
||||
|
||||
export async function getWorkspaceActions() {
|
||||
const responses = await invokeCmd<GetWorkspaceActionsResponse[]>('cmd_workspace_actions');
|
||||
const actions = responses.flatMap((r) =>
|
||||
r.actions.map((a, i) => ({
|
||||
label: a.label,
|
||||
icon: a.icon,
|
||||
call: async (workspace: Workspace) => {
|
||||
const payload: CallWorkspaceActionRequest = {
|
||||
index: i,
|
||||
pluginRefId: r.pluginRefId,
|
||||
args: { workspace },
|
||||
};
|
||||
await invokeCmd('cmd_call_workspace_action', { req: payload });
|
||||
},
|
||||
})),
|
||||
);
|
||||
|
||||
return actions;
|
||||
}
|
||||
Reference in New Issue
Block a user