diff --git a/superset-frontend/src/dashboard/components/AddSliceCard/AddSliceCard.test.jsx b/superset-frontend/src/dashboard/components/AddSliceCard/AddSliceCard.test.tsx similarity index 92% rename from superset-frontend/src/dashboard/components/AddSliceCard/AddSliceCard.test.jsx rename to superset-frontend/src/dashboard/components/AddSliceCard/AddSliceCard.test.tsx index 6b1f44d7e2f2c..26cd7b945d0e9 100644 --- a/superset-frontend/src/dashboard/components/AddSliceCard/AddSliceCard.test.jsx +++ b/superset-frontend/src/dashboard/components/AddSliceCard/AddSliceCard.test.tsx @@ -29,17 +29,14 @@ jest.mock('src/components/DynamicPlugins', () => ({ })); const mockedProps = { - datasourceUrl: null, - datasourceName: '-', - innerRef: null, - isSelected: false, - style: null, - thumbnailUrl: null, - lastModified: null, visType: 'table', sliceName: '-', }; +declare const global: { + featureFlags: Record; +}; + test('do not render thumbnail if feature flag is not set', async () => { global.featureFlags = { [FeatureFlag.THUMBNAILS]: false, diff --git a/superset-frontend/src/dashboard/components/AddSliceCard/index.jsx b/superset-frontend/src/dashboard/components/AddSliceCard/AddSliceCard.tsx similarity index 82% rename from superset-frontend/src/dashboard/components/AddSliceCard/index.jsx rename to superset-frontend/src/dashboard/components/AddSliceCard/AddSliceCard.tsx index eb01af234d937..c87fbf89bb68a 100644 --- a/superset-frontend/src/dashboard/components/AddSliceCard/index.jsx +++ b/superset-frontend/src/dashboard/components/AddSliceCard/AddSliceCard.tsx @@ -16,42 +16,30 @@ * specific language governing permissions and limitations * under the License. */ -import React, { useEffect, useMemo, useRef, useState } from 'react'; -import PropTypes from 'prop-types'; + +import React, { + CSSProperties, + ReactNode, + useEffect, + useMemo, + useRef, + useState, +} from 'react'; import { t, isFeatureEnabled, FeatureFlag, css } from '@superset-ui/core'; import ImageLoader from 'src/components/ListViewCard/ImageLoader'; import { usePluginContext } from 'src/components/DynamicPlugins'; import { Tooltip } from 'src/components/Tooltip'; - -const propTypes = { - datasourceUrl: PropTypes.string, - datasourceName: PropTypes.string, - innerRef: PropTypes.func, - isSelected: PropTypes.bool, - lastModified: PropTypes.string, - sliceName: PropTypes.string.isRequired, - style: PropTypes.object, - thumbnailUrl: PropTypes.string, - visType: PropTypes.string.isRequired, -}; - -const defaultProps = { - datasourceUrl: null, - datasourceName: '-', - innerRef: null, - isSelected: false, - style: null, - thumbnailUrl: null, - lastModified: null, -}; +import { Theme } from '@emotion/react'; const FALLBACK_THUMBNAIL_URL = '/static/assets/images/chart-card-fallback.svg'; -const TruncatedTextWithTooltip = ({ children, ...props }) => { - const [isTruncated, setIsTruncated] = useState(); - const ref = useRef(); +const TruncatedTextWithTooltip: React.FC = ({ children, ...props }) => { + const [isTruncated, setIsTruncated] = useState(false); + const ref = useRef(null); useEffect(() => { - setIsTruncated(ref.current.offsetWidth < ref.current.scrollWidth); + setIsTruncated( + ref.current ? ref.current.offsetWidth < ref.current.scrollWidth : false, + ); }, [children]); const div = ( @@ -72,9 +60,12 @@ const TruncatedTextWithTooltip = ({ children, ...props }) => { return isTruncated ? {div} : div; }; -const MetadataItem = ({ label, value }) => ( +const MetadataItem: React.FC<{ + label: ReactNode; + value: ReactNode; +}> = ({ label, value }) => (
css` + css={(theme: Theme) => css` font-size: ${theme.typography.sizes.s}px; display: flex; justify-content: space-between; @@ -85,7 +76,7 @@ const MetadataItem = ({ label, value }) => ( `} > css` + css={(theme: Theme) => css` margin-right: ${theme.gridUnit * 4}px; color: ${theme.colors.grayscale.base}; `} @@ -102,10 +93,13 @@ const MetadataItem = ({ label, value }) => (
); -const SliceAddedBadgePlaceholder = ({ showThumbnails, placeholderRef }) => ( +const SliceAddedBadgePlaceholder: React.FC<{ + showThumbnails?: boolean; + placeholderRef: (element: HTMLDivElement) => void; +}> = ({ showThumbnails, placeholderRef }) => (
css` + css={(theme: Theme) => css` /* Display styles */ border: 1px solid ${theme.colors.primary.dark1}; border-radius: ${theme.gridUnit}px; @@ -128,9 +122,11 @@ const SliceAddedBadgePlaceholder = ({ showThumbnails, placeholderRef }) => (
); -const SliceAddedBadge = ({ placeholder }) => ( +const SliceAddedBadge: React.FC<{ placeholder?: HTMLDivElement }> = ({ + placeholder, +}) => (
css` + css={(theme: Theme) => css` /* Display styles */ border: 1px solid ${theme.colors.primary.dark1}; border-radius: ${theme.gridUnit}px; @@ -153,19 +149,29 @@ const SliceAddedBadge = ({ placeholder }) => (
); -function AddSliceCard({ +const AddSliceCard: React.FC<{ + datasourceUrl?: string; + datasourceName?: string; + innerRef?: React.RefObject; + isSelected?: boolean; + lastModified?: string; + sliceName: string; + style?: CSSProperties; + thumbnailUrl?: string; + visType: string; +}> = ({ datasourceUrl, - datasourceName, + datasourceName = '-', innerRef, - isSelected, + isSelected = false, lastModified, sliceName, - style, + style = {}, thumbnailUrl, visType, -}) { +}) => { const showThumbnails = isFeatureEnabled(FeatureFlag.THUMBNAILS); - const [sliceAddedBadge, setSliceAddedBadge] = useState(); + const [sliceAddedBadge, setSliceAddedBadge] = useState(); const { mountedPluginMetadata } = usePluginContext(); const vizName = useMemo( () => mountedPluginMetadata[visType].name, @@ -176,7 +182,7 @@ function AddSliceCard({
css` + css={(theme: Theme) => css` border: 1px solid ${theme.colors.grayscale.light2}; border-radius: ${theme.gridUnit}px; background: ${theme.colors.grayscale.light5}; @@ -234,7 +240,7 @@ function AddSliceCard({ >
css` + css={(theme: Theme) => css` margin-bottom: ${theme.gridUnit * 2}px; font-weight: ${theme.typography.weights.bold}; display: flex; @@ -268,9 +274,6 @@ function AddSliceCard({
); -} - -AddSliceCard.propTypes = propTypes; -AddSliceCard.defaultProps = defaultProps; +}; export default AddSliceCard; diff --git a/superset-frontend/src/dashboard/components/AddSliceCard/index.ts b/superset-frontend/src/dashboard/components/AddSliceCard/index.ts new file mode 100644 index 0000000000000..c3736da122cdf --- /dev/null +++ b/superset-frontend/src/dashboard/components/AddSliceCard/index.ts @@ -0,0 +1,22 @@ +/** + * 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 AddSliceCard from './AddSliceCard'; + +export default AddSliceCard;