From 3794eca88e13c3c292b0f64b1acb1169ecbeb83d Mon Sep 17 00:00:00 2001
From: Paul Frazee <pfrazee@gmail.com>
Date: Mon, 25 Jul 2022 14:21:48 -0500
Subject: Add state updates after screen changes

---
 src/view/com/post-thread/PostThread.tsx | 11 +++++++++++
 1 file changed, 11 insertions(+)

(limited to 'src/view/com/post-thread/PostThread.tsx')

diff --git a/src/view/com/post-thread/PostThread.tsx b/src/view/com/post-thread/PostThread.tsx
index 8f70e1493..bc9562ea1 100644
--- a/src/view/com/post-thread/PostThread.tsx
+++ b/src/view/com/post-thread/PostThread.tsx
@@ -1,6 +1,7 @@
 import React, {useState, useEffect} from 'react'
 import {observer} from 'mobx-react-lite'
 import {ActivityIndicator, FlatList, Text, View} from 'react-native'
+import {useFocusEffect} from '@react-navigation/native'
 import {OnNavigateContent} from '../../routes/types'
 import {
   PostThreadViewModel,
@@ -9,6 +10,8 @@ import {
 import {useStores} from '../../../state'
 import {PostThreadItem} from './PostThreadItem'
 
+const UPDATE_DELAY = 2e3 // wait 2s before refetching the thread for updates
+
 export const PostThread = observer(function PostThread({
   uri,
   onNavigateContent,
@@ -18,6 +21,7 @@ export const PostThread = observer(function PostThread({
 }) {
   const store = useStores()
   const [view, setView] = useState<PostThreadViewModel | undefined>()
+  const [lastUpdate, setLastUpdate] = useState<number>(Date.now())
 
   useEffect(() => {
     if (view?.params.uri === uri) {
@@ -30,6 +34,13 @@ export const PostThread = observer(function PostThread({
     newView.setup().catch(err => console.error('Failed to fetch thread', err))
   }, [uri, view?.params.uri, store])
 
+  useFocusEffect(() => {
+    if (Date.now() - lastUpdate > UPDATE_DELAY) {
+      view?.update()
+      setLastUpdate(Date.now())
+    }
+  })
+
   // loading
   // =
   if (
-- 
cgit 1.4.1

