Skip to content

Commit

Permalink
chore: format-all
Browse files Browse the repository at this point in the history
  • Loading branch information
0xemc committed Nov 28, 2023
1 parent 10ac10f commit 4860e05
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 51 deletions.
10 changes: 6 additions & 4 deletions carbonmark/components/pages/Users/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ const Page: NextPage<PageProps> = (props) => {
return (
<>
<PageHead
title={t`${props.carbonmarkUser?.handle || concatAddress(props.userAddress)
} | Profile | Carbonmark`}
mediaTitle={`${props.carbonmarkUser?.handle || concatAddress(props.userAddress)
}'s Profile on Carbonmark`}
title={t`${
props.carbonmarkUser?.handle || concatAddress(props.userAddress)
} | Profile | Carbonmark`}
mediaTitle={`${
props.carbonmarkUser?.handle || concatAddress(props.userAddress)
}'s Profile on Carbonmark`}
metaDescription={t`Create and edit listings, and track your activity with your Carbonmark profile.`}
/>

Expand Down
95 changes: 53 additions & 42 deletions carbonmark/hocs/ConditionalErrorBoundary/index.tsx
Original file line number Diff line number Diff line change
@@ -1,52 +1,63 @@
import { Component, FC, PropsWithChildren } from "react";

type ErrorBoundaryProps<T extends Error> = {
predicate: (error: T | Error | null) => boolean;
fallback: React.ReactNode
} & PropsWithChildren

export class ConditionalErrorBoundary<T extends Error> extends Component<ErrorBoundaryProps<T>, { hasError: boolean, error: T | null }> {
constructor(props: ErrorBoundaryProps<T>) {
super(props);
this.state = { hasError: false, error: null };
}

static getDerivedStateFromError(error: Error) {
// Update state so the next render will show the fallback UI.
return { hasError: true, error };
}

render() {
if (this.state.hasError) {
// If it's a specific error, render children anyway
if (this.props.predicate(this.state.error)) {
this.setState(() => ({ hasError: false, error: null }))
return this.props.children;
}
// You can render any custom fallback UI
return this.props.fallback;
}
predicate: (error: T | Error | null) => boolean;
fallback: React.ReactNode;
} & PropsWithChildren;

export class ConditionalErrorBoundary<T extends Error> extends Component<
ErrorBoundaryProps<T>,
{ hasError: boolean; error: T | null }
> {
constructor(props: ErrorBoundaryProps<T>) {
super(props);
this.state = { hasError: false, error: null };
}

static getDerivedStateFromError(error: Error) {
// Update state so the next render will show the fallback UI.
return { hasError: true, error };
}

render() {
if (this.state.hasError) {
// If it's a specific error, render children anyway
if (this.props.predicate(this.state.error)) {
this.setState(() => ({ hasError: false, error: null }));
return this.props.children;
}
// You can render any custom fallback UI
return this.props.fallback;
}
return this.props.children;
}
}


type WithConditionalErrorBoundaryProps<T, E extends Error> = T & ErrorBoundaryProps<E>;
type WithConditionalErrorBoundaryProps<T, E extends Error> = T &
ErrorBoundaryProps<E>;

/** A decorator version of WithSkeleton */
export function withConditionalErrorBoundary<T, E extends Error>(Component: React.ComponentType<T>, opts: ErrorBoundaryProps<E>) {
// Maintain the previous display name
const displayName = Component.displayName ?? Component.name ?? "Component";

/** Create out wrapped skeleton component */
const ComponentWithConditionalErrorBoundary: FC<WithConditionalErrorBoundaryProps<T, E>> = (props) => (
<ConditionalErrorBoundary predicate={opts.predicate} fallback={opts.fallback}>
<Component {...props} />
</ConditionalErrorBoundary>
);

/** Add another display name for the wrapped version */
ComponentWithConditionalErrorBoundary.displayName = `withConditionalErrorBoundary(${displayName})`;

return ComponentWithConditionalErrorBoundary;
export function withConditionalErrorBoundary<T, E extends Error>(
Component: React.ComponentType<T>,
opts: ErrorBoundaryProps<E>
) {
// Maintain the previous display name
const displayName = Component.displayName ?? Component.name ?? "Component";

/** Create out wrapped skeleton component */
const ComponentWithConditionalErrorBoundary: FC<
WithConditionalErrorBoundaryProps<T, E>
> = (props) => (
<ConditionalErrorBoundary
predicate={opts.predicate}
fallback={opts.fallback}
>
<Component {...props} />
</ConditionalErrorBoundary>
);

/** Add another display name for the wrapped version */
ComponentWithConditionalErrorBoundary.displayName = `withConditionalErrorBoundary(${displayName})`;

return ComponentWithConditionalErrorBoundary;
}
7 changes: 4 additions & 3 deletions carbonmark/pages/portfolio/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { loadTranslation } from "lib/i18n";
import { GetStaticProps } from "next";
import { isNotFoundError } from "next/dist/client/components/not-found";


export const getStaticProps: GetStaticProps = async (ctx) => {
try {
const translation = await loadTranslation(ctx.locale);
Expand All @@ -28,5 +27,7 @@ export const getStaticProps: GetStaticProps = async (ctx) => {
};

/** We want to allow the page to render on user not found (404s) */
export default withConditionalErrorBoundary(Portfolio, { fallback: <h1>User cannot be found</h1>, predicate: isNotFoundError })

export default withConditionalErrorBoundary(Portfolio, {
fallback: <h1>User cannot be found</h1>,
predicate: isNotFoundError,
});
6 changes: 4 additions & 2 deletions carbonmark/pages/users/[user].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ export const getStaticProps: GetStaticProps<PageProps, Params> = async (
return resolveHandle({ handle: params.user, locale });
}
} catch (e) {

console.error("Failed to generate Carbonmark Users Page", e);
return {
notFound: true,
Expand All @@ -151,4 +150,7 @@ export const getStaticPaths = async () => {
};
};

export default withConditionalErrorBoundary(Users, { fallback: <h1>User cannot be found</h1>, predicate: isNotFoundError })
export default withConditionalErrorBoundary(Users, {
fallback: <h1>User cannot be found</h1>,
predicate: isNotFoundError,
});

0 comments on commit 4860e05

Please sign in to comment.