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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| import { useCallback, useEffect, useState } from "react"; import { nanoid } from "nanoid"; import { ActionPanel, Icon, List, LocalStorage } from "@raycast/api";
import { Filter, Todo } from "./types";
import { CreateTodoAction, DeleteTodoAction, EmptyView, ToggleTodoAction } from "./components";
type State = { filter: Filter; isLoading: boolean; searchText: string; todos: Todo[]; visibleTodos: Todo[]; };
export default function Command() { const [state, setState] = useState<State>({ filter: Filter.All, isLoading: true, searchText: "", todos: [], visibleTodos: [], });
useEffect(() => { (async () => { const storedTodos = await LocalStorage.getItem<string>("todos");
if (!storedTodos) { setState((previous) => ({ ...previous, isLoading: false })); return; }
try { const todos: Todo[] = JSON.parse(storedTodos); setState((previous) => ({ ...previous, todos, isLoading: false })); } catch (e) { setState((previous) => ({ ...previous, todos: [], isLoading: false })); } })(); }, []);
useEffect(() => { LocalStorage.setItem("todos", JSON.stringify(state.todos)); }, [state.todos]);
const handleCreate = useCallback( (title: string) => { const newTodos = [...state.todos, { id: nanoid(), title, isCompleted: false }]; setState((previous) => ({ ...previous, todos: newTodos, filter: Filter.All, searchText: "" })); }, [state.todos, setState] );
const handleToggle = useCallback( (index: number) => { const newTodos = [...state.todos]; newTodos[index].isCompleted = !newTodos[index].isCompleted; setState((previous) => ({ ...previous, todos: newTodos })); }, [state.todos, setState] );
const handleDelete = useCallback( (index: number) => { const newTodos = [...state.todos]; newTodos.splice(index, 1); setState((previous) => ({ ...previous, todos: newTodos })); }, [state.todos, setState] );
const filterTodos = useCallback(() => { if (state.filter === Filter.Open) { return state.todos.filter((todo) => !todo.isCompleted); } if (state.filter === Filter.Completed) { return state.todos.filter((todo) => todo.isCompleted); } return state.todos; }, [state.todos, state.filter]);
return ( <List isLoading={state.isLoading} searchText={state.searchText} searchBarAccessory={ <List.Dropdown tooltip="Select Todo List" value={state.filter} onChange={(newValue) => setState((previous) => ({ ...previous, filter: newValue as Filter }))} > <List.Dropdown.Item title="All" value={Filter.All} /> <List.Dropdown.Item title="Open" value={Filter.Open} /> <List.Dropdown.Item title="Completed" value={Filter.Completed} /> </List.Dropdown> } enableFiltering onSearchTextChange={(newValue) => { setState((previous) => ({ ...previous, searchText: newValue })); }} > <EmptyView filter={state.filter} todos={filterTodos()} searchText={state.searchText} onCreate={handleCreate} /> {filterTodos().map((todo, index) => ( <List.Item
|