import { useCedarState } from 'cedar-os';
function TodoComponent() {
const [todos, setTodos] = useCedarState(
'todos',
[
{ id: 1, text: 'Learn Cedar-OS', completed: false },
{ id: 2, text: 'Build amazing AI apps', completed: false },
],
'A list of todo items that users can check off',
{
addTodo: {
name: 'addTodo',
description: 'Add a new todo item',
execute: (currentTodos, args: { text: string }) => {
const newTodo = {
id: Date.now(),
text: args.text,
completed: false,
};
setTodos([...currentTodos, newTodo]);
},
},
toggleTodo: {
name: 'toggleTodo',
description: 'Toggle completion status of a todo',
execute: (currentTodos, args: { id: number }) => {
setTodos(
currentTodos.map((todo) =>
todo.id === args.id
? { ...todo, completed: !todo.completed }
: todo
)
);
},
},
removeTodo: {
name: 'removeTodo',
description: 'Remove a todo item',
execute: (currentTodos, args: { id: number }) => {
setTodos(currentTodos.filter((todo) => todo.id !== args.id));
},
},
}
);
return (
<div>
<h2>My Todos</h2>
{todos.map((todo) => (
<div key={todo.id}>
<input
type='checkbox'
checked={todo.completed}
onChange={() => {
// You can call the setter directly or use custom setters
setTodos(
todos.map((t) =>
t.id === todo.id ? { ...t, completed: !t.completed } : t
)
);
}}
/>
<span>{todo.text}</span>
<button
onClick={() => {
setTodos(todos.filter((t) => t.id !== todo.id));
}}>
Delete
</button>
</div>
))}
<button
onClick={() => {
const newTodo = {
id: Date.now(),
text: 'New todo',
completed: false,
};
setTodos([...todos, newTodo]);
}}>
Add Todo
</button>
</div>
);
}