mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-27 03:08:33 +02:00
Refactor proxy codebase
This commit is contained in:
30
apps/yaak-proxy/hooks/useActionMetadata.ts
Normal file
30
apps/yaak-proxy/hooks/useActionMetadata.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import type { ActionInvocation, ActionMetadata } from '@yaakapp-internal/proxy-lib';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { rpc } from '../lib/rpc';
|
||||
|
||||
/** Look up metadata for a specific action invocation. */
|
||||
export function useActionMetadata(action: ActionInvocation): ActionMetadata | null {
|
||||
const [meta, setMeta] = useState<ActionMetadata | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
getActions().then((actions) => {
|
||||
const match = actions.find(
|
||||
([inv]) => inv.scope === action.scope && inv.action === action.action,
|
||||
);
|
||||
setMeta(match?.[1] ?? null);
|
||||
});
|
||||
}, [action]);
|
||||
|
||||
return meta;
|
||||
}
|
||||
|
||||
let cachedActions: [ActionInvocation, ActionMetadata][] | null = null;
|
||||
|
||||
/** Fetch and cache all action metadata. */
|
||||
async function getActions(): Promise<[ActionInvocation, ActionMetadata][]> {
|
||||
if (!cachedActions) {
|
||||
const { actions } = await rpc('list_actions', {});
|
||||
cachedActions = actions;
|
||||
}
|
||||
return cachedActions;
|
||||
}
|
||||
Reference in New Issue
Block a user