Skip to content

Commit

Permalink
Merge pull request #67 from mbret/develop
Browse files Browse the repository at this point in the history
fix: fixed invalid format
  • Loading branch information
mbret authored Feb 25, 2024
2 parents 02d19db + e651cd0 commit 14772ea
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
22 changes: 14 additions & 8 deletions packages/web/src/books/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import {
} from "../download/states"
import {
getCollectionState,
useCollectionsDictionaryWithoutPrivacy
useCollectionsDictionary
} from "../collections/states"
import { map, switchMap, tap, withLatestFrom } from "rxjs"
import { map, switchMap, withLatestFrom } from "rxjs"
import { plugin } from "../plugins/local"
import { latestDatabase$ } from "../rxdb/useCreateDatabase"
import { useLocalSettings } from "../settings/states"
Expand All @@ -29,7 +29,10 @@ import { DeepReadonlyObject, MangoQuery } from "rxdb"
export const getBooksByIds = async (database: Database) => {
const result = await database.collections.book.find({}).exec()

return keyBy(result, "_id")
return keyBy(
result.map((book) => book.toJSON()),
"_id"
)
}

export const useBooks = ({
Expand Down Expand Up @@ -89,7 +92,7 @@ const getBookState = ({
book,
tags = {}
}: {
collections: ReturnType<typeof useCollectionsDictionaryWithoutPrivacy>["data"]
collections: ReturnType<typeof useCollectionsDictionary>["data"]
book?: BookQueryResult | null
tags: ReturnType<typeof useTagsByIds>["data"]
}) => {
Expand All @@ -113,7 +116,7 @@ export const useBookState = ({
tags: ReturnType<typeof useTagsByIds>["data"]
}) => {
const { data: book } = useBook({ id: bookId })
const { data: collections } = useCollectionsDictionaryWithoutPrivacy()
const { data: collections } = useCollectionsDictionary()

return getBookState({
book,
Expand Down Expand Up @@ -141,7 +144,7 @@ export const getEnrichedBookState = ({
protectedTagIds: ReturnType<typeof useProtectedTagIds>["data"]
tags: ReturnType<typeof useTagsByIds>["data"]
normalizedLinks: ReturnType<typeof useLinks>["data"]
normalizedCollections: ReturnType<typeof useCollectionsDictionaryWithoutPrivacy>["data"]
normalizedCollections: ReturnType<typeof useCollectionsDictionary>["data"]
normalizedBooks: ReturnType<typeof useBooksDic>["data"]
}) => {
const book = getBookState({
Expand All @@ -154,6 +157,9 @@ export const getEnrichedBookState = ({
normalizedBookDownloadsState
})

if (book && !book?.links) {
debugger
}
const linkId = book?.links[0]

if (!book || !linkId) return undefined
Expand Down Expand Up @@ -200,7 +206,7 @@ export const useEnrichedBookState = (param: {
tags: ReturnType<typeof useTagsByIds>["data"]
}) => {
const { data: normalizedLinks } = useLinks()
const { data: normalizedCollections } = useCollectionsDictionaryWithoutPrivacy()
const { data: normalizedCollections } = useCollectionsDictionary()
const { data: normalizedBooks } = useBooksDic()

return getEnrichedBookState({
Expand Down Expand Up @@ -343,7 +349,7 @@ export const useBookCollectionsState = ({
tags: ReturnType<typeof useTagsByIds>["data"]
}) => {
const book = useBookState({ bookId, tags })
const { data: normalizedCollections } = useCollectionsDictionaryWithoutPrivacy()
const { data: normalizedCollections } = useCollectionsDictionary()
const bookIds = useVisibleBookIds()

return book?.collections?.map((id) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { FC, memo } from "react"
import { ListItem, ListItemText, useTheme } from "@mui/material"
import { useCSS } from "../../common/utils"
import { useCollectionsDictionaryWithoutPrivacy } from "../states"
import { useCollectionsDictionary } from "../states"
import { Checkbox } from "../../common/Checkbox"

export const SelectableCollectionListItem: FC<{
id: string
onItemClick?: (tag: string) => void
selected: boolean
}> = memo(({ id, onItemClick, selected }) => {
const { data: collections = {} } = useCollectionsDictionaryWithoutPrivacy()
const { data: collections = {} } = useCollectionsDictionary()
const data = collections[id]
const styles = useStyle()

Expand Down
19 changes: 11 additions & 8 deletions packages/web/src/collections/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ export type Collection = CollectionDocType
export const getCollectionsByIds = async (database: Database) => {
const result = await database.collections.obokucollection.find({}).exec()

return keyBy(result, "_id")
return keyBy(
result.map((item) => item.toJSON()),
"_id"
)
}

export const useCollectionsWithoutPrivacy = () => {
export const useCollections = () => {
return useForeverQuery({
queryKey: ["rxdb", "get", "collections"],
queryFn: () => {
Expand All @@ -27,8 +30,8 @@ export const useCollectionsWithoutPrivacy = () => {
})
}

export const useCollectionsDictionaryWithoutPrivacy = () => {
const result = useCollectionsWithoutPrivacy()
export const useCollectionsDictionary = () => {
const result = useCollections()

return {
...result,
Expand Down Expand Up @@ -68,7 +71,7 @@ export const useCollection = ({ id }: { id?: string }) => {
}

export const useCollectionsWithPrivacy = () => {
const { data: collections } = useCollectionsWithoutPrivacy()
const { data: collections } = useCollections()
const visibleBookIds = useVisibleBookIds()
const { showCollectionWithProtectedContent } = useLocalSettings()

Expand All @@ -94,7 +97,7 @@ export const useCollectionsWithPrivacy = () => {
* @deprecated
*/
export const useCollectionsAsArrayState = () => {
const { data: collectionsDic = {} } = useCollectionsDictionaryWithoutPrivacy()
const { data: collectionsDic = {} } = useCollectionsDictionary()
const visibleBookIds = useVisibleBookIds()
const localSettingsState = useLocalSettings()

Expand Down Expand Up @@ -134,7 +137,7 @@ export const getCollectionState = ({
}: {
id: string
localSettingsState: ReturnType<typeof useLocalSettings>
normalizedCollections: ReturnType<typeof useCollectionsDictionaryWithoutPrivacy>["data"]
normalizedCollections: ReturnType<typeof useCollectionsDictionary>["data"]
bookIds: ReturnType<typeof useVisibleBookIds>
}) => {
const collection = normalizedCollections[id]
Expand All @@ -161,7 +164,7 @@ export const useCollectionState = ({
id: string
localSettingsState: ReturnType<typeof useLocalSettings>
}) => {
const { data: normalizedCollections } = useCollectionsDictionaryWithoutPrivacy()
const { data: normalizedCollections } = useCollectionsDictionary()
const bookIds = useVisibleBookIds()

return getCollectionState({
Expand Down

0 comments on commit 14772ea

Please sign in to comment.