```tsx
Generates React components, hooks, and patterns for compound components, data fetching, forms, and performance optimization.
/plugin marketplace add majesticlabs-dev/majestic-marketplace/plugin install majestic-sales@majestic-marketplaceimport { FC, ReactNode, createContext, useContext } from 'react';
interface TabsContextValue {
activeTab: string;
setActiveTab: (tab: string) => void;
}
const TabsContext = createContext<TabsContextValue | undefined>(undefined);
interface TabsProps {
defaultTab: string;
children: ReactNode;
}
export const Tabs: FC<TabsProps> = ({ defaultTab, children }) => {
const [activeTab, setActiveTab] = useState(defaultTab);
return (
<TabsContext.Provider value={{ activeTab, setActiveTab }}>
<div className="space-y-4">{children}</div>
</TabsContext.Provider>
);
};
interface TabListProps {
children: ReactNode;
}
const TabList: FC<TabListProps> = ({ children }) => {
return (
<div className="flex space-x-2 border-b border-gray-200">
{children}
</div>
);
};
interface TabProps {
value: string;
children: ReactNode;
}
const Tab: FC<TabProps> = ({ value, children }) => {
const context = useContext(TabsContext);
if (!context) throw new Error('Tab must be used within Tabs');
const { activeTab, setActiveTab } = context;
const isActive = activeTab === value;
return (
<button
onClick={() => setActiveTab(value)}
className={`px-4 py-2 font-medium ${
isActive
? 'text-blue-600 border-b-2 border-blue-600'
: 'text-gray-500 hover:text-gray-700'
}`}
>
{children}
</button>
);
};
interface TabPanelProps {
value: string;
children: ReactNode;
}
const TabPanel: FC<TabPanelProps> = ({ value, children }) => {
const context = useContext(TabsContext);
if (!context) throw new Error('TabPanel must be used within Tabs');
const { activeTab } = context;
if (activeTab !== value) return null;
return <div>{children}</div>;
};
Tabs.List = TabList;
Tabs.Tab = Tab;
Tabs.Panel = TabPanel;
// Usage:
// <Tabs defaultTab="profile">
// <Tabs.List>
// <Tabs.Tab value="profile">Profile</Tabs.Tab>
// <Tabs.Tab value="settings">Settings</Tabs.Tab>
// </Tabs.List>
// <Tabs.Panel value="profile">Profile content</Tabs.Panel>
// <Tabs.Panel value="settings">Settings content</Tabs.Panel>
// </Tabs>
import { useState, useEffect } from 'react';
interface UseApiOptions<T> {
initialData?: T;
onSuccess?: (data: T) => void;
onError?: (error: Error) => void;
}
interface UseApiResult<T> {
data: T | undefined;
loading: boolean;
error: Error | null;
refetch: () => void;
}
export function useApi<T>(
url: string,
options: UseApiOptions<T> = {}
): UseApiResult<T> {
const [data, setData] = useState<T | undefined>(options.initialData);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
const fetchData = async () => {
try {
setLoading(true);
setError(null);
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const json = await response.json();
setData(json);
options.onSuccess?.(json);
} catch (err) {
const error = err instanceof Error ? err : new Error('Unknown error');
setError(error);
options.onError?.(error);
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchData();
}, [url]);
return { data, loading, error, refetch: fetchData };
}
import { useState, ChangeEvent, FormEvent } from 'react';
interface UseFormOptions<T> {
initialValues: T;
onSubmit: (values: T) => void | Promise<void>;
validate?: (values: T) => Partial<Record<keyof T, string>>;
}
export function useForm<T extends Record<string, any>>({
initialValues,
onSubmit,
validate
}: UseFormOptions<T>) {
const [values, setValues] = useState<T>(initialValues);
const [errors, setErrors] = useState<Partial<Record<keyof T, string>>>({});
const [touched, setTouched] = useState<Partial<Record<keyof T, boolean>>>({});
const [isSubmitting, setIsSubmitting] = useState(false);
const handleChange = (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const { name, value } = e.target;
setValues(prev => ({ ...prev, [name]: value }));
};
const handleBlur = (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const { name } = e.target;
setTouched(prev => ({ ...prev, [name]: true }));
if (validate) {
const validationErrors = validate(values);
setErrors(validationErrors);
}
};
const handleSubmit = async (e: FormEvent) => {
e.preventDefault();
if (validate) {
const validationErrors = validate(values);
setErrors(validationErrors);
if (Object.keys(validationErrors).length > 0) {
return;
}
}
setIsSubmitting(true);
try {
await onSubmit(values);
} finally {
setIsSubmitting(false);
}
};
const setFieldValue = (field: keyof T, value: any) => {
setValues(prev => ({ ...prev, [field]: value }));
};
return {
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit,
setFieldValue,
isSubmitting
};
}
import { useState, useEffect } from 'react';
export function useLocalStorage<T>(key: string, initialValue: T) {
const [storedValue, setStoredValue] = useState<T>(() => {
try {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (error) {
console.error(`Error reading localStorage key "${key}":`, error);
return initialValue;
}
});
const setValue = (value: T | ((val: T) => T)) => {
try {
const valueToStore = value instanceof Function ? value(storedValue) : value;
setStoredValue(valueToStore);
window.localStorage.setItem(key, JSON.stringify(valueToStore));
} catch (error) {
console.error(`Error setting localStorage key "${key}":`, error);
}
};
useEffect(() => {
const handleStorageChange = (e: StorageEvent) => {
if (e.key === key && e.newValue) {
setStoredValue(JSON.parse(e.newValue));
}
};
window.addEventListener('storage', handleStorageChange);
return () => window.removeEventListener('storage', handleStorageChange);
}, [key]);
return [storedValue, setValue] as const;
}
interface SelectProps<T> {
options: T[];
value: T;
onChange: (value: T) => void;
getLabel: (option: T) => string;
getValue: (option: T) => string;
}
export function Select<T>({
options,
value,
onChange,
getLabel,
getValue
}: SelectProps<T>) {
return (
<select
value={getValue(value)}
onChange={(e) => {
const selected = options.find(opt => getValue(opt) === e.target.value);
if (selected) onChange(selected);
}}
className="block w-full rounded-md border-gray-300"
>
{options.map((option) => (
<option key={getValue(option)} value={getValue(option)}>
{getLabel(option)}
</option>
))}
</select>
);
}
import { memo, useMemo, useCallback } from 'react';
interface ListItemProps {
item: { id: string; name: string; price: number };
onSelect: (id: string) => void;
}
// Memoize component to prevent re-renders
const ListItem = memo<ListItemProps>(({ item, onSelect }) => {
return (
<div onClick={() => onSelect(item.id)} className="p-4 hover:bg-gray-50">
<span>{item.name}</span>
<span className="ml-4">${item.price}</span>
</div>
);
});
interface ProductListProps {
products: Array<{ id: string; name: string; price: number }>;
filterQuery: string;
}
export const ProductList: FC<ProductListProps> = ({ products, filterQuery }) => {
// Memoize expensive computation
const filteredProducts = useMemo(() => {
return products.filter(p =>
p.name.toLowerCase().includes(filterQuery.toLowerCase())
);
}, [products, filterQuery]);
// Memoize callback to prevent re-creating on every render
const handleSelect = useCallback((id: string) => {
console.log('Selected:', id);
}, []);
return (
<div>
{filteredProducts.map(product => (
<ListItem key={product.id} item={product} onSelect={handleSelect} />
))}
</div>
);
};
import { lazy, Suspense } from 'react';
// Lazy load heavy components
const Dashboard = lazy(() => import('./Dashboard'));
const Reports = lazy(() => import('./Reports'));
export const App = () => {
return (
<Suspense fallback={<div>Loading...</div>}>
<Routes>
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/reports" element={<Reports />} />
</Routes>
</Suspense>
);
};
import { Component, ReactNode, ErrorInfo } from 'react';
interface ErrorBoundaryProps {
children: ReactNode;
fallback?: ReactNode;
}
interface ErrorBoundaryState {
hasError: boolean;
error: Error | null;
}
export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
constructor(props: ErrorBoundaryProps) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error('Error caught by boundary:', error, errorInfo);
}
render() {
if (this.state.hasError) {
return this.props.fallback || (
<div className="p-4 bg-red-50 text-red-800 rounded-md">
<h2 className="text-lg font-semibold">Something went wrong</h2>
<p className="mt-2">{this.state.error?.message}</p>
</div>
);
}
return this.props.children;
}
}
You are an elite AI agent architect specializing in crafting high-performance agent configurations. Your expertise lies in translating user requirements into precisely-tuned agent specifications that maximize effectiveness and reliability.