this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Cache account info fetches for 10mins

+65 -49
+65 -49
src/components/account-info.jsx
··· 1 1 import './account-info.css'; 2 2 3 3 import { Menu, MenuDivider, MenuItem, SubMenu } from '@szhsin/react-menu'; 4 + import mem from 'mem'; 4 5 import { 5 6 useCallback, 6 7 useEffect, ··· 54 55 }; 55 56 56 57 const LIMIT = 80; 58 + 59 + const ACCOUNT_INFO_MAX_AGE = 1000 * 60 * 10; // 10 mins 60 + 61 + function fetchFamiliarFollowers(currentID, masto) { 62 + return masto.v1.accounts.familiarFollowers.fetch({ 63 + id: [currentID], 64 + }); 65 + } 66 + const memFetchFamiliarFollowers = mem(fetchFamiliarFollowers, { 67 + maxAge: ACCOUNT_INFO_MAX_AGE, 68 + }); 69 + 70 + async function fetchPostingStats(accountID, masto) { 71 + const fetchStatuses = masto.v1.accounts 72 + .$select(accountID) 73 + .statuses.list({ 74 + limit: 20, 75 + }) 76 + .next(); 77 + 78 + const { value: statuses } = await fetchStatuses; 79 + console.log('fetched statuses', statuses); 80 + const stats = { 81 + total: statuses.length, 82 + originals: 0, 83 + replies: 0, 84 + boosts: 0, 85 + }; 86 + // Categories statuses by type 87 + // - Original posts (not replies to others) 88 + // - Threads (self-replies + 1st original post) 89 + // - Boosts (reblogs) 90 + // - Replies (not-self replies) 91 + statuses.forEach((status) => { 92 + if (status.reblog) { 93 + stats.boosts++; 94 + } else if ( 95 + !!status.inReplyToId && 96 + status.inReplyToAccountId !== status.account.id // Not self-reply 97 + ) { 98 + stats.replies++; 99 + } else { 100 + stats.originals++; 101 + } 102 + }); 103 + 104 + // Count days since last post 105 + stats.daysSinceLastPost = Math.ceil( 106 + (Date.now() - new Date(statuses[statuses.length - 1].createdAt)) / 86400000, 107 + ); 108 + 109 + console.log('posting stats', stats); 110 + return stats; 111 + } 112 + const memFetchPostingStats = mem(fetchPostingStats, { 113 + maxAge: ACCOUNT_INFO_MAX_AGE, 114 + }); 57 115 58 116 function AccountInfo({ 59 117 account, ··· 208 266 const [postingStats, setPostingStats] = useState(); 209 267 const [postingStatsUIState, setPostingStatsUIState] = useState('default'); 210 268 const hasPostingStats = !!postingStats?.total; 211 - const currentIDRef = useRef(); 212 269 213 - const renderFamiliarFollowers = async () => { 214 - if (!currentIDRef.current) return; 215 - const currentID = currentIDRef.current; 270 + const renderFamiliarFollowers = async (currentID) => { 216 271 try { 217 - const fetchFamiliarFollowers = 218 - currentMasto.v1.accounts.familiarFollowers.fetch({ 219 - id: [currentID], 220 - }); 221 - 222 - const followers = await fetchFamiliarFollowers; 272 + const followers = await memFetchFamiliarFollowers( 273 + currentID, 274 + currentMasto, 275 + ); 223 276 console.log('fetched familiar followers', followers); 224 277 setFamiliarFollowers( 225 278 followers[0].accounts.slice(0, FAMILIAR_FOLLOWERS_LIMIT), ··· 232 285 const renderPostingStats = async () => { 233 286 setPostingStatsUIState('loading'); 234 287 try { 235 - const fetchStatuses = masto.v1.accounts 236 - .$select(id) 237 - .statuses.list({ 238 - limit: 20, 239 - }) 240 - .next(); 241 - 242 - const { value: statuses } = await fetchStatuses; 243 - console.log('fetched statuses', statuses); 244 - const stats = { 245 - total: statuses.length, 246 - originals: 0, 247 - replies: 0, 248 - boosts: 0, 249 - }; 250 - // Categories statuses by type 251 - // - Original posts (not replies to others) 252 - // - Threads (self-replies + 1st original post) 253 - // - Boosts (reblogs) 254 - // - Replies (not-self replies) 255 - statuses.forEach((status) => { 256 - if (status.reblog) { 257 - stats.boosts++; 258 - } else if (status.inReplyToAccountId !== id && !!status.inReplyToId) { 259 - stats.replies++; 260 - } else { 261 - stats.originals++; 262 - } 263 - }); 264 - 265 - // Count days since last post 266 - stats.daysSinceLastPost = Math.ceil( 267 - (Date.now() - new Date(statuses[statuses.length - 1].createdAt)) / 268 - 86400000, 269 - ); 270 - 271 - console.log('posting stats', stats); 288 + const stats = await memFetchPostingStats(id, masto); 272 289 setPostingStats(stats); 273 290 setPostingStatsUIState('default'); 274 291 } catch (e) { ··· 279 296 280 297 const onRelationshipChange = useCallback( 281 298 ({ relationship, currentID }) => { 282 - currentIDRef.current = currentID; 283 299 if (!relationship.following) { 284 - renderFamiliarFollowers(); 300 + renderFamiliarFollowers(currentID); 285 301 if (!standalone) { 286 302 renderPostingStats(); 287 303 }