diff --git a/webapp/src/App.js b/webapp/src/App.js index 8eda0c3..6ba4ce7 100644 --- a/webapp/src/App.js +++ b/webapp/src/App.js @@ -8,6 +8,7 @@ import { createTheme, ThemeProvider } from '@mui/material/styles'; import config from './config'; import NotebookModel from './models/NotebookModel'; import DirectoryModel from './models/DirectoryModel'; +import SparkModel from './models/SparkModel'; const theme = createTheme({ components: { @@ -128,22 +129,34 @@ const App = () => { } }; - const handleExistingNotebookClick = (path) => { + const handleExistingNotebookClick = async (path) => { if (handleUnsavedChanges()) { - NotebookModel.fetchNotebook(`${path}`).then((data) => { - if (data.message == 'Token has expired') { - console.error('Token has expired, please log in again'); - logout(); - } else { - console.log('Fetched notebook:', data); - setNotebook(data); - setShowHistoryServer(false); - setShowScheduler(false); - setShowNotebook(true); + try { + const [notebookData, sparkApp] = await Promise.all([ + NotebookModel.fetchNotebook(`${path}`), + SparkModel.getSparkAppByNotebookPath(path) + ]); + + if (notebookData.message === 'Token has expired') { + console.error('Token has expired, please log in again'); + logout(); + return; + } + + console.log('Fetched notebook:', notebookData); + setNotebook(notebookData); + + // Update Spark badge if there's an active Spark app + if (sparkApp) { + setSparkAppId(sparkApp.spark_app_id); + } + + setShowHistoryServer(false); + setShowScheduler(false); + setShowNotebook(true); + } catch (error) { + console.error('Failed to fetch notebook or Spark app:', error); } - }).catch((error) => { - console.error('Failed to fetch notebook:', error); - }); } } diff --git a/webapp/src/models/SparkModel.js b/webapp/src/models/SparkModel.js index bc29db8..d05d374 100644 --- a/webapp/src/models/SparkModel.js +++ b/webapp/src/models/SparkModel.js @@ -94,6 +94,28 @@ spark`; throw error; } } + + static async getSparkAppByNotebookPath(notebookPath) { + const token = sessionStorage.getItem('token'); + try { + const response = await fetch(`${config.serverBaseUrl}/notebook/spark_app/${notebookPath}`, { + headers: { + 'Authorization': `Bearer ${token}`, + 'Content-Type': 'application/json' + } + }); + + if (!response.ok) { + return null; + } + + const sparkApps = await response.json(); + return sparkApps.length > 0 ? sparkApps[0] : null; + } catch (error) { + console.error('Failed to fetch Spark app:', error); + return null; + } + } } export default SparkModel;