mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
ui-next: error boundary
This commit is contained in:
5 files changed
+97
-12
No files matched your search
@@ -1,4 +1,5 @@
|
|||||||
export default function BeforeComponent() {
|
export default function BeforeComponent() {
|
||||||
console.log('before app');
|
console.log('before app');
|
||||||
|
// throw new Error('test error boundary in before interceptor');
|
||||||
return <div>before app via @hydrooj/ui-next-plugin-sample</div>;
|
return <div>before app via @hydrooj/ui-next-plugin-sample</div>;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
interface SlotErrorBoundaryProps {
|
||||||
|
slotName: string;
|
||||||
|
label?: string;
|
||||||
|
children: React.ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SlotErrorBoundaryState {
|
||||||
|
hasError: boolean;
|
||||||
|
error: Error | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class SlotErrorBoundary extends React.Component<SlotErrorBoundaryProps, SlotErrorBoundaryState> {
|
||||||
|
state: SlotErrorBoundaryState = { hasError: false, error: null };
|
||||||
|
|
||||||
|
static getDerivedStateFromError(error: Error): SlotErrorBoundaryState {
|
||||||
|
return { hasError: true, error };
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidCatch(error: Error, info: React.ErrorInfo): void {
|
||||||
|
const tag = this.props.label
|
||||||
|
? `[Hydro] SlotErrorBoundary(${this.props.slotName}/${this.props.label})`
|
||||||
|
: `[Hydro] SlotErrorBoundary(${this.props.slotName})`;
|
||||||
|
console.error(tag, error, info.componentStack);
|
||||||
|
}
|
||||||
|
|
||||||
|
render(): React.ReactNode {
|
||||||
|
if (!this.state.hasError) return this.props.children;
|
||||||
|
|
||||||
|
const { slotName, label } = this.props;
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
padding: '8px 12px',
|
||||||
|
margin: '4px 0',
|
||||||
|
border: '1px solid #e74c3c',
|
||||||
|
borderRadius: '4px',
|
||||||
|
backgroundColor: '#fdf0ed',
|
||||||
|
color: '#c0392b',
|
||||||
|
fontSize: '13px',
|
||||||
|
fontFamily: 'monospace',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<strong>Slot Error</strong> in <code>{slotName}</code>
|
||||||
|
{label && <> / <code>{label}</code></>}
|
||||||
|
<pre style={{ margin: '4px 0 0', whiteSpace: 'pre-wrap' }}>
|
||||||
|
{this.state.error?.message}
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
export { SlotErrorBoundary } from './error-boundary';
|
||||||
export { after, before, intercept, patch, replace, wrap } from './interceptors';
|
export { after, before, intercept, patch, replace, wrap } from './interceptors';
|
||||||
export { installPlugin } from './plugin';
|
export { installPlugin } from './plugin';
|
||||||
export type { PluginAPI, PluginDefinition } from './plugin';
|
export type { PluginAPI, PluginDefinition } from './plugin';
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { SlotErrorBoundary } from './error-boundary';
|
||||||
import { store } from './store';
|
import { store } from './store';
|
||||||
import type { Interceptor, InterceptorOptions, SlotName } from './types';
|
import type { Interceptor, InterceptorOptions, SlotName } from './types';
|
||||||
|
|
||||||
@@ -15,11 +16,14 @@ export function before<P extends Record<string, any> = Record<string, any>>(
|
|||||||
Comp: React.FC<P>,
|
Comp: React.FC<P>,
|
||||||
opts?: InterceptorOptions,
|
opts?: InterceptorOptions,
|
||||||
): () => void {
|
): () => void {
|
||||||
|
const label = `before:${Comp.displayName || Comp.name || '?'}`;
|
||||||
return intercept<P>(
|
return intercept<P>(
|
||||||
name,
|
name,
|
||||||
(props, next) => (
|
(props, next) => (
|
||||||
<>
|
<>
|
||||||
<Comp {...props} />
|
<SlotErrorBoundary slotName={name} label={label}>
|
||||||
|
<Comp {...props} />
|
||||||
|
</SlotErrorBoundary>
|
||||||
{next()}
|
{next()}
|
||||||
</>
|
</>
|
||||||
),
|
),
|
||||||
@@ -33,12 +37,15 @@ export function after<P extends Record<string, any> = Record<string, any>>(
|
|||||||
Comp: React.FC<P>,
|
Comp: React.FC<P>,
|
||||||
opts?: InterceptorOptions,
|
opts?: InterceptorOptions,
|
||||||
): () => void {
|
): () => void {
|
||||||
|
const label = `after:${Comp.displayName || Comp.name || '?'}`;
|
||||||
return intercept<P>(
|
return intercept<P>(
|
||||||
name,
|
name,
|
||||||
(props, next) => (
|
(props, next) => (
|
||||||
<>
|
<>
|
||||||
{next()}
|
{next()}
|
||||||
<Comp {...props} />
|
<SlotErrorBoundary slotName={name} label={label}>
|
||||||
|
<Comp {...props} />
|
||||||
|
</SlotErrorBoundary>
|
||||||
</>
|
</>
|
||||||
),
|
),
|
||||||
{ priority: 100, ...opts },
|
{ priority: 100, ...opts },
|
||||||
@@ -64,9 +71,14 @@ export function replace<P extends Record<string, any> = Record<string, any>>(
|
|||||||
Replacement: React.FC<P>,
|
Replacement: React.FC<P>,
|
||||||
opts?: { id?: string, priority?: number },
|
opts?: { id?: string, priority?: number },
|
||||||
): () => void {
|
): () => void {
|
||||||
|
const label = `replace:${Replacement.displayName || Replacement.name || '?'}`;
|
||||||
return intercept<P>(
|
return intercept<P>(
|
||||||
name,
|
name,
|
||||||
(props, _next) => <Replacement {...props} />,
|
(props, _next) => (
|
||||||
|
<SlotErrorBoundary slotName={name} label={label}>
|
||||||
|
<Replacement {...props} />
|
||||||
|
</SlotErrorBoundary>
|
||||||
|
),
|
||||||
{ priority: 0, ...opts },
|
{ priority: 0, ...opts },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -77,9 +89,14 @@ export function wrap<P extends Record<string, any> = Record<string, any>>(
|
|||||||
Wrapper: React.FC<{ children: React.ReactNode } & P>,
|
Wrapper: React.FC<{ children: React.ReactNode } & P>,
|
||||||
opts?: InterceptorOptions,
|
opts?: InterceptorOptions,
|
||||||
): () => void {
|
): () => void {
|
||||||
|
const label = `wrap:${Wrapper.displayName || Wrapper.name || '?'}`;
|
||||||
return intercept<P>(
|
return intercept<P>(
|
||||||
name,
|
name,
|
||||||
(props, next) => <Wrapper {...props}>{next()}</Wrapper>,
|
(props, next) => (
|
||||||
|
<SlotErrorBoundary slotName={name} label={label}>
|
||||||
|
<Wrapper {...props}>{next()}</Wrapper>
|
||||||
|
</SlotErrorBoundary>
|
||||||
|
),
|
||||||
{ priority: -10, ...opts },
|
{ priority: -10, ...opts },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -1,21 +1,30 @@
|
|||||||
import { useMemo, useSyncExternalStore } from 'react';
|
import { useMemo, useSyncExternalStore } from 'react';
|
||||||
|
import { SlotErrorBoundary } from './error-boundary';
|
||||||
import { store } from './store';
|
import { store } from './store';
|
||||||
import type { InterceptorEntry, SlotName } from './types';
|
import type { InterceptorEntry, SlotName } from './types';
|
||||||
|
|
||||||
function buildChain<P extends Record<string, any>>(
|
function buildChain<P extends Record<string, any>>(
|
||||||
interceptors: InterceptorEntry<P>[],
|
interceptors: InterceptorEntry<P>[],
|
||||||
DefaultComp: React.FC<P>,
|
DefaultComp: React.FC<P>,
|
||||||
|
slotName: SlotName,
|
||||||
): (props: P) => React.ReactNode {
|
): (props: P) => React.ReactNode {
|
||||||
let pipeline: (props: P) => React.ReactNode = (props) => <DefaultComp {...props} />;
|
let pipeline: (props: P) => React.ReactNode = (props) => (
|
||||||
|
<SlotErrorBoundary slotName={slotName} label="default">
|
||||||
|
<DefaultComp {...props} />
|
||||||
|
</SlotErrorBoundary>
|
||||||
|
);
|
||||||
|
|
||||||
for (let i = interceptors.length - 1; i >= 0; i--) {
|
for (let i = interceptors.length - 1; i >= 0; i--) {
|
||||||
const { interceptor } = interceptors[i];
|
const { interceptor, id } = interceptors[i];
|
||||||
const downstream = pipeline;
|
const downstream = pipeline;
|
||||||
|
|
||||||
pipeline = (props: P) =>
|
pipeline = (props: P) => (
|
||||||
interceptor(props, (overrideProps) =>
|
<SlotErrorBoundary slotName={slotName} label={`interceptor:${id}`}>
|
||||||
downstream(overrideProps ? { ...props, ...overrideProps } : props),
|
{interceptor(props, (overrideProps) =>
|
||||||
);
|
downstream(overrideProps ? { ...props, ...overrideProps } : props),
|
||||||
|
)}
|
||||||
|
</SlotErrorBoundary>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return pipeline;
|
return pipeline;
|
||||||
@@ -34,9 +43,13 @@ export function defineSlot<P extends Record<string, any>>(
|
|||||||
const version = useSyncExternalStore(subscribeSlot, getSnapshot);
|
const version = useSyncExternalStore(subscribeSlot, getSnapshot);
|
||||||
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
const chain = useMemo(() => buildChain(store.getInterceptors(name), store.getDefault(name)!), [version]);
|
const chain = useMemo(() => buildChain(store.getInterceptors(name), store.getDefault(name)!, name), [version]);
|
||||||
|
|
||||||
return <>{chain(props)}</>;
|
return (
|
||||||
|
<SlotErrorBoundary slotName={name} label="slot">
|
||||||
|
{chain(props)}
|
||||||
|
</SlotErrorBoundary>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
SlotComponent.displayName = `Slot(${name})`;
|
SlotComponent.displayName = `Slot(${name})`;
|
||||||
|
|||||||
Reference in new issue
Block a user