Certainly!I’ll provide an explanation for each line or block of code inside a comment:

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
// 1-3. Importing necessary hooks and utilities from React, the nanoid library for generating unique IDs, and various components and utilities from the @raycast/api package.
import { useCallback, useEffect, useState } from "react";
import { nanoid } from "nanoid";
import { ActionPanel, Icon, List, LocalStorage } from "@raycast/api";

// 4-5. Importing TypeScript types for Filter and Todo from another file.
import { Filter, Todo } from "./types";

// 6-10. Importing custom components specific to the todo application.
import { CreateTodoAction, DeleteTodoAction, EmptyView, ToggleTodoAction } from "./components";

// 12-18. Defining the shape of the component's state using TypeScript interface.
type State = {
filter: Filter;
isLoading: boolean;
searchText: string;
todos: Todo[];
visibleTodos: Todo[];
};

// 20-21. The main functional component for the todo command.
export default function Command() {
// 22-30. Initializing the component's state with default values.
const [state, setState] = useState<State>({
filter: Filter.All,
isLoading: true,
searchText: "",
todos: [],
visibleTodos: [],
});

// 32-47. Fetching and setting todos from local storage on component mount.
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) {
// Handling errors if the stored data can't be parsed.
setState((previous) => ({ ...previous, todos: [], isLoading: false }));
}
})();
}, []);

// 49-52. Effect hook to store todos in LocalStorage whenever the todos state changes.
useEffect(() => {
LocalStorage.setItem("todos", JSON.stringify(state.todos));
}, [state.todos]);

// 54-61. Callback for creating a new todo.
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]
);

// 63-70. Callback to toggle the completion status of a todo.
const handleToggle = useCallback(
(index: number) => {
const newTodos = [...state.todos];
newTodos[index].isCompleted = !newTodos[index].isCompleted;
setState((previous) => ({ ...previous, todos: newTodos }));
},
[state.todos, setState]
);

// 72-79. Callback for deleting a todo.
const handleDelete = useCallback(
(index: number) => {
const newTodos = [...state.todos];
newTodos.splice(index, 1);
setState((previous) => ({ ...previous, todos: newTodos }));
},
[state.todos, setState]
);

// 81-90. Callback to filter todos based on the current filter state.
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]);

// 92-127. Defines the UI structure using JSX.
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