Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate remote cards over to zustand <> react-query sync #5796

Merged
merged 31 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
098b602
migrate remote cards over to zustand <> react-query sync
walmat May 31, 2024
e6b5fa2
try to fix placement
walmat Jun 3, 2024
f6a6657
bring in promo sheets too
walmat Jun 3, 2024
db272d5
fix lint
walmat Jun 3, 2024
5988d85
chore: merge
walmat Jun 3, 2024
f19b154
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 4, 2024
6251afe
more cleanup
walmat Jun 4, 2024
7f5c9f4
write migration to prevent showing old sheets to users
walmat Jun 4, 2024
e8e4de7
bring back discover search
walmat Jun 4, 2024
e91ea74
fix e2e
walmat Jun 6, 2024
184e1ea
Update src/resources/cards/cardCollectionQuery.ts
walmat Jun 6, 2024
b20aa2c
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 7, 2024
a9eaee1
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 11, 2024
a685cda
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 11, 2024
895a54b
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 11, 2024
d109b95
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 12, 2024
b2d6f20
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 12, 2024
c931d61
move sync components and memo them
walmat Jun 12, 2024
dadf0ab
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 12, 2024
ed5c7b8
wrap in IM
walmat Jun 12, 2024
77b4dad
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 12, 2024
49ffa53
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 13, 2024
1e08f97
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 13, 2024
c818fd5
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 13, 2024
d7eb6d6
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 13, 2024
d3519a5
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 13, 2024
5046499
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 13, 2024
c68eaa3
Merge branch 'develop' into @matthew/fix-frame-drops
walmat Jun 14, 2024
3749f84
cleanup discover home and revert scroll throttle change
walmat Jun 14, 2024
7229bda
rm reactive cards in remote card
walmat Jun 14, 2024
0259898
change refetch interval
walmat Jun 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ import branch from 'react-native-branch';
import { initializeReservoirClient } from '@/resources/reservoir/client';
import { ReviewPromptAction } from '@/storage/schema';
import { handleReviewPromptAction } from '@/utils/reviewAlert';
import { RemotePromoSheetProvider } from '@/components/remote-promo-sheet/RemotePromoSheetProvider';
import { RemoteCardProvider } from '@/components/cards/remote-cards';
import { initializeRemoteConfig } from '@/model/remoteConfig';
import { IS_DEV } from './env';
import { checkIdentifierOnLaunch } from './model/backup';
Expand Down Expand Up @@ -147,11 +145,11 @@ class OldApp extends Component {
const address = await loadAddress();

if (address) {
InteractionManager.runAfterInteractions(() => {
setTimeout(() => {
setTimeout(() => {
InteractionManager.runAfterInteractions(() => {
handleReviewPromptAction(ReviewPromptAction.TimesLaunchedSinceInstall);
}, 10_000);
});
});
}, 10_000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wowow TIL that in JS 10000 === 10_000 is true 🤯

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever I see people arguing about whether or not JavaScript is a good language on Twitter or whatever I cannot help but think about this repo: https://github.com/denysdovhan/wtfjs?tab=readme-ov-file#-examples


checkIdentifierOnLaunch();
}
Expand Down Expand Up @@ -223,14 +221,10 @@ class OldApp extends Component {
<Portal>
<View style={containerStyle}>
{this.state.initialRoute && (
<RemotePromoSheetProvider isWalletReady={this.props.walletReady}>
<RemoteCardProvider>
<InitialRouteContext.Provider value={this.state.initialRoute}>
<RoutesComponent ref={this.handleNavigatorRef} />
<PortalConsumer />
</InitialRouteContext.Provider>
</RemoteCardProvider>
</RemotePromoSheetProvider>
<InitialRouteContext.Provider value={this.state.initialRoute}>
<RoutesComponent ref={this.handleNavigatorRef} />
<PortalConsumer />
</InitialRouteContext.Provider>
)}
<OfflineToast />
</View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import getLayoutProvider from './getLayoutProvider';
import useLayoutItemAnimator from './useLayoutItemAnimator';
import { UniqueAsset } from '@/entities';
import { useRecyclerListViewScrollToTopContext } from '@/navigation/RecyclerListViewScrollToTopContext';
import { useAccountProfile, useAccountSettings, useCoinListEdited, useCoinListEditOptions, useWallets } from '@/hooks';
import { useAccountSettings, useCoinListEdited, useCoinListEditOptions, useWallets } from '@/hooks';
import { useNavigation } from '@/navigation';
import { useTheme } from '@/theme';
import { useRemoteCardContext } from '@/components/cards/remote-cards';
import { remoteCardsStore } from '@/state/remoteCards/remoteCards';
import { useRoute } from '@react-navigation/native';
import Routes from '@/navigation/routesNames';

const dataProvider = new DataProvider((r1, r2) => {
return r1.uid !== r2.uid;
Expand Down Expand Up @@ -58,14 +59,14 @@ const RawMemoRecyclerAssetList = React.memo(function RawRecyclerAssetList({
const y = useRecyclerAssetListPosition()!;

const { name } = useRoute();
const { getCardsForPlacement } = useRemoteCardContext();
const getCardIdsForScreen = remoteCardsStore(state => state.getCardIdsForScreen);
const { isReadOnlyWallet } = useWallets();

const cards = useMemo(() => getCardsForPlacement(name as string), [getCardsForPlacement, name]);
const cardIds = useMemo(() => getCardIdsForScreen(name as keyof typeof Routes), [getCardIdsForScreen, name]);

const layoutProvider = useMemo(
() => getLayoutProvider(briefSectionsData, isCoinListEdited, cards, isReadOnlyWallet),
[briefSectionsData, isCoinListEdited, cards, isReadOnlyWallet]
() => getLayoutProvider(briefSectionsData, isCoinListEdited, cardIds, isReadOnlyWallet),
[briefSectionsData, isCoinListEdited, cardIds, isReadOnlyWallet]
);

const { accountAddress } = useAccountSettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ class BetterLayoutProvider extends LayoutProvider {
}
}

const getLayoutProvider = (
briefSectionsData: BaseCellType[],
isCoinListEdited: boolean,
cards: TrimmedCard[],
isReadOnlyWallet: boolean
) => {
const getLayoutProvider = (briefSectionsData: BaseCellType[], isCoinListEdited: boolean, cardIds: string[], isReadOnlyWallet: boolean) => {
const indicesToOverride = [];
for (let i = 0; i < briefSectionsData.length; i++) {
const val = briefSectionsData[i];
Expand All @@ -61,7 +56,7 @@ const getLayoutProvider = (
dim.height = ViewDimensions[type].height;
dim.width = ViewDimensions[type].width || dim.width;

if ((type === CellType.REMOTE_CARD_CAROUSEL && !cards.length) || (type === CellType.REMOTE_CARD_CAROUSEL && isReadOnlyWallet)) {
if ((type === CellType.REMOTE_CARD_CAROUSEL && !cardIds.length) || (type === CellType.REMOTE_CARD_CAROUSEL && isReadOnlyWallet)) {
dim.height = 0;
}
}
Expand Down
24 changes: 11 additions & 13 deletions src/components/cards/remote-cards/RemoteCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import ConditionalWrap from 'conditional-wrap';

import { Box, Cover, Stack, Text, useForegroundColor } from '@/design-system';
import { ButtonPressAnimation } from '@/components/animations';
import { useRemoteCardContext } from './RemoteCardProvider';
import { IS_ANDROID, IS_IOS } from '@/env';
import { useNavigation } from '@/navigation';
import { Language } from '@/languages';
Expand All @@ -20,6 +19,7 @@ import { FlashList } from '@shopify/flash-list';
import { ButtonPressAnimationTouchEvent } from '@/components/animations/ButtonPressAnimation/types';
import { TrimmedCard } from '@/resources/cards/cardCollectionQuery';
import RemoteSvg from '@/components/svg/RemoteSvg';
import { remoteCardsStore } from '@/state/remoteCards/remoteCards';

const ICON_SIZE = 40;

Expand Down Expand Up @@ -58,18 +58,19 @@ const getColorFromString = (color: string | undefined | null) => {
};

type RemoteCardProps = {
card: TrimmedCard;
cards: TrimmedCard[];
id: string;
gutterSize: number;
carouselRef: React.RefObject<FlashList<TrimmedCard>> | null;
carouselRef: React.RefObject<FlashList<string>> | null;
};

export const RemoteCard: React.FC<RemoteCardProps> = ({ card = {} as TrimmedCard, cards, gutterSize, carouselRef }) => {
export const RemoteCard: React.FC<RemoteCardProps> = ({ id, gutterSize, carouselRef }) => {
const { isDarkMode } = useTheme();
const { navigate } = useNavigation();
const { language } = useAccountSettings();
const { width } = useDimensions();
const { dismissCard } = useRemoteCardContext();
const cards = remoteCardsStore(state => state.cards);
walmat marked this conversation as resolved.
Show resolved Hide resolved
const card = remoteCardsStore(state => state.getCard(id)) ?? ({} as TrimmedCard);
const dismissCard = remoteCardsStore(state => state.dismissCard);
walmat marked this conversation as resolved.
Show resolved Hide resolved

const { cardKey, accentColor, backgroundColor, primaryButton, imageIcon } = card;

Expand All @@ -95,21 +96,18 @@ export const RemoteCard: React.FC<RemoteCardProps> = ({ card = {} as TrimmedCard
e.stopPropagation();
}
analyticsV2.track(analyticsV2.event.remoteCardDismissed, {
cardKey: cardKey ?? 'unknown-backend-driven-card',
cardKey: cardKey ?? card.sys.id ?? 'unknown-backend-driven-card',
});

const isLastCard = cards.length === 1;
const isLastCard = cards.size === 1;

dismissCard(card.sys.id);
if (carouselRef?.current) {
const currentCardIdx = cards.findIndex(c => c.cardKey === cardKey);
if (currentCardIdx === -1) return;

// check if this is the last card and don't scroll if so
if (isLastCard) return;

carouselRef.current.scrollToIndex({
index: currentCardIdx,
index: Array.from(cards.values()).findIndex(c => c.sys.id === card.sys.id),
animated: true,
});
}
Expand Down Expand Up @@ -143,7 +141,7 @@ export const RemoteCard: React.FC<RemoteCardProps> = ({ card = {} as TrimmedCard
}
};

if (!card) {
if (!card || card.dismissed) {
return null;
}

Expand Down
28 changes: 14 additions & 14 deletions src/components/cards/remote-cards/RemoteCardCarousel.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import React, { useMemo, useRef } from 'react';
import React, { useRef } from 'react';
import { CarouselCard } from '../CarouselCard';
import { useRoute } from '@react-navigation/native';
import { IS_TEST } from '@/env';

import { useRemoteCardContext, RemoteCard } from '@/components/cards/remote-cards';
import { RemoteCard } from '@/components/cards/remote-cards';
import { REMOTE_CARDS, getExperimetalFlag } from '@/config';
import { useDimensions, useWallets } from '@/hooks';
import { useRemoteConfig } from '@/model/remoteConfig';
import { FlashList } from '@shopify/flash-list';
import { TrimmedCard } from '@/resources/cards/cardCollectionQuery';
import { remoteCardsStore } from '@/state/remoteCards/remoteCards';
import Routes from '@/navigation/routesNames';

type RenderItemProps = {
item: TrimmedCard;
item: string;
index: number;
};

Expand All @@ -24,35 +25,34 @@ export const getGutterSizeForCardAmount = (amount: number) => {
};

export const RemoteCardCarousel = () => {
const carouselRef = useRef<FlashList<TrimmedCard>>(null);
const carouselRef = useRef<FlashList<string>>(null);
const { name } = useRoute();
const config = useRemoteConfig();
const { isReadOnlyWallet } = useWallets();

const remoteCardsEnabled = getExperimetalFlag(REMOTE_CARDS) || config.remote_cards_enabled;
const { getCardsForPlacement } = useRemoteCardContext();
const { width } = useDimensions();

const data = useMemo(() => getCardsForPlacement(name as string), [getCardsForPlacement, name]);
const remoteCardsEnabled = getExperimetalFlag(REMOTE_CARDS) || config.remote_cards_enabled;
const getCardIdsForScreen = remoteCardsStore(state => state.getCardIdsForScreen);
const cardIds = getCardIdsForScreen(name as keyof typeof Routes);

const gutterSize = getGutterSizeForCardAmount(data.length);
const gutterSize = getGutterSizeForCardAmount(cardIds.length);

const _renderItem = ({ item }: RenderItemProps) => {
return <RemoteCard card={item} cards={data} gutterSize={gutterSize} carouselRef={carouselRef} />;
return <RemoteCard id={item} gutterSize={gutterSize} carouselRef={carouselRef} />;
};

if (isReadOnlyWallet || IS_TEST || !remoteCardsEnabled || !data.length) {
if (isReadOnlyWallet || IS_TEST || !remoteCardsEnabled || !cardIds.length) {
return null;
}

return (
<CarouselCard
key={name as string}
data={data}
data={cardIds}
carouselItem={{
carouselRef,
renderItem: _renderItem,
keyExtractor: item => item.cardKey!,
keyExtractor: item => item,
placeholder: null,
width: width - gutterSize,
height: 88,
Expand Down
54 changes: 0 additions & 54 deletions src/components/cards/remote-cards/RemoteCardProvider.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/components/cards/remote-cards/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './RemoteCardProvider';
export * from './RemoteCard';
export * from './RemoteCardCarousel';
22 changes: 11 additions & 11 deletions src/components/remote-promo-sheet/RemotePromoSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@ import { get } from 'lodash';
import { useNavigation } from '@/navigation/Navigation';
import { PromoSheet } from '@/components/PromoSheet';
import { useTheme } from '@/theme';
import { CampaignCheckResult } from './checkForCampaign';
import { usePromoSheetQuery } from '@/resources/promoSheet/promoSheetQuery';
import { maybeSignUri } from '@/handlers/imgix';
import { campaigns } from '@/storage';
import { delay } from '@/utils/delay';
import { Linking } from 'react-native';
import Routes from '@/navigation/routesNames';
import { Language } from '@/languages';
import { useAccountSettings } from '@/hooks';
import { remotePromoSheetsStore } from '@/state/remotePromoSheets/remotePromoSheets';
import { RootStackParamList } from '@/navigation/types';

const DEFAULT_HEADER_HEIGHT = 285;
const DEFAULT_HEADER_WIDTH = 390;

type RootStackParamList = {
RemotePromoSheet: CampaignCheckResult;
};

type Item = {
title: Record<keyof Language, string>;
description: Record<keyof Language, string>;
Expand Down Expand Up @@ -59,8 +55,15 @@ export function RemotePromoSheet() {
const { language } = useAccountSettings();

useEffect(() => {
remotePromoSheetsStore.setState({
isShown: true,
lastShownTimestamp: Date.now(),
});

return () => {
campaigns.set(['isCurrentlyShown'], false);
remotePromoSheetsStore.setState({
isShown: false,
});
};
}, []);

Expand All @@ -85,7 +88,7 @@ export function RemotePromoSheet() {

const externalNavigation = useCallback(() => {
Linking.openURL(data?.promoSheet?.primaryButtonProps.props.url);
}, []);
}, [data?.promoSheet?.primaryButtonProps.props.url]);

const internalNavigation = useCallback(() => {
goBack();
Expand Down Expand Up @@ -114,13 +117,10 @@ export function RemotePromoSheet() {
} = data.promoSheet;

const accentColor = (colors as { [key: string]: any })[accentColorString as string] ?? accentColorString;

const backgroundColor = (colors as { [key: string]: any })[backgroundColorString as string] ?? backgroundColorString;

const sheetHandleColor = (colors as { [key: string]: any })[sheetHandleColorString as string] ?? sheetHandleColorString;

const backgroundSignedImageUrl = backgroundImage?.url ? maybeSignUri(backgroundImage.url) : undefined;

const headerSignedImageUrl = headerImage?.url ? maybeSignUri(headerImage.url) : undefined;

return (
Expand Down
Loading
Loading