From 738c4783ab05190c14de42ff8a9a452ed7059509 Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Fri, 22 Dec 2023 20:32:08 +0100 Subject: [PATCH] chore: refactor FitButtonBar into several smaller components --- src/FitButtonBar/ClipboardButton.tsx | 85 ++++++++++++ src/FitButtonBar/FitButtonBar.tsx | 198 +-------------------------- src/FitButtonBar/RenameButton.tsx | 41 ++++++ src/FitButtonBar/SaveButton.tsx | 67 +++++++++ src/FitButtonBar/ShareButton.tsx | 24 ++++ 5 files changed, 221 insertions(+), 194 deletions(-) create mode 100644 src/FitButtonBar/ClipboardButton.tsx create mode 100644 src/FitButtonBar/RenameButton.tsx create mode 100644 src/FitButtonBar/SaveButton.tsx create mode 100644 src/FitButtonBar/ShareButton.tsx diff --git a/src/FitButtonBar/ClipboardButton.tsx b/src/FitButtonBar/ClipboardButton.tsx new file mode 100644 index 0000000..072b7b9 --- /dev/null +++ b/src/FitButtonBar/ClipboardButton.tsx @@ -0,0 +1,85 @@ +import clsx from "clsx"; +import React from "react"; + +import { EsiFit, ShipSnapshotContext } from "../ShipSnapshotProvider"; +import { ModalDialog } from "../ModalDialog"; +import { useClipboard } from "../Helpers/Clipboard"; +import { useFormatAsEft } from "../FormatAsEft"; +import { useFormatEftToEsi } from "../FormatEftToEsi"; + +import styles from "./FitButtonBar.module.css"; + +export const ClipboardButton = () => { + const shipSnapshot = React.useContext(ShipSnapshotContext); + const toEft = useFormatAsEft(); + const eftToEsiFit = useFormatEftToEsi(); + const { copy, copied } = useClipboard(); + + const [isPopupOpen, setIsPopupOpen] = React.useState(false); + const [isPasteOpen, setIsPasteOpen] = React.useState(false); + const textAreaRef = React.useRef(null); + + const copyToClipboard = React.useCallback(() => { + const eft = toEft(); + if (eft === undefined) return; + + copy(eft); + + setIsPopupOpen(false); + }, [copy, toEft]); + + const importFromClipboard = React.useCallback(() => { + if (!shipSnapshot.loaded) return; + + const textArea = textAreaRef.current; + if (textArea === null) return; + + const fitString = textArea.value; + if (fitString === "") return; + + let fit: EsiFit | undefined; + if (fitString.startsWith("{")) { + fit = JSON.parse(fitString); + } else { + fit = eftToEsiFit(fitString); + } + if (fit === undefined) return; + + shipSnapshot.changeFit(fit); + + setIsPasteOpen(false); + setIsPopupOpen(false); + }, [eftToEsiFit, shipSnapshot]); + + return <> +
setIsPopupOpen(true)} onMouseOut={() => setIsPopupOpen(false)}> +
+ {copied ? "In Clipboard" : "Clipboard"} +
+
+
+
setIsPasteOpen(true)}> + Import from Clipboard +
+
copyToClipboard()}> + Copy to Clipboard +
+
+
+
+ + setIsPasteOpen(false)} className={styles.paste} title="Import from Clipboard"> +
+
+ Paste your fit here +
+
+