-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add logic to conditionally render an action's redo option
- Loading branch information
Showing
8 changed files
with
315 additions
and
128 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
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
135 changes: 135 additions & 0 deletions
135
src/components/common/ColonyActionsTable/hooks/useBuildRedoEnabledActionsMap.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,135 @@ | ||
import { useEffect, useState, useCallback } from 'react'; | ||
|
||
import { ColonyActionType, useGetColonyHistoricRoleRolesLazyQuery } from '~gql'; | ||
import { type ActivityFeedColonyAction } from '~hooks/useActivityFeed/types.ts'; | ||
import { ExtendedColonyActionType } from '~types/actions.ts'; | ||
import { getHistoricRolesDatabaseId } from '~utils/databaseId.ts'; | ||
import { transformActionRolesToColonyRoles } from '~v5/common/CompletedAction/partials/SetUserRoles/utils.ts'; | ||
|
||
export const useBuildRedoEnabledActionsMap = ({ | ||
colonyActions, | ||
colonyActionsLoading, | ||
}: { | ||
colonyActions: ActivityFeedColonyAction[]; | ||
colonyActionsLoading: boolean; | ||
}) => { | ||
const [redoEnabledActionsMap, setRedoEnabledActionsMap] = useState< | ||
Record<string, boolean> | ||
>({}); | ||
|
||
const [getHistoricRoles] = useGetColonyHistoricRoleRolesLazyQuery({ | ||
fetchPolicy: 'cache-and-network', | ||
}); | ||
|
||
const stringifiedColonyActions = JSON.stringify(colonyActions); | ||
|
||
const checkRedoActionCriteria = useCallback(async () => { | ||
const originalColonyActions = JSON.parse( | ||
stringifiedColonyActions, | ||
) as ActivityFeedColonyAction[]; | ||
|
||
if ( | ||
colonyActionsLoading || | ||
!originalColonyActions || | ||
originalColonyActions.length === 0 | ||
) { | ||
return; | ||
} | ||
|
||
const updatedActionsMap: Record<string, boolean> = {}; | ||
const asyncPromises: Promise<void>[] = []; | ||
|
||
originalColonyActions.forEach((colonyAction) => { | ||
if ( | ||
colonyAction.type === ColonyActionType.SetUserRoles || | ||
colonyAction.type === ColonyActionType.SetUserRolesMotion || | ||
colonyAction.type === ColonyActionType.SetUserRolesMultisig | ||
) { | ||
const { | ||
blockNumber, | ||
colonyAddress, | ||
fromDomain, | ||
recipientAddress, | ||
rolesAreMultiSig, | ||
roles, | ||
motionData, | ||
multiSigData, | ||
} = colonyAction; | ||
|
||
const asyncTask = async () => { | ||
const result = await getHistoricRoles({ | ||
variables: { | ||
id: getHistoricRolesDatabaseId({ | ||
blockNumber, | ||
colonyAddress, | ||
nativeId: fromDomain?.nativeId, | ||
recipientAddress, | ||
isMultiSig: rolesAreMultiSig, | ||
}), | ||
}, | ||
}); | ||
|
||
const dbPermissionsNew = transformActionRolesToColonyRoles( | ||
result?.data?.getColonyHistoricRole || roles, | ||
); | ||
|
||
const isMotion = !!motionData; | ||
const isMultiSig = !!multiSigData; | ||
|
||
updatedActionsMap[colonyAction.transactionHash] = !( | ||
!!dbPermissionsNew.length || | ||
(!!isMotion && !motionData?.isFinalized) || | ||
(!!isMultiSig && !multiSigData?.isExecuted) | ||
); | ||
}; | ||
|
||
asyncPromises.push(asyncTask()); | ||
} else { | ||
switch ( | ||
colonyAction.type as ColonyActionType | ExtendedColonyActionType | ||
) { | ||
case ColonyActionType.AddVerifiedMembers: | ||
case ColonyActionType.AddVerifiedMembersMotion: | ||
case ColonyActionType.AddVerifiedMembersMultisig: | ||
case ColonyActionType.CreateDecisionMotion: | ||
case ColonyActionType.ColonyEdit: | ||
case ColonyActionType.ColonyEditMotion: | ||
case ColonyActionType.ColonyEditMultisig: | ||
case ColonyActionType.CreateDomain: | ||
case ColonyActionType.CreateDomainMotion: | ||
case ColonyActionType.CreateDomainMultisig: | ||
case ColonyActionType.EditDomain: | ||
case ColonyActionType.EditDomainMotion: | ||
case ColonyActionType.EditDomainMultisig: | ||
case ColonyActionType.RemoveVerifiedMembers: | ||
case ColonyActionType.RemoveVerifiedMembersMotion: | ||
case ColonyActionType.RemoveVerifiedMembersMultisig: | ||
case ColonyActionType.UnlockToken: | ||
case ColonyActionType.UnlockTokenMotion: | ||
case ColonyActionType.UnlockTokenMultisig: | ||
case ColonyActionType.VersionUpgrade: | ||
case ColonyActionType.VersionUpgradeMotion: | ||
case ColonyActionType.VersionUpgradeMultisig: | ||
case ExtendedColonyActionType.UpdateColonyObjective: | ||
case ExtendedColonyActionType.UpdateColonyObjectiveMotion: | ||
case ExtendedColonyActionType.UpdateColonyObjectiveMultisig: | ||
updatedActionsMap[colonyAction.transactionHash] = false; | ||
break; | ||
|
||
default: | ||
updatedActionsMap[colonyAction.transactionHash] = true; | ||
} | ||
} | ||
}); | ||
|
||
await Promise.all(asyncPromises); | ||
|
||
setRedoEnabledActionsMap(updatedActionsMap); | ||
}, [colonyActionsLoading, stringifiedColonyActions, getHistoricRoles]); | ||
|
||
useEffect(() => { | ||
checkRedoActionCriteria(); | ||
}, [checkRedoActionCriteria]); | ||
|
||
return { redoEnabledActionsMap }; | ||
}; |
Oops, something went wrong.