From ee53abda84b4dbdd0441334274a36d0bd2f414d6 Mon Sep 17 00:00:00 2001 From: therealsama <263038098+therealsama@users.noreply.github.com> Date: Fri, 27 Nov 2026 01:25:41 +0200 Subject: [PATCH 1/7] refactor: remove dead code and merge duplicated api helpers - Drop the unused Tooltip wrapper and its provider. - Merge the best/new/show/ask ranked-list fetchers into one fetchRankedStories with a single ids cache. - Use signal.throwIfAborted(), isHiringThread() and the story normalizer instead of inline copies. - Define the feed types once (FEED_TYPES) and reuse feedCache's isStale instead of a second 5-minute check. - Trim unused barrel re-exports and fix the two circular imports through components/index.ts. - Delete the unused CachedItem and CachedFeed types. --- src/App.tsx | 81 +++++----- src/api/hn.test.ts | 2 +- src/api/hn.ts | 212 ++++++++------------------- src/components/CommentDetail.tsx | 2 +- src/components/CommentSkeleton.tsx | 2 +- src/components/CommentsSection.tsx | 3 +- src/components/FullScreenComment.tsx | 2 +- src/components/FullScreenItem.tsx | 4 +- src/components/Header.tsx | 7 +- src/components/StoryCardSkeleton.tsx | 2 +- src/components/index.ts | 17 +-- src/components/ui/dropdown-menu.tsx | 5 +- src/components/ui/index.ts | 6 - src/components/ui/tooltip.tsx | 60 -------- src/config/feedTypes.ts | 4 +- src/hooks/usePrefetchSections.ts | 11 +- src/index.css | 2 +- src/test/test-utils.tsx | 22 +-- src/types.ts | 16 +- src/utils/deriveHeaderState.ts | 7 +- 20 files changed, 136 insertions(+), 331 deletions(-) delete mode 100644 src/components/ui/tooltip.tsx diff --git a/src/App.tsx b/src/App.tsx index e963c31..7839df8 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -4,7 +4,6 @@ import { ThemeProvider } from './context/ThemeContext'; import { ScrollContainerProvider } from './context/ScrollContainerContext'; import { useScrollContainer } from './hooks/useScrollContainer'; import { Header, ErrorBoundary, FullScreenCommentSkeletonPanel, StateView, NetworkStatusBar } from './components'; -import { TooltipProvider } from './components/ui'; import { StoryList } from './pages'; import { useCanSwipe } from './hooks/useCanSwipe'; import { useDocumentTitle } from './hooks/useDocumentTitle'; @@ -280,47 +279,45 @@ function LocationAwareErrorBoundary({ children }: { children: ReactNode }) { function App() { return ( - - - - - - - - - - - + + + + + + + + + ); } diff --git a/src/api/hn.test.ts b/src/api/hn.test.ts index d30dcd0..a97d001 100644 --- a/src/api/hn.test.ts +++ b/src/api/hn.test.ts @@ -1611,7 +1611,7 @@ describe('hn API utilities', () => { return firebaseItems.find(fb => fb.id === Number(id)) ?? null; }); - await expect(fetchCommentReplies(liveReply, controller.signal)).rejects.toThrow('Aborted'); + await expect(fetchCommentReplies(liveReply, controller.signal)).rejects.toMatchObject({ name: 'AbortError' }); expect(fetchSpy).not.toHaveBeenCalled(); }); diff --git a/src/api/hn.ts b/src/api/hn.ts index a179289..2748a16 100644 --- a/src/api/hn.ts +++ b/src/api/hn.ts @@ -20,37 +20,23 @@ export class CommentsError extends Error { } } -let bestStoriesCache: { ids: number[] | null; timestamp: number } = { ids: null, timestamp: 0 }; -let showStoriesCache: { ids: number[] | null; timestamp: number } = { ids: null, timestamp: 0 }; -let askStoriesCache: { ids: number[] | null; timestamp: number } = { ids: null, timestamp: 0 }; -let newStoriesCache: { ids: number[] | null; timestamp: number } = { ids: null, timestamp: 0 }; +const rankedIdsCache = new Map(); let storyCommentsCache: { storyId: number; comments: Promise; timestamp: number } | null = null; const CACHE_TTL = 5 * 60 * 1000; // 5 minutes /** @internal Testing helper to reset module-level caches between tests. */ export function __resetFetchCachesForTests() { - bestStoriesCache = { ids: null, timestamp: 0 }; - showStoriesCache = { ids: null, timestamp: 0 }; - askStoriesCache = { ids: null, timestamp: 0 }; - newStoriesCache = { ids: null, timestamp: 0 }; + rankedIdsCache.clear(); storyCommentsCache = null; threadDownloads.clear(); } +const SECONDS_PER_DAY = 86_400; + +// Unix time has no leap seconds, so every UTC day is exactly SECONDS_PER_DAY. function getDayRange(daysAgo = 0) { - const now = new Date(); - const startOfToday = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate())); - - const targetDay = new Date(startOfToday); - targetDay.setUTCDate(targetDay.getUTCDate() - daysAgo); - - const nextDay = new Date(targetDay); - nextDay.setUTCDate(nextDay.getUTCDate() + 1); - - return { - start: Math.floor(targetDay.getTime() / 1000), - end: Math.floor(nextDay.getTime() / 1000), - }; + const day = Math.floor(Date.now() / (SECONDS_PER_DAY * 1000)) - daysAgo; + return { start: day * SECONDS_PER_DAY, end: (day + 1) * SECONDS_PER_DAY }; } // Feed endpoints only return stories (polls among them). @@ -111,15 +97,7 @@ function normalizeFirebaseItem(fbItem: FirebaseItem): Item { // Firebase doesn't distinguish ask/show — they're all type='story'. // Only the Algolia path (via _tags) can set 'ask'/'show' sub-types. // A poll's options are separate items, read with its comments. - return { - ...base, - type: fbItem.type === 'poll' ? 'poll' : 'story', - title: fbItem.title ?? '', - url: fbItem.url, - text: fbItem.text, - points: fbItem.score ?? 0, - commentCount: fbItem.descendants ?? 0, - }; + return normalizeFirebaseToStoryItem(fbItem); } /** @@ -140,8 +118,7 @@ export async function fetchTopStories(limit = 20): Promise { if (stories.length >= limit) break; if (!item || item.deleted || item.dead) continue; if (item.type === 'job') continue; - const title = (item.title ?? '').toLowerCase(); - if (title.includes('who is hiring') || title.includes('who wants to be hired')) continue; + if (isHiringThread(item.title)) continue; stories.push(normalizeFirebaseToStoryItem(item)); } cursor += batchIds.length; @@ -170,12 +147,7 @@ export async function fetchFrontPageForDay(daysAgo = 1): Promise { if (hit.type === 'job' || hit._tags?.includes('job')) { return false; } - const title = (hit.title || '').toLowerCase(); - if (title.includes('who is hiring') || - title.includes('who wants to be hired')) { - return false; - } - return true; + return !isHiringThread(hit.title); }); const sorted = filtered.sort((a, b) => (b.points || 0) - (a.points || 0)); @@ -183,79 +155,14 @@ export async function fetchFrontPageForDay(daysAgo = 1): Promise { return sorted.map(normalizeAlgoliaHit); } -export async function fetchBestStories(offset = 0, limit = 30) { - const now = Date.now(); - - if (!bestStoriesCache.ids || (now - bestStoriesCache.timestamp) >= CACHE_TTL) { - bestStoriesCache = { ids: await hnSdk.readRankedIds('beststories'), timestamp: now }; - } - - const allIds = bestStoriesCache.ids ?? []; - const pageIds = allIds.slice(offset, offset + limit); - - if (pageIds.length === 0) { - return { stories: [] as StoryItem[], hasMore: false, nextOffset: offset }; - } - - const fetched = await Promise.all( - pageIds.map((id: number) => hnSdk.readItem(id).catch(() => null)) - ); - - // The whoishiring threads are type='story', not 'job', so the job filter - // alone lets them through when they rank among the best. - const stories = fetched - .filter((fb): fb is FirebaseItem => - fb != null && - !fb.deleted && - !fb.dead && - fb.type !== 'job' && - !isHiringThread(fb.title) - ) - .map(fb => normalizeFirebaseToStoryItem(fb)); - - return { - stories, - hasMore: offset + limit < allIds.length, - nextOffset: offset + limit, - }; +export function fetchBestStories(offset = 0, limit = 30) { + return fetchRankedStories('beststories', offset, { limit, excludeJobs: true, excludeHiring: true }); } // Newest feed: reverse-chronological submissions from HN `newstories` (≤500, -// HN's hard cap). Mirrors fetchBestStories' offset pagination, including the -// whoishiring filter. -export async function fetchNewStories(offset = 0, limit = 30) { - const now = Date.now(); - - if (!newStoriesCache.ids || (now - newStoriesCache.timestamp) >= CACHE_TTL) { - newStoriesCache = { ids: await hnSdk.readRankedIds('newstories'), timestamp: now }; - } - - const allIds = newStoriesCache.ids ?? []; - const pageIds = allIds.slice(offset, offset + limit); - - if (pageIds.length === 0) { - return { stories: [] as StoryItem[], hasMore: false, nextOffset: offset }; - } - - const fetched = await Promise.all( - pageIds.map((id: number) => hnSdk.readItem(id).catch(() => null)) - ); - - const stories = fetched - .filter((fb): fb is FirebaseItem => - fb != null && - !fb.deleted && - !fb.dead && - fb.type !== 'job' && - !isHiringThread(fb.title) - ) - .map(fb => normalizeFirebaseToStoryItem(fb)); - - return { - stories, - hasMore: offset + limit < allIds.length, - nextOffset: offset + limit, - }; +// HN's hard cap), filtered like the best feed. +export function fetchNewStories(offset = 0, limit = 30) { + return fetchRankedStories('newstories', offset, { limit, excludeJobs: true, excludeHiring: true }); } /** A poll option has no title or comments, so it gets a page of its own. */ @@ -277,21 +184,31 @@ function normalizeFirebaseToStoryItem(fb: FirebaseItem): StoryItem { }; } +async function readRankedIdsCached(list: string): Promise { + const now = Date.now(); + const cached = rankedIdsCache.get(list); + if (cached && now - cached.timestamp < CACHE_TTL) return cached.ids; + const ids = await hnSdk.readRankedIds(list); + rankedIdsCache.set(list, { ids, timestamp: now }); + return ids; +} + +interface RankedPageOptions { + limit: number; + /** Firebase can't tell ask/show from story, so the list sets it. Polls keep theirs. */ + type?: 'show' | 'ask'; + excludeJobs?: boolean; + // The whoishiring threads are type='story', not 'job', so the job filter + // alone lets them through. + excludeHiring?: boolean; +} + async function fetchRankedStories( list: string, - cache: { ids: number[] | null; timestamp: number }, - type: 'show' | 'ask', - offset = 0, - limit = 20, - excludeTitles?: string[], + offset: number, + { limit, type, excludeJobs = false, excludeHiring = false }: RankedPageOptions, ) { - const now = Date.now(); - if (!cache.ids || (now - cache.timestamp) >= CACHE_TTL) { - cache.ids = await hnSdk.readRankedIds(list); - cache.timestamp = now; - } - - const allIds = cache.ids ?? []; + const allIds = await readRankedIdsCached(list); const pageIds = allIds.slice(offset, offset + limit); if (pageIds.length === 0) { @@ -307,9 +224,13 @@ async function fetchRankedStories( fb != null && !fb.deleted && !fb.dead && - !excludeTitles?.some(t => (fb.title ?? '').toLowerCase().includes(t)) + !(excludeJobs && fb.type === 'job') && + !(excludeHiring && isHiringThread(fb.title)) ) - .map(fb => ({ ...normalizeFirebaseToStoryItem(fb), type: fb.type === 'poll' ? 'poll' as const : type })); + .map(fb => { + const story = normalizeFirebaseToStoryItem(fb); + return type && story.type !== 'poll' ? { ...story, type } : story; + }); return { stories, @@ -319,13 +240,13 @@ async function fetchRankedStories( } export function fetchShowStories(offset = 0) { - return fetchRankedStories('showstories', showStoriesCache, 'show', offset); + return fetchRankedStories('showstories', offset, { limit: 20, type: 'show' }); } // Ask excludes the whoishiring threads, matching fetchAskStoriesForDay. Show is // left unfiltered: a Show HN title mentioning hiring is a real project. export function fetchAskStories(offset = 0) { - return fetchRankedStories('askstories', askStoriesCache, 'ask', offset, 20, HIRING_EXCLUDE_TITLES); + return fetchRankedStories('askstories', offset, { limit: 20, type: 'ask', excludeHiring: true }); } /** @@ -378,9 +299,7 @@ export function fetchShowStoriesForDay(daysAgo: number): Promise { } export async function fetchFirebaseItem(id: number | string, signal?: AbortSignal): Promise { - if (signal?.aborted) { - throw new DOMException('Aborted', 'AbortError'); - } + signal?.throwIfAborted(); const item = await hnSdk.readItem(id); if (item == null) { throw new NotFoundError(`Item ${id} not found`); @@ -395,9 +314,7 @@ export async function fetchFirebaseItem(id: number | string, signal?: AbortSigna */ export async function fetchHiddenCommentIds(ids: number[], keepId: number, signal?: AbortSignal): Promise> { const items = await Promise.all(ids.map(id => hnSdk.readItem(id).catch(() => null))); - if (signal?.aborted) { - throw new DOMException('Aborted', 'AbortError'); - } + signal?.throwIfAborted(); return new Set(ids.filter((id, i) => { const item = items[i]; return id !== keepId && !!item && !item.kids?.length && (!!item.dead || !!item.deleted); @@ -637,9 +554,7 @@ async function itemOnly(raw: FirebaseItem): Promise { // order, but HN's pages sort by points; the sort is stable, so ties keep it. async function fetchPollOptions(parts: number[], signal?: AbortSignal): Promise { const items = await Promise.all(parts.map(id => hnSdk.readItem(id))); - if (signal?.aborted) { - throw new DOMException('Aborted', 'AbortError'); - } + signal?.throwIfAborted(); return items .filter((item): item is FirebaseItem => item != null && !item.deleted && !item.dead) .map(item => ({ id: item.id, text: item.text ?? '', points: item.score ?? 0 })) @@ -669,14 +584,12 @@ async function fetchThread( const raw = await itemRead.catch(() => null); if (!raw) throw err; const item = threadItem(raw, await pollOptionsRead); - if (signal?.aborted) throw new DOMException('Aborted', 'AbortError'); + signal?.throwIfAborted(); throw new CommentsError(err instanceof Error ? err.message : String(err), item); }); const [raw, comments] = await Promise.all([itemRead, commentsRead]); - if (signal?.aborted) { - throw new DOMException('Aborted', 'AbortError'); - } + signal?.throwIfAborted(); const [commentTree, pollOptions] = await Promise.all([ buildOrderedCommentTree(comments, itemId, raw.kids, signal), @@ -769,9 +682,7 @@ export async function prefetchItemComments(id: number | string, signal?: AbortSi /** The comment tree plus the item it hangs off, which the ordering reads anyway. */ export async function fetchItemWithComments(id: number | string, signal: AbortSignal | null = null): Promise { - if (signal?.aborted) { - throw new DOMException('Aborted', 'AbortError'); - } + signal?.throwIfAborted(); return downloadThread(Number(id), signal ?? undefined).thread; } @@ -888,16 +799,12 @@ async function hasRepliesOutsideItemTree(id: number, signal?: AbortSignal): Prom export async function fetchCommentReplies(item: AlgoliaItemResponse, signal?: AbortSignal): Promise { if (item.story_id == null) return normalizeAlgoliaItemChildren(item.children ?? []); const mayHaveMore = !!item.children?.length || await hasRepliesOutsideItemTree(item.id, signal); - if (signal?.aborted) { - throw new DOMException('Aborted', 'AbortError'); - } + signal?.throwIfAborted(); if (!mayHaveMore) return []; // Null for a thread bigger than search returns, which keeps the item tree's replies. const storyComments = await fetchStoryComments(item.story_id); - if (signal?.aborted) { - throw new DOMException('Aborted', 'AbortError'); - } + signal?.throwIfAborted(); const replies = flattenAlgoliaItemChildren(item.children ?? []); const seen = new Set(replies.map(c => c.objectID)); @@ -927,9 +834,8 @@ export function normalizeAlgoliaItemChildren(children: AlgoliaItemChild[]): Comm } // Format timestamp as a localized absolute date string for tooltips. -// Guards reject null/undefined/zero (`!timestamp`), NaN, AND ±Infinity — -// the latter would otherwise slip through (`!Infinity` is false, -// `isNaN(Infinity)` is false) and make the formatter throw a RangeError. +// Guards reject null/undefined/zero (`!timestamp`), and `isFinite` rejects NaN +// and ±Infinity, which would otherwise make the formatter throw a RangeError. // The formatter is built once: Date#toLocaleString builds a new one per call, // which showed up in swipe profiles (every comment renders a timestamp). const absoluteTimeFormat = new Intl.DateTimeFormat(undefined, { @@ -937,14 +843,14 @@ const absoluteTimeFormat = new Intl.DateTimeFormat(undefined, { hour: 'numeric', minute: '2-digit', }); export function formatAbsoluteTime(timestamp: number): string { - if (!timestamp || !isFinite(timestamp) || isNaN(timestamp)) return ''; + if (!timestamp || !isFinite(timestamp)) return ''; return absoluteTimeFormat.format(timestamp); } // Long-month date-only (e.g. "October 9, 2006") for profile creation dates // where minute precision isn't meaningful. export function formatAbsoluteDate(timestamp: number): string { - if (!timestamp || !isFinite(timestamp) || isNaN(timestamp)) return ''; + if (!timestamp || !isFinite(timestamp)) return ''; return new Date(timestamp).toLocaleString(undefined, { year: 'numeric', month: 'long', day: 'numeric', }); @@ -953,13 +859,13 @@ export function formatAbsoluteDate(timestamp: number): string { // Safe ISO string for