mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-25 02:08:28 +02:00
Better template function fetching
This commit is contained in:
@@ -1,19 +1,24 @@
|
|||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import type { GetTemplateFunctionsResponse } from '@yaakapp-internal/plugin';
|
import type { GetTemplateFunctionsResponse } from '@yaakapp-internal/plugin';
|
||||||
|
import { useState } from 'react';
|
||||||
import { invokeCmd } from '../lib/tauri';
|
import { invokeCmd } from '../lib/tauri';
|
||||||
import { usePluginsKey } from './usePlugins';
|
import { usePluginsKey } from './usePlugins';
|
||||||
|
|
||||||
export function useTemplateFunctions() {
|
export function useTemplateFunctions() {
|
||||||
const pluginsKey = usePluginsKey();
|
const pluginsKey = usePluginsKey();
|
||||||
|
const [numFns, setNumFns] = useState<number>(0);
|
||||||
|
|
||||||
const result = useQuery({
|
const result = useQuery({
|
||||||
queryKey: ['template_functions', pluginsKey],
|
queryKey: ['template_functions', pluginsKey],
|
||||||
// NOTE: visibilitychange (refetchOnWindowFocus) does not work on Windows, so we'll rely on mount to
|
// Fetch periodically until functions are returned
|
||||||
// refetch template functions for us when. This should handle the case where the plugin system isn't
|
// NOTE: visibilitychange (refetchOnWindowFocus) does not work on Windows, so we'll rely on this logic
|
||||||
// quite ready the first time this is invoked.
|
// to refetch things until that's working again
|
||||||
|
refetchInterval: numFns > 0 ? Infinity : 500,
|
||||||
refetchOnMount: true,
|
refetchOnMount: true,
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
return invokeCmd<GetTemplateFunctionsResponse[]>('cmd_template_functions');
|
const result = await invokeCmd<GetTemplateFunctionsResponse[]>('cmd_template_functions');
|
||||||
|
setNumFns(result.length);
|
||||||
|
return result;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user