|
| 1 | +import { useAppDispatch, useAppSelector } from '../../hooks/state'; |
| 2 | +import { selectCloseModRequested } from '../../state/selectors'; |
| 3 | +import { Button, List, Modal, Typography } from 'antd'; |
| 4 | +import { setCloseRequested } from '../../state/toolset'; |
| 5 | +import { persistJSON, selectModifiedFiles } from '../../state/files'; |
| 6 | +import { invokeWithSchema } from '../../lib/invoke'; |
| 7 | +import z from 'zod'; |
| 8 | +import { useCallback, useEffect, useMemo } from 'react'; |
| 9 | + |
| 10 | +export function ConfirmCloseMod() { |
| 11 | + const dispatch = useAppDispatch(); |
| 12 | + const closeRequested = useAppSelector(selectCloseModRequested); |
| 13 | + const modifiedFiles = useAppSelector(selectModifiedFiles); |
| 14 | + const cancel = useCallback(() => { |
| 15 | + dispatch(setCloseRequested(false)); |
| 16 | + }, [dispatch]); |
| 17 | + const close = useCallback(async () => { |
| 18 | + await invokeWithSchema(z.unknown(), 'toolset_close_confirm'); |
| 19 | + }, []); |
| 20 | + const saveAll = useCallback(async () => { |
| 21 | + for (const file of modifiedFiles) { |
| 22 | + dispatch(persistJSON(file)); |
| 23 | + } |
| 24 | + }, [dispatch, modifiedFiles]); |
| 25 | + const footer = useMemo(() => { |
| 26 | + return ( |
| 27 | + <> |
| 28 | + <Button onClick={cancel}>Cancel</Button> |
| 29 | + <Button onClick={close}>Discard Changes</Button> |
| 30 | + <Button type="primary" onClick={saveAll}> |
| 31 | + Save All |
| 32 | + </Button> |
| 33 | + </> |
| 34 | + ); |
| 35 | + }, [cancel, close, saveAll]); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + if (closeRequested) { |
| 39 | + if (modifiedFiles.length == 0) { |
| 40 | + close(); |
| 41 | + } |
| 42 | + } |
| 43 | + }, [close, closeRequested, modifiedFiles]); |
| 44 | + |
| 45 | + return ( |
| 46 | + <Modal open={closeRequested} onCancel={cancel} footer={footer}> |
| 47 | + <Typography.Paragraph> |
| 48 | + You have unsaved changes in the following files: |
| 49 | + </Typography.Paragraph> |
| 50 | + <Typography.Paragraph> |
| 51 | + <List |
| 52 | + dataSource={modifiedFiles} |
| 53 | + renderItem={(item) => <List.Item>{item}</List.Item>} |
| 54 | + bordered |
| 55 | + /> |
| 56 | + </Typography.Paragraph> |
| 57 | + <Typography.Paragraph> |
| 58 | + Please choose how to continue. |
| 59 | + </Typography.Paragraph> |
| 60 | + </Modal> |
| 61 | + ); |
| 62 | +} |
0 commit comments