diff --git a/packages/web/src/books/states.ts b/packages/web/src/books/states.ts index d1b6d0db..74d61e95 100644 --- a/packages/web/src/books/states.ts +++ b/packages/web/src/books/states.ts @@ -1,6 +1,6 @@ import { intersection } from "lodash" import { useProtectedTagIds, useTagsByIds } from "../tags/helpers" -import { getLinkState, useLink, useLinks } from "../links/states" +import { getLinkState, useLink, useLinksDic } from "../links/states" import { getBookDownloadsState, booksDownloadStateSignal @@ -175,7 +175,7 @@ export const getEnrichedBookState = ({ > protectedTagIds: ReturnType["data"] tags: ReturnType["data"] - normalizedLinks: ReturnType["data"] + normalizedLinks: ReturnType["data"] normalizedCollections: CollectionDocType[] | undefined normalizedBooks: ReturnType["data"] }) => { @@ -248,7 +248,7 @@ export const useEnrichedBookState = ({ protectedTagIds: ReturnType["data"] tags: ReturnType["data"] }) => { - const { data: normalizedLinks } = useLinks() + const { data: normalizedLinks } = useLinksDic() const { data: normalizedCollections } = useCollections() const { data: normalizedBooks } = useBooksDic() @@ -330,7 +330,7 @@ export const useBookLinksState = ({ tags: ReturnType["data"] }) => { const book = useBookState({ bookId, tags }) - const { data: links } = useLinks() + const { data: links } = useLinksDic() return book?.links?.map((id) => getLinkState(links, id)) || [] } diff --git a/packages/web/src/links/states.ts b/packages/web/src/links/states.ts index e1bc2f54..cc682c6f 100644 --- a/packages/web/src/links/states.ts +++ b/packages/web/src/links/states.ts @@ -15,7 +15,19 @@ export const getLinksByIds = async (database: Database) => { export const useLinks = () => { return useForeverQuery({ - queryKey: ["rxdb", "get", "many", "link"], + queryKey: ["rxdb", "get", "many", "links/list"], + queryFn: () => { + return latestDatabase$.pipe( + switchMap((db) => db.collections.link.find({}).$), + map((entries) => entries.map((item) => item.toJSON())) + ) + } + }) +} + +export const useLinksDic = () => { + return useForeverQuery({ + queryKey: ["rxdb", "get", "many", "links/dic"], queryFn: () => { return latestDatabase$.pipe( switchMap((db) => db.collections.link.find({}).$), @@ -57,7 +69,7 @@ const mapLinkTtoState = ({ link }: { link?: LinkDocType | null }) => { } export const getLinkState = ( - linksState: ReturnType["data"] = {}, + linksState: ReturnType["data"] = {}, linkId: string ) => { const link = Object.values(linksState).find((link) => link?._id === linkId) @@ -87,7 +99,7 @@ export const getLinkStateAsync = async ({ * @todo optimize to refresh only when link id change */ export const useLinkState = (linkId: string) => { - const { data: links = {} } = useLinks() + const { data: links = {} } = useLinksDic() const link = Object.values(links).find((link) => link?._id === linkId) diff --git a/packages/web/src/problems/useRepair.ts b/packages/web/src/problems/useRepair.ts index da931694..0856aab6 100644 --- a/packages/web/src/problems/useRepair.ts +++ b/packages/web/src/problems/useRepair.ts @@ -1,8 +1,8 @@ import { BookDocType, CollectionDocType } from "@oboku/shared" import { useMutation } from "reactjrx" import { first, from, map, mergeMap, of } from "rxjs" -import { latestDatabase$ } from "../rxdb/RxDbProvider" import { DeepReadonlyObject } from "rxdb" +import { latestDatabase$ } from "../rxdb/useCreateDatabase" export const useRepair = () => { return useMutation({