I’m currently working on a home screen for my app where users can view their PNL (profit and loss) over different time frames: 1 week, 4 weeks, and 12 weeks. The PNL data is the same for all users since one strategy is applied across their accounts/portfolios. We update the PNL weekly, so each new week’s data should dynamically update the 4-week and 12-week periods.
Under the PNL, I need to display the success fee. For example, if a user invested $10,000 and made a 10% PNL last week, their profit would be $1,000. With a 30% success fee, the fee would be $300. Importantly, the success fee varies for each user.
My stack includes React Native Expo for the front end and Firebase for the backend. I’m trying to decide the best place to handle these calculations—whether to do it client-side or with Firebase Cloud Functions. My main concern is performance, specifically minimizing fetching time.
What’s your take on this? Where should I handle the PNL and success fee calculations to ensure optimal performance? Any advice or similar experiences would be greatly appreciated!
Thanks!
I tried so far normal GET requests to query and fetch some documents on the client side like a list of latest transactions and latest news document, they are part of the same screen, but I’m not sure why they are not fetching instantly, I had them grouped within the same useEffect so maybe it’s better to separate into multiple useEffects?
const [latestNews, setLatestNews] = useState({});
const [userTransactions, setUserTransactions] = useState([]);
useEffect(() => {
const user = auth().currentUser;
const fetchLatestNews = async () => {
const newsRef = firestore().collection('announcements');
const querySnapshot = await newsRef.orderBy('date', 'desc').limit(1).get();
if (!querySnapshot.empty) {
const newsData = querySnapshot.docs[0].data();
setLatestNews(newsData);
} else {
console.log('No news found');
}
};
const fetchTxn = async () => {
if (user) {
console.log(user);
const txnRef = firestore().collection('clients').doc(user.phoneNumber).collection('txn');
const snapshot = await txnRef.get();
const transactions = snapshot.docs.map(doc => ({
id: doc.id,
...doc.data()
}));
setUserTransactions(transactions);
} else {
console.log('User not logged in');
}
};
fetchTxn();
fetchLatestNews();
}, []);