-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve tweet embedding using react-tweet
- Loading branch information
1 parent
172bd84
commit e2017fd
Showing
7 changed files
with
165 additions
and
87 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { type ExtendedRecordMap } from 'notion-types' | ||
import { getPageTweetIds } from 'notion-utils' | ||
import pMap from 'p-map' | ||
import pMemoize from 'p-memoize' | ||
import { getTweet as getTweetData } from 'react-tweet/api' | ||
|
||
import type { ExtendedTweetRecordMap } from './types' | ||
import { db } from './db' | ||
|
||
export async function getTweetsMap( | ||
recordMap: ExtendedRecordMap | ||
): Promise<void> { | ||
const tweetIds = getPageTweetIds(recordMap) | ||
|
||
const tweetsMap = Object.fromEntries( | ||
await pMap( | ||
tweetIds, | ||
async (tweetId: string) => { | ||
return [tweetId, await getTweet(tweetId)] | ||
}, | ||
{ | ||
concurrency: 8 | ||
} | ||
) | ||
) | ||
|
||
;(recordMap as ExtendedTweetRecordMap).tweets = tweetsMap | ||
} | ||
|
||
async function getTweetImpl(tweetId: string): Promise<any> { | ||
if (!tweetId) return null | ||
|
||
const cacheKey = `tweet:${tweetId}` | ||
|
||
try { | ||
try { | ||
const cachedTweet = await db.get(cacheKey) | ||
if (cachedTweet) { | ||
return cachedTweet | ||
} | ||
} catch (err) { | ||
// ignore redis errors | ||
console.warn(`redis error get "${cacheKey}"`, err.message) | ||
} | ||
|
||
const tweetData = await getTweetData(tweetId) | ||
|
||
try { | ||
await db.set(cacheKey, tweetData) | ||
} catch (err) { | ||
// ignore redis errors | ||
console.warn(`redis error set "${cacheKey}"`, err.message) | ||
} | ||
|
||
return tweetData | ||
} catch (err: any) { | ||
console.warn('failed to get tweet', tweetId, err.message) | ||
return null | ||
} | ||
} | ||
|
||
export const getTweet = pMemoize(getTweetImpl) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.