From 655204a14faaa688ba467cf4ce7e5f2a92508eb4 Mon Sep 17 00:00:00 2001 From: Ou Date: Sun, 27 Oct 2024 22:09:17 +0800 Subject: [PATCH] chore: improve item.extra types --- pnpm-lock.yaml | 19 +++++++++++++------ server/sources/_36kr.ts | 4 ++-- server/sources/gelonghui.ts | 2 +- server/sources/ithome.ts | 6 ++---- server/sources/weibo.ts | 2 +- server/sources/zaobao.ts | 6 ++---- server/sources/zhihu.ts | 1 - server/utils/source.ts | 10 +++------- shared/types.ts | 10 +++++++++- src/components/column/card.tsx | 8 ++++---- 10 files changed, 37 insertions(+), 31 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1f5a727..ccea0df 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -209,7 +209,7 @@ importers: version: 0.20.5(vite@5.4.10(@types/node@22.7.9)(terser@5.36.0))(workbox-build@7.1.1(@types/babel__core@7.20.5))(workbox-window@7.1.0) vite-plugin-with-nitro: specifier: 0.0.3 - version: 0.0.3(better-sqlite3@11.5.0)(typescript@5.6.3)(vite@5.4.10(@types/node@22.7.9)(terser@5.36.0)) + version: 0.0.3(better-sqlite3@11.5.0)(idb-keyval@6.2.1)(typescript@5.6.3)(vite@5.4.10(@types/node@22.7.9)(terser@5.36.0)) vite-tsconfig-paths: specifier: ^5.0.1 version: 5.0.1(typescript@5.6.3)(vite@5.4.10(@types/node@22.7.9)(terser@5.36.0)) @@ -3751,6 +3751,9 @@ packages: resolution: {integrity: sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==} engines: {node: '>=0.10.0'} + idb-keyval@6.2.1: + resolution: {integrity: sha512-8Sb3veuYCyrZL+VBt9LJfZjLUPWVvqn8tG28VqYNFCo43KHcKuq+b4EiXGeuaLAQWL2YmyDgMp2aSpH9JHsEQg==} + idb@7.1.1: resolution: {integrity: sha512-gchesWBzyvGHRO9W8tzUWFDycow5gwjvFKfyV9FF32Y7F50yZMp7mP+T2mJIWFx49zicqyC4uefHM17o6xKIVQ==} @@ -9870,6 +9873,9 @@ snapshots: dependencies: safer-buffer: 2.1.2 + idb-keyval@6.2.1: + optional: true + idb@7.1.1: {} ieee754@1.2.1: {} @@ -10461,7 +10467,7 @@ snapshots: natural-compare@1.4.0: {} - nitro-go@0.0.1(better-sqlite3@11.5.0)(typescript@5.6.3): + nitro-go@0.0.1(better-sqlite3@11.5.0)(idb-keyval@6.2.1)(typescript@5.6.3): dependencies: '@cloudflare/kv-asset-handler': 0.3.4 '@netlify/functions': 2.8.2 @@ -10529,7 +10535,7 @@ snapshots: unctx: 2.3.1 unenv: 1.10.0 unimport: 3.13.1(rollup@4.24.0) - unstorage: 1.12.0(ioredis@5.4.1) + unstorage: 1.12.0(idb-keyval@6.2.1)(ioredis@5.4.1) untyped: 1.5.1 unwasm: 0.3.9 transitivePeerDependencies: @@ -11814,7 +11820,7 @@ snapshots: acorn: 8.13.0 webpack-virtual-modules: 0.6.2 - unstorage@1.12.0(ioredis@5.4.1): + unstorage@1.12.0(idb-keyval@6.2.1)(ioredis@5.4.1): dependencies: anymatch: 3.1.3 chokidar: 3.6.0 @@ -11827,6 +11833,7 @@ snapshots: ofetch: 1.4.1 ufo: 1.5.4 optionalDependencies: + idb-keyval: 6.2.1 ioredis: 5.4.1 untun@0.1.3: @@ -11915,12 +11922,12 @@ snapshots: transitivePeerDependencies: - supports-color - vite-plugin-with-nitro@0.0.3(better-sqlite3@11.5.0)(typescript@5.6.3)(vite@5.4.10(@types/node@22.7.9)(terser@5.36.0)): + vite-plugin-with-nitro@0.0.3(better-sqlite3@11.5.0)(idb-keyval@6.2.1)(typescript@5.6.3)(vite@5.4.10(@types/node@22.7.9)(terser@5.36.0)): dependencies: consola: 3.2.3 defu: 6.1.4 h3: h3-nightly@1.13.1-20241013-011028-3bf8af0 - nitropack: nitro-go@0.0.1(better-sqlite3@11.5.0)(typescript@5.6.3) + nitropack: nitro-go@0.0.1(better-sqlite3@11.5.0)(idb-keyval@6.2.1)(typescript@5.6.3) vite: 5.4.10(@types/node@22.7.9)(terser@5.36.0) transitivePeerDependencies: - '@azure/app-configuration' diff --git a/server/sources/_36kr.ts b/server/sources/_36kr.ts index 3591f60..70b30eb 100644 --- a/server/sources/_36kr.ts +++ b/server/sources/_36kr.ts @@ -13,14 +13,14 @@ const quick = defineSource(async () => { const $a = $el.find("a.item-title") const url = $a.attr("href") const title = $a.text() - const relativeDate = $el.find(".time") + const relativeDate = $el.find(".time").text() if (url && title && relativeDate) { news.push({ url: `${baseURL}${url}`, title, id: url, extra: { - date: parseRelativeDate(relativeDate.text()), + date: parseRelativeDate(relativeDate, "Asia/Shanghai").valueOf(), }, }) } diff --git a/server/sources/gelonghui.ts b/server/sources/gelonghui.ts index 9469189..a55b8a7 100644 --- a/server/sources/gelonghui.ts +++ b/server/sources/gelonghui.ts @@ -21,7 +21,7 @@ export default defineSource(async () => { title, id: url, extra: { - date: parseRelativeDate(relatieveTime, "Asia/Shanghai"), + date: parseRelativeDate(relatieveTime, "Asia/Shanghai").valueOf(), info, }, }) diff --git a/server/sources/ithome.ts b/server/sources/ithome.ts index 951b0d5..807b1b7 100644 --- a/server/sources/ithome.ts +++ b/server/sources/ithome.ts @@ -19,12 +19,10 @@ export default defineSource(async () => { url, title, id: url, - extra: { - date: tranformToUTC(date), - }, + pubDate: parseRelativeDate(date, "Asia/Shanghai").valueOf(), }) } } }) - return news.sort((m, n) => n.extra!.date > m.extra!.date ? 1 : -1) + return news.sort((m, n) => n.pubDate! > m.pubDate! ? 1 : -1) }) diff --git a/server/sources/weibo.ts b/server/sources/weibo.ts index 128f2d7..e20e861 100644 --- a/server/sources/weibo.ts +++ b/server/sources/weibo.ts @@ -33,7 +33,7 @@ export default defineSource(async () => { .map((k) => { const keyword = k.word_scheme ? k.word_scheme : `#${k.word}#` return { - id: k.num, + id: k.word, title: k.word, extra: { icon: k.icon && { diff --git a/server/sources/zaobao.ts b/server/sources/zaobao.ts index f0531d2..8cab0b5 100644 --- a/server/sources/zaobao.ts +++ b/server/sources/zaobao.ts @@ -24,11 +24,9 @@ export default defineSource(async () => { url: base + url, title, id: url, - extra: { - date: tranformToUTC(date), - }, + pubDate: parseRelativeDate(date, "Asia/Shanghai").valueOf(), }) } }) - return news.sort((m, n) => n.extra!.date > m.extra!.date ? 1 : -1) + return news.sort((m, n) => n.pubDate! > m.pubDate! ? 1 : -1) }) diff --git a/server/sources/zhihu.ts b/server/sources/zhihu.ts index da8be1e..9953cee 100644 --- a/server/sources/zhihu.ts +++ b/server/sources/zhihu.ts @@ -24,7 +24,6 @@ export default defineSource({ const url = "https://www.zhihu.com/api/v3/feed/topstory/hot-lists/total?limit=20&desktop=true" const res: Res = await $fetch(url) return res.data - .slice(0, 30) .map((k) => { return { id: k.target.id, diff --git a/server/utils/source.ts b/server/utils/source.ts index e4ca770..490584e 100644 --- a/server/utils/source.ts +++ b/server/utils/source.ts @@ -10,14 +10,12 @@ export function defineSource(source: T): T { export function defineRSSSource(url: string, option?: SourceOption): SourceGetter { return async () => { const data = await rss2json(url) - if (!data?.items.length) throw new Error("Cannot fetch data") + if (!data?.items.length) throw new Error("Cannot fetch rss data") return data.items.map(item => ({ title: item.title, url: item.link, id: item.link, - extra: { - date: !option?.hiddenDate && item.created, - }, + pubDate: !option?.hiddenDate ? item.created : undefined, })) } } @@ -40,9 +38,7 @@ export function defineRSSHubSource(route: string, RSSHubOptions?: RSSHubOption, title: item.title, url: item.url, id: item.id ?? item.url, - extra: { - date: !sourceOption?.hiddenDate && item.date_published, - }, + pubDate: !sourceOption?.hiddenDate ? item.date_published : undefined, })) } } diff --git a/shared/types.ts b/shared/types.ts index d813b9b..2f0e877 100644 --- a/shared/types.ts +++ b/shared/types.ts @@ -89,7 +89,15 @@ export interface NewsItem { url: string mobileUrl?: string pubDate?: number | string - extra?: Record + extra?: { + hover?: string + date?: number | string + info?: false | string + icon?: false | string | { + url: string + scale: number + } + } } export interface SourceResponse { diff --git a/src/components/column/card.tsx b/src/components/column/card.tsx index 0f2af41..a051405 100644 --- a/src/components/column/card.tsx +++ b/src/components/column/card.tsx @@ -167,10 +167,10 @@ function ExtraInfo({ item }: { item: NewsItem }) { return <>{item.extra.info} } if (item?.extra?.icon) { - const { url, scale } = item.extra.icon + const { url, scale } = typeof item.extra.icon === "string" ? { url: item.extra.icon, scale: undefined } : item.extra.icon return ( {relativeTime} } @@ -226,7 +226,7 @@ function NewsListTimeLine({ items }: { items: NewsItem[] }) { - - {(item.pubDate || item.extra?.date) && } + {(item.pubDate || item?.extra?.date) && }