Skip to content

Commit

Permalink
feat: preview page
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Dec 23, 2023
1 parent ad8bb11 commit c31e21d
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 2 deletions.
3 changes: 3 additions & 0 deletions assets/locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,8 @@
},
"remoteRulesUrl": {
"message": "Remote radar rules URL"
},
"preview": {
"message": "Preview"
}
}
3 changes: 3 additions & 0 deletions assets/locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,8 @@
},
"remoteRulesUrl": {
"message": "远程 Radar Rules 地址"
},
"preview": {
"message": "预览"
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
"react-hot-toast": "^2.4.1",
"react-router-dom": "^6.21.1",
"route-recognizer": "^0.3.4",
"rss-parser": "3.13.0",
"tailwind-merge": "^2.2.0",
"tailwindcss-animate": "^1.0.7",
"usehooks-ts": "^2.9.1"
"usehooks-ts": "^2.9.1",
"xss": "1.0.14"
},
"devDependencies": {
"@egoist/tailwindcss-icons": "^1.7.1",
Expand Down
44 changes: 43 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/popup/RSSItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import { quickSubscriptions } from "~/lib/quick-subscriptions"
function RSSItem({
item,
type,
hidePreview,
}: {
item: RSSData
type: string
hidePreview?: boolean
}) {
const [config, setConfig] = useState(defaultConfig)
getConfig().then(setConfig)
Expand Down Expand Up @@ -61,6 +63,16 @@ function RSSItem({
{chrome.i18n.getMessage(copied ? "copied" : "copy")}
</Button>
)}
{!item.isDocs && !hidePreview && (
<Button variant="rss" size="sm" className="border-[#0ea5e9] text-[#0ea5e9] hover:bg-[#0ea5e9]">
<a
target="_blank"
href={`/tabs/preview.html?url=${encodedUrl}`}
>
{chrome.i18n.getMessage("preview")}
</a>
</Button>
)}
{quickSubscriptions.map((quickSubscription) => {
if (item.isDocs
|| !config.submitto[quickSubscription.key]
Expand Down
102 changes: 102 additions & 0 deletions src/tabs/preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { useEffect, useState } from "react";
import Parser from "rss-parser";
import "~/lib/style.css";
import RSSHubIcon from "data-base64:~/assets/icon.png"
import xss from "xss";
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "~/lib/components/Card"
import RSSItem from "~/popup/RSSItem";

const parser = new Parser();

function PreviewPage() {
const url = new URLSearchParams(window.location.search).get("url");

const [parsed, setParsed] = useState<Parser.Output<{
[key: string]: any;
}> | undefined>();
const [error, setError] = useState<Event>();

useEffect(() => {
fetch(url, {
mode: "no-cors",
}).then(res => {
res.text().then(text => {
parser.parseString(text).then(result => {
setParsed(result);
}).catch((e) => {
setError(e)
})
}).catch((e) => {
setError(e)
})
}).catch((e) => {
setError(e)
})
}, [])
console.log("error", error)

return (
<div className="max-w-screen-lg mx-auto py-16 text-base space-y-8">
<div className="flex">
{parsed?.image?.url && (
<div className="w-24 h-24 overflow-hidden rounded-xl mr-8 object-cover">
<img className="object-cover" src={parsed?.image?.url || RSSHubIcon} />
</div>
)}
<div className="space-y-2 flex-1">
<h1 className="text-3xl font-bold text-primary">{parsed?.title}</h1>
<div className="text-zinc-600">{parsed?.description}</div>
<div className="text-zinc-600">
<a className="underline" href={parsed?.link}>{parsed?.link}</a>
</div>
</div>
</div>
<div className="w-fit flex items-center justify-center">
<RSSItem item={{
title: "",
url,
image: RSSHubIcon,
}} type="currentPageRSS" hidePreview={true} />
</div>
<div className="space-y-8">
{parsed?.items?.map((item, index) => (
<Card key={index}>
<CardHeader>
<CardTitle><a href={item.link}>{item.title}</a></CardTitle>
<CardDescription>{item.pubDate}</CardDescription>
</CardHeader>
<CardContent>
<div className="text-zinc-600 text-sm max-h-96 overflow-y-auto [&_img]:max-h-60 [&_img]:m-auto [&_p]:my-2" dangerouslySetInnerHTML={{
__html: xss(item["content:encoded"] || item["content"])
}}></div>
</CardContent>
<CardFooter>
<div className="text-zinc-400">
Source: <a className="underline break-all" href={item.link}>{item.link}</a>
</div>
</CardFooter>
</Card>
))}
</div>
{error && (
<div className="text-red-600">
Error: {error.toString()}
</div>
)}
{!parsed && (
<div className="text-zinc-400">
Loading...
</div>
)}
</div>
);
}

export default PreviewPage;

0 comments on commit c31e21d

Please sign in to comment.