Skip to content

Commit

Permalink
feat: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
mbret committed Apr 10, 2024
1 parent c26ce87 commit 79025a3
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions packages/web/src/reading/BackToReadingDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef } from "react"
import { memo, useEffect, useRef } from "react"
import { useLocation } from "react-router-dom"
import { ROUTES } from "../constants"
import {
Expand All @@ -12,8 +12,7 @@ import { useCreateBackToBookDialog } from "./useCreateBackToBookDialog"

const BASE_READER_ROUTE = ROUTES.READER.replace(`/:id`, ``)

export const BackToReadingDialog = () => {
const isOpen = useRef(false)
export const BackToReadingDialog = memo(() => {
const bookBeingRead = useSignalValue(bookBeingReadStateSignal)
const { data: book, isSuccess } = useBook({ id: bookBeingRead })
const { title } = getMetadataFromBook(book)
Expand All @@ -22,11 +21,11 @@ export const BackToReadingDialog = () => {
)
const location = useLocation()

const { mutate } = useCreateBackToBookDialog()
const { mutate, submittedAt } = useCreateBackToBookDialog()

useEffect(() => {
if (
isOpen.current ||
submittedAt ||
hasOpenedReaderAlready ||
!bookBeingRead ||
location.pathname.startsWith(BASE_READER_ROUTE) ||
Expand All @@ -35,17 +34,16 @@ export const BackToReadingDialog = () => {
return
}

isOpen.current = true

mutate({ bookId: bookBeingRead, title })
}, [
bookBeingRead,
location,
hasOpenedReaderAlready,
title,
mutate,
isSuccess
isSuccess,
submittedAt
])

return null
}
})

0 comments on commit 79025a3

Please sign in to comment.