2023-04-11 14:47:54 +02:00
|
|
|
import { SKIP_CHANGE_REQUEST } from 'component/providers/AccessProvider/permissions';
|
2022-11-14 12:34:38 +01:00
|
|
|
import React from 'react';
|
|
|
|
import { useChangeRequestConfig } from './api/getters/useChangeRequestConfig/useChangeRequestConfig';
|
2023-04-11 14:47:54 +02:00
|
|
|
import { useCheckProjectPermissions } from './useHasAccess';
|
2022-11-04 09:50:00 +01:00
|
|
|
|
2022-11-14 12:34:38 +01:00
|
|
|
export const useChangeRequestsEnabled = (projectId: string) => {
|
|
|
|
const { data } = useChangeRequestConfig(projectId);
|
2023-04-11 14:47:54 +02:00
|
|
|
const checkAccess = useCheckProjectPermissions(projectId);
|
2022-11-14 12:34:38 +01:00
|
|
|
|
|
|
|
const isChangeRequestConfigured = React.useCallback(
|
|
|
|
(environment: string): boolean => {
|
2023-04-11 14:47:54 +02:00
|
|
|
const canSkipChangeRequest = checkAccess(
|
|
|
|
SKIP_CHANGE_REQUEST,
|
2023-10-02 14:25:46 +02:00
|
|
|
environment,
|
2023-04-11 14:47:54 +02:00
|
|
|
);
|
|
|
|
return canSkipChangeRequest
|
|
|
|
? false
|
2023-10-02 14:25:46 +02:00
|
|
|
: data.some((draft) => {
|
2023-04-11 14:47:54 +02:00
|
|
|
return (
|
|
|
|
draft.environment === environment &&
|
|
|
|
draft.changeRequestEnabled
|
|
|
|
);
|
|
|
|
});
|
2022-11-14 12:34:38 +01:00
|
|
|
},
|
2023-10-02 14:25:46 +02:00
|
|
|
[JSON.stringify(data)],
|
2022-11-07 09:16:29 +01:00
|
|
|
);
|
2022-11-14 12:34:38 +01:00
|
|
|
|
2023-06-16 13:36:23 +02:00
|
|
|
const isChangeRequestConfiguredForReview = React.useCallback(
|
|
|
|
(environment: string): boolean => {
|
2023-10-02 14:25:46 +02:00
|
|
|
return data.some((draft) => {
|
2023-06-16 13:36:23 +02:00
|
|
|
return (
|
|
|
|
draft.environment === environment &&
|
|
|
|
draft.changeRequestEnabled
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
2023-10-02 14:25:46 +02:00
|
|
|
[JSON.stringify(data)],
|
2023-06-16 13:36:23 +02:00
|
|
|
);
|
|
|
|
|
2022-11-14 12:34:38 +01:00
|
|
|
const isChangeRequestConfiguredInAnyEnv = React.useCallback((): boolean => {
|
2023-10-02 14:25:46 +02:00
|
|
|
return data.some((draft) => draft.changeRequestEnabled);
|
2022-11-24 16:16:14 +01:00
|
|
|
}, [JSON.stringify(data)]);
|
2022-11-14 12:34:38 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
isChangeRequestConfigured,
|
|
|
|
isChangeRequestConfiguredInAnyEnv,
|
2023-06-16 13:36:23 +02:00
|
|
|
isChangeRequestConfiguredForReview,
|
2022-11-14 12:34:38 +01:00
|
|
|
};
|
2022-11-04 09:50:00 +01:00
|
|
|
};
|