Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
prasunanand committed Dec 16, 2024
1 parent 179ffb7 commit 7c59e7f
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 36 deletions.
4 changes: 4 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var Zasper Application
type Application struct {
BaseUrl string
StaticUrl string
UserName string
HomeDir string
JupyterConfigDir string
JupyterDataDir string
Expand All @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion kernel/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "{}"
Expand Down
11 changes: 7 additions & 4 deletions kernel/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ import (
"fmt"
"net"
"os"
"runtime"
"strconv"

xrand "golang.org/x/exp/rand"

"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 '-'.
Expand Down
2 changes: 2 additions & 0 deletions ui/src/ide/editor/notebook/NotebookEditor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

.editor-select{
width: 200px;
font-weight: 300;
font-size: 16px;
}

.activeCell{
Expand Down
69 changes: 39 additions & 30 deletions ui/src/ide/editor/notebook/NotebookEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -61,6 +61,7 @@ export default function NotebookEditor(props) {
const [kernelMap, setKernelMap] = useAtom(kernelsAtom);
const [session, setSession] = useState<ISession | null>();
const [kernelStatus, setKernelStatus] = useState("idle")
const [userName] = useAtom(userNameAtom)

interface IClient {
send: any;
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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) => {
Expand Down
4 changes: 4 additions & 0 deletions ui/src/ide/sidebar/FileBrowser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -20,6 +22,7 @@ export default function FileBrowser({ sendDataToParent, display }: FileBrowserPr
const [contents, setContents] = useState<IContent[]>([]);
const [cwd] = useState<string>('');
const [projectName, setProjectName] = useState('')
const [userName, setUserName] = useAtom(userNameAtom)

const FetchData = async () => {
const res = await fetch(BaseApiUrl + '/api/contents?type=notebook&hash=0', {
Expand All @@ -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)

};

Expand Down
9 changes: 9 additions & 0 deletions ui/src/ide/topbar/TopBar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 9 additions & 1 deletion ui/src/ide/topbar/Topbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>(false);
const [showFileAutocomplete, setShowFileAutocomplete] = useState<boolean>(false);
const [userName] = useAtom(userNameAtom)

const commands = [
{
Expand Down Expand Up @@ -63,7 +66,7 @@ export default function Topbar (props) {
<div className='col-3'>
<img className='logo-white' src='./images/logo-white.svg' alt='#' />
</div>
<div className='col-9'>
<div className='col-8'>
<div className='searchArea'>
<div className='search-wraper'>
<button className='openCommandPaletteButton' onClick={toggleFileAutoComplete}>Type your search here <img src='./images/icons/search.svg' alt='#' /></button>
Expand All @@ -85,6 +88,11 @@ export default function Topbar (props) {
)}
</div>
</div>
<div className='col-1'>
<div className='userName'>
<span>{userName}</span>
</div>
</div>
</div>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions ui/src/store/AppState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const kernelspecsAtom = atom<IKernelspecsState>({})
export const kernelsAtom = atom<IKernelsState>({})
export const terminalsAtom = atom<ITerminalsState>({})
export const terminalsCountAtom = atom<number>(0)
export const userNameAtom = atom<string>("")


// left statusBar
Expand Down
10 changes: 10 additions & 0 deletions utils/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ import (
"os/exec"
"path/filepath"
"regexp"
"runtime"
)

func GetHomeDir() string {
dir, _ := os.Getwd()
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)
Expand Down

0 comments on commit 7c59e7f

Please sign in to comment.