2023-12-01 22:45:42 +00:00
|
|
|
import { useEffect } from 'react';
|
2024-03-26 13:19:40 +00:00
|
|
|
import { useRecoilState, useRecoilValue } from 'recoil';
|
2023-12-01 22:45:42 +00:00
|
|
|
|
2024-08-28 15:15:54 +00:00
|
|
|
import { useIsLogged } from '@/auth/hooks/useIsLogged';
|
2024-03-26 13:19:40 +00:00
|
|
|
import { currentUserState } from '@/auth/states/currentUserState';
|
2024-04-30 16:48:07 +00:00
|
|
|
import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';
|
2023-12-01 21:06:38 +00:00
|
|
|
import { useFindManyObjectMetadataItems } from '@/object-metadata/hooks/useFindManyObjectMetadataItems';
|
2023-12-01 22:45:42 +00:00
|
|
|
import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState';
|
2024-07-30 10:36:39 +00:00
|
|
|
import { WorkspaceActivationStatus } from '~/generated/graphql';
|
Activate/Deactivate workflow and Discard Draft (#7022)
## Setup
This PR can be tested only if some feature flags have specific values:
- `IsWorkflowEnabled` equals `true`
- `IsQueryRunnerTwentyORMEnabled` equals `false`
These feature flags weren't committed to don't break other branches.
## What this PR brings
- Display buttons to activate and deactivate a workflow version and a
button to discard the current draft version. I also scaffolded a "Test"
button, which doesn't do anything for now.
- Wired the activate, deactivate and discard draft buttons to the
backend.
- Made it possible to "edit" active and deactivated versions by
automatically creating a new draft version when the user tries to edit
the version.
- Hide the "Discard Draft", button if the current version is not a draft
or is the first version ever created.
- On the backend, don't consider discarded drafts when checking if a new
draft version can be created.
- On the backend, disallow deleting the first created workflow version.
Otherwise, we will end up with a blank canvas in the front end, and it
will be impossible to recover from it.
- On the backend, disallow running deactivation steps if the workflow
version is not currently active. Previously, we were throwing, which is
unnecessary as it's a valid case.
## Spotted bugs that we must dive into
### Duplicate workflow versions in Apollo cache
https://github.com/user-attachments/assets/7cfffd06-11e0-417a-8da0-f9a5f43b84e2
---------
Co-authored-by: Charles Bochet <charles@twenty.com>
2024-09-25 16:09:31 +00:00
|
|
|
import { generatedMockObjectMetadataItems } from '~/testing/mock-data/objectMetadataItems';
|
2023-12-01 22:45:42 +00:00
|
|
|
import { isDeeplyEqual } from '~/utils/isDeeplyEqual';
|
2024-03-26 13:19:40 +00:00
|
|
|
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';
|
2023-12-01 21:06:38 +00:00
|
|
|
|
|
|
|
|
export const ObjectMetadataItemsLoadEffect = () => {
|
2024-03-26 13:19:40 +00:00
|
|
|
const currentUser = useRecoilValue(currentUserState);
|
2024-04-30 16:48:07 +00:00
|
|
|
const currentWorkspace = useRecoilValue(currentWorkspaceState);
|
2024-08-28 15:15:54 +00:00
|
|
|
const isLoggedIn = useIsLogged();
|
2024-04-30 16:48:07 +00:00
|
|
|
|
2024-05-08 07:28:28 +00:00
|
|
|
const { objectMetadataItems: newObjectMetadataItems, loading } =
|
2024-03-26 13:19:40 +00:00
|
|
|
useFindManyObjectMetadataItems({
|
2024-08-28 15:15:54 +00:00
|
|
|
skip: !isLoggedIn,
|
2024-03-26 13:19:40 +00:00
|
|
|
});
|
2023-12-01 22:45:42 +00:00
|
|
|
|
|
|
|
|
const [objectMetadataItems, setObjectMetadataItems] = useRecoilState(
|
2024-03-20 13:21:58 +00:00
|
|
|
objectMetadataItemsState,
|
2023-12-01 22:45:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-04-30 16:48:07 +00:00
|
|
|
const toSetObjectMetadataItems =
|
|
|
|
|
isUndefinedOrNull(currentUser) ||
|
2024-07-30 10:36:39 +00:00
|
|
|
currentWorkspace?.activationStatus !== WorkspaceActivationStatus.Active
|
Activate/Deactivate workflow and Discard Draft (#7022)
## Setup
This PR can be tested only if some feature flags have specific values:
- `IsWorkflowEnabled` equals `true`
- `IsQueryRunnerTwentyORMEnabled` equals `false`
These feature flags weren't committed to don't break other branches.
## What this PR brings
- Display buttons to activate and deactivate a workflow version and a
button to discard the current draft version. I also scaffolded a "Test"
button, which doesn't do anything for now.
- Wired the activate, deactivate and discard draft buttons to the
backend.
- Made it possible to "edit" active and deactivated versions by
automatically creating a new draft version when the user tries to edit
the version.
- Hide the "Discard Draft", button if the current version is not a draft
or is the first version ever created.
- On the backend, don't consider discarded drafts when checking if a new
draft version can be created.
- On the backend, disallow deleting the first created workflow version.
Otherwise, we will end up with a blank canvas in the front end, and it
will be impossible to recover from it.
- On the backend, disallow running deactivation steps if the workflow
version is not currently active. Previously, we were throwing, which is
unnecessary as it's a valid case.
## Spotted bugs that we must dive into
### Duplicate workflow versions in Apollo cache
https://github.com/user-attachments/assets/7cfffd06-11e0-417a-8da0-f9a5f43b84e2
---------
Co-authored-by: Charles Bochet <charles@twenty.com>
2024-09-25 16:09:31 +00:00
|
|
|
? generatedMockObjectMetadataItems
|
2024-04-30 16:48:07 +00:00
|
|
|
: newObjectMetadataItems;
|
2024-05-08 07:28:28 +00:00
|
|
|
if (
|
|
|
|
|
!loading &&
|
|
|
|
|
!isDeeplyEqual(objectMetadataItems, toSetObjectMetadataItems)
|
|
|
|
|
) {
|
2024-04-29 21:33:23 +00:00
|
|
|
setObjectMetadataItems(toSetObjectMetadataItems);
|
2023-12-01 22:45:42 +00:00
|
|
|
}
|
2024-04-29 21:33:23 +00:00
|
|
|
}, [
|
|
|
|
|
currentUser,
|
2024-04-30 16:48:07 +00:00
|
|
|
currentWorkspace?.activationStatus,
|
2024-05-08 07:28:28 +00:00
|
|
|
loading,
|
2024-04-29 21:33:23 +00:00
|
|
|
newObjectMetadataItems,
|
|
|
|
|
objectMetadataItems,
|
|
|
|
|
setObjectMetadataItems,
|
|
|
|
|
]);
|
2023-12-01 21:06:38 +00:00
|
|
|
|
|
|
|
|
return <></>;
|
|
|
|
|
};
|