Skip to content

Commit

Permalink
Merge branch 'save-check' into preview
Browse files Browse the repository at this point in the history
  • Loading branch information
banzhe committed Jan 16, 2025
2 parents a2cda60 + 0719d4d commit 1bcadcd
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/plugin/assets/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@
"open-showcase-page": "Open Showcase Page",
"open-home-page": "Open Home Page",
"save-page-not-available": "Save Page (Not Available)",
"language": "Language:"
"language": "Language:",
"loading-archived-list": "Loading Archived List",
"no-archived-pages": "No Archived Pages"
}
4 changes: 3 additions & 1 deletion packages/plugin/assets/locales/zh-CN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@
"open-showcase-page": "打开 Showcase 页面",
"open-home-page": "打开首页",
"save-page-not-available": "保存页面(不可用)",
"language": "语言: "
"language": "语言: ",
"loading-archived-list": "加载已存档列表中",
"no-archived-pages": "没有存档页面"
}
13 changes: 13 additions & 0 deletions packages/plugin/background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,16 @@ onMessage('generate-tag', async ({ data: { title, pageDesc, tagLanguage, preferr
tags,
}
})

onMessage('query-by-url', async ({ data: { pageUrl } }) => {
const pages = await request('/pages/query_by_url', {
method: 'POST',
body: JSON.stringify({ pageUrl }),
headers: {
'Content-Type': 'application/json',
},
})
return {
pages,
}
})
8 changes: 8 additions & 0 deletions packages/plugin/popup/components/PluginHomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isNil } from '@web-archive/shared/utils'
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@web-archive/shared/components/tooltip'
import { useTranslation } from 'react-i18next'
import { getCurrentTab } from '../utils/tab'
import SavedPageList from './SavedPageList'
import { ThemeToggle } from '~/popup/components/ThemeToggle'
import type { PageType } from '~/popup/PopupPage'

Expand Down Expand Up @@ -124,6 +125,13 @@ function PluginHomePage({ setActivePage }: PluginHomePageProps) {
!saveAvailabel ? t('save-page-not-available') : t('save-page')
}
</Button>
{
saveAvailabel && (
<SavedPageList>
</SavedPageList>
)
}

</div>
)
}
Expand Down
75 changes: 75 additions & 0 deletions packages/plugin/popup/components/SavedPageList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { isNil } from '@web-archive/shared/utils'
import { sendMessage } from 'webext-bridge/popup'
import { useRequest } from 'ahooks'
import { LoaderCircle } from 'lucide-react'
import { useTranslation } from 'react-i18next'
import { getCurrentTab } from '../utils/tab'

async function getSavedPages() {
const currentTab = await getCurrentTab()
if (isNil(currentTab?.id) || isNil(currentTab.url)) {
return []
}

const { pages } = await sendMessage('query-by-url', {
pageUrl: currentTab.url,
})

return pages
}

function formatTime(date: string) {
const timestamp = Date.parse(`${date}Z`)
const dateObj = new Date(timestamp)
return dateObj.toLocaleString()
}

function SavedPageList() {
const { t } = useTranslation()
const { data: savedPages, loading } = useRequest(getSavedPages)

async function handlePageClick(pageId: number) {
const { serverUrl } = await sendMessage('get-server-url', {})
window.open(`${serverUrl}/#/page/${pageId}`, '_blank')
}

if (loading) {
return (
<div className="w-full h-8 bg-secondary rounded flex items-center justify-center">
<LoaderCircle size={14} className="animate-spin mr-2"></LoaderCircle>
<div>{t('loading-archived-list')}</div>
</div>
)
}

if (!savedPages?.length) {
return (
<div className="w-full h-8 bg-secondary rounded flex items-center justify-center">
<div className="text-secondary-foreground select-none">{t('no-archived-pages')}</div>
</div>
)
}

return (
<div>
<div className="w-full max-h-32 overflow-auto rounded space-y-2 custom-scrollbar pr-[4px] mr-[-4px]">
{
savedPages?.map((page) => {
return (
<div
key={page.id}
className="flex justify-between bg-primary/20 rounded p-2 select-none cursor-pointer"
onClick={() => handlePageClick(page.id)}
>
<div className="flex-1">{page.title}</div>
<div>{formatTime(page.createdAt)}</div>
</div>
)
})
}
</div>
</div>
)
}

export default SavedPageList
9 changes: 8 additions & 1 deletion packages/plugin/shim.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ProtocolWithReturn } from 'webext-bridge'
import type { AITagConfig, Tag } from '@web-archive/shared/types'
import type { AITagConfig, Page, Tag } from '@web-archive/shared/types'
import type { SeriableSingleFileTask } from './background/processor'
import type { LoadStage, SingleFileSetting } from '~/utils/singleFile'

Expand Down Expand Up @@ -41,5 +41,12 @@ declare module 'webext-bridge' {
'scrape-available': ProtocolWithReturn<{ tabId: number }, { available: boolean }>
'get-ai-tag-config': ProtocolWithReturn<{}, { aiTagConfig: AITagConfig }>
'generate-tag': ProtocolWithReturn<GenerateTagProps, { tags: string[] }>
'query-by-url': ProtocolWithReturn<{ pageUrl: string }, {
pages: Array<{
id: number
title: string
createdAt: string
}>
}>
}
}
30 changes: 29 additions & 1 deletion packages/server/src/api/pages.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Hono } from 'hono'
import { validator } from 'hono/validator'
import { isNil, isNotNil, isNumberString } from '@web-archive/shared/utils'
import { z } from 'zod'
import type { HonoTypeUserInformation } from '~/constants/binding'
import result from '~/utils/result'
import { clearDeletedPage, deletePageById, getPageById, insertPage, queryDeletedPage, queryPage, queryRecentSavePage, restorePage, selectPageTotalCount, updatePage } from '~/model/page'
import { clearDeletedPage, deletePageById, getPageById, insertPage, queryDeletedPage, queryPage, queryPageByUrl, queryRecentSavePage, restorePage, selectPageTotalCount, updatePage } from '~/model/page'
import { getFolderById, restoreFolder } from '~/model/folder'
import { getFileFromBucket, saveFileToBucket } from '~/utils/file'
import { updateShowcase } from '~/model/showcase'
Expand Down Expand Up @@ -130,6 +131,33 @@ app.post(
},
)

app.post(
'/query_by_url',
validator('json', (value, c) => {
const errorMsg = {
message: 'Page URL is required',
}
const schema = z.object({
pageUrl: z.string(errorMsg).min(1, errorMsg),
})

const parsed = schema.safeParse(value)
if (!parsed.success) {
if (parsed.error.errors.length > 0) {
return c.json(result.error(400, parsed.error.errors[0].message))
}
return c.json(result.error(400, 'Invalid request'))
}

return parsed.data
}),
async (c) => {
const { pageUrl } = c.req.valid('json')
const pages = await queryPageByUrl(c.env.DB, pageUrl)
return c.json(result.success(pages))
},
)

app.get('/recent_save', async (c) => {
const pages = await queryRecentSavePage(c.env.DB)
return c.json(result.success(pages))
Expand Down
7 changes: 7 additions & 0 deletions packages/server/src/model/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ async function queryPage(DB: D1Database, options: { folderId?: number, pageNumbe
return sqlResult.results
}

async function queryPageByUrl(DB: D1Database, pageUrl: string) {
const sql = `SELECT * FROM pages WHERE pageUrl = ? AND isDeleted = 0`
const result = await DB.prepare(sql).bind(pageUrl).all<Page>()
return result.results
}

async function selectDeletedPageTotalCount(DB: D1Database) {
const sql = `
SELECT COUNT(*) as count FROM pages
Expand Down Expand Up @@ -240,6 +246,7 @@ async function updatePage(DB: D1Database, options: UpdatePageOptions) {
export {
selectPageTotalCount,
queryPage,
queryPageByUrl,
selectDeletedPageTotalCount,
queryDeletedPage,
deletePageById,
Expand Down

0 comments on commit 1bcadcd

Please sign in to comment.