Skip to content

Commit 7f6b8bf

Browse files
committed
refactor(banners): lift up show responsability
1 parent 137bfba commit 7f6b8bf

File tree

2 files changed

+10
-38
lines changed

2 files changed

+10
-38
lines changed

packages/banners/src/translators/reactjs/useBanner.test.ts

+3-19
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,27 @@ describe('useBanner', () => {
2020
it('should return the correct initial state', () => {
2121
const id = 'test-banner'
2222
const dismissedAt = 0
23-
const shouldShowCb = jest.fn().mockReturnValue(true)
2423

2524
;(manager.dismissedAt as jest.Mock).mockReturnValueOnce(dismissedAt)
2625

27-
const {result} = renderHook(() => useBanner({id, manager, shouldShowCb}))
26+
const {result} = renderHook(() => useBanner({id, manager}))
2827

2928
expect(result.current.dismissedAt).toBe(dismissedAt)
30-
expect(result.current.shouldShow).toBe(true)
31-
expect(shouldShowCb).toHaveBeenCalledWith({dismissedAt})
29+
expect(result.current.dismissedAt).toBe(dismissedAt)
3230
})
3331

3432
it('should call dismiss method on manager', () => {
3533
const id = 'test-banner'
3634
const dismissedAt = 0
37-
const shouldShowCb = jest.fn().mockReturnValue(true)
3835

3936
;(manager.dismissedAt as jest.Mock).mockReturnValueOnce(dismissedAt)
4037

41-
const {result} = renderHook(() => useBanner({id, manager, shouldShowCb}))
38+
const {result} = renderHook(() => useBanner({id, manager}))
4239

4340
act(() => {
4441
result.current.dismiss()
4542
})
4643

4744
expect(manager.dismiss).toHaveBeenCalledWith(id)
4845
})
49-
50-
it('should update shouldShow based on shouldShowCb', () => {
51-
const id = 'test-banner'
52-
const dismissedAt = 0
53-
const shouldShowCb = jest.fn().mockReturnValue(false)
54-
55-
;(manager.dismissedAt as jest.Mock).mockReturnValueOnce(dismissedAt)
56-
57-
const {result} = renderHook(() => useBanner({id, manager, shouldShowCb}))
58-
59-
expect(result.current.shouldShow).toBe(false)
60-
expect(shouldShowCb).toHaveBeenCalledWith({dismissedAt})
61-
})
6246
})

packages/banners/src/translators/reactjs/useBanner.ts

+7-19
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,20 @@ import {Banners} from '@yoroi/types'
22
import {freeze} from 'immer'
33
import * as React from 'react'
44

5-
/**
6-
* Custom hook to manage the state of a banner.
7-
*
8-
* @param {Object} params - The parameters for the hook.
9-
* @param {string} params.id - The unique identifier for the banner.
10-
* @param {Banners.Manager} params.manager - The manager responsible for handling banner state.
11-
* @param {Function} params.shouldShowCb - Callback function to determine if the banner should be shown.
12-
* @param {Object} params.shouldShowCb.params - Parameters for the callback function.
13-
* @param {number} params.shouldShowCb.params.dismissedAt - The timestamp when the banner was dismissed.
14-
* @returns {Object} An object containing the banner's dismissed timestamp, a function to dismiss the banner, and a boolean indicating if the banner should be shown.
15-
* @note Remember wrapping the shouldShowCb function in a useCallback hook to prevent unnecessary re-renders.
16-
*/
175
export function useBanner({
186
id,
197
manager,
20-
shouldShowCb,
218
}: Readonly<{
229
id: string
2310
manager: Readonly<Banners.Manager>
24-
shouldShowCb: ({dismissedAt}: {dismissedAt: number}) => boolean
2511
}>) {
12+
const [dismissedAt, setDismissedAt] = React.useState(manager.dismissedAt(id))
2613
return React.useMemo(() => {
27-
const dismissedAt = manager.dismissedAt(id)
28-
const dismiss = () => manager.dismiss(id)
29-
const shouldShow = shouldShowCb({dismissedAt})
14+
const dismiss = () => {
15+
manager.dismiss(id)
16+
setDismissedAt(manager.dismissedAt(id))
17+
}
3018

31-
return freeze({dismissedAt, dismiss, shouldShow})
32-
}, [id, manager, shouldShowCb])
19+
return freeze({dismissedAt, dismiss})
20+
}, [id, manager, dismissedAt])
3321
}

0 commit comments

Comments
 (0)