import React from 'react' import {ActivityIndicator, Pressable, StyleSheet, View} from 'react-native' import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import {msg, Trans} from '@lingui/macro' import {useLingui} from '@lingui/react' import {useFocusEffect} from '@react-navigation/native' import {NativeStackScreenProps} from '@react-navigation/native-stack' import {track} from '#/lib/analytics/analytics' import {logger} from '#/logger' import { usePinFeedMutation, usePreferencesQuery, useSetSaveFeedsMutation, useUnpinFeedMutation, } from '#/state/queries/preferences' import {useSetMinimalShellMode} from '#/state/shell' import {useAnalytics} from 'lib/analytics/analytics' import {useHaptics} from 'lib/haptics' import {usePalette} from 'lib/hooks/usePalette' import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries' import {CommonNavigatorParams} from 'lib/routes/types' import {colors, s} from 'lib/styles' import {FeedSourceCard} from 'view/com/feeds/FeedSourceCard' import {TextLink} from 'view/com/util/Link' import {Text} from 'view/com/util/text/Text' import * as Toast from 'view/com/util/Toast' import {ViewHeader} from 'view/com/util/ViewHeader' import {CenteredView, ScrollView} from 'view/com/util/Views' const HITSLOP_TOP = { top: 20, left: 20, bottom: 5, right: 20, } const HITSLOP_BOTTOM = { top: 5, left: 20, bottom: 20, right: 20, } type Props = NativeStackScreenProps export function SavedFeeds({}: Props) { const pal = usePalette('default') const {_} = useLingui() const {isMobile, isTabletOrDesktop} = useWebMediaQueries() const {screen} = useAnalytics() const setMinimalShellMode = useSetMinimalShellMode() const {data: preferences} = usePreferencesQuery() const { mutateAsync: setSavedFeeds, variables: optimisticSavedFeedsResponse, reset: resetSaveFeedsMutationState, error: setSavedFeedsError, } = useSetSaveFeedsMutation() /* * Use optimistic data if exists and no error, otherwise fallback to remote * data */ const currentFeeds = optimisticSavedFeedsResponse && !setSavedFeedsError ? optimisticSavedFeedsResponse : preferences?.feeds || {saved: [], pinned: []} const unpinned = currentFeeds.saved.filter(f => { return !currentFeeds.pinned?.includes(f) }) useFocusEffect( React.useCallback(() => { screen('SavedFeeds') setMinimalShellMode(false) }, [screen, setMinimalShellMode]), ) return ( Pinned Feeds {preferences?.feeds ? ( !currentFeeds.pinned.length ? ( You don't have any pinned feeds. ) : ( currentFeeds.pinned.map(uri => ( )) ) ) : ( )} Saved Feeds {preferences?.feeds ? ( !unpinned.length ? ( You don't have any saved feeds. ) : ( unpinned.map(uri => ( )) ) ) : ( )} Feeds are custom algorithms that users build with a little coding expertise.{' '} {' '} for more information. ) } function ListItem({ feedUri, isPinned, currentFeeds, setSavedFeeds, resetSaveFeedsMutationState, }: { feedUri: string // uri isPinned: boolean currentFeeds: {saved: string[]; pinned: string[]} setSavedFeeds: ReturnType['mutateAsync'] resetSaveFeedsMutationState: ReturnType< typeof useSetSaveFeedsMutation >['reset'] }) { const pal = usePalette('default') const {_} = useLingui() const playHaptic = useHaptics() const {isPending: isPinPending, mutateAsync: pinFeed} = usePinFeedMutation() const {isPending: isUnpinPending, mutateAsync: unpinFeed} = useUnpinFeedMutation() const isPending = isPinPending || isUnpinPending const onTogglePinned = React.useCallback(async () => { playHaptic() try { resetSaveFeedsMutationState() if (isPinned) { await unpinFeed({uri: feedUri}) } else { await pinFeed({uri: feedUri}) } } catch (e) { Toast.show(_(msg`There was an issue contacting the server`)) logger.error('Failed to toggle pinned feed', {message: e}) } }, [ playHaptic, resetSaveFeedsMutationState, isPinned, unpinFeed, feedUri, pinFeed, _, ]) const onPressUp = React.useCallback(async () => { if (!isPinned) return // create new array, do not mutate const pinned = [...currentFeeds.pinned] const index = pinned.indexOf(feedUri) if (index === -1 || index === 0) return ;[pinned[index], pinned[index - 1]] = [pinned[index - 1], pinned[index]] try { await setSavedFeeds({saved: currentFeeds.saved, pinned}) track('CustomFeed:Reorder', { uri: feedUri, index: pinned.indexOf(feedUri), }) } catch (e) { Toast.show(_(msg`There was an issue contacting the server`)) logger.error('Failed to set pinned feed order', {message: e}) } }, [feedUri, isPinned, setSavedFeeds, currentFeeds, _]) const onPressDown = React.useCallback(async () => { if (!isPinned) return const pinned = [...currentFeeds.pinned] const index = pinned.indexOf(feedUri) if (index === -1 || index >= pinned.length - 1) return ;[pinned[index], pinned[index + 1]] = [pinned[index + 1], pinned[index]] try { await setSavedFeeds({saved: currentFeeds.saved, pinned}) track('CustomFeed:Reorder', { uri: feedUri, index: pinned.indexOf(feedUri), }) } catch (e) { Toast.show(_(msg`There was an issue contacting the server`)) logger.error('Failed to set pinned feed order', {message: e}) } }, [feedUri, isPinned, setSavedFeeds, currentFeeds, _]) return ( {isPinned ? ( ({ opacity: state.hovered || state.focused || isPending ? 0.5 : 1, })}> ({ opacity: state.hovered || state.focused || isPending ? 0.5 : 1, })}> ) : null} ({ opacity: state.hovered || state.focused || isPending ? 0.5 : 1, })}> ) } const styles = StyleSheet.create({ desktopContainer: { borderLeftWidth: 1, borderRightWidth: 1, // @ts-ignore only rendered on web minHeight: '100vh', }, empty: { paddingHorizontal: 20, paddingVertical: 20, borderRadius: 8, marginHorizontal: 10, marginTop: 10, }, title: { paddingHorizontal: 14, paddingTop: 20, paddingBottom: 10, borderBottomWidth: 1, }, itemContainer: { flexDirection: 'row', alignItems: 'center', borderBottomWidth: 1, paddingRight: 16, }, webArrowButtonsContainer: { paddingLeft: 16, flexDirection: 'column', justifyContent: 'space-around', }, webArrowUpButton: { marginBottom: 10, }, noTopBorder: { borderTopWidth: 0, }, footerText: { paddingHorizontal: 26, paddingTop: 22, paddingBottom: 100, }, noBorder: { borderBottomWidth: 0, borderRightWidth: 0, borderLeftWidth: 0, borderTopWidth: 0, }, })