-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
217 lines (187 loc) · 6.86 KB
/
Copy pathapp.js
File metadata and controls
217 lines (187 loc) · 6.86 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const STORAGE_KEY = "roastmaster.history";
const ROAST_LIBRARY = [
"Is that all you got?",
"Your procrastination is an art form.",
"Even your excuses are tired.",
"Goals fear you’ll never meet them.",
"You call that effort? Try again.",
"Motivation is filing a missing persons report.",
"Your comfort zone has overstay fees.",
"Ambition left the chat.",
"Hustle? More like gentle stroll.",
"Dreams need actions, not naps.",
];
const taskForm = document.getElementById("taskForm");
const titleInput = document.getElementById("title");
const notesInput = document.getElementById("notes");
const taskList = document.getElementById("taskList");
const clearAllButton = document.getElementById("clearAll");
const loadSampleButton = document.getElementById("loadSample");
const toggleCompletedCheckbox = document.getElementById("toggleCompleted");
const template = document.getElementById("taskTemplate");
const networkStatus = document.getElementById("networkStatus");
const roastButton = document.getElementById("roastButton");
const stats = {
total: document.getElementById("totalCount"),
completed: document.getElementById("completedCount"),
active: document.getElementById("activeCount"),
};
const createId = () => crypto.randomUUID();
const pickRandomRoast = () =>
ROAST_LIBRARY[Math.floor(Math.random() * ROAST_LIBRARY.length)];
const loadRoasts = () => {
try {
const saved = localStorage.getItem(STORAGE_KEY);
return saved ? JSON.parse(saved) : [];
} catch (error) {
console.error("Failed to parse roasts", error);
return [];
}
};
let roasts = loadRoasts();
const persistRoasts = () => {
localStorage.setItem(STORAGE_KEY, JSON.stringify(roasts));
};
const formatDate = (timestamp) => {
const date = new Date(timestamp);
return `${date.getMonth() + 1}月${date.getDate()}日 ${String(date.getHours()).padStart(2, "0")}:${String(
date.getMinutes()
).padStart(2, "0")}`;
};
const renderStats = () => {
const total = roasts.length;
const completed = roasts.filter((item) => item.acknowledged).length;
stats.total.textContent = total;
stats.completed.textContent = completed;
stats.active.textContent = total - completed;
};
const renderRoasts = () => {
taskList.innerHTML = "";
const shouldHideCompleted = toggleCompletedCheckbox.checked;
const visibleRoasts = shouldHideCompleted ? roasts.filter((item) => !item.acknowledged) : roasts;
if (!visibleRoasts.length) {
const emptyState = document.createElement("li");
emptyState.className = "empty";
emptyState.textContent = shouldHideCompleted ? "暂无待领悟的毒舌" : "暂时还没有毒舌历史,先来一句吧";
taskList.appendChild(emptyState);
renderStats();
return;
}
visibleRoasts.forEach((item) => {
const node = template.content.firstElementChild.cloneNode(true);
node.querySelector("[data-title]").textContent = item.roast;
node.querySelector("[data-notes]").textContent = item.complaint
? `你的抱怨:${item.complaint}${item.notes ? ` | 细节:${item.notes}` : ""}`
: "无抱怨,AI 自带毒舌";
node.querySelector("[data-created]").textContent = `生成于 ${formatDate(item.createdAt)}`;
const checkbox = node.querySelector("[data-complete]");
checkbox.checked = item.acknowledged;
checkbox.addEventListener("change", () => {
item.acknowledged = checkbox.checked;
persistRoasts();
renderRoasts();
});
const deleteButton = node.querySelector("[data-delete]");
deleteButton.addEventListener("click", () => {
roasts = roasts.filter((entry) => entry.id !== item.id);
persistRoasts();
renderRoasts();
});
taskList.appendChild(node);
});
renderStats();
};
const addRoast = (complaint, notes) => {
const roastLine = `${pickRandomRoast()}${complaint ? ` | 你的抱怨:“${complaint}”` : ""}`;
const newEntry = {
id: createId(),
roast: roastLine,
complaint,
notes,
acknowledged: false,
createdAt: Date.now(),
};
roasts = [newEntry, ...roasts];
persistRoasts();
renderRoasts();
};
const setInteractionState = (isLoading) => {
roastButton.disabled = isLoading;
loadSampleButton.disabled = isLoading;
clearAllButton.disabled = isLoading;
roastButton.textContent = isLoading ? "Roasting..." : "Roast Me! (求骂醒)";
};
const simulateRoastRequest = async (complaint, notes) => {
networkStatus.textContent = "AI 正在酝酿毒舌,稍等片刻...";
setInteractionState(true);
try {
await fetch("https://jsonplaceholder.typicode.com/todos/1");
networkStatus.textContent = "网络畅通,AI 正在思考如何毒舌...";
} catch (error) {
console.warn("Network flaky, proceeding with roast", error);
networkStatus.textContent = "网络有点不稳,但毒舌照常进行...";
}
setTimeout(() => {
addRoast(complaint, notes);
networkStatus.innerHTML = '<span class="badge">完成</span> 最新毒舌已加入历史';
setInteractionState(false);
taskForm.reset();
titleInput.focus();
}, 1000);
};
const loadSampleRoasts = () => {
const samples = [
{
complaint: "周末又想躺平",
notes: "还没写完的论文",
},
{ complaint: "健身卡吃灰", notes: "办卡三个月一次没去" },
{ complaint: "想转行但害怕失败", notes: "每天只看教程不动手" },
{ complaint: "刷短视频停不下来", notes: "原本想学英语" },
];
networkStatus.textContent = "正在加载毒舌样例...";
loadSampleButton.disabled = true;
loadSampleButton.textContent = "加载中...";
setTimeout(() => {
const imported = samples.map((item) => ({
id: createId(),
roast: pickRandomRoast(),
complaint: item.complaint,
notes: item.notes,
acknowledged: false,
createdAt: Date.now(),
}));
roasts = [...imported, ...roasts];
persistRoasts();
renderRoasts();
networkStatus.innerHTML = `<span class="badge">完成</span> 已加载 ${imported.length} 条毒舌样例`;
loadSampleButton.disabled = false;
loadSampleButton.textContent = "加载毒舌样例";
}, 800);
};
const clearAllRoasts = () => {
if (!roasts.length) return;
const confirmed = confirm("确认清空毒舌历史吗?此操作不可撤销。");
if (!confirmed) return;
roasts = [];
persistRoasts();
renderRoasts();
};
const init = () => {
renderRoasts();
taskForm.addEventListener("submit", (event) => {
event.preventDefault();
const complaint = titleInput.value.trim();
const notes = notesInput.value.trim();
if (!complaint) {
networkStatus.textContent = "请先输入你的槽点,再让 AI 开炮。";
titleInput.focus();
return;
}
simulateRoastRequest(complaint, notes);
});
loadSampleButton.addEventListener("click", loadSampleRoasts);
clearAllButton.addEventListener("click", clearAllRoasts);
toggleCompletedCheckbox.addEventListener("change", renderRoasts);
};
init();