From 1211c353d0ef67f7a2d97e819dac488b14b73a08 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Thu, 3 Aug 2023 12:00:38 -0500 Subject: [PATCH] resolve did before loading feed (#1092) * resolve did before loading feed * add loader * wrap with authRequired, handle errors --- src/view/screens/CustomFeed.tsx | 108 +++++++++++++++++++++++++++++--- 1 file changed, 98 insertions(+), 10 deletions(-) diff --git a/src/view/screens/CustomFeed.tsx b/src/view/screens/CustomFeed.tsx index d5ecff04..265f8a94 100644 --- a/src/view/screens/CustomFeed.tsx +++ b/src/view/screens/CustomFeed.tsx @@ -1,13 +1,14 @@ import React, {useMemo, useRef} from 'react' import {NativeStackScreenProps} from '@react-navigation/native-stack' import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' +import {useNavigation} from '@react-navigation/native' import {usePalette} from 'lib/hooks/usePalette' import {HeartIcon, HeartIconSolid} from 'lib/icons' import {CommonNavigatorParams} from 'lib/routes/types' import {makeRecordUri} from 'lib/strings/url-helpers' import {colors, s} from 'lib/styles' import {observer} from 'mobx-react-lite' -import {FlatList, StyleSheet, View} from 'react-native' +import {FlatList, StyleSheet, View, ActivityIndicator} from 'react-native' import {useStores} from 'state/index' import {PostsFeedModel} from 'state/models/feeds/posts' import {useCustomFeed} from 'lib/hooks/useCustomFeed' @@ -34,17 +35,98 @@ import {EmptyState} from 'view/com/util/EmptyState' import {useAnalytics} from 'lib/analytics/analytics' import {NativeDropdown, DropdownItem} from 'view/com/util/forms/NativeDropdown' import {makeProfileLink} from 'lib/routes/links' +import {resolveName} from 'lib/api' +import {CenteredView} from 'view/com/util/Views' +import {NavigationProp} from 'lib/routes/types' type Props = NativeStackScreenProps + export const CustomFeedScreen = withAuthRequired( - observer(({route}: Props) => { + observer((props: Props) => { + const pal = usePalette('default') + const store = useStores() + const navigation = useNavigation() + + const {name: handleOrDid} = props.route.params + + const [feedOwnerDid, setFeedOwnerDid] = React.useState() + const [error, setError] = React.useState() + + const onPressBack = React.useCallback(() => { + if (navigation.canGoBack()) { + navigation.goBack() + } else { + navigation.navigate('Home') + } + }, [navigation]) + + React.useEffect(() => { + /* + * We must resolve the DID of the feed owner before we can fetch the feed. + */ + async function fetchDid() { + try { + const did = await resolveName(store, handleOrDid) + setFeedOwnerDid(did) + } catch (e) { + setError( + `We're sorry, but we were unable to resolve this feed. If this persists, please contact the feed creator, @${handleOrDid}.`, + ) + } + } + + fetchDid() + }, [store, handleOrDid, setFeedOwnerDid]) + + if (error) { + return ( + + + + Could not load feed + + + {error} + + + + + + + + ) + } + + return feedOwnerDid ? ( + + ) : ( + + + + + + ) + }), +) + +export const CustomFeedScreenInner = observer( + ({route, feedOwnerDid}: Props & {feedOwnerDid: string}) => { const store = useStores() const pal = usePalette('default') const {track} = useAnalytics() - const {rkey, name} = route.params + const {rkey, name: handleOrDid} = route.params const uri = useMemo( - () => makeRecordUri(name, 'app.bsky.feed.generator', rkey), - [rkey, name], + () => makeRecordUri(feedOwnerDid, 'app.bsky.feed.generator', rkey), + [rkey, feedOwnerDid], ) const scrollElRef = useRef(null) const currentFeed = useCustomFeed(uri) @@ -101,10 +183,10 @@ export const CustomFeedScreen = withAuthRequired( }, [store, currentFeed]) const onPressShare = React.useCallback(() => { - const url = toShareUrl(`/profile/${name}/feed/${rkey}`) + const url = toShareUrl(`/profile/${handleOrDid}/feed/${rkey}`) shareUrl(url) track('CustomFeed:Share') - }, [name, rkey, track]) + }, [handleOrDid, rkey, track]) const onScrollToTop = React.useCallback(() => { scrollElRef.current?.scrollToOffset({offset: 0, animated: true}) @@ -310,7 +392,7 @@ export const CustomFeedScreen = withAuthRequired( ) - }), + }, ) const styles = StyleSheet.create({ @@ -430,4 +512,10 @@ const styles = StyleSheet.create({ position: 'relative', top: 2, }, + notFoundContainer: { + margin: 10, + paddingHorizontal: 18, + paddingVertical: 14, + borderRadius: 6, + }, })