Files
flipper/desktop/plugins/public/navigation/components/SaveBookmarkDialog.tsx
Anton Nikolaev d782f19001 Refactor plugin to make it fast refreshable
Summary: Refactored Navigation plugin to make it fast-refreshable: moved the main component into a separate file and exported all components as named functions. Without these changes every change of UI triggered full reload.

Reviewed By: timur-valiev

Differential Revision: D29814077

fbshipit-source-id: 5285bdc5f14a5163f9501c0d45a3affefb08fc8e
2021-07-21 07:25:06 -07:00

118 lines
2.9 KiB
TypeScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {Button, FlexColumn, Input, Sheet, styled} from 'flipper';
import React, {useState} from 'react';
import {Bookmark, URI} from '../types';
type Props = {
uri: string | null;
edit: boolean;
shouldShow: boolean;
onHide?: () => void;
onRemove: (uri: URI) => void;
onSubmit: (bookmark: Bookmark) => void;
};
const Container = styled(FlexColumn)({
padding: 10,
width: 400,
});
const Title = styled.div({
fontWeight: 500,
marginTop: 8,
marginLeft: 2,
marginBottom: 8,
});
const URIContainer = styled.div({
marginLeft: 2,
marginBottom: 8,
overflowWrap: 'break-word',
});
const ButtonContainer = styled.div({
marginLeft: 'auto',
});
const NameInput = styled(Input)({
margin: 0,
marginBottom: 10,
height: 30,
});
export function SaveBookmarkDialog(props: Props) {
const {edit, shouldShow, onHide, onRemove, onSubmit, uri} = props;
const [commonName, setCommonName] = useState('');
if (uri == null || !shouldShow) {
return null;
} else {
return (
<Sheet onHideSheet={onHide}>
{(onHide: () => void) => {
return (
<Container>
<Title>
{edit ? 'Edit bookmark...' : 'Save to bookmarks...'}
</Title>
<NameInput
placeholder="Name..."
value={commonName}
onChange={(event: React.ChangeEvent<HTMLInputElement>) =>
setCommonName(event.target.value)
}
/>
<URIContainer>{uri}</URIContainer>
<ButtonContainer>
<Button
onClick={() => {
onHide();
setCommonName('');
}}
compact
padded>
Cancel
</Button>
{edit ? (
<Button
type="danger"
onClick={() => {
onHide();
onRemove(uri);
setCommonName('');
}}
compact
padded>
Remove
</Button>
) : null}
<Button
type="primary"
onClick={() => {
onHide();
onSubmit({uri, commonName});
// The component state is remembered even after unmounting.
// Thus it is necessary to reset the commonName here.
setCommonName('');
}}
compact
padded>
Save
</Button>
</ButtonContainer>
</Container>
);
}}
</Sheet>
);
}
}