File size: 14,947 Bytes
47da283 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
import {
Avatar,
Button,
Divider,
Listbox,
ListboxItem,
ListboxSection,
Modal,
ModalBody,
ModalContent,
ModalFooter,
ModalHeader,
Switch,
Textarea,
Tooltip,
useDisclosure,
Link,
} from '@nextui-org/react';
import { PlusIcon } from '@web/components/PlusIcon';
import { trpc } from '@web/utils/trpc';
import { useMemo, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { toast } from 'sonner';
import dayjs from 'dayjs';
import { serverOriginUrl } from '@web/utils/env';
import ArticleList from './list';
const Feeds = () => {
const { id } = useParams();
const { isOpen, onOpen, onOpenChange, onClose } = useDisclosure();
const { refetch: refetchFeedList, data: feedData } = trpc.feed.list.useQuery(
{},
{
refetchOnWindowFocus: true,
},
);
const navigate = useNavigate();
const queryUtils = trpc.useUtils();
const { mutateAsync: getMpInfo, isLoading: isGetMpInfoLoading } =
trpc.platform.getMpInfo.useMutation({});
const { mutateAsync: updateMpInfo } = trpc.feed.edit.useMutation({});
const { mutateAsync: addFeed, isLoading: isAddFeedLoading } =
trpc.feed.add.useMutation({});
const { mutateAsync: refreshMpArticles, isLoading: isGetArticlesLoading } =
trpc.feed.refreshArticles.useMutation();
const {
mutateAsync: getHistoryArticles,
isLoading: isGetHistoryArticlesLoading,
} = trpc.feed.getHistoryArticles.useMutation();
const { data: inProgressHistoryMp, refetch: refetchInProgressHistoryMp } =
trpc.feed.getInProgressHistoryMp.useQuery(undefined, {
refetchOnWindowFocus: true,
refetchInterval: 10 * 1e3,
refetchOnMount: true,
refetchOnReconnect: true,
});
const { data: isRefreshAllMpArticlesRunning } =
trpc.feed.isRefreshAllMpArticlesRunning.useQuery();
const { mutateAsync: deleteFeed, isLoading: isDeleteFeedLoading } =
trpc.feed.delete.useMutation({});
const [wxsLink, setWxsLink] = useState('');
const [currentMpId, setCurrentMpId] = useState(id || '');
const handleConfirm = async () => {
console.log('wxsLink', wxsLink);
// TODO show operation in progress
const wxsLinks = wxsLink.split('\n').filter((link) => link.trim() !== '');
for (const link of wxsLinks) {
console.log('add wxsLink', link);
const res = await getMpInfo({ wxsLink: link });
if (res[0]) {
const item = res[0];
await addFeed({
id: item.id,
mpName: item.name,
mpCover: item.cover,
mpIntro: item.intro,
updateTime: item.updateTime,
status: 1,
});
await refreshMpArticles({ mpId: item.id });
toast.success('添加成功', {
description: `公众号 ${item.name}`,
});
await queryUtils.article.list.reset();
} else {
toast.error('添加失败', { description: '请检查链接是否正确' });
}
}
refetchFeedList();
setWxsLink('');
onClose();
};
const isActive = (key: string) => {
return currentMpId === key;
};
const currentMpInfo = useMemo(() => {
return feedData?.items.find((item) => item.id === currentMpId);
}, [currentMpId, feedData?.items]);
const handleExportOpml = async (ev) => {
ev.preventDefault();
ev.stopPropagation();
if (!feedData?.items?.length) {
console.warn('没有订阅源');
return;
}
let opmlContent = `<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
<head>
<title>WeWeRSS 所有订阅源</title>
</head>
<body>
`;
feedData?.items.forEach((sub) => {
opmlContent += ` <outline text="${sub.mpName}" type="rss" xmlUrl="${window.location.origin}/feeds/${sub.id}.atom" htmlUrl="${window.location.origin}/feeds/${sub.id}.atom"/>\n`;
});
opmlContent += ` </body>
</opml>`;
const blob = new Blob([opmlContent], { type: 'text/xml;charset=utf-8;' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'WeWeRSS-All.opml';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
return (
<>
<div className="h-full flex justify-between">
<div className="w-64 p-4 h-full">
<div className="pb-4 flex justify-between align-middle items-center">
<Button
color="primary"
size="sm"
onPress={onOpen}
endContent={<PlusIcon />}
>
添加
</Button>
<div className="font-normal text-sm">
共{feedData?.items.length || 0}个订阅
</div>
</div>
{feedData?.items ? (
<Listbox
aria-label="订阅源"
emptyContent="暂无订阅"
onAction={(key) => setCurrentMpId(key as string)}
>
<ListboxSection showDivider>
<ListboxItem
key={''}
href={`/feeds`}
className={isActive('') ? 'bg-primary-50 text-primary' : ''}
startContent={<Avatar name="ALL"></Avatar>}
>
全部
</ListboxItem>
</ListboxSection>
<ListboxSection className="overflow-y-auto h-[calc(100vh-260px)]">
{feedData?.items.map((item) => {
return (
<ListboxItem
href={`/feeds/${item.id}`}
className={
isActive(item.id) ? 'bg-primary-50 text-primary' : ''
}
key={item.id}
startContent={<Avatar src={item.mpCover}></Avatar>}
>
{item.mpName}
</ListboxItem>
);
}) || []}
</ListboxSection>
</Listbox>
) : (
''
)}
</div>
<div className="flex-1 h-full flex flex-col">
<div className="p-4 pb-0 flex justify-between">
<h3 className="text-medium font-mono flex-1 overflow-hidden text-ellipsis break-keep text-nowrap pr-1">
{currentMpInfo?.mpName || '全部'}
</h3>
{currentMpInfo ? (
<div className="flex h-5 items-center space-x-4 text-small">
<div className="font-light">
最后更新时间:
{dayjs(currentMpInfo.syncTime * 1e3).format(
'YYYY-MM-DD HH:mm:ss',
)}
</div>
<Divider orientation="vertical" />
<Tooltip
content="频繁调用可能会导致一段时间内不可用"
color="danger"
>
<Link
size="sm"
href="#"
isDisabled={isGetArticlesLoading}
onClick={async (ev) => {
ev.preventDefault();
ev.stopPropagation();
await refreshMpArticles({ mpId: currentMpInfo.id });
await refetchFeedList();
await queryUtils.article.list.reset();
}}
>
{isGetArticlesLoading ? '更新中...' : '立即更新'}
</Link>
</Tooltip>
<Divider orientation="vertical" />
{currentMpInfo.hasHistory === 1 && (
<>
<Tooltip
content={
inProgressHistoryMp?.id === currentMpInfo.id
? `正在获取第${inProgressHistoryMp.page}页...`
: `历史文章需要分批次拉取,请耐心等候,频繁调用可能会导致一段时间内不可用`
}
color={
inProgressHistoryMp?.id === currentMpInfo.id
? 'primary'
: 'danger'
}
>
<Link
size="sm"
href="#"
isDisabled={
(inProgressHistoryMp?.id
? inProgressHistoryMp?.id !== currentMpInfo.id
: false) ||
isGetHistoryArticlesLoading ||
isGetArticlesLoading
}
onClick={async (ev) => {
ev.preventDefault();
ev.stopPropagation();
if (inProgressHistoryMp?.id === currentMpInfo.id) {
await getHistoryArticles({
mpId: '',
});
} else {
await getHistoryArticles({
mpId: currentMpInfo.id,
});
}
await refetchInProgressHistoryMp();
}}
>
{inProgressHistoryMp?.id === currentMpInfo.id
? `停止获取历史文章`
: `获取历史文章`}
</Link>
</Tooltip>
<Divider orientation="vertical" />
</>
)}
<Tooltip content="启用服务端定时更新">
<div>
<Switch
size="sm"
onValueChange={async (value) => {
await updateMpInfo({
id: currentMpInfo.id,
data: {
status: value ? 1 : 0,
},
});
await refetchFeedList();
}}
isSelected={currentMpInfo?.status === 1}
></Switch>
</div>
</Tooltip>
<Divider orientation="vertical" />
<Tooltip content="仅删除订阅源,已获取的文章不会被删除">
<Link
href="#"
color="danger"
size="sm"
isDisabled={isDeleteFeedLoading}
onClick={async (ev) => {
ev.preventDefault();
ev.stopPropagation();
if (window.confirm('确定删除吗?')) {
await deleteFeed(currentMpInfo.id);
navigate('/feeds');
await refetchFeedList();
}
}}
>
删除
</Link>
</Tooltip>
<Divider orientation="vertical" />
<Tooltip
content={
<div>
可添加.atom/.rss/.json格式输出, limit=20&page=1控制分页
</div>
}
>
<Link
size="sm"
showAnchorIcon
target="_blank"
href={`${serverOriginUrl}/feeds/${currentMpInfo.id}.atom`}
color="foreground"
>
RSS
</Link>
</Tooltip>
</div>
) : (
<div className="flex gap-2">
<Tooltip
content="频繁调用可能会导致一段时间内不可用"
color="danger"
>
<Link
size="sm"
href="#"
isDisabled={
isRefreshAllMpArticlesRunning || isGetArticlesLoading
}
onClick={async (ev) => {
ev.preventDefault();
ev.stopPropagation();
await refreshMpArticles({});
await refetchFeedList();
await queryUtils.article.list.reset();
}}
>
{isRefreshAllMpArticlesRunning || isGetArticlesLoading
? '更新中...'
: '更新全部'}
</Link>
</Tooltip>
<Link
href="#"
color="foreground"
onClick={handleExportOpml}
size="sm"
>
导出OPML
</Link>
<Divider orientation="vertical" />
<Link
size="sm"
showAnchorIcon
target="_blank"
href={`${serverOriginUrl}/feeds/all.atom`}
color="foreground"
>
RSS
</Link>
</div>
)}
</div>
<div className="p-2 overflow-y-auto">
<ArticleList></ArticleList>
</div>
</div>
</div>
<Modal isOpen={isOpen} onOpenChange={onOpenChange}>
<ModalContent>
{(onClose) => (
<>
<ModalHeader className="flex flex-col gap-1">
添加公众号源
</ModalHeader>
<ModalBody>
<Textarea
value={wxsLink}
onValueChange={setWxsLink}
autoFocus
label="分享链接"
placeholder="输入公众号文章分享链接,一行一条,如 https://mp.weixin.qq.com/s/xxxxxx https://mp.weixin.qq.com/s/xxxxxx"
variant="bordered"
/>
</ModalBody>
<ModalFooter>
<Button color="danger" variant="flat" onPress={onClose}>
取消
</Button>
<Button
color="primary"
isDisabled={
!wxsLink.startsWith('https://mp.weixin.qq.com/s/')
}
onPress={handleConfirm}
isLoading={
isAddFeedLoading ||
isGetMpInfoLoading ||
isGetArticlesLoading
}
>
确定
</Button>
</ModalFooter>
</>
)}
</ModalContent>
</Modal>
</>
);
};
export default Feeds;
|