File size: 1,890 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
import { FunctionComponent, useEffect } from 'react';
import { useDispatch } from 'calypso/state';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import * as HostInfo from '../host-info';

interface Props {
	field: string;
	host: string;
	info: HostInfo.Info[] | HostInfo.InfoSplit;
	protocol: 'ftp' | 'ssh' | 'dynamic-ssh';
}

const InlineInfo: FunctionComponent< Props > = ( { field, host, info, protocol } ) => {
	const dispatch = useDispatch();

	const choseSplitInfo = ( splitInfo: HostInfo.InfoSplit ) =>
		protocol === 'ftp' ? splitInfo.ftp : splitInfo.sftp;

	const infoToRender = HostInfo.infoIsSplit( info ) ? choseSplitInfo( info ) : info;

	useEffect( () => {
		dispatch(
			recordTracksEvent( 'calypso_jetpack_advanced_credentials_flow_inline_info_view', {
				field,
				host,
				protocol,
			} )
		);
	}, [ dispatch, field, host, protocol ] );

	return (
		<div className="inline-info">
			{ infoToRender.map( ( infom, topLevelIndex ) => {
				if ( HostInfo.infoIsLink( infom ) ) {
					return (
						<a key={ topLevelIndex } href={ infom.link } target="_blank" rel="noreferrer noopener">
							{ infom.text }
						</a>
					);
				} else if ( HostInfo.infoIsText( infom ) ) {
					return <p key={ topLevelIndex }>{ infom.text }</p>;
				} else if ( HostInfo.infoIsOrderedList( infom ) ) {
					return (
						<ol key={ topLevelIndex }>
							{ infom.items.map( ( text, index ) => (
								<li key={ index }>{ text }</li>
							) ) }
						</ol>
					);
				} else if ( HostInfo.infoIsUnorderedList( infom ) ) {
					return (
						<ul key={ topLevelIndex }>
							{ infom.items.map( ( text, index ) => (
								<li key={ index }>{ text }</li>
							) ) }
						</ul>
					);
				} else if ( HostInfo.infoIsLine( infom ) ) {
					return <hr key={ topLevelIndex } />;
				}
				return null;
			} ) }
		</div>
	);
};

export default InlineInfo;