refactor(link-preview): split the 747-line hook into lib/link-preview modules - #380
Merged
Merged
Conversation
… modules hooks/use-link-preview.ts held URL recognition, an HTML meta parser, the proxy/IPFS fetch layer, the preview builder and its cache, two text helpers and the React hook. Each is now its own module under lib/link-preview: urls (pure, with a Vitest spec), parse-html, fetch (owns CORS_PROXY_INFO next to the proxy list it describes), preview (cache + dedupe), types. The hook is 66 lines and consumers import the helpers from lib, not from a hook. Behaviour kept: the same meta-tag precedence, timeouts, size cap, gateway/proxy order, direct-fetch allow list, and the never-rejecting preview fallback. The parser's regexes are generated from the tag name instead of listed out per tag, and the fetch layer has one timeout wrapper and one first-success loop instead of three copies.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Team Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Deploying yappr with
|
| Latest commit: |
fc35ae8
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://5ed6aaf3.yappr.pages.dev |
| Branch Preview URL: | https://refactor-link-preview-split.yappr.pages.dev |
shouldSkipPreview uses the shared case-insensitive isIpfsProtocol like fetch.ts does. firstSuccessful loses its dead all-failed message and generic (every error is swallowed into the basic preview anyway); basicPreview absorbs its one-caller helper; the hook hoists its repeated error reset; the public entry is getLinkPreview to sit beside getCachedPreview rather than beside the low-level fetcher; module docs sit above imports in all four files.
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
hooks/use-link-preview.tswas six things in one file. It is now:lib/link-preview/urls.ts: YouTube id, Yappr post id, direct-image and skip detection,extractFirstUrl/stripTrailingPunctuation. Pure; Vitest spec alongside (15 cases).lib/link-preview/parse-html.ts: Open Graph / Twitter /<title>extraction. Per-tag regex lists are generated from the tag name instead of copied out by hand.lib/link-preview/fetch.ts: the proxy / direct / IPFS-gateway fetch layer, withCORS_PROXY_INFOnext to the proxy list it documents. One timeout wrapper and one first-success loop replace the three hand-rolled copies.lib/link-preview/preview.ts: builds the preview, owns the module cache and in-flight dedupe, never rejects.lib/link-preview/types.ts:LinkPreviewData(was declared in the presentational component).hooks/use-link-preview.ts: the React hook, 66 lines.Consumers (
post-content,link-preview,blocknote-schema,use-yappr-post-reference, settings) import the helpers fromlibrather than from a hook.Behaviour preserved: meta-tag precedence, 8s timeout, 5 MB cap, gateway then proxy order, direct-fetch allow list,
resolvedUrlforipfs://, and the basic-preview fallback on any failure.Part of the anti-slop cleanup series (after #370–#379).
Test plan
npm run lint(zero warnings),tsc --noEmit,npm run test(110 specs incl. 15 new),npx knip,npm run buildall green locallyipfs://link and an ordinary page; the "learn more" privacy modal on a preview; the proxy list in Settings🤖 Generated with Claude Code