mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-24 10:21:15 +01:00
Got key values working
This commit is contained in:
26
src-web/hooks/useKeyValues.ts
Normal file
26
src-web/hooks/useKeyValues.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
||||
import { invoke } from '@tauri-apps/api';
|
||||
import type { KeyValue } from '../lib/models';
|
||||
|
||||
export function keyValueQueryKey(namespace: string, key: string) {
|
||||
return ['key_value', { namespace, key }];
|
||||
}
|
||||
|
||||
export function useKeyValues(namespace: string, key: string) {
|
||||
const query = useQuery<KeyValue | null>({
|
||||
initialData: null,
|
||||
queryKey: keyValueQueryKey(namespace, key),
|
||||
queryFn: async () => invoke('get_key_value', { namespace, key }),
|
||||
});
|
||||
|
||||
const mutate = useMutation<KeyValue, unknown, KeyValue['value']>({
|
||||
mutationFn: (value) => {
|
||||
return invoke('set_key_value', { namespace, key, value });
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
value: query.data?.value ?? null,
|
||||
set: (value: KeyValue['value']) => mutate.mutate(value),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user