mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-10-05 02:41:26 +00:00
fix(tweet-preview): guard JSON.parse to avoid crashing on malformed tweet data
TweetPreview was passing the result of `JSON.parse(data)` straight into `enrichTweet` whenever `data` was a string. If a document's stored tweet metadata was ever a non-JSON string, truncated JSON, or `"null"` / `"123"`, the unguarded parse threw and crashed the React tree rendering the document modal or memory card. Extract the parse into a `parseTweetData` helper that returns `null` when the input is missing, fails to parse, or doesn't resolve to an object. TweetPreview now renders nothing in those cases instead of crashing. Also widen the prop type from `Tweet` to `Tweet | string` to reflect the runtime input both call sites already pass.
This commit is contained in:
parent
a8d36df38e
commit
e5ead50a3d
1 changed files with 14 additions and 2 deletions
|
|
@ -117,14 +117,26 @@ function CustomTweetMedia({
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseTweetData(data: Tweet | string): Tweet | null {
|
||||||
|
if (!data) return null
|
||||||
|
if (typeof data !== "string") return data
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(data)
|
||||||
|
return parsed && typeof parsed === "object" ? (parsed as Tweet) : null
|
||||||
|
} catch {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function TweetPreview({
|
export function TweetPreview({
|
||||||
data,
|
data,
|
||||||
noBgColor,
|
noBgColor,
|
||||||
}: {
|
}: {
|
||||||
data: Tweet
|
data: Tweet | string
|
||||||
noBgColor?: boolean
|
noBgColor?: boolean
|
||||||
}) {
|
}) {
|
||||||
const parsedTweet = typeof data === "string" ? JSON.parse(data) : data
|
const parsedTweet = parseTweetData(data)
|
||||||
|
if (!parsedTweet) return null
|
||||||
const tweet = enrichTweet(parsedTweet)
|
const tweet = enrichTweet(parsedTweet)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue