import { useState } from 'react' import Popover from '@material-ui/core/Popover' import OutBound from 'src/components/OutBound' import ReactGA from 'react-ga' import { Link, routes, navigate } from '@redwoodjs/router' import { useAuth } from '@redwoodjs/auth' import { useMutation } from '@redwoodjs/web' import { toast } from '@redwoodjs/web/toast' import Button from 'src/components/Button' import ImageUploader from 'src/components/ImageUploader' import Svg from '../Svg/Svg' import LoginModal from 'src/components/LoginModal' import { FORK_PART_MUTATION } from 'src/components/IdePartCell' import { QUERY as UsersPartsQuery } from 'src/components/PartsOfUserCell' import useUser from 'src/helpers/hooks/useUser' import useKeyPress from 'src/helpers/hooks/useKeyPress' import { captureAndSaveViewport } from 'src/helpers/cloudinary' const IdeToolbar = ({ canEdit, isChanges, onSave, onExport, userNamePart, isDraft, code, onCapture, }) => { const [anchorEl, setAnchorEl] = useState(null) const [whichPopup, setWhichPopup] = useState(null) const [isLoginModalOpen, setIsLoginModalOpen] = useState(false) const { isAuthenticated, currentUser } = useAuth() const showForkButton = !(canEdit || isDraft) const [title, setTitle] = useState('untitled-part') const [captureState, setCaptureState] = useState(false) const { user } = useUser() useKeyPress((e) => { const rx = /INPUT|SELECT|TEXTAREA/i const didPressBackspaceOutsideOfInput = (e.key == 'Backspace' || e.keyCode == 8) && !rx.test(e.target.tagName) if (didPressBackspaceOutsideOfInput) { e.preventDefault() } }) const [forkPart] = useMutation(FORK_PART_MUTATION, { refetchQueries: [ { query: UsersPartsQuery, variables: { userName: userNamePart?.userName || user?.userName }, }, ], }) const handleClick = ({ event, whichPopup }) => { setAnchorEl(event.currentTarget) setWhichPopup(whichPopup) } const handleClose = () => { setAnchorEl(null) setWhichPopup(null) } const saveFork = async () => { const { publicId } = await captureAndSaveViewport() return forkPart({ variables: { input: { userId: currentUser.sub, title, code, mainImage: publicId, }, }, }) } const handleSave = async () => { if (isDraft && isAuthenticated) { const { data } = await saveFork() navigate( routes.ide({ userName: data?.forkPart?.user?.userName, partTitle: data?.forkPart?.title, }) ) toast.success(`Part created with title: ${data?.forkPart?.title}.`) } else if (isAuthenticated) onSave() else recordedLogin() } const handleSaveAndEdit = async () => { const { data } = await saveFork() const { user: { userName }, title: partTitle, } = data?.forkPart || { user: {} } navigate(routes.part({ userName, partTitle })) } const recordedLogin = async () => { ReactGA.event({ category: 'login', action: 'ideToolbar signup prompt from fork', }) setIsLoginModalOpen(true) } const handleDownload = (url) => { const aTag = document.createElement('a') document.body.appendChild(aTag) aTag.href = url aTag.style.display = 'none' aTag.download = `CadHub_${Date.now()}.jpg` aTag.click() document.body.removeChild(aTag) } const anchorOrigin = { vertical: 'bottom', horizontal: 'center', } const transformOrigin = { vertical: 'top', horizontal: 'center', } const id = open ? 'simple-popover' : undefined return (
If this problem is frustrating to you, leave a comment on its{' '}