File size: 2,612 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { FormInputValidation } from '@automattic/components';
import { Button } from '@wordpress/components';
import { useI18n } from '@wordpress/react-i18n';
import { useEffect, useState } from 'react';
import { GitHubRepositoryData } from '../../use-github-repositories-query';
import { CodeHighlighter } from '../code-highlighter';
import { useCreateWorkflow } from './use-create-workflow';
import { Workflow } from './use-deployment-workflows-query';

import './style.scss';

interface NewWorkflowWizardProps {
	repository: Pick< GitHubRepositoryData, 'id' | 'owner' | 'name' >;
	repositoryBranch: string;
	workflows?: Workflow[];
	templateName: string;
	exampleTemplate: string;
	onWorkflowCreated( path: string ): void;
}

const WORKFLOWS_DIRECTORY = '.github/workflows/';
const RECOMMENDED_WORKFLOW_PATH = WORKFLOWS_DIRECTORY + 'wpcom.yml';

export const NewWorkflowWizard = ( {
	repository,
	workflows,
	repositoryBranch,
	onWorkflowCreated,
	templateName,
	exampleTemplate,
}: NewWorkflowWizardProps ) => {
	const { __ } = useI18n();

	const { createWorkflow, isPending } = useCreateWorkflow( {
		onSuccess: () => {
			onWorkflowCreated( RECOMMENDED_WORKFLOW_PATH );
		},
	} );

	const [ error, setError ] = useState< string >();

	useEffect( () => {
		const existingWorkflow = !! workflows?.find(
			( workflow ) => workflow.workflow_path === RECOMMENDED_WORKFLOW_PATH
		);

		if ( existingWorkflow ) {
			setError(
				__(
					'A workflow file with this name already exists. Installing this workflow will overwrite it.'
				)
			);
			return;
		}

		setError( undefined );
	}, [ workflows, __ ] );

	return (
		<div className="github-deployments-new-workflow-wizard">
			<div className="github-deployments-new-workflow-wizard__workflow-file">
				<div className="github-deployments-new-workflow-wizard__workflow-file-name">
					<span>{ RECOMMENDED_WORKFLOW_PATH }</span>
				</div>

				<CodeHighlighter content={ exampleTemplate } />
			</div>

			{ error && (
				<FormInputValidation css={ { paddingBottom: '0 !important' } } isError text={ error } />
			) }

			<div css={ { marginTop: '16px' } }>
				<Button
					type="button"
					variant="secondary"
					disabled={ isPending }
					isBusy={ isPending }
					onClick={ () =>
						createWorkflow( {
							repositoryId: repository.id,
							repositoryOwner: repository.owner,
							repositoryName: repository.name,
							branchName: repositoryBranch,
							fileName: RECOMMENDED_WORKFLOW_PATH,
							workflowTemplate: templateName,
						} )
					}
				>
					{ __( 'Install workflow for me' ) }
				</Button>
			</div>
		</div>
	);
};