-
-
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: cleanup remove all downloaded files
- Loading branch information
Showing
10 changed files
with
104 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import localforage from "localforage" | ||
import { DOWNLOAD_PREFIX } from "../constants.shared" | ||
|
||
export const getBookKeysFromStorage = async () => | ||
(await localforage.keys()) | ||
.filter((key) => key.startsWith(DOWNLOAD_PREFIX)) | ||
.map((key) => ({ key, bookId: key.replace(`${DOWNLOAD_PREFIX}-`, "") })) |
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 |
---|---|---|
@@ -1,40 +1,66 @@ | ||
import { useCallback } from "react" | ||
import { useDatabase } from "../rxdb" | ||
import { useRemoveDownloadFile } from "./useRemoveDownloadFile" | ||
import { plugin } from "../plugins/local" | ||
import { plugin as localPlugin } from "../plugins/local" | ||
import { useMutation } from "reactjrx" | ||
import { combineLatest, first, from, map, of, switchMap, tap } from "rxjs" | ||
import { getBookKeysFromStorage } from "./helpers" | ||
import { latestDatabase$ } from "../rxdb/RxDbProvider" | ||
|
||
export const useRemoveAllDownloadedFiles = () => { | ||
const { db } = useDatabase() | ||
const { mutateAsync: removeDownloadFile } = useRemoveDownloadFile() | ||
|
||
return useCallback( | ||
async (bookIds: string[]) => { | ||
return await Promise.all( | ||
bookIds.map(async (id) => { | ||
const book = await db?.book | ||
.findOne({ | ||
selector: { | ||
_id: id | ||
} | ||
}) | ||
.exec() | ||
return useMutation({ | ||
mutationFn: () => { | ||
return combineLatest([ | ||
latestDatabase$, | ||
from(getBookKeysFromStorage()) | ||
]).pipe( | ||
first(), | ||
switchMap(([db, keys]) => { | ||
const books$ = from( | ||
db.book | ||
.find({ | ||
selector: { | ||
_id: { | ||
$in: keys.map(({ bookId }) => bookId) | ||
} | ||
} | ||
}) | ||
.exec() | ||
) | ||
|
||
const linkIds = book?.links ?? [] | ||
return books$.pipe( | ||
switchMap((books) => | ||
combineLatest( | ||
books.map((book) => { | ||
// valid to remove | ||
if (book.links.length === 0) return of(book) | ||
|
||
if (linkIds.length > 0) { | ||
const links = await db?.link.findByIds(linkIds).exec() | ||
return from(db.link.findByIds(book.links).exec()).pipe( | ||
switchMap((links) => { | ||
const fileLink = Array.from(links?.values() ?? []).find( | ||
({ type }) => type === localPlugin.type | ||
) | ||
|
||
const fileLink = Array.from(links?.values() ?? []).find( | ||
({ type }) => type === plugin.type | ||
) | ||
|
||
if (fileLink) return | ||
} | ||
// local book, don't remove | ||
if (fileLink) return of(null) | ||
|
||
return removeDownloadFile({ bookId: id }) | ||
return of(book) | ||
}) | ||
) | ||
}) | ||
) | ||
), | ||
map((books) => books.filter((book) => !!book)), | ||
switchMap((books) => | ||
combineLatest( | ||
books.map((book) => | ||
from(removeDownloadFile({ bookId: book._id })) | ||
) | ||
) | ||
) | ||
) | ||
}) | ||
) | ||
}, | ||
[db, removeDownloadFile] | ||
) | ||
} | ||
}) | ||
} |
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
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
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,27 @@ | ||
import { useMutation } from "reactjrx" | ||
import { useRemoveAllDownloadedFiles } from "../download/useRemoveAllDownloadedFiles" | ||
import { from, of } from "rxjs" | ||
|
||
export const useRemoveAllDownloads = ({ | ||
onSuccess | ||
}: { | ||
onSuccess: () => void | ||
}) => { | ||
const { mutateAsync: deleteAllDownloadedFiles } = | ||
useRemoveAllDownloadedFiles() | ||
|
||
return useMutation({ | ||
onSuccess, | ||
mutationFn: () => { | ||
const isConfirmed = confirm( | ||
"Are you sure you want to delete all downloads at once?" | ||
) | ||
|
||
if (isConfirmed) { | ||
return from(deleteAllDownloadedFiles()) | ||
} | ||
|
||
return of(null) | ||
} | ||
}) | ||
} |