-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Create workflow version show page #7466
Changes from all commits
81698f5
9ae2fe3
33b700c
c509972
3d0837a
89f0030
fe75e3c
325d3da
6ad7994
fa01695
6e16db3
124d09b
84661ef
b011297
b712d69
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,127 @@ | ||||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; | ||||
import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords'; | ||||
import { useFindOneRecord } from '@/object-record/hooks/useFindOneRecord'; | ||||
import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord'; | ||||
import { Button } from '@/ui/input/button/components/Button'; | ||||
import { useActivateWorkflowVersion } from '@/workflow/hooks/useActivateWorkflowVersion'; | ||||
import { useCreateNewWorkflowVersion } from '@/workflow/hooks/useCreateNewWorkflowVersion'; | ||||
import { useDeactivateWorkflowVersion } from '@/workflow/hooks/useDeactivateWorkflowVersion'; | ||||
import { useWorkflowVersion } from '@/workflow/hooks/useWorkflowVersion'; | ||||
import { Workflow, WorkflowVersion } from '@/workflow/types/Workflow'; | ||||
import { IconPencil, IconPlayerStop, IconPower, isDefined } from 'twenty-ui'; | ||||
|
||||
export const RecordShowPageWorkflowVersionHeader = ({ | ||||
workflowVersionId, | ||||
}: { | ||||
workflowVersionId: string; | ||||
}) => { | ||||
const workflowVersion = useWorkflowVersion(workflowVersionId); | ||||
|
||||
const workflowVersionRelatedWorkflowQuery = useFindOneRecord< | ||||
Pick<Workflow, '__typename' | 'id' | 'lastPublishedVersionId'> | ||||
>({ | ||||
objectNameSingular: CoreObjectNameSingular.Workflow, | ||||
objectRecordId: workflowVersion?.workflowId, | ||||
recordGqlFields: { | ||||
id: true, | ||||
lastPublishedVersionId: true, | ||||
}, | ||||
skip: !isDefined(workflowVersion), | ||||
}); | ||||
|
||||
// TODO: In the future, use the workflow.status property to determine if there is a draft version | ||||
const { | ||||
records: draftWorkflowVersions, | ||||
loading: loadingDraftWorkflowVersions, | ||||
} = useFindManyRecords<WorkflowVersion>({ | ||||
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. could you add a comment there? This part should be removed once statuses are fixed on workflow. That will save us a query 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. I will do. However, we'll have to keep that code as we need to know what is the id of the current draft version – if any – to make the "Use as Draft" button work. See: twenty/packages/twenty-front/src/modules/workflow/components/RecordShowPageWorkflowVersionHeader.tsx Line 83 in 3e75a60
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. Oh... Too bad |
||||
objectNameSingular: CoreObjectNameSingular.WorkflowVersion, | ||||
filter: { | ||||
workflowId: { | ||||
eq: workflowVersion?.workflow.id, | ||||
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. style: Ensure workflowVersion.workflow is defined before accessing id |
||||
}, | ||||
status: { | ||||
eq: 'DRAFT', | ||||
}, | ||||
}, | ||||
skip: !isDefined(workflowVersion), | ||||
limit: 1, | ||||
}); | ||||
|
||||
const showUseAsDraftButton = | ||||
!loadingDraftWorkflowVersions && | ||||
isDefined(workflowVersion) && | ||||
!workflowVersionRelatedWorkflowQuery.loading && | ||||
isDefined(workflowVersionRelatedWorkflowQuery.record) && | ||||
workflowVersion.status !== 'DRAFT' && | ||||
workflowVersion.id !== | ||||
workflowVersionRelatedWorkflowQuery.record.lastPublishedVersionId; | ||||
|
||||
const hasAlreadyDraftVersion = | ||||
!loadingDraftWorkflowVersions && draftWorkflowVersions.length > 0; | ||||
|
||||
const isWaitingForWorkflowVersion = !isDefined(workflowVersion); | ||||
|
||||
const { activateWorkflowVersion } = useActivateWorkflowVersion(); | ||||
const { deactivateWorkflowVersion } = useDeactivateWorkflowVersion(); | ||||
const { createNewWorkflowVersion } = useCreateNewWorkflowVersion(); | ||||
|
||||
const { updateOneRecord: updateOneWorkflowVersion } = | ||||
useUpdateOneRecord<WorkflowVersion>({ | ||||
objectNameSingular: CoreObjectNameSingular.WorkflowVersion, | ||||
}); | ||||
|
||||
return ( | ||||
<> | ||||
{showUseAsDraftButton ? ( | ||||
<Button | ||||
title={`Use as Draft${hasAlreadyDraftVersion ? ' (override)' : ''}`} | ||||
variant="secondary" | ||||
Icon={IconPencil} | ||||
disabled={isWaitingForWorkflowVersion} | ||||
onClick={async () => { | ||||
if (hasAlreadyDraftVersion) { | ||||
await updateOneWorkflowVersion({ | ||||
idToUpdate: draftWorkflowVersions[0].id, | ||||
updateOneRecordInput: { | ||||
trigger: workflowVersion.trigger, | ||||
steps: workflowVersion.steps, | ||||
}, | ||||
}); | ||||
} else { | ||||
await createNewWorkflowVersion({ | ||||
workflowId: workflowVersion.workflow.id, | ||||
name: `v${workflowVersion.workflow.versions.length + 1}`, | ||||
status: 'DRAFT', | ||||
trigger: workflowVersion.trigger, | ||||
steps: workflowVersion.steps, | ||||
}); | ||||
} | ||||
}} | ||||
/> | ||||
) : null} | ||||
|
||||
{workflowVersion?.status === 'DRAFT' || | ||||
workflowVersion?.status === 'DEACTIVATED' ? ( | ||||
<Button | ||||
title="Activate" | ||||
variant="secondary" | ||||
Icon={IconPower} | ||||
disabled={isWaitingForWorkflowVersion} | ||||
onClick={() => { | ||||
return activateWorkflowVersion(workflowVersion.id); | ||||
}} | ||||
/> | ||||
) : workflowVersion?.status === 'ACTIVE' ? ( | ||||
<Button | ||||
title="Deactivate" | ||||
variant="secondary" | ||||
Icon={IconPlayerStop} | ||||
disabled={isWaitingForWorkflowVersion} | ||||
onClick={() => { | ||||
return deactivateWorkflowVersion(workflowVersion.id); | ||||
}} | ||||
/> | ||||
) : null} | ||||
</> | ||||
); | ||||
}; |
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.
style: Consider adding a null check for workflowVersion before accessing workflowId