ktmud commented on a change in pull request #13037: URL: https://github.com/apache/superset/pull/13037#discussion_r577979866
########## File path: superset-frontend/src/explore/components/ExploreActionButtons.tsx ########## @@ -0,0 +1,204 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import React, { useState } from 'react'; +import cx from 'classnames'; +import { t } from '@superset-ui/core'; +import Icon from 'src/components/Icon'; +import { Tooltip } from 'src/common/components/Tooltip'; +import copyTextToClipboard from 'src/utils/copy'; +import withToasts from 'src/messageToasts/enhancers/withToasts'; +import { useUrlShortener } from 'src/common/hooks/useUrlShortener'; +import EmbedCodeButton from './EmbedCodeButton'; +import ConnectedDisplayQueryButton from './DisplayQueryButton'; +import { exportChart, getExploreLongUrl } from '../exploreUtils'; + +type ActionButtonProps = { + icon: React.ReactElement; + text?: string; + tooltip: string; + className?: string; + onClick: React.MouseEventHandler<HTMLElement>; + onTooltipVisibilityChange?: (visible: boolean) => void; + 'data-test'?: string; +}; + +type ExploreActionButtonsProps = { + actions: { redirectSQLLab: Function; openPropertiesModal: Function }; + canDownload: boolean; + chartHeight: number; + chartStatus: string; + latestQueryFormData: {}; + queriesResponse: {}; + slice: { slice_name: string }; + addDangerToast: Function; +}; + +const ActionButton = (props: ActionButtonProps) => { + const { + icon, + text, + tooltip, + className, + onTooltipVisibilityChange, + ...rest + } = props; + return ( + <Tooltip + id={`${icon}-tooltip`} + placement="top" + title={tooltip} + trigger={['hover']} + onVisibleChange={onTooltipVisibilityChange} + > + <div + role="button" + tabIndex={0} + css={{ '&:focus, &:focus:active': { outline: 0 } }} + className={className || 'btn btn-default btn-sm'} + style={{ height: 30 }} + {...rest} + > + {icon} + {text && <span style={{ marginLeft: 5 }}>{text}</span>} + </div> + </Tooltip> + ); +}; + +const ExploreActionButtons = (props: ExploreActionButtonsProps) => { + const { + actions, + canDownload, + chartHeight, + chartStatus, + latestQueryFormData, + queriesResponse, + slice, + addDangerToast, + } = props; + + const copyTooltipText = t('Copy chart URL to clipboard'); + const [copyTooltip, setCopyTooltip] = useState(copyTooltipText); + const longUrl = getExploreLongUrl(latestQueryFormData); + const getShortUrl = useUrlShortener(longUrl); + + const doCopyLink = async () => { + try { + setCopyTooltip(t('Loading...')); + const shortUrl = await getShortUrl(); + await copyTextToClipboard(shortUrl); + setCopyTooltip(t('Copied to clipboard!')); + } catch (error) { + setCopyTooltip(t('Sorry, your browser does not support copying.')); + } Review comment: ```suggestion let shortUrl; try { setCopyTooltip(t('Loading...')); shortUrl = await getShortUrl(); } catch (error) { setCopyTooltip(t('ERROR: failed to generate chart URL.')); return; } try { await copyTextToClipboard(shortUrl); setCopyTooltip(t('Copied to clipboard!')); } catch (error) { setCopyTooltip(t('Sorry, your browser does not support copying.')); } ``` I think we can use two try/catch to distinguish backend error from browser support error. For browsers that does not support copying, maybe you can also show the shortened URL in the tooltip (or an alert box, or a modal) so users can copy it themselves. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
