forked from BeOnAuto/auto-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent-wrapper.tsx
More file actions
59 lines (50 loc) · 1.55 KB
/
component-wrapper.tsx
File metadata and controls
59 lines (50 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use client';
import * as React from 'react';
import { cn } from '@/registry/new-york-v4/lib/utils';
export function ComponentWrapper({
className,
name,
children,
...props
}: React.ComponentPropsWithoutRef<'div'> & { name: string }) {
return (
<ComponentErrorBoundary name={name}>
<div
id={name}
data-name={name.toLowerCase()}
className={cn('flex w-full scroll-mt-16 flex-col rounded-lg border', className)}
{...props}
>
<div className="border-b px-4 py-3">
<div className="text-sm font-medium">{getComponentName(name)}</div>
</div>
<div className="flex flex-1 items-center gap-2 p-4">{children}</div>
</div>
</ComponentErrorBoundary>
);
}
class ComponentErrorBoundary extends React.Component<
{ children: React.ReactNode; name: string },
{ hasError: boolean }
> {
constructor(props: { children: React.ReactNode; name: string }) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError() {
return { hasError: true };
}
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
console.error(`Error in component ${this.props.name}:`, error, errorInfo);
}
render() {
if (this.state.hasError) {
return <div className="p-4 text-red-500">Something went wrong in component: {this.props.name}</div>;
}
return this.props.children;
}
}
function getComponentName(name: string) {
// convert kebab-case to title case
return name.replace(/-/g, ' ').replace(/\b\w/g, (char) => char.toUpperCase());
}