1- import { generateRandomString } from '../helper' ;
21import { useSuspendedBackendaiClient , useWebUINavigate } from '../hooks' ;
3- import {
4- AppOption ,
5- SessionLauncherFormValue ,
6- } from '../pages/SessionLauncherPage' ;
7- import { SessionLauncherPageLocationState } from './LocationStateBreadCrumb' ;
2+ import { PrimaryAppOption } from './ComputeSessionNodeItems/SessionActionButtons' ;
83import { CloudDownloadOutlined } from '@ant-design/icons' ;
9- import { Button , Form , FormInstance , FormProps , Input } from 'antd' ;
4+ import { App , Form , FormInstance , FormProps , Input } from 'antd' ;
5+ import {
6+ BAIButton ,
7+ generateRandomString ,
8+ useErrorMessageResolver ,
9+ } from 'backend.ai-ui' ;
1010import { useRef } from 'react' ;
1111import { useTranslation } from 'react-i18next' ;
12+ import {
13+ StartSessionWithDefaultValue ,
14+ useStartSession ,
15+ } from 'src/hooks/useStartSession' ;
1216
1317const regularizeGithubURL = ( url : string ) => {
1418 url = url . replace ( '/blob/' , '/' ) ;
@@ -20,8 +24,57 @@ const ImportNotebook: React.FC<FormProps> = (props) => {
2024 url : string ;
2125 } > | null > ( null ) ;
2226 const { t } = useTranslation ( ) ;
27+ const app = App . useApp ( ) ;
2328 const webuiNavigate = useWebUINavigate ( ) ;
24- useSuspendedBackendaiClient ( ) ;
29+ const baiClient = useSuspendedBackendaiClient ( ) ;
30+ const { startSessionWithDefault, upsertSessionNotification } =
31+ useStartSession ( ) ;
32+ const { getErrorMessage } = useErrorMessageResolver ( ) ;
33+
34+ const handleNotebookImport = async ( url : string ) => {
35+ const notebookURL = regularizeGithubURL ( url ) ;
36+ const fileName = notebookURL . split ( '/' ) . pop ( ) ;
37+
38+ const launcherValue : StartSessionWithDefaultValue = {
39+ sessionName : `imported-notebook-${ generateRandomString ( 5 ) } ` ,
40+ environments : {
41+ version : baiClient . _config . default_import_environment ,
42+ } ,
43+ bootstrap_script : '#!/bin/sh\ncurl -O ' + notebookURL ,
44+ } ;
45+
46+ const results = await startSessionWithDefault ( launcherValue ) ;
47+ if ( results . fulfilled && results . fulfilled . length > 0 ) {
48+ // Handle successful result
49+ upsertSessionNotification ( results . fulfilled , [
50+ {
51+ // TODO: send appOption
52+ extraData : {
53+ appName : 'jupyter' ,
54+ urlPostfix : '&redirect=/notebooks/' + fileName ,
55+ } as PrimaryAppOption ,
56+ } ,
57+ ] ) ;
58+ }
59+
60+ if ( results ?. rejected && results . rejected . length > 0 ) {
61+ const error = results . rejected [ 0 ] . reason ;
62+ app . modal . error ( {
63+ title : error ?. title ,
64+ content : getErrorMessage ( error ) ,
65+ } ) ;
66+ }
67+
68+ webuiNavigate ( '/session' ) ;
69+
70+ // navigateWithSessionLauncher(launcherValue, {
71+ // appOption: {
72+ // runtime: 'jupyter',
73+ // filename: fileName,
74+ // } as AppOption,
75+ // });
76+ } ;
77+
2578 return (
2679 < Form ref = { formRef } layout = "inline" { ...props } >
2780 < Form . Item
@@ -46,45 +99,20 @@ const ImportNotebook: React.FC<FormProps> = (props) => {
4699 >
47100 < Input placeholder = { t ( 'import.NotebookURL' ) } />
48101 </ Form . Item >
49- < Button
102+ < BAIButton
50103 icon = { < CloudDownloadOutlined /> }
51104 type = "primary"
52- onClick = { ( ) => {
53- formRef . current
105+ action = { async ( ) => {
106+ const values = await formRef . current
54107 ?. validateFields ( )
55- . then ( ( values ) => {
56- const notebookURL = regularizeGithubURL ( values . url ) ;
57- const launcherValue : DeepPartial < SessionLauncherFormValue > = {
58- sessionName : 'imported-notebook-' + generateRandomString ( 5 ) ,
59- environments : {
60- environment : 'cr.backend.ai/stable/python' ,
61- } ,
62- bootstrap_script : '#!/bin/sh\ncurl -O ' + notebookURL ,
63- } ;
64- const params = new URLSearchParams ( ) ;
65- params . set ( 'step' , '4' ) ;
66- params . set ( 'formValues' , JSON . stringify ( launcherValue ) ) ;
67- params . set (
68- 'appOption' ,
69- JSON . stringify ( {
70- runtime : 'jupyter' ,
71- filename : notebookURL . split ( '/' ) . pop ( ) ,
72- } as AppOption ) ,
73- ) ;
74- webuiNavigate ( `/session/start?${ params . toString ( ) } ` , {
75- state : {
76- from : {
77- pathname : '/import' ,
78- label : t ( 'webui.menu.Import&Run' ) ,
79- } ,
80- } as SessionLauncherPageLocationState ,
81- } ) ;
82- } )
83- . catch ( ( ) => { } ) ;
108+ . catch ( ( ) => undefined ) ;
109+ if ( values ) {
110+ await handleNotebookImport ( values . url ) ;
111+ }
84112 } }
85113 >
86114 { t ( 'import.GetAndRunNotebook' ) }
87- </ Button >
115+ </ BAIButton >
88116 </ Form >
89117 ) ;
90118} ;
0 commit comments