|
| 1 | +/** |
| 2 | + * AppWithHooks Component |
| 3 | + * |
| 4 | + * Demonstrates the new React hooks from @objectstack/client-react |
| 5 | + * This is an alternative to App.tsx showing the hooks-based approach |
| 6 | + */ |
| 7 | + |
| 8 | +import { useState, useEffect } from 'react'; |
| 9 | +import { ObjectStackClient } from '@objectstack/client'; |
| 10 | +import type { Task } from './types'; |
| 11 | +import { TaskFormWithHooks } from './components/TaskFormWithHooks'; |
| 12 | +import { TaskListWithHooks } from './components/TaskListWithHooks'; |
| 13 | +import './App.css'; |
| 14 | + |
| 15 | +export function AppWithHooks() { |
| 16 | + const [client, setClient] = useState<ObjectStackClient | null>(null); |
| 17 | + const [editingTask, setEditingTask] = useState<Task | null>(null); |
| 18 | + const [refreshTrigger, setRefreshTrigger] = useState(0); |
| 19 | + const [connected, setConnected] = useState(false); |
| 20 | + const [error, setError] = useState<string | null>(null); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + initializeClient(); |
| 24 | + }, []); |
| 25 | + |
| 26 | + async function initializeClient() { |
| 27 | + try { |
| 28 | + const stackClient = new ObjectStackClient({ |
| 29 | + baseUrl: '' |
| 30 | + }); |
| 31 | + |
| 32 | + await stackClient.connect(); |
| 33 | + |
| 34 | + setClient(stackClient); |
| 35 | + setConnected(true); |
| 36 | + console.log('✅ ObjectStack Client connected (via MSW)'); |
| 37 | + } catch (err) { |
| 38 | + setError(err instanceof Error ? err.message : 'Failed to initialize client'); |
| 39 | + console.error('Failed to initialize client:', err); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + function handleFormSuccess() { |
| 44 | + setEditingTask(null); |
| 45 | + setRefreshTrigger(prev => prev + 1); |
| 46 | + } |
| 47 | + |
| 48 | + function handleEditTask(task: Task) { |
| 49 | + setEditingTask(task); |
| 50 | + if (window.innerWidth < 1024) { |
| 51 | + window.scrollTo({ top: 0, behavior: 'smooth' }); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + function handleCancelEdit() { |
| 56 | + setEditingTask(null); |
| 57 | + } |
| 58 | + |
| 59 | + if (error) { |
| 60 | + return ( |
| 61 | + <div className="min-h-screen flex items-center justify-center p-6"> |
| 62 | + <div className="max-w-md w-full p-8 border border-error-light bg-background rounded-lg shadow-sm text-center"> |
| 63 | + <h1 className="text-xl font-bold text-error mb-2">Connection Error</h1> |
| 64 | + <p className="text-accents-5 mb-6">{error}</p> |
| 65 | + <button |
| 66 | + onClick={initializeClient} |
| 67 | + className="px-4 py-2 bg-foreground text-background rounded-md hover:bg-accents-7 transition-colors" |
| 68 | + > |
| 69 | + Retry |
| 70 | + </button> |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + if (!connected || !client) { |
| 77 | + return ( |
| 78 | + <div className="min-h-screen flex flex-col items-center justify-center p-6 space-y-4"> |
| 79 | + <div className="w-8 h-8 rounded-full border-4 border-accents-2 border-t-foreground animate-spin"></div> |
| 80 | + <div className="text-center"> |
| 81 | + <h1 className="text-lg font-semibold mb-1">Connecting to ObjectStack...</h1> |
| 82 | + <p className="text-accents-5 text-sm">Initializing MSW and ObjectStack Client...</p> |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + return ( |
| 89 | + <div className="max-w-6xl mx-auto px-6 py-12 min-h-screen font-sans"> |
| 90 | + <header className="text-center mb-16"> |
| 91 | + <h1 className="text-4xl font-extrabold tracking-tight mb-4 text-foreground"> |
| 92 | + ObjectStack React Hooks |
| 93 | + </h1> |
| 94 | + <p className="text-accents-5 text-lg mb-6"> |
| 95 | + Using <code className="text-sm bg-accents-1 px-1.5 py-0.5 rounded font-mono text-accents-6">@objectstack/client-react</code> hooks with Mock Service Worker |
| 96 | + </p> |
| 97 | + <div className="inline-flex items-center gap-2 px-3 py-1 rounded-full bg-success-lighter border border-success-lighter text-success-dark text-sm font-medium"> |
| 98 | + <span className="relative flex h-2 w-2"> |
| 99 | + <span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-success opacity-75"></span> |
| 100 | + <span className="relative inline-flex rounded-full h-2 w-2 bg-success"></span> |
| 101 | + </span> |
| 102 | + React Hooks - Type-safe & Declarative |
| 103 | + </div> |
| 104 | + </header> |
| 105 | + |
| 106 | + <main className="grid grid-cols-1 lg:grid-cols-12 gap-12 text-sm"> |
| 107 | + <section className="lg:col-span-4 lg:sticky lg:top-6 lg:self-start"> |
| 108 | + <TaskFormWithHooks |
| 109 | + client={client} |
| 110 | + editingTask={editingTask} |
| 111 | + onSuccess={handleFormSuccess} |
| 112 | + onCancel={handleCancelEdit} |
| 113 | + /> |
| 114 | + </section> |
| 115 | + |
| 116 | + <section className="lg:col-span-8"> |
| 117 | + <TaskListWithHooks |
| 118 | + client={client} |
| 119 | + onEdit={handleEditTask} |
| 120 | + key={refreshTrigger} |
| 121 | + /> |
| 122 | + </section> |
| 123 | + </main> |
| 124 | + |
| 125 | + <footer className="mt-16 pt-8 border-t border-accents-2 text-center text-sm text-accents-4 space-y-2"> |
| 126 | + <p> |
| 127 | + This example demonstrates the new React hooks from @objectstack/client-react. |
| 128 | + Features: useQuery, useMutation, usePagination with type safety. |
| 129 | + </p> |
| 130 | + <p className="font-mono text-xs text-accents-3"> |
| 131 | + React Hooks + TypeScript + Vite + MSW + @objectstack/client-react |
| 132 | + </p> |
| 133 | + </footer> |
| 134 | + </div> |
| 135 | + ); |
| 136 | +} |
0 commit comments