-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor FitButtonBar into several smaller components (#57)
- Loading branch information
Showing
5 changed files
with
221 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<HTMLTextAreaElement>(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 <> | ||
<div className={styles.popupButton} onMouseOver={() => setIsPopupOpen(true)} onMouseOut={() => setIsPopupOpen(false)}> | ||
<div className={styles.button}> | ||
{copied ? "In Clipboard" : "Clipboard"} | ||
</div> | ||
<div className={clsx(styles.popup, {[styles.collapsed]: !isPopupOpen})}> | ||
<div> | ||
<div className={styles.button} onClick={() => setIsPasteOpen(true)}> | ||
Import from Clipboard | ||
</div> | ||
<div className={clsx(styles.button, styles.buttonMax)} onClick={() => copyToClipboard()}> | ||
Copy to Clipboard | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<ModalDialog visible={isPasteOpen} onClose={() => setIsPasteOpen(false)} className={styles.paste} title="Import from Clipboard"> | ||
<div> | ||
<div> | ||
Paste your fit here | ||
</div> | ||
<div> | ||
<textarea autoFocus className={styles.pasteTextarea} ref={textAreaRef} /> | ||
</div> | ||
<span className={clsx(styles.button, styles.buttonSmall)} onClick={() => importFromClipboard()}> | ||
Import | ||
</span> | ||
</div> | ||
</ModalDialog> | ||
</> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import clsx from "clsx"; | ||
import React from "react"; | ||
|
||
import { ShipSnapshotContext } from "../ShipSnapshotProvider"; | ||
import { ModalDialog } from "../ModalDialog"; | ||
|
||
import styles from "./FitButtonBar.module.css"; | ||
|
||
export const RenameButton = () => { | ||
const shipSnapshot = React.useContext(ShipSnapshotContext); | ||
|
||
const [isRenameOpen, setIsRenameOpen] = React.useState(false); | ||
const [rename, setRename] = React.useState(""); | ||
|
||
const saveRename = React.useCallback(() => { | ||
shipSnapshot?.setName(rename); | ||
setIsRenameOpen(false); | ||
}, [rename, shipSnapshot]); | ||
|
||
const openRename = React.useCallback(() => { | ||
setRename(shipSnapshot?.fit?.name ?? ""); | ||
setIsRenameOpen(true); | ||
}, [shipSnapshot]); | ||
|
||
return <> | ||
<div className={styles.button} onClick={() => openRename()}> | ||
Rename | ||
</div> | ||
|
||
<ModalDialog visible={isRenameOpen} onClose={() => setIsRenameOpen(false)} title="Fit Name"> | ||
<div> | ||
<span className={styles.renameEdit}> | ||
<input type="text" autoFocus value={rename} onChange={(e) => setRename(e.target.value)} /> | ||
</span> | ||
<span className={clsx(styles.button, styles.buttonSmall)} onClick={() => saveRename()}> | ||
Save | ||
</span> | ||
</div> | ||
</ModalDialog> | ||
</> | ||
} |
Oops, something went wrong.