Skip to content

Commit 000b9cf

Browse files
committed
chore(wallet-mobile): add exmple of banner managed by banners package
1 parent 91be713 commit 000b9cf

File tree

6 files changed

+155
-8
lines changed

6 files changed

+155
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {time} from '../../../kernel/constants'
2+
import {shouldShowDRepConsiderDelegating} from './should-show-drep-consider-delegating'
3+
4+
describe('shouldShowDRepConsiderDelegating', () => {
5+
const moreThanOneMonthAgo = Date.now() - time.oneMonth - time.oneSecond
6+
const lessThanOneSecondAgo = Date.now() - time.oneSecond
7+
8+
it.each`
9+
isStaking | currentDRepIdHex | yoroiDRepIdHex | dismissedAt | expected
10+
${true} | ${'DE'} | ${'AD'} | ${moreThanOneMonthAgo} | ${true}
11+
${false} | ${'DE'} | ${'AD'} | ${moreThanOneMonthAgo} | ${false}
12+
${true} | ${'DE'} | ${'AD'} | ${lessThanOneSecondAgo} | ${false}
13+
${true} | ${'AD'} | ${'AD'} | ${moreThanOneMonthAgo} | ${false}
14+
`(
15+
'returns $expected when isStaking is $isStaking, currentDRepId is $currentDRepId, yoroiDRep is $yoroiDRepId, and dismissedAt is $dismissedAt',
16+
({isStaking, currentDRepIdHex, yoroiDRepIdHex, dismissedAt, expected}) => {
17+
const result = shouldShowDRepConsiderDelegating({
18+
isStaking,
19+
currentDRepIdHex,
20+
yoroiDRepIdHex,
21+
dismissedAt,
22+
})
23+
expect(result).toBe(expected)
24+
},
25+
)
26+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {time} from '../../../kernel/constants'
2+
3+
export const shouldShowDRepConsiderDelegating = ({
4+
isStaking,
5+
currentDRepIdHex,
6+
yoroiDRepIdHex,
7+
dismissedAt,
8+
}: Readonly<{
9+
isStaking: boolean
10+
currentDRepIdHex: string
11+
yoroiDRepIdHex: string
12+
dismissedAt: number
13+
}>) => {
14+
const isYoroiDRep = currentDRepIdHex === yoroiDRepIdHex
15+
const hasBeenThirtyDays = dismissedAt + time.oneMonth < Date.now()
16+
return !isYoroiDRep && hasBeenThirtyDays && isStaking
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum BannerId {
2+
DRepConsiderDelegating = 'drep-consider-delegating',
3+
}
4+
5+
export type BannerStorageKey = `${BannerId}`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {useBanner, useBanners} from '@yoroi/banners'
2+
import * as React from 'react'
3+
import {Animated, Button, Text} from 'react-native'
4+
5+
import {yoroiDRepIdHex} from '../../../kernel/constants'
6+
import {shouldShowDRepConsiderDelegating} from '../common/should-show-drep-consider-delegating'
7+
import {BannerId, BannerStorageKey} from '../common/types'
8+
9+
type Props = {
10+
isStaking: boolean
11+
currentDRepIdHex: string
12+
}
13+
14+
export const ShowBannerDRepConsiderDelegating = ({isStaking, currentDRepIdHex}: Props) => {
15+
const {manager} = useBanners<BannerStorageKey>()
16+
const {dismiss, dismissedAt} = useBanner({id: BannerId.DRepConsiderDelegating, manager})
17+
18+
const fadeAnim = React.useRef(new Animated.Value(1)).current
19+
const [isFadingOut, setIsFadingOut] = React.useState(false)
20+
21+
const shouldShow = shouldShowDRepConsiderDelegating({
22+
yoroiDRepIdHex,
23+
currentDRepIdHex,
24+
isStaking,
25+
dismissedAt,
26+
})
27+
28+
if (!shouldShow && !isFadingOut) {
29+
return null
30+
}
31+
32+
const handleDismiss = () => {
33+
if (isFadingOut) return
34+
35+
setIsFadingOut(true)
36+
Animated.timing(fadeAnim, {
37+
toValue: 0,
38+
duration: 300,
39+
useNativeDriver: true,
40+
}).start(() => {
41+
dismiss()
42+
43+
fadeAnim.setValue(1)
44+
setIsFadingOut(false)
45+
})
46+
}
47+
48+
return (
49+
<Animated.View style={{opacity: fadeAnim}}>
50+
<Text>Consider delegating to DREP {dismissedAt}</Text>
51+
52+
<Button onPress={handleDismiss} title="Dismiss" />
53+
</Animated.View>
54+
)
55+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,53 @@
1+
import {bannersManagerMaker, BannersProvider} from '@yoroi/banners'
2+
import {observableStorageMaker} from '@yoroi/common'
3+
import {useTheme} from '@yoroi/theme'
14
import * as React from 'react'
25
import {View} from 'react-native'
6+
import {SafeAreaView} from 'react-native-safe-area-context'
7+
8+
import {Button} from '../../components/Button/Button'
9+
import {Spacer} from '../../components/Spacer/Spacer'
10+
import {BannerId, BannerStorageKey} from '../Banners/common/types'
11+
import {ShowBannerDRepConsiderDelegating} from '../Banners/useCases/ShowBannerDRepConsiderDelegating'
12+
import {useSelectedWallet} from '../WalletManager/common/hooks/useSelectedWallet'
13+
14+
// --------------------------------------------------------------------------------------------------
15+
// PINEED: this component can be replaced at anytime by anyone
316

4-
// Please, clean component after use
517
export const Playground = () => {
6-
return <View></View>
7-
}
18+
const {
19+
atoms: {pl_lg, pr_lg},
20+
color: {el_primary_medium},
21+
} = useTheme()
22+
const {wallet} = useSelectedWallet()
23+
const walletStorage = wallet.networkManager.rootStorage.join(`${wallet.id}/`)
24+
const bannersStorage = observableStorageMaker<false, BannerStorageKey>(walletStorage.join('banners/'))
25+
const bannersManager = bannersManagerMaker<BannerStorageKey>({
26+
storage: bannersStorage,
27+
})
28+
const [refresh, setRefresh] = React.useState(0)
29+
30+
bannersStorage.onChange([BannerId.DRepConsiderDelegating], () => {
31+
console.log(`manager.dismissedAt:`, bannersManager.dismissedAt(BannerId.DRepConsiderDelegating))
32+
})
833

9-
// const styles = StyleSheet.create({})
34+
return (
35+
<SafeAreaView edges={['top', 'left', 'right']} style={[pl_lg, pr_lg]}>
36+
<Button
37+
title="Reset"
38+
onPress={() => {
39+
bannersStorage.setItem(BannerId.DRepConsiderDelegating, 0)
40+
setRefresh(() => refresh + 1)
41+
}}
42+
/>
43+
44+
<Spacer height={20} />
45+
46+
<View style={{backgroundColor: el_primary_medium}}>
47+
<BannersProvider manager={bannersManager} key={refresh}>
48+
<ShowBannerDRepConsiderDelegating isStaking={true} currentDRepIdHex="123" />
49+
</BannersProvider>
50+
</View>
51+
</SafeAreaView>
52+
)
53+
}

apps/wallet-mobile/translations/messages/src/legacy/Dashboard/Dashboard.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"defaultMessage": "!!!Go to Staking Center",
55
"file": "src/legacy/Dashboard/Dashboard.tsx",
66
"start": {
7-
"line": 241,
7+
"line": 237,
88
"column": 23,
9-
"index": 7828
9+
"index": 7685
1010
},
1111
"end": {
12-
"line": 244,
12+
"line": 240,
1313
"column": 3,
14-
"index": 7961
14+
"index": 7818
1515
}
1616
}
1717
]

0 commit comments

Comments
 (0)