Improve feed/profile cache logs and add Expo app agent notes

This commit is contained in:
Adolfo Reyna
2026-02-20 20:10:07 -05:00
parent 009f1ec792
commit 487edb4a62
3 changed files with 205 additions and 15 deletions

View File

@@ -10,6 +10,11 @@ import * as Updates from 'expo-updates';
import AsyncStorage from '@react-native-async-storage/async-storage';
const FEED_LOG_PREFIX = '[Feed]';
const logFeed = (...args) => {
if (__DEV__) console.log(FEED_LOG_PREFIX, ...args);
};
const storeFeed = async (value) => {
try {
const jsonValue = JSON.stringify(value)
@@ -66,26 +71,38 @@ async function onFetchUpdateAsync() {
let Feed = ({ navigation, route }) => {
let [Posts, setPosts] = useState([]);
const flatListRef = React.useRef()
console.log("Render Feed");
logFeed('render', {
posts: Posts.length,
hasRouteParams: !!route?.params,
reRender: !!route?.params?.reRender,
targetProfile: route?.params?.profileid || null,
});
const url = Linking.useURL();
useEffect(() => {
if (prevLink === url || !url) return;
prevLink = url;
logFeed('deep-link', url);
handleURL(url, navigation);
}, [url]);
useEffect(() => {
let subscribed = true;
const getData = async () => {
logFeed('load:start', {
reRender: !!route?.params?.reRender,
targetProfile: route?.params?.profileid || null,
});
// TODO: Check for internet connection
const internet = true;
if (internet) {
//byPass and load
let loggedIn = await API.isLoggedIn();
logFeed('session:checked', { loggedIn });
if (!loggedIn) return navigation.reset({
index: 0,
routes: [{ name: 'Login' }],
});
if (route.params && route.params.profileid) {
logFeed('redirect:profile', { profileid: route.params.profileid });
return navigation.navigate('Profile', { profileid: route.params.profileid });
}
}
@@ -103,10 +120,9 @@ let Feed = ({ navigation, route }) => {
posthog.capture('login');
}
});
console.log("Feed from cache")
let cacheFeed = await getFeed() || [];
logFeed('cache:read', { count: cacheFeed.length });
if (cacheFeed.length && subscribed) setPosts(cacheFeed);
console.log("Feed from server")
}
await onFetchUpdateAsync();
flatListRef.current?.scrollToOffset({ animated: true, offset: 0 })
@@ -115,12 +131,14 @@ let Feed = ({ navigation, route }) => {
const safePosts = Array.isArray(posts) ? posts : [];
setPosts(safePosts);
storeFeed(safePosts);
logFeed('network:loaded', { count: safePosts.length, cached: true });
}
console.log("Feed, end useEffect")
logFeed('load:end');
}
getData()
return () => {
subscribed = false;
logFeed('load:cleanup');
}
}, [route.params]);
const renderPost = (({ item }) => {
@@ -143,7 +161,13 @@ let Feed = ({ navigation, route }) => {
//ListHeaderComponent={<NewPost newPostCB={(newPost) => setPosts([newPost, ...Posts])} />}
refreshing={Posts.length === 0}
onRefresh={() => {
API.getPosts().then((data) => setPosts(Array.isArray(data) ? data : []));
logFeed('refresh:start');
API.getPosts().then((data) => {
const safePosts = Array.isArray(data) ? data : [];
setPosts(safePosts);
storeFeed(safePosts);
logFeed('refresh:end', { count: safePosts.length, cached: true });
});
}}
initialNumToRender={3}
maxToRenderPerBatch={3}