File size: 1,552 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
import { addQueryArgs } from '@wordpress/url';
import { PANEL_MAPPINGS } from '../constants';
import { useHelpCenterContext } from '../contexts/HelpCenterContext';
import { useSiteSlug } from './use-site-slug';

/**
 * Given the name of a Calypso customizer panel, returns an object containing
 * the section or panel to be used in autofocus. Returns null if the panel is
 * not recognized.
 * @param  panel Calypso panel slug
 * @returns WordPress autofocus argument object
 */
export function getCustomizerFocus( panel: string ) {
	if ( PANEL_MAPPINGS.hasOwnProperty( panel ) ) {
		const [ key, value ] = PANEL_MAPPINGS[ panel ];
		return { [ `autofocus[${ key }]` ]: value };
	}

	return null;
}
const panels = [ 'root', 'homepage', 'identity', 'menus' ] as const;

export function useCustomizerUrls() {
	const { site } = useHelpCenterContext();
	const siteSlug = useSiteSlug();
	const returnUrl = window.location.href;

	return panels.reduce(
		( acc, panel ) => {
			if ( ! site?.jetpack && siteSlug ) {
				const panelPath = panel === 'root' ? '' : panel;
				const url = [ '' ]
					.concat( [ 'customize', panelPath, siteSlug ].filter( Boolean ) )
					.join( '/' );
				acc[ panel ] = addQueryArgs( url, {
					return: returnUrl,
				} );
			} else {
				const customizerUrl = site?.options.admin_url + 'customize.php';

				acc[ panel ] = addQueryArgs( customizerUrl, {
					return: returnUrl,
					...( panel ? getCustomizerFocus( panel ) : {} ),
				} );
			}
			return acc;
		},
		{} as Record< ( typeof panels )[ number ], string >
	);
}