Skip to content

Commit 90a2ce4

Browse files
committed
refactor: fix potential race in recent apps
This does not fix an existing bug, because `AppIcon` is already rendered with `key={app.id}`, but let's future-proof it.
1 parent 2ef0aa7 commit 90a2ce4

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

packages/frontend/src/components/screens/MainScreen/MainScreen.tsx

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import type { T } from '@deltachat/jsonrpc-client'
4141
import CreateChat from '../../dialogs/CreateChat'
4242
import { runtime } from '@deltachat-desktop/runtime-interface'
4343
import asyncThrottle from '@jcoreio/async-throttle'
44-
import { useFetch } from '../../../hooks/useFetch'
44+
import { useFetch, useRpcFetch } from '../../../hooks/useFetch'
4545
import { getLogger } from '@deltachat-desktop/shared/logger'
4646

4747
const log = getLogger('MainScreen')
@@ -481,29 +481,25 @@ function ChatNavButtons({ chat }: { chat: T.FullChat }) {
481481
}
482482

483483
function AppIcon({ accountId, app }: { accountId: number; app: T.Message }) {
484-
const [webxdcInfo, setWebxdcInfo] = useState<T.WebxdcMessageInfo | null>(null)
485-
const [isLoadingWebxdcInfo, setIsLoadingWebxdcInfo] = useState(true)
484+
const tx = useTranslationFunction()
486485

487-
useEffect(() => {
488-
if (app.viewType === 'Webxdc') {
489-
setIsLoadingWebxdcInfo(true)
490-
BackendRemote.rpc
491-
.getWebxdcInfo(accountId, app.id)
492-
.then((info: T.WebxdcMessageInfo) => {
493-
setWebxdcInfo(info)
494-
})
495-
.catch((error: any) => {
496-
console.error('Failed to load webxdc info for app:', app.id, error)
497-
setWebxdcInfo(null)
498-
})
499-
.finally(() => {
500-
setIsLoadingWebxdcInfo(false)
501-
})
502-
}
503-
}, [accountId, app.id, app.viewType])
486+
const webxdcInfoFetch = useRpcFetch(BackendRemote.rpc.getWebxdcInfo, [
487+
accountId,
488+
app.id,
489+
])
490+
if (webxdcInfoFetch.result?.ok === false) {
491+
log.error(
492+
'Failed to load webxdc info for app:',
493+
app.id,
494+
webxdcInfoFetch.result.err
495+
)
496+
}
504497

505-
const appName =
506-
webxdcInfo?.name || (isLoadingWebxdcInfo ? 'Loading...' : 'Unknown App')
498+
const appName = webxdcInfoFetch.loading
499+
? tx('loading')
500+
: webxdcInfoFetch.result.ok
501+
? webxdcInfoFetch.result.value.name
502+
: 'Unknown App'
507503

508504
return (
509505
<Button
@@ -512,8 +508,12 @@ function AppIcon({ accountId, app }: { accountId: number; app: T.Message }) {
512508
className={styles.webxdcIconButton}
513509
title={appName}
514510
aria-label={appName}
511+
aria-busy={webxdcInfoFetch.loading}
515512
onClick={() => {
516-
openWebxdc(app, webxdcInfo ?? undefined)
513+
openWebxdc(
514+
app,
515+
webxdcInfoFetch.result?.ok ? webxdcInfoFetch.result.value : undefined
516+
)
517517
}}
518518
>
519519
<img

0 commit comments

Comments
 (0)