From 7c59e7fa4bdb7b7d10919c573ed7cd0ebbf6f6a7 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Mon, 16 Dec 2024 13:51:36 +0530 Subject: [PATCH] refactoring --- app.go | 4 ++ core/config.go | 2 + kernel/message.go | 2 +- kernel/utils.go | 11 +-- .../ide/editor/notebook/NotebookEditor.scss | 2 + ui/src/ide/editor/notebook/NotebookEditor.tsx | 69 +++++++++++-------- ui/src/ide/sidebar/FileBrowser.tsx | 4 ++ ui/src/ide/topbar/TopBar.scss | 9 +++ ui/src/ide/topbar/Topbar.tsx | 10 ++- ui/src/store/AppState.tsx | 1 + utils/paths.go | 10 +++ 11 files changed, 88 insertions(+), 36 deletions(-) diff --git a/app.go b/app.go index 9bc9f1a..929be78 100644 --- a/app.go +++ b/app.go @@ -32,11 +32,15 @@ import ( // Response structure to return as JSON type InfoResponse struct { ProjectName string `json:"project"` + UserName string `json:"username"` + OS string `json:"os"` } func InfoHandler(w http.ResponseWriter, r *http.Request) { response := InfoResponse{ ProjectName: core.Zasper.ProjectName, + UserName: core.Zasper.UserName, + OS: core.Zasper.OSName, } w.Header().Set("Content-Type", "application/json") diff --git a/core/config.go b/core/config.go index 7e945b9..07057be 100644 --- a/core/config.go +++ b/core/config.go @@ -11,6 +11,7 @@ var Zasper Application type Application struct { BaseUrl string StaticUrl string + UserName string HomeDir string JupyterConfigDir string JupyterDataDir string @@ -29,6 +30,7 @@ func SetUpZasper(cwd string) Application { BaseUrl: "https://zasper.io", ProjectName: utils.GetProjectName(cwd), HomeDir: cwd, + UserName: utils.GetUsername(), StaticUrl: "./images", OSName: runtime.GOOS, JupyterConfigDir: utils.GetJupyterConfigDir(), diff --git a/kernel/message.go b/kernel/message.go index 3aa705d..b79e50f 100644 --- a/kernel/message.go +++ b/kernel/message.go @@ -55,7 +55,7 @@ func (ks *KernelSession) createMsg(msgType string, func (ks *KernelSession) MessageFromString(value string) Message { msg := Message{} - msg.Header = ks.newMsgHeader(value, getUsername(), ks.Key) + msg.Header = ks.newMsgHeader(value, GetUsername(), ks.Key) msg.MsgId = msg.Header.MsgID msg.Content = "{}" msg.Metadata = "{}" diff --git a/kernel/utils.go b/kernel/utils.go index 41ff745..f7f99c2 100644 --- a/kernel/utils.go +++ b/kernel/utils.go @@ -6,6 +6,7 @@ import ( "fmt" "net" "os" + "runtime" "strconv" xrand "golang.org/x/exp/rand" @@ -13,12 +14,14 @@ import ( "github.com/rs/zerolog/log" ) -func getUsername() string { - // note that this may be USER on some UNIX platforms - // return os.Getenv("USERNAME") +func GetUsername() string { + // Check if the OS is Windows + if runtime.GOOS == "windows" { + return os.Getenv("USERNAME") // Windows typically uses "USERNAME" + } + // For UNIX-like systems (Linux, macOS), use "USER" return os.Getenv("USER") } - func newID() string { // newID generates a new random ID as a string. // The ID format is 32 random bytes as hex-encoded text, with chunks separated by '-'. diff --git a/ui/src/ide/editor/notebook/NotebookEditor.scss b/ui/src/ide/editor/notebook/NotebookEditor.scss index db189e3..7962181 100644 --- a/ui/src/ide/editor/notebook/NotebookEditor.scss +++ b/ui/src/ide/editor/notebook/NotebookEditor.scss @@ -31,6 +31,8 @@ .editor-select{ width: 200px; + font-weight: 300; + font-size: 16px; } .activeCell{ diff --git a/ui/src/ide/editor/notebook/NotebookEditor.tsx b/ui/src/ide/editor/notebook/NotebookEditor.tsx index 0a7d2a7..7d72a57 100644 --- a/ui/src/ide/editor/notebook/NotebookEditor.tsx +++ b/ui/src/ide/editor/notebook/NotebookEditor.tsx @@ -9,7 +9,7 @@ import NbButtons from './NbButtons'; import Cell, { CodeMirrorRef, ICell } from './Cell'; import { useAtom } from 'jotai'; import { themeAtom } from '../../../store/Settings'; -import { IKernel, kernelsAtom } from '../../../store/AppState'; +import { IKernel, kernelsAtom, userNameAtom } from '../../../store/AppState'; const debugMode = false; @@ -61,6 +61,7 @@ export default function NotebookEditor(props) { const [kernelMap, setKernelMap] = useAtom(kernelsAtom); const [session, setSession] = useState(); const [kernelStatus, setKernelStatus] = useState("idle") + const [userName] = useAtom(userNameAtom) interface IClient { send: any; @@ -247,7 +248,7 @@ export default function NotebookEditor(props) { msg_id: cellId, msg_type: 'execute_request', session: session.id, - username: 'prasunanand', + username: userName, version: '5.2', }, metadata: { @@ -276,37 +277,45 @@ export default function NotebookEditor(props) { const getTimeStamp = () => new Date().toISOString(); const addCellUp = () => { - setNotebook((prevNotebook) => ({ - ...prevNotebook, - cells: [ - ...prevNotebook.cells, - { - execution_count: 0, - source: '', - cell_type: 'raw', - id: uuidv4(), - reload: false, - outputs: '', - }, - ], - })); + setNotebook((prevNotebook) => { + const newCell = { + execution_count: 0, + source: '', + cell_type: 'code', + id: uuidv4(), + reload: false, + outputs: '', + }; + + const updatedCells = [ + ...prevNotebook.cells.slice(0, focusedIndex), + newCell, // The new cell to be inserted + ...prevNotebook.cells.slice(focusedIndex), + ]; + + return { ...prevNotebook, cells: updatedCells }; + }); }; const addCellDown = () => { - setNotebook((prevNotebook) => ({ - ...prevNotebook, - cells: [ - ...prevNotebook.cells, - { - execution_count: 0, - source: '', - cell_type: 'raw', - id: uuidv4(), - reload: false, - outputs: '', - }, - ], - })); + setNotebook((prevNotebook) => { + const newCell = { + execution_count: 0, + source: '', + cell_type: 'code', + id: uuidv4(), + reload: false, + outputs: '', + }; + + const updatedCells = [ + ...prevNotebook.cells.slice(0, focusedIndex + 1), + newCell, + ...prevNotebook.cells.slice(focusedIndex + 1), + ]; + + return { ...prevNotebook, cells: updatedCells }; + }); }; const deleteCell = (index: number) => { diff --git a/ui/src/ide/sidebar/FileBrowser.tsx b/ui/src/ide/sidebar/FileBrowser.tsx index ad44031..5a7f547 100644 --- a/ui/src/ide/sidebar/FileBrowser.tsx +++ b/ui/src/ide/sidebar/FileBrowser.tsx @@ -2,6 +2,8 @@ import React, { useEffect, useState } from 'react'; import { BaseApiUrl } from '../config'; import ContextMenu from './ContextMenu'; import getFileExtension from '../utils'; +import { useAtom } from 'jotai'; +import { userNameAtom } from '../../store/AppState'; interface IContent { @@ -20,6 +22,7 @@ export default function FileBrowser({ sendDataToParent, display }: FileBrowserPr const [contents, setContents] = useState([]); const [cwd] = useState(''); const [projectName, setProjectName] = useState('') + const [userName, setUserName] = useAtom(userNameAtom) const FetchData = async () => { const res = await fetch(BaseApiUrl + '/api/contents?type=notebook&hash=0', { @@ -32,6 +35,7 @@ export default function FileBrowser({ sendDataToParent, display }: FileBrowserPr const res2 = await fetch(BaseApiUrl + '/api/info'); const resJson2 = await res2.json(); setProjectName(resJson2.project.toUpperCase()); + setUserName(resJson2.username) }; diff --git a/ui/src/ide/topbar/TopBar.scss b/ui/src/ide/topbar/TopBar.scss index cd802d5..657ecdd 100644 --- a/ui/src/ide/topbar/TopBar.scss +++ b/ui/src/ide/topbar/TopBar.scss @@ -7,4 +7,13 @@ width: 100%; padding-left: 45px; background-color: #5f568f; +} + +.userName{ + font-weight: 400; + margin-top: 2px; + border-radius: 5px; + vertical-align: middle; + text-align: center; + padding: 5px 10px; } \ No newline at end of file diff --git a/ui/src/ide/topbar/Topbar.tsx b/ui/src/ide/topbar/Topbar.tsx index 18c970a..d5827a0 100644 --- a/ui/src/ide/topbar/Topbar.tsx +++ b/ui/src/ide/topbar/Topbar.tsx @@ -2,11 +2,14 @@ import React, { useState, useEffect } from 'react' import './TopBar.scss' import CommandPalette from '../command/CommandPalette'; import FileAutocomplete from '../command/FileAutoComplete'; +import { useAtom } from 'jotai'; +import { userNameAtom } from '../../store/AppState'; export default function Topbar (props) { const [showCommandPalette, setShowCommandPalette] = useState(false); const [showFileAutocomplete, setShowFileAutocomplete] = useState(false); + const [userName] = useAtom(userNameAtom) const commands = [ { @@ -63,7 +66,7 @@ export default function Topbar (props) {
#
-
+
@@ -85,6 +88,11 @@ export default function Topbar (props) { )}
+
+
+ {userName} +
+
diff --git a/ui/src/store/AppState.tsx b/ui/src/store/AppState.tsx index 08f78f0..ba4cbbd 100644 --- a/ui/src/store/AppState.tsx +++ b/ui/src/store/AppState.tsx @@ -37,6 +37,7 @@ export const kernelspecsAtom = atom({}) export const kernelsAtom = atom({}) export const terminalsAtom = atom({}) export const terminalsCountAtom = atom(0) +export const userNameAtom = atom("") // left statusBar diff --git a/utils/paths.go b/utils/paths.go index 0f273a7..bb9dd6f 100644 --- a/utils/paths.go +++ b/utils/paths.go @@ -6,6 +6,7 @@ import ( "os/exec" "path/filepath" "regexp" + "runtime" ) func GetHomeDir() string { @@ -13,6 +14,15 @@ func GetHomeDir() string { return dir } +func GetUsername() string { + // Check if the OS is Windows + if runtime.GOOS == "windows" { + return os.Getenv("USERNAME") // Windows typically uses "USERNAME" + } + // For UNIX-like systems (Linux, macOS), use "USER" + return os.Getenv("USER") +} + func GetProjectName(absPath string) string { // Get the last part of the path (i.e., the project name) projectName := filepath.Base(absPath)