fix unmount

This commit is contained in:
SrGooglo 2025-02-05 02:39:10 +00:00
parent dacca7021c
commit ac93563a5e

View File

@ -10,7 +10,7 @@ import "./index.less"
export class Tab extends React.Component { export class Tab extends React.Component {
state = { state = {
error: null error: null,
} }
// handle on error // handle on error
@ -20,35 +20,36 @@ export class Tab extends React.Component {
render() { render() {
if (this.state.error) { if (this.state.error) {
return <antd.Result return (
<antd.Result
status="error" status="error"
title="Error" title="Error"
subTitle={this.state.error} subTitle={this.state.error}
/> />
)
} }
return <> return <>{this.props.children}</>
{this.props.children}
</>
} }
} }
export const Panel = (props) => { export const Panel = (props) => {
return <div return (
{...props.props ?? {}} <div
className={classnames( {...(props.props ?? {})}
"panel", className={classnames("panel", props.align, props.className)}
props.align,
props.className
)}
> >
{props.children} {props.children}
</div> </div>
)
} }
export class PagePanelWithNavMenu extends React.Component { export class PagePanelWithNavMenu extends React.Component {
state = { state = {
activeTab: new URLSearchParams(window.location.search).get("type") ?? this.props.defaultTab ?? this.props.tabs[0].key, activeTab:
new URLSearchParams(window.location.search).get("type") ??
this.props.defaultTab ??
this.props.tabs[0].key,
renders: [], renders: [],
} }
@ -62,7 +63,7 @@ export class PagePanelWithNavMenu extends React.Component {
id: id, id: id,
component: component, component: component,
options: options, options: options,
ref: React.createRef() ref: React.createRef(),
}) })
this.setState({ this.setState({
@ -79,7 +80,7 @@ export class PagePanelWithNavMenu extends React.Component {
this.setState({ this.setState({
renders: renders, renders: renders,
}) })
} },
} }
componentDidMount() { componentDidMount() {
@ -97,7 +98,9 @@ export class PagePanelWithNavMenu extends React.Component {
delete app.layout.page_panels delete app.layout.page_panels
if (!app.isMobile) { if (!app.isMobile) {
if (app.layout.header) {
app.layout.header.render(null) app.layout.header.render(null)
}
} else { } else {
app.layout.top_bar.renderDefault() app.layout.top_bar.renderDefault()
} }
@ -128,12 +131,18 @@ export class PagePanelWithNavMenu extends React.Component {
tab = this.props.tabs.find((children) => children.key === key) tab = this.props.tabs.find((children) => children.key === key)
} else { } else {
if (!tab.children) { if (!tab.children) {
console.error("PagePanelWithNavMenu: tab.children is not defined") console.error(
"PagePanelWithNavMenu: tab.children is not defined",
)
return tab = null return (tab = null)
} }
tab = tab.children.find((children) => children.key === `${activeTabDirectory[index - 1]}.${key}`) tab = tab.children.find(
(children) =>
children.key ===
`${activeTabDirectory[index - 1]}.${key}`,
)
} }
}) })
@ -142,11 +151,13 @@ export class PagePanelWithNavMenu extends React.Component {
return this.props.onNotFound() return this.props.onNotFound()
} }
return <antd.Result return (
<antd.Result
status="404" status="404"
title="404" title="404"
subTitle="Sorry, the tab you visited does not exist." subTitle="Sorry, the tab you visited does not exist."
/> />
)
} }
const componentProps = tab.props ?? this.props.tabProps const componentProps = tab.props ?? this.props.tabProps
@ -186,19 +197,26 @@ export class PagePanelWithNavMenu extends React.Component {
}) })
} }
console.warn("PagePanelWithNavMenu: transition is enabled but document.startViewTransition is not compatible with your browser") console.warn(
"PagePanelWithNavMenu: transition is enabled but document.startViewTransition is not compatible with your browser",
)
if (this.primaryPanelRef.current && this.primaryPanelRef.current?.classList) { if (
this.primaryPanelRef.current &&
this.primaryPanelRef.current?.classList
) {
// set to primary panel fade-opacity-leave class // set to primary panel fade-opacity-leave class
this.primaryPanelRef.current.classList.add("fade-opacity-leave") this.primaryPanelRef.current.classList.add("fade-opacity-leave")
// remove fade-opacity-leave class after animation // remove fade-opacity-leave class after animation
setTimeout(() => { setTimeout(() => {
this.primaryPanelRef.current.classList.remove("fade-opacity-leave") this.primaryPanelRef.current.classList.remove(
"fade-opacity-leave",
)
}, 300) }, 300)
} }
await new Promise(resolve => setTimeout(resolve, 200)) await new Promise((resolve) => setTimeout(resolve, 200))
} }
return this.tabChange(key) return this.tabChange(key)
@ -206,7 +224,9 @@ export class PagePanelWithNavMenu extends React.Component {
getItems = (items) => { getItems = (items) => {
if (!Array.isArray(items)) { if (!Array.isArray(items)) {
console.error(`[items] is not an (array), received (${typeof items})`) console.error(
`[items] is not an (array), received (${typeof items})`,
)
return [] return []
} }
@ -225,44 +245,50 @@ export class PagePanelWithNavMenu extends React.Component {
} }
render() { render() {
return <> return (
{ <>
app.isMobile && app.layout.top_bar.render(<NavMenu {app.isMobile &&
app.layout.top_bar.render(
<NavMenu
activeKey={this.state.activeTab} activeKey={this.state.activeTab}
items={this.getItems(this.props.tabs)} items={this.getItems(this.props.tabs)}
onClickItem={(key) => this.handleTabChange(key)} onClickItem={(key) => this.handleTabChange(key)}
/>) />,
} )}
{ {!app.isMobile &&
!app.isMobile && app.layout.header.render(<NavMenu app.layout.header.render(
<NavMenu
header={this.props.navMenuHeader} header={this.props.navMenuHeader}
activeKey={this.state.activeTab} activeKey={this.state.activeTab}
items={this.getItems([...this.props.tabs ?? [], ...this.props.extraItems ?? []])} items={this.getItems([
...(this.props.tabs ?? []),
...(this.props.extraItems ?? []),
])}
onClickItem={(key) => this.handleTabChange(key)} onClickItem={(key) => this.handleTabChange(key)}
renderNames renderNames
> >
{ {Array.isArray(this.state.renders) && [
Array.isArray(this.state.renders) && [
this.state.renders.map((render, index) => { this.state.renders.map((render, index) => {
return React.createElement(render.component, { return React.createElement(
render.component,
{
...render.options.props, ...render.options.props,
ref: render.ref ref: render.ref,
}) },
}) )
] }),
} ]}
</NavMenu>) </NavMenu>,
} )}
<div className="pagePanels"> <div className="pagePanels">
<div className="panel" ref={this.primaryPanelRef}> <div className="panel" ref={this.primaryPanelRef}>
{ {this.renderActiveTab()}
this.renderActiveTab()
}
</div> </div>
</div> </div>
</> </>
)
} }
} }