2024-03-18 13:58:05 +01:00
|
|
|
import type { IChangeRequestEnvironmentConfig } from 'component/changeRequest/changeRequest.types';
|
2023-08-04 14:57:26 +02:00
|
|
|
import React from 'react';
|
|
|
|
import { useChangeRequestConfig } from './api/getters/useChangeRequestConfig/useChangeRequestConfig';
|
|
|
|
|
|
|
|
export const getHighestChangeRequestEnv =
|
|
|
|
(data: IChangeRequestEnvironmentConfig[]) => (): string | undefined => {
|
2023-10-02 14:25:46 +02:00
|
|
|
const changeRequestEnvs = data.filter(
|
|
|
|
(env) => env.changeRequestEnabled,
|
|
|
|
);
|
2023-08-04 14:57:26 +02:00
|
|
|
|
|
|
|
const env =
|
2023-10-02 14:25:46 +02:00
|
|
|
changeRequestEnvs.find((env) => env.type === 'production') ??
|
2023-08-04 14:57:26 +02:00
|
|
|
changeRequestEnvs[0];
|
|
|
|
|
|
|
|
return env?.environment;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useHighestPermissionChangeRequestEnvironment = (
|
2023-10-02 14:25:46 +02:00
|
|
|
projectId?: string,
|
2023-08-04 14:57:26 +02:00
|
|
|
) => {
|
|
|
|
const { data } = useChangeRequestConfig(projectId || '');
|
|
|
|
|
|
|
|
return React.useCallback(getHighestChangeRequestEnv(data), [
|
|
|
|
JSON.stringify(data),
|
|
|
|
]);
|
|
|
|
};
|