Auth plugins (#155)

This commit is contained in:
Gregory Schier
2025-01-17 05:53:03 -08:00
committed by GitHub
parent e21df98a30
commit bd322162c8
56 changed files with 5468 additions and 1474 deletions

View File

@@ -0,0 +1,38 @@
import { useQuery } from '@tanstack/react-query';
import type { GetHttpAuthenticationResponse } from '@yaakapp-internal/plugins';
import { useAtomValue } from 'jotai';
import { atom, useSetAtom } from 'jotai/index';
import { useState } from 'react';
import { invokeCmd } from '../lib/tauri';
const httpAuthenticationAtom = atom<GetHttpAuthenticationResponse[]>([]);
const orderedHttpAuthenticationAtom = atom((get) =>
get(httpAuthenticationAtom).sort((a, b) => a.name.localeCompare(b.name)),
);
export function useHttpAuthentication() {
return useAtomValue(orderedHttpAuthenticationAtom);
}
export function useSubscribeHttpAuthentication() {
const [numResults, setNumResults] = useState<number>(0);
const setAtom = useSetAtom(httpAuthenticationAtom);
useQuery({
queryKey: ['http_authentication'],
// Fetch periodically until functions are returned
// NOTE: visibilitychange (refetchOnWindowFocus) does not work on Windows, so we'll rely on this logic
// to refetch things until that's working again
// TODO: Update plugin system to wait for plugins to initialize before sending the first event to them
refetchInterval: numResults > 0 ? Infinity : 1000,
refetchOnMount: true,
queryFn: async () => {
const result = await invokeCmd<GetHttpAuthenticationResponse[]>(
'cmd_get_http_authentication',
);
setNumResults(result.length);
setAtom(result);
return result;
},
});
}

View File

@@ -0,0 +1,18 @@
import { useEffect } from 'react';
export function useKeyboardEvent(
event: 'keyup' | 'keydown',
key: KeyboardEvent['key'],
cb: () => void,
) {
useEffect(() => {
const fn = (e: KeyboardEvent) => {
if (e.key === key) cb();
};
document.addEventListener(event, fn);
return () => document.removeEventListener(event, fn);
// Don't have `cb` as a dep for caller convenience
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [event]);
}

View File

@@ -1,5 +1,4 @@
import type { HttpResponse } from '@yaakapp-internal/models';
import { showAlert } from '../lib/alert';
import { trackEvent } from '../lib/analytics';
import { getHttpRequest } from '../lib/store';
import { invokeCmd } from '../lib/tauri';
@@ -23,6 +22,5 @@ export function useSendAnyHttpRequest() {
});
},
onSettled: () => trackEvent('http_request', 'send'),
onError: (err) => showAlert({ id: 'send-failed', title: 'Send Failed', body: err }),
});
}

View File

@@ -9,10 +9,10 @@ import { usePluginsKey } from './usePlugins';
const templateFunctionsAtom = atom<TemplateFunction[]>([]);
export function useTwigCompletionOptions(
export function useTemplateFunctionCompletionOptions(
onClick: (fn: TemplateFunction, ragTag: string, pos: number) => void,
) {
const templateFunctions = useTemplateFunctions();
const templateFunctions = useAtomValue(templateFunctionsAtom);
return useMemo<TwigCompletionOption[]>(() => {
return (
templateFunctions.map((fn) => {
@@ -37,10 +37,6 @@ export function useTwigCompletionOptions(
}, [onClick, templateFunctions]);
}
export function useTemplateFunctions() {
return useAtomValue(templateFunctionsAtom);
}
export function useSubscribeTemplateFunctions() {
const pluginsKey = usePluginsKey();
const [numFns, setNumFns] = useState<number>(0);