File size: 2,432 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
import '@automattic/calypso-polyfills';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { useTranslate } from 'i18n-calypso';
import { FunctionComponent } from 'react';
import { createRoot } from 'react-dom/client';
import JetpackLogo from 'calypso/components/jetpack-logo';
import config from '../lib/config-api';
import getSiteAdminUrl from '../lib/selectors/get-site-admin-url';
import getSiteStatsBaseUrl from '../lib/selectors/get-site-stats-base-url';
import setLocale from '../lib/set-locale';
import Highlights from './highlights';
import MiniChart from './mini-chart';
import Modules from './modules';

import './index.scss';

/**
 * Loads and runs the main chunk for Stats Widget.
 */
export function init() {
	const currentSiteId = config( 'blog_id' );
	const localeSlug = config( 'i18n_locale_slug' ) || config( 'i18n_default_locale_slug' ) || 'en';

	const statsBaseUrl = getSiteStatsBaseUrl();
	const adminBaseUrl = getSiteAdminUrl( currentSiteId );

	const queryClient = new QueryClient();

	// Ensure locale files are loaded before rendering.
	setLocale( localeSlug ).then( () => {
		const statsWidgetEl = document.getElementById( 'dashboard_stats' );
		if ( ! statsWidgetEl ) {
			return;
		}
		const App: FunctionComponent = () => {
			const translate = useTranslate();
			return (
				<div id="stats-widget-content" className="stats-widget-content">
					<MiniChart
						siteId={ currentSiteId }
						gmtOffset={ config( 'gmt_offset' ) }
						statsBaseUrl={ statsBaseUrl }
					/>
					<div className="stats-widget-wrapper">
						<Highlights
							siteId={ currentSiteId }
							gmtOffset={ config( 'gmt_offset' ) }
							statsBaseUrl={ statsBaseUrl }
						/>
						<Modules siteId={ currentSiteId } adminBaseUrl={ adminBaseUrl } />
						<div className="stats-widget-footer">
							<a
								href="https://jetpack.com/redirect/?source=jetpack-stats-widget-logo-link"
								target="_blank"
								rel="noreferrer noopener"
								aria-label="Jetpack Stats Website"
							>
								<JetpackLogo size={ 20 } monochrome full />
							</a>
							<a href={ `${ statsBaseUrl }/stats/day/${ currentSiteId }` }>
								{ translate( 'View all stats' ) }
							</a>
						</div>
					</div>
				</div>
			);
		};
		const root = createRoot( statsWidgetEl );
		root.render(
			<QueryClientProvider client={ queryClient }>
				<App />
			</QueryClientProvider>
		);
	} );
}