Repository navigation
Derive the invite Approver row from the real approval workflows #100327
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
Changes from all commits
507492c
a938794
37b65d4
9fdd377
d010de7
d1c7391
1de1a9a
febbbf3
5bce7a5
1b01fcf
c7ec704
5c8aa37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /** | ||
| * Hook that derives a workspace's approval workflows from the source of truth and reports whether the workspace | ||
| * actually has a custom (advanced) approval workflow, so callers don't trust the stale `policy.approvalMode` flag. | ||
| */ | ||
| import {isHRAdvancedMode} from '@libs/merge/HRUtils'; | ||
| import {isControlPolicy} from '@libs/PolicyUtils'; | ||
| import {convertApprovalWorkflowRulesToWorkflows, convertPolicyEmployeesToApprovalWorkflows, filterRulesForPolicy, getApprovalWorkflowRulesForPolicy} from '@libs/WorkflowUtils'; | ||
|
|
||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {PersonalDetailsList, Policy} from '@src/types/onyx'; | ||
| import type ApprovalWorkflow from '@src/types/onyx/ApprovalWorkflow'; | ||
| import type {Member} from '@src/types/onyx/ApprovalWorkflow'; | ||
| import type Rule from '@src/types/onyx/Rule'; | ||
|
|
||
| import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; | ||
|
|
||
| import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails'; | ||
| import useLocalize from './useLocalize'; | ||
| import useOnyx from './useOnyx'; | ||
| import usePermissions from './usePermissions'; | ||
| import {useAllPersonalDetails} from './usePersonalDetails'; | ||
|
|
||
| type UseApprovalWorkflowsResult = { | ||
| /** Every approval workflow the workspace's data describes, derived from the policy employees or the approval-workflow rules */ | ||
| approvalWorkflows: ApprovalWorkflow[]; | ||
|
|
||
| /** The subset of `approvalWorkflows` the Workflows tab displays: only the default workflow unless the workspace uses advanced approvals */ | ||
| filteredApprovalWorkflows: ApprovalWorkflow[]; | ||
|
|
||
| /** List of available members that can be selected in a workflow */ | ||
| availableMembers: Member[]; | ||
|
|
||
| /** Emails that are already used as approvers in the configured workflows */ | ||
| usedApproverEmails: string[]; | ||
|
|
||
| /** Whether the workspace has a custom (advanced) approval workflow on top of the plain default one */ | ||
| isAdvanceApproval: boolean; | ||
|
|
||
| /** The workspace's approval-workflow rules, exposed so consumers don't need a second `RULE` subscription */ | ||
| rulesCollection: OnyxCollection<Rule>; | ||
|
|
||
| /** Personal details, exposed so consumers don't need a second `PERSONAL_DETAILS_LIST` subscription */ | ||
| personalDetails: OnyxEntry<PersonalDetailsList>; | ||
| }; | ||
|
|
||
| /** | ||
| * Derives approval workflows from policy employees or approval rules when `MULTIPLE_APPROVERS` is enabled. | ||
| * | ||
| * Prefer `isAdvanceApproval` over `policy.approvalMode`: the stored value is updated optimistically and can drift from the actual workflow structure. | ||
| */ | ||
| function useApprovalWorkflows(policy: OnyxEntry<Policy>): UseApprovalWorkflowsResult { | ||
| const policyID = policy?.id; | ||
| const {localeCompare} = useLocalize(); | ||
| const {isBetaEnabled} = usePermissions(); | ||
| const {login: currentUserLogin = ''} = useCurrentUserPersonalDetails(); | ||
| const [personalDetails] = useAllPersonalDetails(); | ||
| const [rulesCollection] = useOnyx(ONYXKEYS.COLLECTION.RULE, {selector: (rules: OnyxCollection<Rule>) => filterRulesForPolicy(rules, policyID)}); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Valid — fixed in a938794. Confirmed the mechanism: Went with the fetch rather than deferring: the collection is never fetched at all on that path, so deferring would hide the row indefinitely, and blocking submit would break |
||
|
|
||
| const isMultipleApproversBetaEnabled = isBetaEnabled(CONST.BETAS.MULTIPLE_APPROVERS); | ||
| const params = { | ||
| policy, | ||
| personalDetails: personalDetails ?? {}, | ||
| localeCompare, | ||
| currentUserLogin, | ||
| rules: getApprovalWorkflowRulesForPolicy(rulesCollection, policyID), | ||
| }; | ||
| const {approvalWorkflows, availableMembers, usedApproverEmails} = isMultipleApproversBetaEnabled | ||
| ? convertApprovalWorkflowRulesToWorkflows(params) | ||
| : convertPolicyEmployeesToApprovalWorkflows(params); | ||
|
|
||
| const isAdvanceApproval = (approvalWorkflows.length > 1 || (approvalWorkflows?.at(0)?.approvers ?? []).length > 1) && isControlPolicy(policy); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For a non-beta Control workspace whose Useful? React with 👍 / 👎.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The consequence is real but the premise state isn't reachable, so I don't think this needs a change. The
Toggling Workflows off is also rejected by the backend when a custom workflow exists, which closes the other route into a non- The suggested fix isn't clearly safer either: routing goes through |
||
|
|
||
| const filteredApprovalWorkflows = | ||
| isMultipleApproversBetaEnabled || | ||
| policy?.approvalMode === CONST.POLICY.APPROVAL_MODE.ADVANCED || | ||
| policy?.approvalMode === CONST.POLICY.APPROVAL_MODE.DYNAMICEXTERNAL || | ||
| isHRAdvancedMode(policy) | ||
| ? approvalWorkflows | ||
| : approvalWorkflows.filter((workflow) => workflow.isDefault); | ||
|
|
||
| return {approvalWorkflows, filteredApprovalWorkflows, availableMembers, usedApproverEmails, isAdvanceApproval, rulesCollection, personalDetails}; | ||
| } | ||
|
|
||
| export default useApprovalWorkflows; | ||
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.
❌ CONSISTENCY-13 (docs)
This is a new, non-trivial module file (a hook plus a result type), but its first non-import token is the
UseApprovalWorkflowsResulttype declaration with no header comment describing the file. A one-line header orients the next reader immediately instead of forcing them to scan down to the function JSDoc to learn the module's purpose.Add a short header comment at the top (after the imports or above them), e.g.:
Reviewed at: 5bce7a5 | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
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.
@MelvinBot address #100327 (comment)
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.
Added a file header to
useApprovalWorkflows.tsin 1b01fcf. It describes what the hook derives and why callers should use it instead ofpolicy.approvalMode. Lint passes.