-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(permit): typefy hooks and allow only 1 permit (#4726)
* fix(hooks): prevent duplicated hooks * feat: add typedHooks and types * feat: generify buildAppDataHooks * feat: use typedHooks everywhere * chore: remove merge and duplicate hooks logic * chore: linter fix * chore: fix build * fix: keep hook store hooks when permit is not in use * fix: use hooks from parameter rather than what's in the current appData * refactor: rename updateHooksOnAppData to replaceHooksOnAppData * chore: remove debug logs (#4722)
- Loading branch information
1 parent
bfb1f29
commit 0e59ce1
Showing
22 changed files
with
163 additions
and
80 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
export { getAppData } from './utils/fullAppData' | ||
export * from './updater/AppDataUpdater' | ||
export { useAppData, useUploadAppData } from './hooks' | ||
export { useAppData, useAppDataHooks, useUploadAppData } from './hooks' | ||
export { filterPermitSignerPermit } from './utils/appDataFilter' | ||
export { updateHooksOnAppData, buildAppData } from './utils/buildAppData' | ||
export { replaceHooksOnAppData, buildAppData } from './utils/buildAppData' | ||
export { buildAppDataHooks } from './utils/buildAppDataHooks' | ||
export * from './utils/getAppDataHooks' | ||
export type { AppDataInfo, UploadAppDataParams } from './types' | ||
export { addPermitHookToHooks, removePermitHookFromHooks } from './utils/typedHooks' | ||
export type { AppDataInfo, UploadAppDataParams, TypedAppDataHooks } from './types' |
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
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
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
12 changes: 6 additions & 6 deletions
12
apps/cowswap-frontend/src/modules/appData/utils/buildAppDataHooks.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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import { AppDataHooks, PostHooks, PreHooks } from '../types' | ||
import { AppDataHooks, CowHook } from '../types' | ||
|
||
export function buildAppDataHooks({ | ||
export function buildAppDataHooks<T extends CowHook[], V extends AppDataHooks>({ | ||
preInteractionHooks, | ||
postInteractionHooks, | ||
}: { | ||
preInteractionHooks?: PreHooks | ||
postInteractionHooks?: PostHooks | ||
}): AppDataHooks | undefined { | ||
preInteractionHooks?: T | ||
postInteractionHooks?: T | ||
}): V | undefined { | ||
if (!preInteractionHooks?.length && !postInteractionHooks?.length) { | ||
return undefined | ||
} | ||
|
||
return { | ||
...(preInteractionHooks?.length ? { pre: preInteractionHooks } : undefined), | ||
...(postInteractionHooks?.length ? { post: postInteractionHooks } : undefined), | ||
} | ||
} as V | ||
} |
70 changes: 70 additions & 0 deletions
70
apps/cowswap-frontend/src/modules/appData/utils/typedHooks.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,70 @@ | ||
import { buildAppDataHooks } from './buildAppDataHooks' | ||
|
||
import { AppDataHooks, CowHook, TypedAppDataHooks, TypedCowHook } from '../types' | ||
|
||
export function cowHookToTypedCowHook(hook: CowHook, type: TypedCowHook['type']): TypedCowHook { | ||
return { ...hook, type } | ||
} | ||
|
||
export function typedAppDataHooksToAppDataHooks(hooks: TypedAppDataHooks | undefined): AppDataHooks | undefined { | ||
if (!hooks) { | ||
return hooks | ||
} | ||
|
||
const { pre, post, ...rest } = hooks | ||
|
||
return { | ||
...rest, | ||
pre: pre ? pre?.map(typedCowHookToCowHook) : undefined, | ||
post: post ? post?.map(typedCowHookToCowHook) : undefined, | ||
} | ||
} | ||
|
||
function typedCowHookToCowHook({ type: _, ...rest }: TypedCowHook): CowHook { | ||
return rest | ||
} | ||
|
||
export function addPermitHookToHooks(hooks: TypedAppDataHooks | undefined, permit: CowHook): AppDataHooks | undefined { | ||
if (!hooks) { | ||
return buildAppDataHooks({ preInteractionHooks: [permit] }) | ||
} | ||
|
||
const { pre, ...rest } = hooks | ||
|
||
// Replace permit if existing | ||
let replaced = false | ||
|
||
const updatedPreHooks = | ||
pre?.reduce<TypedCowHook[]>((acc, hook) => { | ||
if (hook.type === 'permit') { | ||
replaced = true | ||
acc.push({ ...hook, ...permit }) | ||
} else { | ||
acc.push(hook) | ||
} | ||
return acc | ||
}, []) || [] | ||
|
||
if (!replaced) { | ||
updatedPreHooks.push({ ...permit, type: 'permit' }) | ||
} | ||
|
||
return typedAppDataHooksToAppDataHooks({ ...rest, pre: updatedPreHooks }) | ||
} | ||
|
||
export function removePermitHookFromHooks(hooks: TypedAppDataHooks | undefined): AppDataHooks | undefined { | ||
if (!hooks) { | ||
return hooks | ||
} | ||
|
||
const { pre, ...rest } = hooks | ||
|
||
const filteredPre = pre?.reduce<TypedCowHook[]>((acc, hook) => { | ||
if (hook.type !== 'permit') { | ||
acc.push(hook) | ||
} | ||
return acc | ||
}, []) | ||
|
||
return typedAppDataHooksToAppDataHooks({ ...rest, pre: filteredPre }) | ||
} |
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.