From 83d8fde5be3528bfc72d9b241717647c92126479 Mon Sep 17 00:00:00 2001 From: Josh Hunt Date: Thu, 8 Aug 2024 11:18:37 +0100 Subject: [PATCH] E2C: Post success toast when migration finishes (#91640) --- .../features/migrate-to-cloud/onprem/Page.tsx | 3 + .../onprem/useNotifyOnSuccess.tsx | 60 +++++++++++++++++++ public/locales/en-US/grafana.json | 7 +++ public/locales/pseudo-LOCALE/grafana.json | 7 +++ 4 files changed, 77 insertions(+) create mode 100644 public/app/features/migrate-to-cloud/onprem/useNotifyOnSuccess.tsx diff --git a/public/app/features/migrate-to-cloud/onprem/Page.tsx b/public/app/features/migrate-to-cloud/onprem/Page.tsx index 00b8d0d522d..e091c4c11da 100644 --- a/public/app/features/migrate-to-cloud/onprem/Page.tsx +++ b/public/app/features/migrate-to-cloud/onprem/Page.tsx @@ -24,6 +24,7 @@ import { MigrationSummary } from './MigrationSummary'; import { ResourcesTable } from './ResourcesTable'; import { BuildSnapshotCTA, CreatingSnapshotCTA } from './SnapshotCTAs'; import { SupportedTypesDisclosure } from './SupportedTypesDisclosure'; +import { useNotifySuccessful } from './useNotifyOnSuccess'; /** * Here's how migrations work: @@ -119,6 +120,8 @@ export const Page = () => { const [performCancelSnapshot, cancelSnapshotResult] = useCancelSnapshotMutation(); const [performDisconnect, disconnectResult] = useDeleteSessionMutation(); + useNotifySuccessful(snapshot.data); + const sessionUid = session.data?.uid; const snapshotUid = snapshot.data?.uid; const isInitialLoading = session.isLoading; diff --git a/public/app/features/migrate-to-cloud/onprem/useNotifyOnSuccess.tsx b/public/app/features/migrate-to-cloud/onprem/useNotifyOnSuccess.tsx new file mode 100644 index 00000000000..1042791730c --- /dev/null +++ b/public/app/features/migrate-to-cloud/onprem/useNotifyOnSuccess.tsx @@ -0,0 +1,60 @@ +import { t } from 'i18next'; +import { useRef, useEffect } from 'react'; + +import { useAppNotification } from 'app/core/copy/appNotification'; + +import { GetSnapshotResponseDto, SnapshotDto } from '../api'; + +export function useNotifySuccessful(snapshot: GetSnapshotResponseDto | undefined) { + const previousStatusRef = useRef(undefined); + const notifyApp = useAppNotification(); + + useEffect(() => { + const status = snapshot?.status; + const didJustFinish = + previousStatusRef.current !== 'FINISHED' && previousStatusRef.current !== undefined && status === 'FINISHED'; + + previousStatusRef.current = status; // must be AFTER the check above + + if (!didJustFinish) { + return; + } + + if (snapshot) { + const title = t('migrate-to-cloud.onprem.success-title', 'Migration completed!'); + const message = getTranslatedMessage(snapshot); + + notifyApp.success(title, message); + } + }, [notifyApp, snapshot]); +} + +function getTranslatedMessage(snapshot: GetSnapshotResponseDto) { + const types: string[] = []; + + for (const [type, count] of Object.entries(snapshot.stats?.types ?? {})) { + if (count <= 0) { + continue; + } + + // We don't have per-resource status counts, so there's no way to accurately pluralize these + // so we just don't :) + if (type === 'DASHBOARD') { + types.push(t('migrate-to-cloud.migrated-counts.dashboards', 'dashboards')); + } else if (type === 'DATASOURCE') { + types.push(t('migrate-to-cloud.migrated-counts.datasources', 'data sources')); + } else if (type === 'FOLDER') { + types.push(t('migrate-to-cloud.migrated-counts.folders', 'folders')); + } + } + + const successCount = snapshot?.stats?.statuses?.['OK'] ?? 0; + + const message = t( + 'migrate-to-cloud.onprem.success-message', + 'Successfully migrated {{successCount}} {{types, list}} to your Grafana Cloud instance.', + { successCount, types } + ); + + return message; +} diff --git a/public/locales/en-US/grafana.json b/public/locales/en-US/grafana.json index 74e4296fc69..84a34e98efb 100644 --- a/public/locales/en-US/grafana.json +++ b/public/locales/en-US/grafana.json @@ -1090,6 +1090,11 @@ "link-title": "View the full migration guide", "title": "Let us help you migrate to this stack" }, + "migrated-counts": { + "dashboards": "dashboards", + "datasources": "data sources", + "folders": "folders" + }, "migration-token": { "delete-button": "Delete token", "delete-modal-body": "If you've already used this token with a self-managed installation, that installation will no longer be able to upload content.", @@ -1121,6 +1126,8 @@ "migration-finished-with-warnings-body": "The migration has completed with some warnings. Check individual resources for more details", "snapshot-error-status-body": "There was an error creating the snapshot or starting the migration process. See the Grafana server logs for more details", "snapshot-error-status-title": "Error migrating resources", + "success-message": "Successfully migrated {{successCount}} {{types, list}} to your Grafana Cloud instance.", + "success-title": "Migration completed!", "upload-snapshot-error-title": "Error uploading snapshot" }, "pdc": { diff --git a/public/locales/pseudo-LOCALE/grafana.json b/public/locales/pseudo-LOCALE/grafana.json index 70255535804..067128ad03d 100644 --- a/public/locales/pseudo-LOCALE/grafana.json +++ b/public/locales/pseudo-LOCALE/grafana.json @@ -1090,6 +1090,11 @@ "link-title": "Vįęŵ ŧĥę ƒūľľ mįģřäŧįőʼn ģūįđę", "title": "Ŀęŧ ūş ĥęľp yőū mįģřäŧę ŧő ŧĥįş şŧäčĸ" }, + "migrated-counts": { + "dashboards": "đäşĥþőäřđş", + "datasources": "đäŧä şőūřčęş", + "folders": "ƒőľđęřş" + }, "migration-token": { "delete-button": "Đęľęŧę ŧőĸęʼn", "delete-modal-body": "Ĩƒ yőū'vę äľřęäđy ūşęđ ŧĥįş ŧőĸęʼn ŵįŧĥ ä şęľƒ-mäʼnäģęđ įʼnşŧäľľäŧįőʼn, ŧĥäŧ įʼnşŧäľľäŧįőʼn ŵįľľ ʼnő ľőʼnģęř þę äþľę ŧő ūpľőäđ čőʼnŧęʼnŧ.", @@ -1121,6 +1126,8 @@ "migration-finished-with-warnings-body": "Ŧĥę mįģřäŧįőʼn ĥäş čőmpľęŧęđ ŵįŧĥ şőmę ŵäřʼnįʼnģş. Cĥęčĸ įʼnđįvįđūäľ řęşőūřčęş ƒőř mőřę đęŧäįľş", "snapshot-error-status-body": "Ŧĥęřę ŵäş äʼn ęřřőř čřęäŧįʼnģ ŧĥę şʼnäpşĥőŧ őř şŧäřŧįʼnģ ŧĥę mįģřäŧįőʼn přőčęşş. Ŝęę ŧĥę Ğřäƒäʼnä şęřvęř ľőģş ƒőř mőřę đęŧäįľş", "snapshot-error-status-title": "Ēřřőř mįģřäŧįʼnģ řęşőūřčęş", + "success-message": "Ŝūččęşşƒūľľy mįģřäŧęđ {{successCount}} {{types, list}} ŧő yőūř Ğřäƒäʼnä Cľőūđ įʼnşŧäʼnčę.", + "success-title": "Mįģřäŧįőʼn čőmpľęŧęđ!", "upload-snapshot-error-title": "Ēřřőř ūpľőäđįʼnģ şʼnäpşĥőŧ" }, "pdc": {