import React, { useEffect, useState } from 'react';
import { init, boot, shutdown } from '@adoptai/sdk';
function App() {
const [user, setUser] = useState(null);
// Initialize SDK once
useEffect(() => {
const initAdoptAI = async () => {
try {
init('your-license-key');
} catch (error) {
console.error('Failed to initialize AdoptAI:', error);
}
};
initAdoptAI();
}, []);
// Boot or shutdown based on user state
useEffect(() => {
const handleUserChange = async () => {
if (user) {
try {
boot(user.id, {
name: user.name,
email: user.email,
plan: user.plan
}, {
products: ['sidebar', 'spotlight'],
copilotHeaders: {
Authorization: `Bearer ${user.token}`
}
});
} catch (error) {
console.error('Failed to boot AdoptAI:', error);
}
} else {
shutdown();
}
};
handleUserChange();
}, [user]);
return (
<div className="app">
<h1>My React App</h1>
{user ? (
<div>
<p>Welcome, {user.name}!</p>
<button onClick={() => setUser(null)}>Logout</button>
</div>
) : (
<button onClick={() => setUser({
id: 'user-123',
name: 'John Doe',
email: 'john@example.com',
plan: 'pro',
token: 'auth-token'
})}>
Login
</button>
)}
<div id="adopt-widget-container"></div>
</div>
);
}