Skip to content

Commit 718332d

Browse files
authored
feat: add autostart stopped instance when has retpath (#20)
1 parent 669d6e9 commit 718332d

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

base-environments/docker-provider-farm/farm-nginx.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ http {
108108
expires -1;
109109
add_header 'Cache-Control'
110110
'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
111-
# todo:
112-
return 301 https://your-farm.host/instance/$hash;
111+
# example of redirect with retpath for auto start instance
112+
return 301 https://your-farm.host/instance/$hash?retpath=$scheme://$host$request_uri;
113113
}
114114

115115
error_page 404 /error404;

src/ui/containers/Instance/layouts/utils.tsx

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, {useEffect, useState} from 'react';
22

33
import {useDataManager} from '@gravity-ui/data-source';
44
import {House} from '@gravity-ui/icons';
5-
import {matchPath} from 'react-router-dom';
5+
import {matchPath, useSearchParams} from 'react-router-dom';
66

77
import type {InstanceWithProviderStatus} from '../../../../shared/common';
88
import {uiRoutes} from '../../../../shared/uiRoutes';
@@ -14,14 +14,57 @@ import {useInstanceActions} from '../actions';
1414
import {i18n} from './i18n';
1515

1616
const toastName = 'stop-instance-alert';
17+
const autoStartToastName = 'auto-start-instance-alert';
18+
19+
const showErrorToast = (err: Error) => {
20+
toaster.add({
21+
name: 'error-toast-start-instance',
22+
title: i18nInstanceActions('start-instance-error'),
23+
content: err.message,
24+
theme: 'danger',
25+
});
26+
};
1727

1828
export const useInstanceStopWarning = (instance?: InstanceWithProviderStatus) => {
1929
const {startInstance} = useInstanceActions();
30+
const [searchParams] = useSearchParams();
2031
const dataManager = useDataManager();
2132
const [isToastVisible, setIsToastVisible] = useState(false);
33+
const retpath = searchParams.get('retpath') ?? '';
34+
const autoStartEnable = Boolean(retpath);
2235

2336
useEffect(() => {
24-
if (instance?.providerStatus === 'stopped' && !isToastVisible && !toaster.has(toastName)) {
37+
if (instance?.providerStatus === 'running' && autoStartEnable) {
38+
window.location.href = retpath;
39+
}
40+
41+
if (
42+
instance?.providerStatus === 'stopped' &&
43+
!isToastVisible &&
44+
!toaster.has(autoStartToastName) &&
45+
autoStartEnable
46+
) {
47+
setIsToastVisible(true); // so that the toast is not recreated with every render
48+
49+
startInstance(instance)
50+
.then(() => {
51+
toaster.add({
52+
name: autoStartToastName,
53+
title: i18nInstanceActions('starting'),
54+
content: i18nInstanceActions('auto-start-instance-description'),
55+
theme: 'info',
56+
autoHiding: 4000,
57+
});
58+
})
59+
.catch((err) => showErrorToast(err));
60+
}
61+
62+
if (
63+
instance?.providerStatus === 'stopped' &&
64+
!isToastVisible &&
65+
!toaster.has(toastName) &&
66+
!autoStartEnable
67+
) {
2568
setIsToastVisible(true); // so that the toast is not recreated with every render
2669

2770
toaster.add({
@@ -33,11 +76,13 @@ export const useInstanceStopWarning = (instance?: InstanceWithProviderStatus) =>
3376
{
3477
label: i18nInstanceActions('start'),
3578
onClick: () => {
36-
startInstance(instance).then(() => {
37-
setTimeout(() => {
38-
setIsToastVisible(false);
39-
}, 1000);
40-
});
79+
startInstance(instance)
80+
.then(() => {
81+
setTimeout(() => {
82+
setIsToastVisible(false);
83+
}, 1000);
84+
})
85+
.catch((err) => showErrorToast(err));
4186
},
4287
},
4388
],
@@ -47,8 +92,9 @@ export const useInstanceStopWarning = (instance?: InstanceWithProviderStatus) =>
4792
if (instance?.providerStatus !== 'stopped') {
4893
setIsToastVisible(false);
4994
toaster.remove(toastName);
95+
toaster.remove(autoStartToastName);
5096
}
51-
}, [instance, dataManager, isToastVisible, startInstance]);
97+
}, [instance, dataManager, isToastVisible, startInstance, autoStartEnable, retpath]);
5298

5399
useEffect(() => {
54100
return () => {

src/ui/i18n-common/i18nInstanceActions/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616
"restart-confirm": "Are you sure you want to restart the instance?",
1717
"rebuild-confirm": "Are you sure you want to rebuild the instance?",
1818
"stop-warning-title": "Instance is stopped",
19-
"stop-warning-description": "To go to the instance, you need to start it"
19+
"stop-warning-description": "To go to the instance, you need to start it",
20+
"auto-start-instance-description": "Once launched, the instance will open automatically.",
21+
"start-instance-error": "Instance start error"
2022
}

src/ui/i18n-common/i18nInstanceActions/ru.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616
"restart-confirm": "Вы действительно хотите перезапустить инстанс?",
1717
"rebuild-confirm": "Вы действительно хотите пересобрать инстанс?",
1818
"stop-warning-title": "Инстанс остановлен",
19-
"stop-warning-description": "Чтобы перейти к инстансу, сначала его нужно запустить"
19+
"stop-warning-description": "Чтобы перейти к инстансу, сначала его нужно запустить",
20+
"auto-start-instance-description": "После запуска инстанс автоматически откроется",
21+
"start-instance-error": "Ошибка запуска инстанса"
2022
}

0 commit comments

Comments
 (0)