|
1 | | -importReactfrom'react' |
| 1 | +import{useEffect,useRef}from'react' |
2 | 2 | import{useNavigate}from'react-router-dom' |
3 | 3 |
|
4 | 4 | importROUTESfrom'~/constants/routes' |
| 5 | +importgetChatIndexfrom'~/services/getChatIndex' |
| 6 | +importsaveChatfrom'~/services/saveChat' |
| 7 | +importStorefrom'~/services/store' |
| 8 | +import{disChats,useChats}from'~/stores/chats' |
| 9 | +import{deleteChat}from'~/stores/chats/actions' |
5 | 10 | importButtonfrom'~/components/button' |
6 | | -import{ButtonsContainer,Container}from'./style' |
7 | | - |
| 11 | +import{ButtonsContainer,Chat,ChatsContainer,Container}from'./style' |
8 | 12 |
|
9 | 13 | exportdefaultfunctionMenu(){ |
10 | 14 |
|
11 | 15 | constnavigate=useNavigate() |
| 16 | +constchats=useChats('chats') |
| 17 | +constdeletedChatRef=useRef(false) |
| 18 | + |
| 19 | +functionhandleDeleteChat(){ |
| 20 | +constchatIndex=getChatIndex() |
| 21 | +if(!Array.isArray(chats[chatIndex]))return |
| 22 | +constconfirmed=confirm('Are you sure you want to delete?') |
| 23 | +if(!confirmed)return |
| 24 | +deletedChatRef.current=true |
| 25 | +disChats(deleteChat(chatIndex)) |
| 26 | +} |
| 27 | + |
| 28 | +functionhandleSaveChat(){ |
| 29 | +constchatIndex=getChatIndex() |
| 30 | +saveChat(chatIndex,chats[chatIndex]) |
| 31 | +} |
| 32 | + |
| 33 | +useEffect(()=>{ |
| 34 | +if(!deletedChatRef.current)return |
| 35 | +deletedChatRef.current=false |
| 36 | +navigate(ROUTES.ROOT) |
| 37 | +Store.set('chats',chats) |
| 38 | +},[chats]) |
12 | 39 |
|
13 | 40 | return( |
14 | 41 | <Container> |
| 42 | +<ChatsContainer> |
| 43 | +{chats.map((messages,index)=>{ |
| 44 | +const{ content}=messages[0] |
| 45 | +return( |
| 46 | +<Chatkey={index}to={`${ROUTES.CHAT}/${++index}`} |
| 47 | +title={content} |
| 48 | +> |
| 49 | +{content} |
| 50 | +</Chat> |
| 51 | +) |
| 52 | +})} |
| 53 | +</ChatsContainer> |
15 | 54 | <ButtonsContainer> |
16 | 55 | <Button |
17 | 56 | style={{padding:'6px',paddingBottom:'3px'}} |
18 | 57 | onClick={()=>navigate(ROUTES.CONFIG)} |
19 | 58 | > |
20 | 59 | <svgwidth="16px"height="16px"viewBox="0 0 32 32"fill="#fff"stroke="#fff"><gid="SVGRepo_bgCarrier"stroke-width="0"></g><gid="SVGRepo_tracerCarrier"stroke-linecap="round"stroke-linejoin="round"></g><gid="SVGRepo_iconCarrier"><title>Configurations</title><pathd="M23.265,24.381l.9-.894c4.164.136,4.228-.01,4.411-.438l1.144-2.785L29.805,20l-.093-.231c-.049-.122-.2-.486-2.8-2.965V15.5c3-2.89,2.936-3.038,2.765-3.461L28.538,9.225c-.171-.422-.236-.587-4.37-.474l-.9-.93a20.166,20.166,0,0,0-.141-4.106l-.116-.263-2.974-1.3c-.438-.2-.592-.272-3.4,2.786l-1.262-.019c-2.891-3.086-3.028-3.03-3.461-2.855L9.149,3.182c-.433.175-.586.237-.418,4.437l-.893.89c-4.162-.136-4.226.012-4.407.438L2.285,11.733,2.195,12l.094.232c.049.12.194.48,2.8,2.962l0,1.3c-3,2.89-2.935,3.038-2.763,3.462l1.138,2.817c.174.431.236.584,4.369.476l.9.935a20.243,20.243,0,0,0,.137,4.1l.116.265,2.993,1.308c.435.182.586.247,3.386-2.8l1.262.016c2.895,3.09,3.043,3.03,3.466,2.859l2.759-1.115C23.288,28.644,23.44,28.583,23.265,24.381ZM11.407,17.857a4.957,4.957,0,1,1,6.488,2.824A5.014,5.014,0,0,1,11.407,17.857Z"></path></g></svg> |
21 | 60 | </Button> |
| 61 | +<Button |
| 62 | +style={{padding:'6px',paddingBottom:'3px'}} |
| 63 | +onClick={handleDeleteChat} |
| 64 | +> |
| 65 | +<svgfill="#fff"width="16px"height="16px"viewBox="0 0 24 24"><gid="SVGRepo_bgCarrier"stroke-width="0"></g><gid="SVGRepo_tracerCarrier"stroke-linecap="round"stroke-linejoin="round"></g><gid="SVGRepo_iconCarrier"><title>Delete Chat</title><pathd="M5.755,20.283,4,8H20L18.245,20.283A2,2,0,0,1,16.265,22H7.735A2,2,0,0,1,5.755,20.283ZM21,4H16V3a1,1,0,0,0-1-1H9A1,1,0,0,0,8,3V4H3A1,1,0,0,0,3,6H21a1,1,0,0,0,0-2Z"></path></g></svg> |
| 66 | +</Button> |
| 67 | +<Button |
| 68 | +style={{padding:'6px',paddingBottom:'3px'}} |
| 69 | +onClick={handleSaveChat} |
| 70 | +> |
| 71 | +<svgwidth="16px"height="16px"viewBox="0 0 32 32"fill="#fff"><gid="SVGRepo_bgCarrier"stroke-width="0"></g><gid="SVGRepo_tracerCarrier"stroke-linecap="round"stroke-linejoin="round"></g><gid="SVGRepo_iconCarrier"><title>Save Chat</title><desc>Created with Sketch Beta.</desc><defs></defs><gid="Page-1"stroke="none"stroke-width="1"fill="none"fill-rule="evenodd"><gid="Icon-Set-Filled"transform="translate(-154.000000, -517.000000)"fill="#fff"><pathd="M172,522 C172,521.447 172.448,521 173,521 C173.552,521 174,521.447 174,522 L174,526 C174,526.553 173.552,527 173,527 C172.448,527 172,526.553 172,526 L172,522 L172,522 Z M163,529 L177,529 C177.552,529 178,528.553 178,528 L178,517 L162,517 L162,528 C162,528.553 162.448,529 163,529 L163,529 Z M182,517 L180,517 L180,529 C180,530.104 179.104,531 178,531 L162,531 C160.896,531 160,530.104 160,529 L160,517 L158,517 C155.791,517 154,518.791 154,521 L154,545 C154,547.209 155.791,549 158,549 L182,549 C184.209,549 186,547.209 186,545 L186,521 C186,518.791 184.209,517 182,517 L182,517 Z"id="save-floppy"></path></g></g></g></svg> |
| 72 | +</Button> |
22 | 73 | </ButtonsContainer> |
23 | 74 | </Container> |
24 | 75 | ) |
|