Skip to content

Commit

Permalink
Merge pull request #60 from Ray-D-Song/download-button
Browse files Browse the repository at this point in the history
feat: add a download button
  • Loading branch information
banzhe authored Dec 4, 2024
2 parents 56f67b8 + 24d1df4 commit 45e2b3e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
5 changes: 2 additions & 3 deletions packages/web/src/components/iframe-page-content.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
interface IframePageContentProps {
pageHtml: string
pageContentUrl: string
}

function IframePageContent({ pageHtml }: IframePageContentProps) {
const pageContentUrl = URL.createObjectURL(new Blob([pageHtml], { type: 'text/html' }))
function IframePageContent({ pageContentUrl }: IframePageContentProps) {
return (
<iframe
src={pageContentUrl}
Expand Down
8 changes: 5 additions & 3 deletions packages/web/src/hooks/useObjectUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { useEffect, useState } from 'react'
* @param initialObject - `null` or an instance of `File`, `Blob` or `MediaSource`.
* @source https://github.com/VitorLuizC/use-object-url/blob/master/src/index.ts
*/
function useObjectURL(initialObject: null | File | Blob | MediaSource) {
function useObjectURL(initialObject: null | File | Blob | MediaSource | string | undefined) {
const [objectURL, setObjectURL] = useState<null | string>(null)

const [object, setObject] = useState<null | File | Blob | MediaSource>(
const [object, setObject] = useState<null | File | Blob | MediaSource | string | undefined>(
initialObject,
)

Expand All @@ -20,7 +20,9 @@ function useObjectURL(initialObject: null | File | Blob | MediaSource) {
return
}

const objectURL = URL.createObjectURL(object)
const targetObject = (typeof object === 'string' ? new Blob([object], { type: 'text/html' }) : object)

const objectURL = URL.createObjectURL(targetObject)
setObjectURL(objectURL)
return () => {
URL.revokeObjectURL(objectURL)
Expand Down
38 changes: 27 additions & 11 deletions packages/web/src/pages/(layout)/page.[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import IframePageContent from '~/components/iframe-page-content'
import LoadingWrapper from '~/components/loading-wrapper'
import ReadabilityPageContent from '~/components/readability-page-content'
import { deletePage, getPageDetail } from '~/data/page'
import { useObjectURL } from '~/hooks/useObjectUrl'
import { useNavigate, useParams } from '~/router'
import AppContext from '~/store/app'

Expand Down Expand Up @@ -52,15 +53,18 @@ function ArchivePage() {
window.history.back()
}

const { data: pageContentUrl, loading: pageLoading } = useRequest(async () => {
const pageHtml = await getPageContent(slug)
return pageHtml
})
useEffect(() => {
return () => {
pageContentUrl && URL.revokeObjectURL(pageContentUrl)
}
}, [pageContentUrl])
const { objectURL: pageContentUrl, setObject } = useObjectURL(null)
const { data: pageHtml, loading: pageLoading } = useRequest(
async () => {
const pageHtml = await getPageContent(slug)
return pageHtml
},
{
onSuccess: (pageHtml) => {
setObject(pageHtml)
},
},
)

const { runAsync: runDeletePage } = useRequest(
deletePage,
Expand All @@ -86,6 +90,18 @@ function ArchivePage() {
<ArrowLeft className="w-5 h-5" />
</Button>
<div className="flex space-x-2">
<a
href={pageContentUrl ?? ''}
download={`${pageDetail?.title ?? 'Download'}.html`}
>
<Button
variant="default"
size="sm"
>
Download
</Button>
</a>

<Button
variant="secondary"
size="sm"
Expand All @@ -105,8 +121,8 @@ function ArchivePage() {
<div className="flex-1 p-4 w-full">
<LoadingWrapper loading={pageLoading}>
{readMode
? <ReadabilityPageContent pageHtml={pageContentUrl || ''} />
: <IframePageContent pageHtml={pageContentUrl || ''} />}
? <ReadabilityPageContent pageHtml={pageHtml || ''} />
: <IframePageContent pageContentUrl={pageContentUrl || ''} />}
</LoadingWrapper>
</div>
</main>
Expand Down

0 comments on commit 45e2b3e

Please sign in to comment.