CI / install-and-build (pull_request) Successful in 1m29s
CI / format (pull_request) Successful in 51s
CI / typecheck-source (pull_request) Successful in 41s
CI / typecheck-tests (pull_request) Successful in 39s
CI / test (pull_request) Successful in 51s
CI / lint (pull_request) Successful in 21s
Co-Authored-By: gpt-5.6-terra <noreply@openai.com>
21 lines
648 B
TypeScript
21 lines
648 B
TypeScript
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],
|
|
);
|
|
}
|