Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Studio: Add notification when the preview site is created #952

Merged
merged 9 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 3 additions & 44 deletions src/hooks/use-archive-site.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import * as Sentry from '@sentry/electron/renderer';
import { sprintf } from '@wordpress/i18n';
import { useI18n } from '@wordpress/react-i18n';
import { useCallback, useEffect, useMemo } from 'react';
import { useCallback, useMemo } from 'react';
import { DEMO_SITE_SIZE_LIMIT_BYTES, DEMO_SITE_SIZE_LIMIT_GB } from 'src/constants';
import { useSyncSites } from 'src/hooks/sync-sites';
import { useArchiveErrorMessages } from 'src/hooks/use-archive-error-messages';
import { useAuth } from 'src/hooks/use-auth';
import { useSiteDetails } from 'src/hooks/use-site-details';
import { SnapshotStatus, SnapshotStatusResponse, useSnapshots } from 'src/hooks/use-snapshots';
import { useSnapshots } from 'src/hooks/use-snapshots';
import { getIpcApi } from 'src/lib/get-ipc-api';
import { isWpcomNetworkError } from 'src/lib/is-wpcom-network-error';

export function useArchiveSite() {
const { uploadingSites, setUploadingSites, selectedSite } = useSiteDetails();
const { snapshots, addSnapshot, updateSnapshot, fetchSnapshotUsage } = useSnapshots();
const { snapshots, addSnapshot, fetchSnapshotUsage } = useSnapshots();
const isUploadingSiteId = useCallback(
( localSiteId: string ) => uploadingSites[ localSiteId ] || false,
[ uploadingSites ]
Expand All @@ -22,47 +22,6 @@ export function useArchiveSite() {
const { __ } = useI18n();
const { connectedSites } = useSyncSites();

useEffect( () => {
if ( ! client ) {
// Can't poll for snapshots if logged out
return;
}

const loadingSnapshots = snapshots.filter( ( snapshot ) => snapshot.isLoading );
if ( loadingSnapshots.length === 0 ) {
return;
}
const intervalId = setInterval( async () => {
for ( const snapshot of loadingSnapshots ) {
if ( snapshot.isLoading ) {
try {
const response: SnapshotStatusResponse = await client.req.get(
'/jurassic-ninja/status',
{
apiNamespace: 'wpcom/v2',
site_id: snapshot.atomicSiteId,
}
);
if ( response.status === SnapshotStatus.Active ) {
updateSnapshot( {
...snapshot,
isLoading: false,
} );
}
} catch ( error ) {
updateSnapshot( {
...snapshot,
isLoading: false,
} );
}
}
}
}, 3000 );
return () => {
clearInterval( intervalId );
};
}, [ client, snapshots, updateSnapshot ] );

const getNextSequenceNumber = (
siteId: string,
snapshots: Array< Snapshot >,
Expand Down
47 changes: 47 additions & 0 deletions src/hooks/use-snapshots.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Sentry from '@sentry/electron/renderer';
import { sprintf } from '@wordpress/i18n';
import { useI18n } from '@wordpress/react-i18n';
import React, {
createContext,
Expand Down Expand Up @@ -307,6 +308,52 @@ export const SnapshotProvider: React.FC< { children: ReactNode } > = ( { childre
fetchStats();
}, [ client, fetchSnapshotUsage, initiated, snapshots.length ] );

useEffect( () => {
if ( ! client ) {
// Can't poll for snapshots if logged out
return;
}

const loadingSnapshots = snapshots.filter( ( snapshot ) => snapshot.isLoading );
if ( loadingSnapshots.length === 0 ) {
return;
}

const intervalId = setInterval( async () => {
for ( const snapshot of loadingSnapshots ) {
if ( snapshot.isLoading ) {
try {
const response: SnapshotStatusResponse = await client.req.get(
'/jurassic-ninja/status',
{
apiNamespace: 'wpcom/v2',
site_id: snapshot.atomicSiteId,
}
);
if ( response.status === SnapshotStatus.Active ) {
updateSnapshot( {
...snapshot,
isLoading: false,
} );
getIpcApi().showNotification( {
title: snapshot.name,
body: sprintf( __( "Preview site '%s' has been created." ), snapshot.url ),
} );
}
} catch ( error ) {
updateSnapshot( {
...snapshot,
isLoading: false,
} );
}
}
}
}, 3000 );
return () => {
clearInterval( intervalId );
};
}, [ __, client, snapshots, updateSnapshot ] );

const value: SnapshotContextType = useMemo(
() => ( {
snapshots,
Expand Down
Loading