Skip to content

Commit

Permalink
Merge pull request #8103 from google/benbowler/8081-display-offline-n…
Browse files Browse the repository at this point in the history
…otification

Update offline notification to use SK GET route.
  • Loading branch information
tofumatt committed Jan 30, 2024
2 parents 521db6c + 3adb4e1 commit c173f6c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
3 changes: 3 additions & 0 deletions assets/js/components/DashboardEntityApp.js
Expand Up @@ -61,6 +61,7 @@ import EntityBannerNotifications from './notifications/EntityBannerNotifications
import DashboardSharingSettingsButton from './dashboard-sharing/DashboardSharingSettingsButton';
import useViewOnly from '../hooks/useViewOnly';
import OfflineNotification from './notifications/OfflineNotification';
import { useMonitorInternetConnection } from '../hooks/useMonitorInternetConnection';
const { useSelect } = Data;

function DashboardEntityApp() {
Expand Down Expand Up @@ -122,6 +123,8 @@ function DashboardEntityApp() {
);
} );

useMonitorInternetConnection();

let lastWidgetAnchor = null;

if ( isMonetizationActive ) {
Expand Down
3 changes: 3 additions & 0 deletions assets/js/components/DashboardMainApp.js
Expand Up @@ -72,6 +72,7 @@ import {
} from '../modules/analytics-4/datastore/constants';
import { EDIT_SCOPE } from '../modules/analytics/datastore/constants';
import OfflineNotification from './notifications/OfflineNotification';
import { useMonitorInternetConnection } from '../hooks/useMonitorInternetConnection';
const { useSelect, useDispatch } = Data;

export default function DashboardMainApp() {
Expand Down Expand Up @@ -219,6 +220,8 @@ export default function DashboardMainApp() {
select( CORE_USER ).isKeyMetricsWidgetHidden()
);

useMonitorInternetConnection();

let lastWidgetAnchor = null;

if ( isMonetizationActive ) {
Expand Down
22 changes: 12 additions & 10 deletions assets/js/hooks/useMonitorInternetConnection.js
Expand Up @@ -24,12 +24,12 @@ import { useLifecycles, useInterval } from 'react-use';
/**
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';
import { useCallback } from '@wordpress/element';

/**
* Internal dependencies
*/
import API from 'googlesitekit-api';
import Data from 'googlesitekit-data';
import { CORE_UI } from '../googlesitekit/datastore/ui/constants';

Expand All @@ -54,19 +54,21 @@ export function useMonitorInternetConnection() {
}

try {
// Use apiFetch directly, instead of our wrapper API, to have
// control over the error handling/catching, as status is switched to offline
// if request fails to reach the server.
const onlineResponse = await apiFetch( {
method: 'GET',
path: '/google-site-kit/v1/core/site/data/health-checks',
} );
const connectionCheckResponse = await API.get(
'core',
'site',
'connection-check',
undefined,
{
useCache: false,
}
);

// We are only interested if the request was successful, to
// confirm online status.
const canReachInternetURL = !! onlineResponse.checks;
const canReachConnectionCheck = !! connectionCheckResponse;

setIsOnline( canReachInternetURL );
setIsOnline( canReachConnectionCheck );
} catch ( err ) {
setIsOnline( false );
}
Expand Down
2 changes: 1 addition & 1 deletion assets/js/hooks/useMonitorInternetConnection.test.js
Expand Up @@ -41,7 +41,7 @@ describe( 'useMonitorInternetConnection', () => {
};

const healthCheckEndpoint = new RegExp(
'google-site-kit/v1/core/site/data/health-checks'
'google-site-kit/v1/core/site/data/connection-check'
);

const healthCheckResponse = {
Expand Down
14 changes: 14 additions & 0 deletions includes/Core/Util/Health_Checks.php
Expand Up @@ -95,6 +95,20 @@ private function get_rest_routes() {
),
)
),
new REST_Route(
'core/site/data/connection-check',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => function() {
return 'true';
},
'permission_callback' => function () {
return current_user_can( Permissions::VIEW_SHARED_DASHBOARD ) || current_user_can( Permissions::SETUP );
},
),
)
),
);
}

Expand Down

0 comments on commit c173f6c

Please sign in to comment.