Skip to content

Commit 0513e24

Browse files
FEAT: Restrict history list to logged-in users and add login prompt
1 parent 8c867c3 commit 0513e24

1 file changed

Lines changed: 77 additions & 67 deletions

File tree

frontend/src/pages/Chat.jsx

Lines changed: 77 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export default function Chat() {
301301
<div className="mt-4 rounded-2xl border border-[#E8E2D8] bg-white p-4 shadow-sm">
302302
<div className="flex items-center justify-between mb-2">
303303
<div className="text-sm font-semibold">Lịch sử hội đáp</div>
304-
{(token || IS_OFFLINE) && history.length > 0 && (
304+
{(token || (IS_OFFLINE && userId)) && history.length > 0 && (
305305
<button
306306
className="text-xs text-[#D64545]"
307307
onClick={async () => {
@@ -321,86 +321,96 @@ export default function Chat() {
321321
</div>
322322
<div className="space-y-2 text-xs text-[#5B5B57]">
323323
{historyError && <div>{historyError}</div>}
324-
{!token && !IS_OFFLINE && <div>Đăng nhập để xem lịch sử.</div>}
325-
{(token || IS_OFFLINE) && history.length === 0 && <div>Chưa có hội đáp.</div>}
326-
{(token || IS_OFFLINE) && history.map((h) => (
327-
<div key={h.conversation_id} className="flex w-full items-center justify-between rounded-md border border-transparent hover:border-[#E2D8C8] px-2 py-1">
328-
<div className="flex-1">
329-
{editingId === h.conversation_id ? (
330-
<input
331-
className="w-full rounded-md border border-[#E2D8C8] px-2 py-1 text-xs"
332-
value={titleDraft}
333-
onChange={(e) => setTitleDraft(e.target.value)}
334-
placeholder="Nhập tên cuộc trò chuyện"
335-
/>
336-
) : (
337-
<button
338-
className="w-full text-left"
339-
onClick={async () => {
340-
if (!token) return;
341-
await loadConversation(h.conversation_id);
342-
}}
343-
>
344-
{h.title || h.conversation_id}
345-
</button>
346-
)}
347-
</div>
348-
<div className="ml-2 text-xs">{h.message_count} tin</div>
349-
{editingId === h.conversation_id ? (
350-
<>
351-
<button
352-
className="ml-2 text-xs text-[var(--c-primary)]"
353-
onClick={async () => {
354-
await renameConversation(h.conversation_id, titleDraft);
355-
setEditingId(null);
356-
}}
357-
>
358-
Lưu
359-
</button>
360-
<button
361-
className="ml-2 text-xs text-[#5B5B57]"
362-
onClick={() => setEditingId(null)}
363-
>
364-
Hủy
365-
</button>
366-
</>
367-
) : (
368-
<div className="relative ml-2">
369-
<button
370-
className="text-xs text-[#7A6D5B] px-1"
371-
onClick={() => setMenuId(menuId === h.conversation_id ? null : h.conversation_id)}
372-
>
373-
...
374-
</button>
375-
{menuId === h.conversation_id && (
376-
<div className="absolute right-0 top-full z-10 mt-1 w-28 rounded-md border border-[#E2D8C8] bg-white shadow-sm">
324+
325+
{/* Login Prompt for Guest Users */}
326+
{(!token && !userId && IS_OFFLINE) || (!token && !IS_OFFLINE) ? (
327+
<div className="text-center py-4 bg-[#F7F5F2] rounded-lg">
328+
<div className="mb-2">Đăng ký/đăng nhập để lưu lịch sử chat</div>
329+
<Link to="/auth" className="inline-block px-3 py-1 bg-[var(--c-primary)] text-white rounded hover:opacity-90 transition">
330+
Đăng nhập ngay
331+
</Link>
332+
</div>
333+
) : (
334+
<>
335+
{history.length === 0 && <div>Chưa có hội đáp.</div>}
336+
{history.map((h) => (
337+
<div key={h.conversation_id} className="flex w-full items-center justify-between rounded-md border border-transparent hover:border-[#E2D8C8] px-2 py-1">
338+
<div className="flex-1">
339+
{editingId === h.conversation_id ? (
340+
<input
341+
className="w-full rounded-md border border-[#E2D8C8] px-2 py-1 text-xs"
342+
value={titleDraft}
343+
onChange={(e) => setTitleDraft(e.target.value)}
344+
placeholder="Nhập tên cuộc trò chuyện"
345+
/>
346+
) : (
377347
<button
378-
className="block w-full px-3 py-2 text-left text-xs hover:bg-[#F7F5F2]"
379-
onClick={() => {
380-
setEditingId(h.conversation_id);
381-
setTitleDraft(h.title || '');
382-
setMenuId(null);
348+
className="w-full text-left"
349+
onClick={async () => {
350+
if (!token) return;
351+
await loadConversation(h.conversation_id);
383352
}}
384353
>
385-
Đổi tên
354+
{h.title || h.conversation_id}
386355
</button>
356+
)}
357+
</div>
358+
<div className="ml-2 text-xs">{h.message_count} tin</div>
359+
{editingId === h.conversation_id ? (
360+
<>
387361
<button
388-
className="block w-full px-3 py-2 text-left text-xs text-[#D64545] hover:bg-[#F7F5F2]"
362+
className="ml-2 text-xs text-[var(--c-primary)]"
389363
onClick={async () => {
390-
setMenuId(null);
391-
await deleteConversation(h.conversation_id);
364+
await renameConversation(h.conversation_id, titleDraft);
365+
setEditingId(null);
392366
}}
393367
>
394-
Xóa chat
368+
Lưu
369+
</button>
370+
<button
371+
className="ml-2 text-xs text-[#5B5B57]"
372+
onClick={() => setEditingId(null)}
373+
>
374+
Hủy
375+
</button>
376+
</>
377+
) : (
378+
<div className="relative ml-2">
379+
<button
380+
className="text-xs text-[#7A6D5B] px-1"
381+
onClick={() => setMenuId(menuId === h.conversation_id ? null : h.conversation_id)}
382+
>
383+
...
395384
</button>
385+
{menuId === h.conversation_id && (
386+
<div className="absolute right-0 top-full z-10 mt-1 w-28 rounded-md border border-[#E2D8C8] bg-white shadow-sm">
387+
<button
388+
className="block w-full px-3 py-2 text-left text-xs hover:bg-[#F7F5F2]"
389+
onClick={() => {
390+
setEditingId(h.conversation_id);
391+
setTitleDraft(h.title || '');
392+
setMenuId(null);
393+
}}
394+
>
395+
Đổi tên
396+
</button>
397+
<button
398+
className="block w-full px-3 py-2 text-left text-xs text-[#D64545] hover:bg-[#F7F5F2]"
399+
onClick={async () => {
400+
setMenuId(null);
401+
await deleteConversation(h.conversation_id);
402+
}}
403+
>
404+
Xóa chat
405+
</button>
406+
</div>
407+
)}
396408
</div>
397409
)}
398410
</div>
399-
)}
411+
))}
400412
</div>
401-
))}
402413
</div>
403-
</div>
404414
</section>
405415

406416
<section className="order-1 lg:order-2">

0 commit comments

Comments
 (0)