Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
mudream4869 committed Jul 10, 2024
2 parents 35e74d0 + a56c26e commit 0da15ba
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 12 deletions.
20 changes: 20 additions & 0 deletions cmd/toolgui-demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
_ "embed"
"errors"
"fmt"
"log"

Expand Down Expand Up @@ -282,6 +283,25 @@ func MiscPage(p *framework.Params) error {
tcmisc.ProgressBar(prgbarCompCol, 30, "progress_bar")
})

tccontent.Divider(p.Main)

errorCompCol, errorCodeCol := tclayout.Column2(p.Main, "show_error")
if tcinput.Button(p.State, errorCompCol, "Show error") {
return errors.New("new error")
}
tccontent.Code(errorCodeCol, `if tcinput.Button(p.State, errorCompCol, "Show error") {
return errors.New("New error")
}`, "go")

tccontent.Divider(p.Main)

panicCompCol, panicCodeCol := tclayout.Column2(p.Main, "show_panic")
tcmisc.Echo(panicCodeCol, code, func() {
if tcinput.Button(p.State, panicCompCol, "Show panic") {
panic("show panic")
}
})

return nil
}

Expand Down
1 change: 1 addition & 0 deletions toolgui-e2e/cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { defineConfig } = require("cypress");

module.exports = defineConfig({
e2e: {
projectId: "rtf9hd",
baseUrl: 'http://localhost:3000',
setupNodeEvents(on, config) {
// implement node event listeners here
Expand Down
13 changes: 13 additions & 0 deletions toolgui-e2e/cypress/e2e/misc.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
describe('template spec', () => {
it('Error handling', () => {
cy.visit('/misc')
cy.contains('Show error').click()
cy.contains('new error').should('exist')
})

it('Panic handling', () => {
cy.visit('/misc')
cy.contains('Show panic').click()
cy.contains('show panic').should('exist')
})
})
23 changes: 18 additions & 5 deletions toolgui-web/lib/src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AppConf } from './AppConf';
import { AppNavbar } from './AppNavbar';
import { AppBody } from './AppBody';
import { setIcon } from '../util/seticon';
import { AppError, Error } from './AppError';

const NOTIFY_TYPE_CREATE = 1
const NOTIFY_TYPE_UPDATE = 2
Expand All @@ -28,6 +29,7 @@ interface AppState {
running: boolean
pageFound: boolean
pageName: string
error: Error | null
}

export class App extends Component<AppProps, AppState> {
Expand Down Expand Up @@ -61,27 +63,29 @@ export class App extends Component<AppProps, AppState> {

this.state = {
forest: new Forest([
props.appConf.root_container_id,
props.appConf.main_container_id,
props.appConf.sidebar_container_id,
]),
running: false,
pageFound: pageFound,
pageName: pageName,
error: null,
}
}

componentDidMount() {
this.update({})
}

startUpdating() {
startUpdate() {
this.setState((prevState) => {
const newForest = prevState.forest.swallowCopy()
newForest.setToRemoving()

return {
running: true,
forest: newForest,
error: null,
}
})
}
Expand Down Expand Up @@ -126,24 +130,31 @@ export class App extends Component<AppProps, AppState> {
}
}

finishUpdating() {
finishUpdate(pack: any) {
this.setState((prevState) => {
const newForest = prevState.forest.swallowCopy()
newForest.removeNodeWithRemovingTag()
var err: Error | null = null
if (!pack.success) {
err = {
msg: pack.error
}
}

return {
running: false,
forest: newForest,
error: err,
}
})
}

update(event: UpdateEvent) {
this.props.updater(event,
() => { this.startUpdating() },
() => { this.startUpdate() },
clearState,
(pack) => { this.receiveNotifyPack(pack) },
() => { this.finishUpdating() })
(pack) => { this.finishUpdate(pack) })
}

render() {
Expand All @@ -161,6 +172,8 @@ export class App extends Component<AppProps, AppState> {
pageFound={this.state.pageFound}
forest={this.state.forest}
update={(e) => { this.update(e) }} />

<AppError error={this.state.error} />
</div >
)
}
Expand Down
2 changes: 1 addition & 1 deletion toolgui-web/lib/src/app/AppBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class AppBody extends Component<AppBodyProps> {
}

rootNode() {
return this.props.forest.nodes[this.props.appConf.root_container_id]
return this.props.forest.nodes[this.props.appConf.main_container_id]
}

sidebarNode() {
Expand Down
2 changes: 1 addition & 1 deletion toolgui-web/lib/src/app/AppConf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface AppConf {
page_names: string[]
page_confs: { [page_name: string]: any }

root_container_id: string,
main_container_id: string,
sidebar_container_id: string,

hash_page_name_mode: boolean,
Expand Down
27 changes: 27 additions & 0 deletions toolgui-web/lib/src/app/AppError.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React, { Component, ReactNode } from "react";

export interface Error {
msg: string
}

interface AppErrorProps {
error: Error | null
}

export class AppError extends Component<AppErrorProps> {
render(): ReactNode {
if (!this.props.error) {
return <></>
}

return (
<div className="container" style={{ paddingTop: '10px' }}>
<article className="message is-danger">
<div className="message-body">
{this.props.error.msg}
</div>
</article>
</div>
)
}
}
5 changes: 4 additions & 1 deletion toolgui-web/lib/src/app/Nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export class Forest {
if (nodeID in this.nodes) {
const parentID = this.nodes[nodeID].parentID
const idx = this.nodes[parentID].children.findIndex(n => n.props.id === nodeID)
this.nodes[parentID].children.splice(idx, 1)
// TBD: Why we need check here?
if (idx != -1) {
this.nodes[parentID].children.splice(idx, 1)
}
}

// create or modify node in node pool
Expand Down
2 changes: 1 addition & 1 deletion toolgui/executor/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (e *WebExecutor) handleUpdate(ws *websocket.Conn) {
}
}

err = e.app.Run(pageName, state, sendNotifyPack)
err = e.app.RunWithHandlingPanic(pageName, state, sendNotifyPack)
if err != nil {
websocket.JSON.Send(ws, &resultPack{
Error: err.Error(),
Expand Down
23 changes: 20 additions & 3 deletions toolgui/framework/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package framework

import (
"errors"
"log"

"github.com/mudream4869/toolgui/toolgui/tgutil"
)

var ErrPageNotFound = errors.New("page not found")
var ErrPanic = errors.New("panic")

// MainContainerID is the id of root container.
// The creation of root container won't trigger SendNotifyPackFunc.
const MainContainerID string = "container_main"

func realRootContainerID() string {
func realMainContainerID() string {
return NewContainer(MainContainerID, nil).ID
}

Expand Down Expand Up @@ -61,7 +63,7 @@ type AppConf struct {

HashPageNameMode bool `json:"hash_page_name_mode"`

RootContainerID string `json:"root_container_id"`
MainContainerID string `json:"main_container_id"`
SidebarContainerID string `json:"sidebar_container_id"`
}

Expand Down Expand Up @@ -132,13 +134,28 @@ func (app *App) AppConf() *AppConf {
PageNames: app.pageNames,
PageConfs: app.pageConfs,

RootContainerID: realRootContainerID(),
MainContainerID: realMainContainerID(),
SidebarContainerID: realSidebarContainerID(),

HashPageNameMode: app.hashPageNameMode,
}
}

func (app *App) RunWithHandlingPanic(
name string, state *State, notifyFunc SendNotifyPackFunc) (err error) {

defer func() {
r := recover()
if r != nil {
log.Println("Panic", r)
err = tgutil.Errorf("%w: %v", ErrPanic, r)
}
}()

err = app.Run(name, state, notifyFunc)
return
}

// Run run a page which named `name` with state
func (app *App) Run(name string, state *State, notifyFunc SendNotifyPackFunc) error {
pageFunc, ok := app.pageFuncs[name]
Expand Down

0 comments on commit 0da15ba

Please sign in to comment.