diff --git a/src/menu/url_menu.tsx b/src/menu/url_menu.tsx index 1fb69f0..f8385ad 100644 --- a/src/menu/url_menu.tsx +++ b/src/menu/url_menu.tsx @@ -1,75 +1,45 @@ import * as queryString from 'query-string'; -import * as React from 'react'; import {analyticsEvent} from '../util/analytics'; import {Button, Form, Header, Icon, Input, Modal} from 'semantic-ui-react'; import {FormattedMessage} from 'react-intl'; import {MenuItem, MenuType} from './menu_item'; import {RouteComponentProps} from 'react-router-dom'; +import {useEffect, useRef, useState} from 'react'; interface Props { menuType: MenuType; } -interface State { - dialogOpen: boolean; - url?: string; -} - /** Displays and handles the "Open URL" menu. */ -export class UrlMenu extends React.Component< - RouteComponentProps & Props, - State -> { - state: State = {dialogOpen: false}; +export function UrlMenu(props: RouteComponentProps & Props) { + const [dialogOpen, setDialogOpen] = useState(false); + const [url, setUrl] = useState(''); + const inputRef = useRef(null); - inputRef: React.RefObject = React.createRef(); - - /** Opens the "Load from URL" dialog. */ - private openDialog() { - this.setState(Object.assign({}, this.state, {dialogOpen: true}), () => - this.inputRef.current!.focus(), - ); - } - - /** Cancels any of the open dialogs. */ - private handleClose() { - this.setState( - Object.assign({}, this.state, { - dialogOpen: false, - }), - ); - } + useEffect(() => { + if (dialogOpen) { + setUrl(''); + inputRef.current!.focus(); + } + }, [dialogOpen]); /** Load button clicked in the "Load from URL" dialog. */ - private handleLoad() { - this.setState( - Object.assign({}, this.state, { - dialogOpen: false, - }), - ); - if (this.state.url) { + function handleLoad() { + setDialogOpen(false); + if (url) { analyticsEvent('url_selected'); - this.props.history.push({ + props.history.push({ pathname: '/view', - search: queryString.stringify({url: this.state.url}), + search: queryString.stringify({url}), }); } } - /** Called when the URL input is typed into. */ - private handleUrlChange(value: string) { - this.setState( - Object.assign({}, this.state, { - url: value, - }), - ); - } - - private loadFromUrlModal() { + function loadFromUrlModal() { return ( this.handleClose()} + open={dialogOpen} + onClose={() => setDialogOpen(false)} centered={false} >
@@ -80,12 +50,13 @@ export class UrlMenu extends React.Component< />
-
this.handleLoad()}> + this.handleUrlChange(data.value)} - ref={this.inputRef} + value={url} + onChange={(_, data) => setUrl(data.value)} + ref={inputRef} />

- - @@ -119,21 +90,16 @@ export class UrlMenu extends React.Component< ); } - render() { - return ( - <> - this.openDialog()} - menuType={this.props.menuType} - > - - - - {this.loadFromUrlModal()} - - ); - } + return ( + <> + setDialogOpen(true)} menuType={props.menuType}> + + + + {loadFromUrlModal()} + + ); }