feat: initial commit
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import { use, useCallback } from "react";
|
||||
import { DbContext } from "../contexts/Db.js";
|
||||
import type { DbInterface } from "../../db/types/DbInterface.js";
|
||||
import type { Store } from "../store/store.js";
|
||||
import { useStore } from "../store/react.js";
|
||||
|
||||
export function useAction<P extends any[], R>(
|
||||
action: (...params: [...P, { db: DbInterface; store: Store }]) => Promise<R>
|
||||
): (...args: P) => Promise<R> {
|
||||
const db = use(DbContext);
|
||||
const store = useStore();
|
||||
|
||||
return useCallback(
|
||||
async (...params: P) => {
|
||||
if (!db) throw new Error("DB not present");
|
||||
return action(...params, { db, store });
|
||||
},
|
||||
[action, db]
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user