Skip to content

Commit

Permalink
Merge pull request #47 from Ray-D-Song/optimize
Browse files Browse the repository at this point in the history
plugin optimize and add configuration for showing rencent saved page
  • Loading branch information
Ray-D-Song authored Nov 14, 2024
2 parents fd01a7e + 5d02a5c commit 109a8c0
Show file tree
Hide file tree
Showing 23 changed files with 472 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/preview-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- main
- hotfix
- preview
- read_mode
- optimize

jobs:
deploy:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web-archive",
"version": "0.1.0",
"version": "0.1.1-alpha.1",
"packageManager": "[email protected]+sha512.f549b8a52c9d2b8536762f99c0722205efc5af913e77835dbccc3b0b0b2ca9e7dc8022b78062c17291c48e88749c70ce88eb5a74f1fa8c4bf5e18bb46c8bd83a",
"description": "Self-host your own web archive, powered by Cloudflare Workers and React.",
"author": "Ray-D-Song & banzhe",
Expand Down
16 changes: 16 additions & 0 deletions packages/plugin/background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ onMessage('get-all-folders', async () => {
}
})

onMessage('create-folder', async ({ data: { name } }) => {
try {
const folder = await request('/folders/create', {
method: 'POST',
body: JSON.stringify({ name }),
headers: {
'Content-Type': 'application/json',
},
})
return folder
}
catch (e) {
return undefined
}
})

onMessage('get-all-tags', async () => {
const tags = await request('/tags/all', {
method: 'GET',
Expand Down
10 changes: 7 additions & 3 deletions packages/plugin/background/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface SeriableSingleFileTask {
pageDesc: string
folderId: string
bindTags: string[]
isShowcased: boolean
startTimeStamp: number
endTimeStamp?: number
errorMessage?: string
Expand Down Expand Up @@ -70,6 +71,7 @@ type CreateTaskOptions = {
folderId: string
screenshot?: string
bindTags: string[]
isShowcased: boolean
}
singleFileSetting: SingleFileSetting
}
Expand All @@ -84,7 +86,7 @@ async function scrapePageData(singleFileSetting: SingleFileSetting, tabId: numbe
}

async function uploadPageData(pageForm: CreateTaskOptions['pageForm'] & { content: string }) {
const { href, title, pageDesc, folderId, screenshot, content } = pageForm
const { href, title, pageDesc, folderId, screenshot, content, isShowcased } = pageForm

const form = new FormData()
form.append('title', title)
Expand All @@ -93,6 +95,7 @@ async function uploadPageData(pageForm: CreateTaskOptions['pageForm'] & { conten
form.append('folderId', folderId)
form.append('bindTags', JSON.stringify(pageForm.bindTags))
form.append('pageFile', new Blob([content], { type: 'text/html' }))
form.append('isShowcased', isShowcased ? '1' : '0')
if (screenshot) {
form.append('screenshot', base64ToBlob(screenshot, 'image/webp'))
}
Expand All @@ -104,7 +107,7 @@ async function uploadPageData(pageForm: CreateTaskOptions['pageForm'] & { conten

async function createAndRunTask(options: CreateTaskOptions) {
const { singleFileSetting, tabId, pageForm } = options
const { href, title, pageDesc, folderId, screenshot, bindTags } = pageForm
const { href, title, pageDesc, folderId, screenshot, bindTags, isShowcased } = pageForm

const uuid = crypto.randomUUID()
const task: SeriableSingleFileTask = {
Expand All @@ -117,6 +120,7 @@ async function createAndRunTask(options: CreateTaskOptions) {
pageDesc,
folderId,
bindTags,
isShowcased,
startTimeStamp: Date.now(),
}

Expand All @@ -129,7 +133,7 @@ async function createAndRunTask(options: CreateTaskOptions) {
task.status = 'uploading'
await saveTaskList()

await uploadPageData({ content, href, title, pageDesc, folderId, screenshot, bindTags })
await uploadPageData({ content, href, title, pageDesc, folderId, screenshot, bindTags, isShowcased })
task.status = 'done'
task.endTimeStamp = Date.now()
await saveTaskList()
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"128": "assets/icon.png"
},
"description": "SingleFile with categories and exhibition pages",
"version": "0.1.0",
"version": "0.1.1-alpha.1",
"manifest_version": 3,
"action": {
"default_icon": "assets/icon.png",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "plugin",
"type": "module",
"version": "0.1.0",
"version": "0.1.1-alpha.1",
"private": true,
"description": "",
"author": "",
Expand Down
59 changes: 59 additions & 0 deletions packages/plugin/popup/components/NewFolderDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Dialog, DialogContent, DialogDescription, DialogTitle } from '@web-archive/shared/components/dialog'
import { Button } from '@web-archive/shared/components/button'
import { Input } from '@web-archive/shared/components/input'
import { useState } from 'react'
import toast from 'react-hot-toast'
import { useRequest } from 'ahooks'
import { sendMessage } from 'webext-bridge/popup'

interface NewFolderProps {
afterSubmit: (folder: { id: number, name: string }) => void
open: boolean
setOpen: (open: boolean) => void
}

async function createFolder(name: string): Promise<{ id: number, name: string }> {
const newFolder = await sendMessage('create-folder', { name })
if (!newFolder) {
throw new Error('Failed to create folder')
}
return newFolder
}

function NewFolderDialog({ afterSubmit, open, setOpen }: NewFolderProps) {
const [name, setName] = useState('')
const { run } = useRequest(
createFolder,
{
manual: true,
onSuccess: (folder) => {
toast.success('Folder created')
setOpen(false)
setName('')
afterSubmit(folder)
},
onError: (error) => {
toast.error(error.message)
},
},
)
const handleSubmit = () => {
if (name.length === 0) {
toast.error('Folder name is required')
return
}
run(name)
}
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="w-64">
<DialogTitle>Create New Folder</DialogTitle>
<DialogDescription></DialogDescription>
<Input value={name} onChange={e => setName(e.target.value)} placeholder="Folder Name" />
<Button onClick={handleSubmit}>Create</Button>
</DialogContent>
</Dialog>
)
}

export default NewFolderDialog
104 changes: 80 additions & 24 deletions packages/plugin/popup/components/PluginHomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Button } from '@web-archive/shared/components/button'
import { useRequest } from 'ahooks'
import { History, House, LogOut, Settings } from 'lucide-react'
import { History, House, LogOut, Settings, SquareLibrary } from 'lucide-react'
import { sendMessage } from 'webext-bridge/popup'
import { isNil } from '@web-archive/shared/utils'
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@web-archive/shared/components/tooltip'
import { getCurrentTab } from '../utils/tab'
import { ThemeToggle } from '~/popup/components/ThemeToggle'
import type { PageType } from '~/popup/PopupPage'
Expand All @@ -22,6 +23,11 @@ function PluginHomePage({ setActivePage }: PluginHomePageProps) {
window.open(serverUrl, '_blank')
}

async function openShowCasePage() {
const { serverUrl } = await sendMessage('get-server-url', {})
window.open(`${serverUrl}/#/showcase/folder`, '_blank')
}

const { data: saveAvailabel } = useRequest(async () => {
const currentTab = await getCurrentTab()
if (isNil(currentTab?.id)) {
Expand All @@ -33,30 +39,80 @@ function PluginHomePage({ setActivePage }: PluginHomePageProps) {

return (
<div className="w-80 space-y-2 p-4">
<div className="h-6 mb-2 items-center flex justify-between">
<div className="flex space-x-3">
<House
className="cursor-pointer"
onClick={openServerPage}
>
</House>
<ThemeToggle></ThemeToggle>
<Settings
className="cursor-pointer"
onClick={() => { setActivePage('setting') }}
>
</Settings>
<History
className="cursor-pointer"
onClick={() => { setActivePage('history') }}
/>
</div>
<TooltipProvider>
<div className="h-6 mb-2 items-center flex justify-between">
<div className="flex space-x-3">
<Tooltip>
<TooltipTrigger>
<House
className="cursor-pointer"
onClick={openServerPage}
>
</House>
</TooltipTrigger>
<TooltipContent>
<div className="text-sm">Open Home Page</div>
</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger>
<SquareLibrary
className="cursor-pointer"
onClick={openShowCasePage}
>
</SquareLibrary>
</TooltipTrigger>
<TooltipContent>
<div className="text-sm">Open Showcase Page</div>
</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger>
<ThemeToggle></ThemeToggle>
</TooltipTrigger>
<TooltipContent>
<div className="text-sm">Toggle Theme</div>
</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger>
<Settings
className="cursor-pointer"
onClick={() => { setActivePage('setting') }}
>
</Settings>
</TooltipTrigger>
<TooltipContent>
<div className="text-sm">Change Plugin Settings</div>
</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger>
<History
className="cursor-pointer"
onClick={() => { setActivePage('history') }}
/>
</TooltipTrigger>
<TooltipContent>
<div className="text-sm">View Save History</div>
</TooltipContent>
</Tooltip>

</div>

<LogOut
className="cursor-pointer"
onClick={logout}
/>
</div>
<Tooltip>
<TooltipTrigger>
<LogOut
className="cursor-pointer"
onClick={logout}
/>
</TooltipTrigger>
<TooltipContent>
<div className="text-sm">Logout</div>
</TooltipContent>
</Tooltip>
</div>
</TooltipProvider>
<Button
className="w-full select-none"
disabled={!saveAvailabel}
Expand Down
Loading

0 comments on commit 109a8c0

Please sign in to comment.