Skip to content

Commit

Permalink
Merge pull request #24 from Envoy-VC/feat/store-values
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-hm authored Oct 7, 2024
2 parents e9c0f69 + 1fb7a6b commit fb716df
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/client-react-hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from "./use-nil-fetch-value";
export * from "./use-nil-set-store-acl";
export * from "./use-nil-store-program";
export * from "./use-nil-store-value";
export * from "./use-nil-store-values";
export * from "./use-nil-update-value";
export * from "./use-nillion";
export * from "./use-nillion-auth";
49 changes: 49 additions & 0 deletions packages/client-react-hooks/src/use-nil-store-values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";

import { Days, NadaValues, StoreAcl, StoreId } from "@nillion/client-core";

import { createStoreCacheKey } from "./cache-key";
import { nilHookBaseResult, UseNilHook } from "./nil-hook-base";
import { useNillion } from "./use-nillion";

interface ExecuteArgs {
values: NadaValues;
ttl: Days;
acl?: StoreAcl;
}

type ExecuteResult = StoreId;

type UseNilStoreValues = UseNilHook<ExecuteArgs, ExecuteResult>;

export const useNilStoreValues = (): UseNilStoreValues => {
const { client: nilClient } = useNillion();
const queryClient = useQueryClient();

const mutationFn = async (args: ExecuteArgs): Promise<ExecuteResult> => {
const { values, ttl, acl } = args;
const response = await nilClient.storeValues({
values,
ttl,
acl,
});
if (response.err) throw response.err as Error;

const id = response.ok;
const key = createStoreCacheKey(id);
queryClient.setQueryData(key, id);
return id;
};

const mutate = useMutation({
mutationFn,
});

return {
execute: (args: ExecuteArgs) => {
mutate.mutate(args);
},
executeAsync: async (args: ExecuteArgs) => mutate.mutateAsync(args),
...nilHookBaseResult(mutate),
};
};

0 comments on commit fb716df

Please sign in to comment.