I think I have a working document cache solution that's actually pretty good.
This commit is contained in:
23
src/context/document/hooks.ts
Normal file
23
src/context/document/hooks.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import type { DocumentId } from "@/lib/types";
|
||||
import { useContext } from "react";
|
||||
import { DocumentContext } from "./DocumentContext";
|
||||
|
||||
export function useDocument(id: DocumentId) {
|
||||
const ctx = useContext(DocumentContext);
|
||||
if (!ctx)
|
||||
throw new Error("useDocument must be used within a DocumentProvider");
|
||||
return {
|
||||
docResult: ctx.cache.documents[id],
|
||||
dispatch: ctx.dispatch,
|
||||
};
|
||||
}
|
||||
|
||||
export function useDocumentCache() {
|
||||
const ctx = useContext(DocumentContext);
|
||||
if (!ctx)
|
||||
throw new Error("useDocument must be used within a DocumentProvider");
|
||||
return {
|
||||
cache: ctx.cache,
|
||||
dispatch: ctx.dispatch,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user