-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added collection reading state
- Loading branch information
Showing
12 changed files
with
202 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
70 changes: 70 additions & 0 deletions
70
packages/web/src/library/collections/CollectionStateDialog.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { | ||
Button, | ||
Dialog, | ||
DialogActions, | ||
DialogContent, | ||
DialogTitle, | ||
FormControl, | ||
FormControlLabel, | ||
Radio, | ||
RadioGroup | ||
} from "@mui/material" | ||
import { FC } from "react" | ||
import { useSignalValue } from "reactjrx" | ||
import { collectionsListSignal } from "./state" | ||
|
||
type State = ReturnType<(typeof collectionsListSignal)["getValue"]> | ||
type ReadingState = State["readingState"] | ||
|
||
export const getDisplayableReadingState = (readingState: ReadingState) => { | ||
switch (readingState) { | ||
case "ongoing": | ||
return "Ongoing" | ||
case "finished": | ||
return "Finished" | ||
default: | ||
return "Any" | ||
} | ||
} | ||
|
||
export const CollectionReadingStateDialog: FC<{ | ||
open: boolean | ||
onClose: () => void | ||
}> = ({ open, onClose }) => { | ||
const state = useSignalValue(collectionsListSignal) | ||
const readingStates = ["any", "ongoing", "finished"] as ReadingState[] | ||
|
||
return ( | ||
<Dialog onClose={onClose} open={open}> | ||
<DialogTitle>Reading state</DialogTitle> | ||
<DialogContent> | ||
<FormControl> | ||
<RadioGroup | ||
name="collection-state-radio-group" | ||
value={state.readingState} | ||
onChange={(event) => { | ||
collectionsListSignal.setValue((state) => ({ | ||
...state, | ||
readingState: event.target.value as ReadingState | ||
})) | ||
}} | ||
> | ||
{readingStates.map((readingState) => ( | ||
<FormControlLabel | ||
key={readingState} | ||
value={readingState} | ||
control={<Radio />} | ||
label={getDisplayableReadingState(readingState)} | ||
/> | ||
))} | ||
</RadioGroup> | ||
</FormControl> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button onClick={onClose} autoFocus> | ||
Ok | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { signal } from "reactjrx" | ||
|
||
export const collectionsListSignal = signal<{ | ||
readingState: "ongoing" | "finished" | "any" | ||
}>({ | ||
key: "collectionsListSignal", | ||
default: { | ||
readingState: "any" | ||
} | ||
}) |
76 changes: 76 additions & 0 deletions
76
packages/web/src/library/collections/useLibraryCollections.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { useCollectionsWithPrivacy } from "../../collections/states" | ||
import { useBooks } from "../../books/states" | ||
import { useMemo } from "react" | ||
import { libraryStateSignal } from "../states" | ||
import { useSignalValue } from "reactjrx" | ||
import { collectionsListSignal } from "./state" | ||
import { ReadingStateState } from "@oboku/shared" | ||
|
||
export const useLibraryCollections = () => { | ||
const { showNotInterestedCollections } = useSignalValue( | ||
libraryStateSignal, | ||
({ showNotInterestedCollections }) => ({ showNotInterestedCollections }) | ||
) | ||
const collectionReadingState = useSignalValue( | ||
collectionsListSignal, | ||
(state) => state.readingState | ||
) | ||
const { data: visibleCollections } = useCollectionsWithPrivacy() | ||
const { data: books } = useBooks() | ||
|
||
const collectionIds = useMemo( | ||
() => | ||
visibleCollections | ||
?.filter((collection) => { | ||
const booksFromCollection = | ||
books?.filter((book) => collection.books.includes(book._id)) ?? [] | ||
|
||
const onlyWantOngoingAndIsFinished = | ||
collectionReadingState === "ongoing" && | ||
booksFromCollection.length > 0 && | ||
booksFromCollection.every( | ||
(book) => | ||
book.readingStateCurrentState === ReadingStateState.Finished | ||
) | ||
|
||
const onlyWantFinishedAndHasSomeNonFinished = | ||
collectionReadingState === "finished" && | ||
(booksFromCollection.some( | ||
(book) => | ||
book.readingStateCurrentState !== ReadingStateState.Finished | ||
) || | ||
booksFromCollection.length === 0) | ||
|
||
if ( | ||
onlyWantFinishedAndHasSomeNonFinished || | ||
onlyWantOngoingAndIsFinished | ||
) { | ||
return false | ||
} | ||
|
||
if ( | ||
collection.books.length === 0 || | ||
!books?.length || | ||
showNotInterestedCollections | ||
) | ||
return true | ||
|
||
const hasOneInterestedBook = collection.books.some((bookId) => { | ||
const book = books?.find((item) => item._id === bookId) | ||
|
||
return !book?.isNotInterested | ||
}) | ||
|
||
return hasOneInterestedBook | ||
}) | ||
.map((collection) => collection._id), | ||
[ | ||
visibleCollections, | ||
books, | ||
showNotInterestedCollections, | ||
collectionReadingState | ||
] | ||
) | ||
|
||
return { data: collectionIds } | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters