-
Notifications
You must be signed in to change notification settings - Fork 485
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
feat: targeted features #4992
Merged
Merged
feat: targeted features #4992
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { FEATURES } from '@/utils/chains' | ||
|
||
export const TARGETED_FEATURES = [ | ||
{ id: 3, feature: FEATURES.TARGETED_NESTED_SAFES }, | ||
{ id: 4, feature: FEATURES.TARGETED_MASS_PAYOUTS }, | ||
] as const |
140 changes: 140 additions & 0 deletions
140
apps/web/src/features/targetedFeatures/hooks/__tests__/useIsOutreachSafe.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { faker } from '@faker-js/faker' | ||
import * as targetedMessages from '@safe-global/store/gateway/AUTO_GENERATED/targeted-messages' | ||
|
||
import * as useSafeInfoHook from '@/hooks/useSafeInfo' | ||
import { safeInfoBuilder } from '@/tests/builders/safe' | ||
import { renderHook } from '@/tests/test-utils' | ||
import { useIsOutreachSafe } from '../useIsOutreachSafe' | ||
|
||
describe('useIsOutreachSafe', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('returns true if the Safe is targeted for messaging', () => { | ||
const safeInfo = safeInfoBuilder().build() | ||
const outreachId = faker.number.int() | ||
jest.spyOn(useSafeInfoHook, 'default').mockReturnValue({ | ||
safeAddress: safeInfo.address.value, | ||
safe: { | ||
...safeInfo, | ||
deployed: true, | ||
}, | ||
safeLoaded: true, | ||
safeLoading: false, | ||
safeError: undefined, | ||
}) | ||
jest.spyOn(targetedMessages, 'useTargetedMessagingGetTargetedSafeV1Query').mockReturnValue({ | ||
data: { | ||
outreachId, | ||
address: safeInfo.address.value, | ||
}, | ||
refetch: jest.fn(), | ||
}) | ||
|
||
const { result } = renderHook(() => useIsOutreachSafe(outreachId)) | ||
|
||
expect(result.current).toBe(true) | ||
}) | ||
|
||
it('returns false if the Safe is not targeted for messaging', () => { | ||
const safeInfo = safeInfoBuilder().build() | ||
const outreachId = faker.number.int() | ||
jest.spyOn(useSafeInfoHook, 'default').mockReturnValue({ | ||
safeAddress: safeInfo.address.value, | ||
safe: { | ||
...safeInfo, | ||
deployed: true, | ||
}, | ||
safeLoaded: true, | ||
safeLoading: false, | ||
safeError: undefined, | ||
}) | ||
jest.spyOn(targetedMessages, 'useTargetedMessagingGetTargetedSafeV1Query').mockReturnValue({ | ||
data: undefined, | ||
error: new Error('Safe not targeted'), | ||
refetch: jest.fn(), | ||
}) | ||
|
||
const { result } = renderHook(() => useIsOutreachSafe(outreachId)) | ||
|
||
expect(result.current).toBe(false) | ||
}) | ||
|
||
it('returns false if the data is not available', () => { | ||
const safeInfo = safeInfoBuilder().build() | ||
const outreachId = faker.number.int() | ||
jest.spyOn(useSafeInfoHook, 'default').mockReturnValue({ | ||
safeAddress: safeInfo.address.value, | ||
safe: { | ||
...safeInfo, | ||
deployed: true, | ||
}, | ||
safeLoaded: true, | ||
safeLoading: false, | ||
safeError: undefined, | ||
}) | ||
jest.spyOn(targetedMessages, 'useTargetedMessagingGetTargetedSafeV1Query').mockReturnValue({ | ||
data: undefined, // Yet to be fetched | ||
refetch: jest.fn(), | ||
}) | ||
|
||
const { result } = renderHook(() => useIsOutreachSafe(outreachId)) | ||
|
||
expect(result.current).toBe(false) | ||
}) | ||
|
||
it('returns false if the outreachId does not match', () => { | ||
const safeInfo = safeInfoBuilder().build() | ||
const outreachId = faker.number.int() | ||
const otherOutreachId = 'OTHER_FEATURE' | ||
jest.spyOn(useSafeInfoHook, 'default').mockReturnValue({ | ||
safeAddress: safeInfo.address.value, | ||
safe: { | ||
...safeInfo, | ||
deployed: true, | ||
}, | ||
safeLoaded: true, | ||
safeLoading: false, | ||
safeError: undefined, | ||
}) | ||
jest.spyOn(targetedMessages, 'useTargetedMessagingGetTargetedSafeV1Query').mockReturnValue({ | ||
data: { | ||
outreachId: otherOutreachId, | ||
address: safeInfo.address.value, | ||
}, | ||
refetch: jest.fn(), | ||
}) | ||
|
||
const { result } = renderHook(() => useIsOutreachSafe(outreachId)) | ||
|
||
expect(result.current).toBe(false) | ||
}) | ||
|
||
it('returns false if the address does not match', () => { | ||
const safeInfo = safeInfoBuilder().build() | ||
const otherAddress = faker.finance.ethereumAddress() | ||
const outreachId = faker.number.int() | ||
jest.spyOn(useSafeInfoHook, 'default').mockReturnValue({ | ||
safeAddress: safeInfo.address.value, | ||
safe: { | ||
...safeInfo, | ||
deployed: true, | ||
}, | ||
safeLoaded: true, | ||
safeLoading: false, | ||
safeError: undefined, | ||
}) | ||
jest.spyOn(targetedMessages, 'useTargetedMessagingGetTargetedSafeV1Query').mockReturnValue({ | ||
data: { | ||
outreachId, | ||
address: otherAddress, | ||
}, | ||
refetch: jest.fn(), | ||
}) | ||
|
||
const { result } = renderHook(() => useIsOutreachSafe(outreachId)) | ||
|
||
expect(result.current).toBe(false) | ||
}) | ||
}) |
74 changes: 74 additions & 0 deletions
74
apps/web/src/features/targetedFeatures/hooks/__tests__/useIsTargetedFeature.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { faker } from '@faker-js/faker' | ||
|
||
import * as useOutreachSafeHook from '@/features/targetedFeatures/hooks/useIsOutreachSafe' | ||
import * as useChainsHook from '@/hooks/useChains' | ||
import * as useLocalStorageHook from '@/services/local-storage/useLocalStorage' | ||
import { renderHook, waitFor } from '@/tests/test-utils' | ||
import { useIsTargetedFeature } from '../useIsTargetedFeature' | ||
import { TARGETED_FEATURES } from '../../constants' | ||
|
||
const targetedFeatures = TARGETED_FEATURES.map((f) => f.feature) | ||
|
||
describe('useIsTargetedFeature', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('returns true if the Safe is targeted and the feature is enabled', () => { | ||
const feature = faker.helpers.arrayElement(targetedFeatures) | ||
jest.spyOn(useChainsHook, 'useHasFeature').mockReturnValue(true) | ||
jest.spyOn(useOutreachSafeHook, 'useIsOutreachSafe').mockReturnValue(true) | ||
jest.spyOn(useLocalStorageHook, 'default').mockReturnValue([[feature], jest.fn()]) | ||
|
||
const { result } = renderHook(() => useIsTargetedFeature(feature)) | ||
|
||
expect(result.current).toBe(true) | ||
}) | ||
|
||
it('returns true if the the feature is unlocked and enabled', () => { | ||
const feature = faker.helpers.arrayElement(targetedFeatures) | ||
jest.spyOn(useChainsHook, 'useHasFeature').mockReturnValue(true) | ||
jest.spyOn(useOutreachSafeHook, 'useIsOutreachSafe').mockReturnValue(false) | ||
jest.spyOn(useLocalStorageHook, 'default').mockReturnValue([[feature], jest.fn()]) | ||
|
||
const { result } = renderHook(() => useIsTargetedFeature(feature)) | ||
|
||
expect(result.current).toBe(true) | ||
}) | ||
|
||
it('returns false if the Safe is targeted but the feature is disabled', () => { | ||
const feature = faker.helpers.arrayElement(targetedFeatures) | ||
jest.spyOn(useChainsHook, 'useHasFeature').mockReturnValue(false) | ||
jest.spyOn(useOutreachSafeHook, 'useIsOutreachSafe').mockReturnValue(true) | ||
jest.spyOn(useLocalStorageHook, 'default').mockReturnValue([[], jest.fn()]) | ||
|
||
const { result } = renderHook(() => useIsTargetedFeature(feature)) | ||
|
||
expect(result.current).toBe(false) | ||
}) | ||
|
||
it('returns false if the Safe is targeted and the feature is unlocked', () => { | ||
const feature = faker.helpers.arrayElement(targetedFeatures) | ||
jest.spyOn(useChainsHook, 'useHasFeature').mockReturnValue(false) | ||
jest.spyOn(useOutreachSafeHook, 'useIsOutreachSafe').mockReturnValue(true) | ||
jest.spyOn(useLocalStorageHook, 'default').mockReturnValue([[feature], jest.fn()]) | ||
|
||
const { result } = renderHook(() => useIsTargetedFeature(feature)) | ||
|
||
expect(result.current).toBe(false) | ||
}) | ||
|
||
it('caches targeted/enabled features', () => { | ||
const feature = faker.helpers.arrayElement(targetedFeatures) | ||
const setLocalStorageMock = jest.fn() | ||
jest.spyOn(useChainsHook, 'useHasFeature').mockReturnValue(true) | ||
jest.spyOn(useOutreachSafeHook, 'useIsOutreachSafe').mockReturnValue(true) | ||
jest.spyOn(useLocalStorageHook, 'default').mockReturnValue([[feature], jest.fn()]) | ||
|
||
renderHook(() => useIsTargetedFeature(feature)) | ||
|
||
waitFor(() => { | ||
expect(setLocalStorageMock).toHaveBeenCalledWith([feature]) | ||
}) | ||
}) | ||
}) |
18 changes: 18 additions & 0 deletions
18
apps/web/src/features/targetedFeatures/hooks/useIsOutreachSafe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useTargetedMessagingGetTargetedSafeV1Query } from '@safe-global/store/gateway/AUTO_GENERATED/targeted-messages' | ||
|
||
import useSafeInfo from '@/hooks/useSafeInfo' | ||
import { sameAddress } from '@/utils/addresses' | ||
|
||
export function useIsOutreachSafe(outreachId: number): boolean { | ||
const { safe } = useSafeInfo() | ||
const { data } = useTargetedMessagingGetTargetedSafeV1Query( | ||
{ | ||
outreachId, | ||
chainId: safe.chainId, | ||
safeAddress: safe.address.value, | ||
}, | ||
{ skip: !safe.address.value }, | ||
) | ||
|
||
return data?.outreachId === outreachId && sameAddress(data.address, safe.address.value) | ||
} |
29 changes: 29 additions & 0 deletions
29
apps/web/src/features/targetedFeatures/hooks/useIsTargetedFeature.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useEffect } from 'react' | ||
|
||
import { useIsOutreachSafe } from '@/features/targetedFeatures/hooks/useIsOutreachSafe' | ||
import { useHasFeature } from '@/hooks/useChains' | ||
import useLocalStorage from '@/services/local-storage/useLocalStorage' | ||
import { TARGETED_FEATURES } from '../constants' | ||
|
||
const UNLOCKED_FEATURES_LS_KEY = 'unlockedFeatures' | ||
|
||
type TargetedFeatures = (typeof TARGETED_FEATURES)[number]['feature'] | ||
|
||
export function useIsTargetedFeature(feature: TargetedFeatures): boolean { | ||
const hasFeature = useHasFeature(feature) | ||
|
||
const outreachId = TARGETED_FEATURES.find((f) => f.feature === feature)!.id | ||
const isTargeted = useIsOutreachSafe(outreachId) | ||
|
||
// Should a targeted Safe have been opened, we "unlock" the feature across the app | ||
const [unlockedFeatures = [], setUnlockedFeatures] = | ||
useLocalStorage<Array<TargetedFeatures>>(UNLOCKED_FEATURES_LS_KEY) | ||
const isUnlocked = unlockedFeatures.includes(feature) | ||
useEffect(() => { | ||
if (hasFeature && isTargeted && !isUnlocked) { | ||
setUnlockedFeatures([...unlockedFeatures, feature]) | ||
} | ||
}, [feature, hasFeature, isTargeted, isUnlocked, setUnlockedFeatures, unlockedFeatures]) | ||
|
||
return !!hasFeature && (isTargeted || isUnlocked) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is no longer in the types.