|
| 1 | +import { useState, useCallback } from "react"; |
| 2 | +import { useTranslation } from "react-i18next"; |
| 3 | +import styles from "./HistoryItem.module.css"; |
| 4 | +import { DefaultButton } from "@fluentui/react"; |
| 5 | +import { Delete24Regular } from "@fluentui/react-icons"; |
| 6 | + |
| 7 | +export interface HistoryData { |
| 8 | + id: string; |
| 9 | + title: string; |
| 10 | + timestamp: number; |
| 11 | +} |
| 12 | + |
| 13 | +interface HistoryItemProps { |
| 14 | + item: HistoryData; |
| 15 | + onSelect: (id: string) => void; |
| 16 | + onDelete: (id: string) => void; |
| 17 | +} |
| 18 | + |
| 19 | +export function HistoryItem({ item, onSelect, onDelete }: HistoryItemProps) { |
| 20 | + const [isModalOpen, setIsModalOpen] = useState(false); |
| 21 | + |
| 22 | + const handleDelete = useCallback(() => { |
| 23 | + setIsModalOpen(false); |
| 24 | + onDelete(item.id); |
| 25 | + }, [item.id, onDelete]); |
| 26 | + |
| 27 | + return ( |
| 28 | + <div className={styles.historyItem}> |
| 29 | + <button onClick={() => onSelect(item.id)} className={styles.historyItemButton}> |
| 30 | + <div className={styles.historyItemTitle}>{item.title}</div> |
| 31 | + </button> |
| 32 | + <button onClick={() => setIsModalOpen(true)} className={styles.deleteButton} aria-label="delete this chat history"> |
| 33 | + <Delete24Regular className={styles.deleteIcon} /> |
| 34 | + </button> |
| 35 | + <DeleteHistoryModal isOpen={isModalOpen} onClose={() => setIsModalOpen(false)} onConfirm={handleDelete} /> |
| 36 | + </div> |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +function DeleteHistoryModal({ isOpen, onClose, onConfirm }: { isOpen: boolean; onClose: () => void; onConfirm: () => void }) { |
| 41 | + if (!isOpen) return null; |
| 42 | + const { t } = useTranslation(); |
| 43 | + return ( |
| 44 | + <div className={styles.modalOverlay}> |
| 45 | + <div className={styles.modalContent}> |
| 46 | + <h2 className={styles.modalTitle}>{t("history.deleteModalTitle")}</h2> |
| 47 | + <p className={styles.modalDescription}>{t("history.deleteModalDescription")}</p> |
| 48 | + <div className={styles.modalActions}> |
| 49 | + <DefaultButton onClick={onClose} className={styles.modalCancelButton}> |
| 50 | + {t("history.cancelLabel")} |
| 51 | + </DefaultButton> |
| 52 | + <DefaultButton onClick={onConfirm} className={styles.modalConfirmButton}> |
| 53 | + {t("history.deleteLabel")} |
| 54 | + </DefaultButton> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + </div> |
| 58 | + ); |
| 59 | +} |
0 commit comments