ChatGPT-Next-Web/app/components/error.tsx

75 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-06-27 07:13:45 +00:00
"use client";
import React from "react";
import { IconButton } from "./button";
import GithubIcon from "../icons/github.svg";
2023-04-26 18:00:22 +00:00
import ResetIcon from "../icons/reload.svg";
import { ISSUE_URL } from "../constant";
2023-04-26 18:00:22 +00:00
import Locale from "../locales";
import { showConfirm } from "./ui-lib";
import { useSyncStore } from "../store/sync";
2024-09-08 05:23:40 +00:00
import { useChatStore } from "../store/chat";
interface IErrorBoundaryState {
hasError: boolean;
error: Error | null;
info: React.ErrorInfo | null;
}
export class ErrorBoundary extends React.Component<any, IErrorBoundaryState> {
constructor(props: any) {
super(props);
this.state = { hasError: false, error: null, info: null };
}
componentDidCatch(error: Error, info: React.ErrorInfo) {
// Update state with error details
this.setState({ hasError: true, error, info });
}
2023-04-26 18:00:22 +00:00
clearAndSaveData() {
try {
useSyncStore.getState().export();
} finally {
2024-09-08 05:23:40 +00:00
useChatStore.getState().clearAllData();
}
2023-04-26 18:00:22 +00:00
}
render() {
if (this.state.hasError) {
// Render error message
return (
<div className="error">
<h2>Oops, something went wrong!</h2>
<pre>
<code>{this.state.error?.toString()}</code>
<code>{this.state.info?.componentStack}</code>
</pre>
2023-04-26 18:00:22 +00:00
<div style={{ display: "flex", justifyContent: "space-between" }}>
<a href={ISSUE_URL} className="report">
<IconButton
text="Report This Error"
icon={<GithubIcon />}
bordered
/>
</a>
<IconButton
2023-04-26 18:00:22 +00:00
icon={<ResetIcon />}
text="Clear All Data"
onClick={async () => {
2023-06-28 17:09:51 +00:00
if (await showConfirm(Locale.Settings.Danger.Reset.Confirm)) {
this.clearAndSaveData();
}
}}
bordered
/>
2023-04-26 18:00:22 +00:00
</div>
</div>
);
}
// if no error occurred, render children
return this.props.children;
}
}