{"version":3,"file":"BookMatchPercentageForUser-9BeSFO68.js","sources":["../../../app/javascript/components/match/BookMatchPercentage/MatchPercentageBadge.tsx","../../../app/javascript/queries/recommendations/getBookMatchScoreForUser.ts","../../../app/javascript/hooks/useBookMatchScore.ts","../../../app/javascript/components/match/BookMatchPercentage/BookMatchPercentageForUser.tsx"],"sourcesContent":["import Badge from \"hardcover-ui/components/Badge\";\nimport Icon from \"hardcover-ui/components/Icon\";\nimport { BadgeThemeProps } from \"hardcover-ui/theme/components/badge\";\nimport sparkles from \"icons/solid/sparkles.svg\";\n\ninterface BookMatchPercentageForUser {\n matchScore?: number;\n size?: BadgeThemeProps[\"size\"];\n}\n\nexport default function MatchPercentageBadge({\n size = \"sm\",\n matchScore,\n}: BookMatchPercentageForUser) {\n if (!matchScore) {\n return false;\n }\n\n return (\n \n \n {matchScore}%\n \n );\n}\n","import { gql } from \"graphql-tag\";\n\nexport const findBookMatchScoreForUser = gql`\n query FindBookMatchScoreForUser($userId: bigint!, $bookId: bigint!) {\n recommendations(\n where: {\n subject_type: { _eq: \"User\" }\n subject_id: { _eq: $userId }\n item_type: { _eq: \"Book\" }\n item_id: { _eq: $bookId }\n }\n limit: 10\n ) {\n ...MatchPercentageFragment\n }\n }\n`;\n","import { findBookMatchScoreForUser } from \"queries/recommendations/getBookMatchScoreForUser\";\nimport useCurrentUserId from \"./user/useCurrentUserId\";\nimport { useQuery } from \"@apollo/client/react\";\nimport { useDispatch } from \"react-redux\";\nimport { setMatchScore } from \"store/currentUser/currentUserSlice\";\nimport { useUserBookMatchScore } from \"store/currentUser/currentUserSelector\";\nimport { useUserBook } from \"store/books/booksSelector\";\nimport { slugIdConstants } from \"types/UserBookStatusType\";\n\nexport default function useBookMatchScore({\n bookId,\n isVisible\n}: {\n bookId: number;\n isVisible: boolean;\n}) {\n const dispatch = useDispatch();\n const userId = useCurrentUserId();\n const userBook = useUserBook(bookId);\n\n // Get the actual score from Redux\n const score = useUserBookMatchScore(bookId);\n\n // Skip fetching match percentage if:\n // Not logged in\n // Already marked as read\n // Already loaded\n useQuery(findBookMatchScoreForUser, {\n skip: userBook?.statusId === slugIdConstants.read || score === null || score > 0 || !isVisible,\n variables: {\n bookId,\n userId,\n },\n onCompleted: (data) => {\n if (data.recommendations.length === 0) {\n dispatch(setMatchScore({ bookId, score: null }));\n } else {\n dispatch(setMatchScore({ bookId, score: data.recommendations[0].score }));\n }\n },\n });\n\n return score;\n};","import { useRef } from \"react\";\nimport useOnScreen from \"hooks/useIsOnScreen\";\nimport { BadgeThemeProps } from \"hardcover-ui/theme/components/badge\";\nimport MatchPercentageBadge from \"./MatchPercentageBadge\";\nimport useBookMatchScore from \"hooks/useBookMatchScore\";\n\ninterface BookMatchPercentageForUserType {\n bookId: number;\n size?: BadgeThemeProps[\"size\"];\n}\n\nexport default function BookMatchPercentageForUser({\n bookId,\n size = \"sm\",\n}: BookMatchPercentageForUserType) {\n const ref = useRef(null);\n const isVisible = useOnScreen(ref, \"200px\");\n const score = useBookMatchScore({bookId, isVisible});\n\n // No score available\n if (score === null) {\n return <>;\n }\n\n // Loading the score\n if (score === undefined) {\n return ;\n }\n\n return ;\n}\n"],"names":["MatchPercentageBadge","size","matchScore","Badge","jsx","Icon","sparkles","findBookMatchScoreForUser","gql","useBookMatchScore","bookId","isVisible","dispatch","useDispatch","userId","useCurrentUserId","userBook","useUserBook","score","useUserBookMatchScore","useQuery","slugIdConstants","data","setMatchScore","BookMatchPercentageForUser","ref","useRef","useOnScreen","Fragment"],"mappings":"+dAUA,SAAwBA,EAAqB,CAC3C,KAAAC,EAAO,KACP,WAAAC,CACF,EAA+B,CAC7B,OAAKA,SAKFC,EAAM,CAAA,QAAQ,SAAS,KAAAF,EAAY,UAAU,mBAC5C,SAAA,CAAAG,EAAA,IAACC,EAAK,CAAA,KAAMC,EAAU,KAAK,KAAK,SAC/B,OAAM,CAAA,SAAA,CAAAJ,EAAW,GAAA,EAAC,CACrB,CAAA,CAAA,EAPO,EASX,CCtBO,MAAMK,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