Back

75% Statements 6/8
66.66% Branches 4/6
100% Functions 2/2
75% Lines 6/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130                                                                        137x   56x           81x                                   137x   56x                                                                                               81x                                  
import { createInterpolateElement } from '@wordpress/element';
import { __, _x, sprintf } from '@wordpress/i18n';
import { Link } from '@wordpress/ui';
import { EMPTY_ARRAY } from '../../social-store/constants';
import type { ConnectionService } from '../../types';
import type { ReactNode } from 'react';
 
/**
 * One input a service needs before its connect popup can open.
 */
export type ConnectField = {
	/**
	 * The input name, which is also the connect URL param it maps to.
	 */
	name: string;
	type: 'text' | 'password';
	label: string;
	placeholder: string;
	/**
	 * Plain-text helper copy.
	 */
	description?: string;
	/**
	 * Helper copy carrying semantics (links, emphasis) that plain text cannot.
	 */
	details?: ReactNode;
};
 
/**
 * The sentence introducing a service's input step.
 *
 * @param id - The ID of the service.
 *
 * @return The intro copy, or `undefined` for services that need no inputs.
 */
export function getConnectIntro( id: ConnectionService[ 'id' ] ): string | undefined {
	switch ( id ) {
		case 'bluesky':
			return __(
				'To share to Bluesky please enter your Bluesky handle and app password below, then click connect.',
				'jetpack-publicize-pkg'
			);
 
		case 'mastodon':
			return __(
				'To share to Mastodon please enter your Mastodon username below, then click connect.',
				'jetpack-publicize-pkg'
			);
 
		default:
			return undefined;
	}
}
 
/**
 * The inputs a service needs before its connect popup can open.
 *
 * @param id - The ID of the service.
 *
 * @return The service's fields, empty for services that need no inputs.
 */
export function getConnectFields( id: ConnectionService[ 'id' ] ): Array< ConnectField > {
	switch ( id ) {
		case 'bluesky':
			return [
				{
					name: 'handle',
					type: 'text',
					label: _x( 'Handle', 'The handle of a social media account.', 'jetpack-publicize-pkg' ),
					placeholder: 'username.bsky.social',
					description: __(
						'You can find the handle in your Bluesky profile.',
						'jetpack-publicize-pkg'
					),
					details: createInterpolateElement(
						sprintf(
							/* translators: %s is the bluesky handle suffix like .bsky.social */
							__(
								'This can either be %s or just the domain name if you are using a custom domain.',
								'jetpack-publicize-pkg'
							),
							'<strong>username.bsky.social</strong>'
						),
						{
							strong: <strong />,
						}
					),
				},
				{
					name: 'app_password',
					type: 'password',
					label: __( 'App password', 'jetpack-publicize-pkg' ),
					placeholder: 'xxxx-xxxx-xxxx-xxxx',
					details: createInterpolateElement(
						__(
							'App password is needed to safely connect your account. App password is different from your account password. You can <link>generate it in Bluesky</link>.',
							'jetpack-publicize-pkg'
						),
						{
							link: (
								<Link
									openInNewTab
									href="https://bsky.app/settings/app-passwords"
									children={ null }
								/>
							),
						}
					),
				},
			];
 
		case 'mastodon':
			return [
				{
					name: 'instance',
					type: 'text',
					label: _x( 'Handle', 'The handle of a social media account.', 'jetpack-publicize-pkg' ),
					placeholder: '@username@mastodon.social',
					description: __(
						'You can find the handle in your Mastodon profile.',
						'jetpack-publicize-pkg'
					),
				},
			];
 
		default:
			return EMPTY_ARRAY;
	}
}