Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Convert SnippetSidebar to TypeScript (#75659)

Andrei Chernykhc1c9bb4f1c1e
diff --git a/frontend/src/metabase/plugins/index.ts b/frontend/src/metabase/plugins/index.ts
index f1691d2f6c0..57bb81a7d9c 100644
--- a/frontend/src/metabase/plugins/index.ts
+++ b/frontend/src/metabase/plugins/index.ts
@@ -138,6 +138,8 @@ export {
type MoveSnippetModalProps,
type SnippetCollectionPermissionsModalProps,
type SnippetCollectionPickerModalProps,
+ type SnippetSidebarContext,
+ type SnippetSidebarMenuOption,
} from "./oss/snippets";
export {
PLUGIN_TRANSFORMS,
diff --git a/frontend/src/metabase/plugins/oss/core.ts b/frontend/src/metabase/plugins/oss/core.ts
index b65bea4ab5c..f0d5c2d2a2f 100644
--- a/frontend/src/metabase/plugins/oss/core.ts
+++ b/frontend/src/metabase/plugins/oss/core.ts
@@ -1,5 +1,5 @@
import type { Middleware } from "@reduxjs/toolkit";
-import type { ComponentType } from "react";
+import type { ComponentType, ReactNode } from "react";
import { t } from "ttag";
import noResultsSource from "assets/img/no_results.svg";
@@ -11,6 +11,12 @@ import type {
import type { UiParameter } from "metabase-lib/v1/parameters/types";
import type { Dashboard } from "metabase-types/api";
+import type {
+ SnippetSidebarContext,
+ SnippetSidebarMenuOption,
+ SnippetSidebarRowRenderers,
+} from "./snippets";
+
// Types
export type IllustrationValue = {
src: string;
@@ -111,9 +117,17 @@ const getDefaultFormWidgets = (): Record<string, ComponentType<any>> => ({});
export const PLUGIN_FORM_WIDGETS = getDefaultFormWidgets();
-const getDefaultSnippetSidebarPlusMenuOptions = () => [];
-const getDefaultSnippetSidebarRowRenderers = () => ({});
-const getDefaultSnippetSidebarHeaderButtons = () => [];
+const getDefaultSnippetSidebarPlusMenuOptions = (): ((
+ snippetSidebar: SnippetSidebarContext,
+) => SnippetSidebarMenuOption)[] => [];
+const getDefaultSnippetSidebarRowRenderers =
+ (): SnippetSidebarRowRenderers => ({
+ collection: null,
+ });
+const getDefaultSnippetSidebarHeaderButtons = (): ((
+ snippetSidebar: SnippetSidebarContext,
+ opts: { className?: string },
+) => ReactNode)[] => [];
export const PLUGIN_SNIPPET_SIDEBAR_PLUS_MENU_OPTIONS =
getDefaultSnippetSidebarPlusMenuOptions();
diff --git a/frontend/src/metabase/plugins/oss/snippets.ts b/frontend/src/metabase/plugins/oss/snippets.ts
index dfd6dae0882..1230d845e06 100644
--- a/frontend/src/metabase/plugins/oss/snippets.ts
+++ b/frontend/src/metabase/plugins/oss/snippets.ts
@@ -1,6 +1,8 @@
-import type { Component, ComponentType } from "react";
+import type { ComponentType } from "react";
+import type { Dispatch } from "metabase/redux/store";
import type {
+ Collection,
CollectionId,
IconName,
NativeQuerySnippet,
@@ -10,24 +12,26 @@ import { PluginPlaceholder } from "../components/PluginPlaceholder";
export type SnippetSidebarMenuOption = {
icon: IconName;
- name?: string;
- label?: string;
+ name: string;
onClick: () => void;
};
-export type SnippetSidebarState = {
- permissionsModalCollectionId?: CollectionId | null;
+// EE plugins receive the SnippetSidebar class instance; this is the subset of
+// its props they rely on.
+export type SnippetSidebarContext = {
+ props: {
+ snippetCollection: Collection;
+ dispatch: Dispatch;
+ };
};
-export type SnippetSidebarProps = {
- snippetCollection: { id: CollectionId | null };
+export type SnippetSidebarRowRenderers = {
+ collection: ComponentType<{
+ item: Collection;
+ setSnippetCollectionId?: (id: CollectionId) => void;
+ }> | null;
};
-export type SnippetSidebarComponent = Component<
- SnippetSidebarProps,
- SnippetSidebarState
->;
-
export type SnippetCollectionPickerModalProps = {
isOpen: boolean;
onSelect: (collectionId: CollectionId | null) => void;
diff --git a/frontend/src/metabase/query_builder/components/DatasetEditor/DatasetEditor.unit.spec.tsx b/frontend/src/metabase/query_builder/components/DatasetEditor/DatasetEditor.unit.spec.tsx
index 48ca45c53bf..a06ba607e91 100644
--- a/frontend/src/metabase/query_builder/components/DatasetEditor/DatasetEditor.unit.spec.tsx
+++ b/frontend/src/metabase/query_builder/components/DatasetEditor/DatasetEditor.unit.spec.tsx
@@ -64,6 +64,10 @@ const defaultDatasetEditorProps = {
dataReferenceStack: [],
pushDataReferenceStack: noop,
popDataReferenceStack: noop,
+ setModalSnippet: noop,
+ openSnippetModalWithSelectedText: noop,
+ insertSnippet: noop,
+ snippetCollectionId: null,
};
const renderDatasetEditor = async (card: Card | UnsavedCard) => {
diff --git a/frontend/src/metabase/query_builder/components/DatasetEditor/DatasetEditorInner.tsx b/frontend/src/metabase/query_builder/components/DatasetEditor/DatasetEditorInner.tsx
index b343a8939d3..be742464211 100644
--- a/frontend/src/metabase/query_builder/components/DatasetEditor/DatasetEditorInner.tsx
+++ b/frontend/src/metabase/query_builder/components/DatasetEditor/DatasetEditorInner.tsx
@@ -63,8 +63,10 @@ import {
} from "metabase-lib/v1/metadata/utils/models";
import type NativeQuery from "metabase-lib/v1/queries/NativeQuery";
import type {
+ CollectionId,
DatasetColumn,
Field,
+ NativeQuerySnippet,
RawSeries,
ResultsMetadata,
VisualizationDisplay,
@@ -127,6 +129,11 @@ export type DatasetEditorInnerProps = {
toggleTemplateTagsEditor: () => void;
toggleDataReference: () => void;
toggleSnippetSidebar: () => void;
+ setModalSnippet: (snippet: NativeQuerySnippet) => void;
+ openSnippetModalWithSelectedText: () => void;
+ insertSnippet: (snippet: NativeQuerySnippet) => void;
+ snippetCollectionId: CollectionId | null;
+ setSnippetCollectionId?: (id: CollectionId | null) => void;
forwardedRef?: React.Ref<HTMLDivElement>;
dataReferenceStack: DataReferenceItem[];
diff --git a/frontend/src/metabase/query_builder/components/view/View/NativeQueryRightSidebar/NativeQueryRightSidebar.tsx b/frontend/src/metabase/query_builder/components/view/View/NativeQueryRightSidebar/NativeQueryRightSidebar.tsx
index f7158adee9f..38720945976 100644
--- a/frontend/src/metabase/query_builder/components/view/View/NativeQueryRightSidebar/NativeQueryRightSidebar.tsx
+++ b/frontend/src/metabase/query_builder/components/view/View/NativeQueryRightSidebar/NativeQueryRightSidebar.tsx
@@ -14,9 +14,11 @@ import { useDispatch, useSelector } from "metabase/redux";
import type Question from "metabase-lib/v1/Question";
import type Database from "metabase-lib/v1/metadata/Database";
import type {
+ CollectionId,
DatabaseId,
EmbeddingParameterVisibility,
NativeDatasetQuery,
+ NativeQuerySnippet,
RowValue,
TemplateTag,
TemplateTagId,
@@ -31,6 +33,11 @@ interface NativeQueryRightSidebarProps {
toggleTemplateTagsEditor: () => void;
toggleDataReference: () => void;
toggleSnippetSidebar: () => void;
+ setModalSnippet: (snippet: NativeQuerySnippet) => void;
+ openSnippetModalWithSelectedText: () => void;
+ insertSnippet: (snippet: NativeQuerySnippet) => void;
+ snippetCollectionId: CollectionId | null;
+ setSnippetCollectionId?: (id: CollectionId | null) => void;
showTimelineEvents: () => void;
hideTimelineEvents: () => void;
selectTimelineEvents: () => void;
diff --git a/frontend/src/metabase/querying/components/SnippetSidebar/SnippetRow/SnippetRow.jsx b/frontend/src/metabase/querying/components/SnippetSidebar/SnippetRow/SnippetRow.tsx
similarity index 88%
rename from frontend/src/metabase/querying/components/SnippetSidebar/SnippetRow/SnippetRow.jsx
rename to frontend/src/metabase/querying/components/SnippetSidebar/SnippetRow/SnippetRow.tsx
index 302e7782559..cd6aee99aaf 100644
--- a/frontend/src/metabase/querying/components/SnippetSidebar/SnippetRow/SnippetRow.jsx
+++ b/frontend/src/metabase/querying/components/SnippetSidebar/SnippetRow/SnippetRow.tsx
@@ -1,4 +1,3 @@
-/* eslint-disable react/prop-types */
import cx from "classnames";
import { useState } from "react";
import { t } from "ttag";
@@ -7,15 +6,24 @@ import { useUpdateSnippetMutation } from "metabase/api";
import { Button } from "metabase/common/components/Button";
import CS from "metabase/css/core/index.css";
import { Ellipsified, Flex, Icon } from "metabase/ui";
+import type { NativeQuerySnippet } from "metabase-types/api";
import SnippetRowS from "./SnippetRow.module.css";
+interface SnippetRowProps {
+ item: NativeQuerySnippet;
+ // absent in the archived snippets view
+ insertSnippet?: (snippet: NativeQuerySnippet) => void;
+ setModalSnippet?: (snippet: NativeQuerySnippet) => void;
+ canWrite: boolean;
+}
+
export function SnippetRow({
item: snippet,
insertSnippet,
setModalSnippet,
canWrite,
-}) {
+}: SnippetRowProps) {
const [isOpen, setIsOpen] = useState(false);
const [updateSnippet] = useUpdateSnippetMutation();
@@ -52,7 +60,7 @@ export function SnippetRow({
? () => setIsOpen(true)
: (e) => {
e.stopPropagation();
- insertSnippet(snippet);
+ insertSnippet?.(snippet);
}
}
miw={0}
@@ -102,7 +110,7 @@ export function SnippetRow({
onClick={
snippet.archived
? () => updateSnippet({ id: snippet.id, archived: false })
- : () => setModalSnippet(snippet)
+ : () => setModalSnippet?.(snippet)
}
borderless
medium
diff --git a/frontend/src/metabase/querying/components/SnippetSidebar/SnippetSidebar.jsx b/frontend/src/metabase/querying/components/SnippetSidebar/SnippetSidebar.tsx
similarity index 78%
rename from frontend/src/metabase/querying/components/SnippetSidebar/SnippetSidebar.jsx
rename to frontend/src/metabase/querying/components/SnippetSidebar/SnippetSidebar.tsx
index 0e310c66d31..eca04fac0ca 100644
--- a/frontend/src/metabase/querying/components/SnippetSidebar/SnippetSidebar.jsx
+++ b/frontend/src/metabase/querying/components/SnippetSidebar/SnippetSidebar.tsx
@@ -1,6 +1,4 @@
-/* eslint-disable react/prop-types */
-import PropTypes from "prop-types";
-import * as React from "react";
+import { Component, createRef } from "react";
import { t } from "ttag";
import _ from "underscore";
@@ -20,9 +18,18 @@ import {
PLUGIN_SNIPPET_SIDEBAR_HEADER_BUTTONS,
PLUGIN_SNIPPET_SIDEBAR_PLUS_MENU_OPTIONS,
PLUGIN_SNIPPET_SIDEBAR_ROW_RENDERERS,
+ type SnippetSidebarMenuOption,
} from "metabase/plugins";
import { useDispatch, useSelector } from "metabase/redux";
+import type { Dispatch } from "metabase/redux/store";
import { Box, Button, Flex, Icon, Menu } from "metabase/ui";
+import type {
+ Collection,
+ CollectionId,
+ CollectionItem,
+ CollectionItemModel,
+ NativeQuerySnippet,
+} from "metabase-types/api";
import { SnippetRow } from "./SnippetRow";
import S from "./SnippetSidebar.module.css";
@@ -30,30 +37,45 @@ import { SnippetSidebarEmptyState } from "./SnippetSidebarEmptyState";
const MIN_SNIPPETS_FOR_SEARCH = 1;
-/**
- * @typedef {import("metabase/plugins").SnippetSidebarProps} SnippetSidebarProps
- * @typedef {import("metabase/plugins").SnippetSidebarState} SnippetSidebarState
- */
+type RowItem = (NativeQuerySnippet | Collection | CollectionItem) & {
+ model?: CollectionItemModel;
+};
+
+interface SnippetSidebarProps {
+ onClose: () => void;
+ setModalSnippet: (snippet: NativeQuerySnippet) => void;
+ openSnippetModalWithSelectedText: () => void;
+ insertSnippet: (snippet: NativeQuerySnippet) => void;
+ snippetCollectionId: CollectionId | null;
+ setSnippetCollectionId?: (id: CollectionId | null) => void;
+}
+
+interface SnippetSidebarInnerProps extends SnippetSidebarProps {
+ snippets: NativeQuerySnippet[];
+ snippetCollections: Collection[];
+ snippetCollection: Collection;
+ search: CollectionItem[];
+ isRemoteSyncReadOnly: boolean;
+ dispatch: Dispatch;
+}
+
+interface SnippetSidebarInnerState {
+ showSearch: boolean;
+ searchString: string;
+ showArchived: boolean;
+}
-/**
- * @extends {React.Component<SnippetSidebarProps, SnippetSidebarState>}
- */
-class SnippetSidebarInner extends React.Component {
- state = {
+class SnippetSidebarInner extends Component<
+ SnippetSidebarInnerProps,
+ SnippetSidebarInnerState
+> {
+ state: SnippetSidebarInnerState = {
showSearch: false,
searchString: "",
showArchived: false,
};
- static propTypes = {
- onClose: PropTypes.func.isRequired,
- setModalSnippet: PropTypes.func.isRequired,
- openSnippetModalWithSelectedText: PropTypes.func.isRequired,
- insertSnippet: PropTypes.func.isRequired,
- isRemoteSyncReadOnly: PropTypes.bool.isRequired,
- };
-
- searchBox = React.createRef();
+ searchBox = createRef<HTMLInputElement>();
componentDidUpdate() {
if (this.state.showSearch) {
@@ -102,7 +124,7 @@ class SnippetSidebarInner extends React.Component {
const collectionsById = _.indexBy(snippetCollections, "id");
const snippetsById = _.indexBy(snippets, "id");
- const hydrateSearchItem = (item) => {
+ const hydrateSearchItem = (item: CollectionItem): RowItem => {
const model = item.model || "snippet";
const full =
model === "collection"
@@ -111,7 +133,7 @@ class SnippetSidebarInner extends React.Component {
return full ? { ...full, model } : item;
};
- const displayedItems = showSearch
+ const displayedItems: RowItem[] = showSearch
? snippets.filter((snippet) =>
snippet.name.toLowerCase().includes(searchString.toLowerCase()),
)
@@ -132,11 +154,17 @@ class SnippetSidebarInner extends React.Component {
? snippetCollection.parent_id
: null;
- this.props.setSnippetCollectionId(targetId);
+ this.props.setSnippetCollectionId?.(targetId ?? null);
};
const showAddMenu =
snippetCollection.can_write && !showSearch && !isRemoteSyncReadOnly;
+ const newSnippetMenuItem: SnippetSidebarMenuOption = {
+ icon: "snippet",
+ name: t`New snippet`,
+ onClick: openSnippetModalWithSelectedText,
+ };
+
return (
<SidebarContent footer={this.footer()}>
{!showSearch &&
@@ -188,11 +216,9 @@ class SnippetSidebarInner extends React.Component {
justify="flex-end"
data-testid="snippet-header-buttons"
>
- {[
- ...PLUGIN_SNIPPET_SIDEBAR_HEADER_BUTTONS.map((f) =>
- f(this, { className: S.HeaderButton }),
- ),
- ]}
+ {PLUGIN_SNIPPET_SIDEBAR_HEADER_BUTTONS.map((f) =>
+ f(this, { className: S.HeaderButton }),
+ )}
{snippets.length >= MIN_SNIPPETS_FOR_SEARCH && (
<Button
@@ -216,11 +242,7 @@ class SnippetSidebarInner extends React.Component {
</Menu.Target>
<Menu.Dropdown>
{[
- {
- icon: "snippet",
- name: t`New snippet`,
- onClick: openSnippetModalWithSelectedText,
- },
+ newSnippetMenuItem,
...PLUGIN_SNIPPET_SIDEBAR_PLUS_MENU_OPTIONS.map(
(f) => f(this),
),
@@ -247,7 +269,6 @@ class SnippetSidebarInner extends React.Component {
key={`${item.model || "snippet"}-${item.id}`}
item={item}
type={item.model || "snippet"}
- setSidebarState={this.setState.bind(this)}
canWrite={
snippetCollection.can_write && !isRemoteSyncReadOnly
}
@@ -262,7 +283,9 @@ class SnippetSidebarInner extends React.Component {
}
}
-function SnippetSidebarWithSearch(props) {
+type SnippetSidebarWithSearchProps = Omit<SnippetSidebarInnerProps, "search">;
+
+function SnippetSidebarWithSearch(props: SnippetSidebarWithSearchProps) {
const collectionId =
props.snippetCollectionId === null ? "root" : props.snippetCollectionId;
const {
@@ -281,7 +304,7 @@ function SnippetSidebarWithSearch(props) {
);
}
-export function SnippetSidebar(props) {
+export function SnippetSidebar(props: SnippetSidebarProps) {
const dispatch = useDispatch();
const isRemoteSyncReadOnly = useSelector(
PLUGIN_REMOTE_SYNC.getIsRemoteSyncReadOnly,
@@ -324,17 +347,24 @@ export function SnippetSidebar(props) {
);
}
-function ArchivedSnippetsInner(props) {
- const {
- onBack,
- snippets,
- snippetCollections,
- archivedSnippetCollections,
- isRemoteSyncReadOnly,
- } = props;
+interface ArchivedSnippetsInnerProps {
+ onBack: () => void;
+ snippets: NativeQuerySnippet[];
+ snippetCollections: Collection[];
+ archivedSnippetCollections: Collection[];
+ isRemoteSyncReadOnly: boolean;
+}
+
+function ArchivedSnippetsInner({
+ onBack,
+ snippets,
+ snippetCollections,
+ archivedSnippetCollections,
+ isRemoteSyncReadOnly,
+}: ArchivedSnippetsInnerProps) {
const collectionsById = _.indexBy(
snippetCollections.concat(archivedSnippetCollections),
- (c) => canonicalCollectionId(c.id),
+ (c) => String(canonicalCollectionId(c.id)),
);
return (
@@ -358,7 +388,6 @@ function ArchivedSnippetsInner(props) {
type="snippet"
canWrite={
collectionsById[
- // `String` used to appease flow
String(canonicalCollectionId(snippet.collection_id))
].can_write && !isRemoteSyncReadOnly
}
@@ -368,7 +397,7 @@ function ArchivedSnippetsInner(props) {
);
}
-function ArchivedSnippets(props) {
+function ArchivedSnippets(props: { onBack: () => void }) {
const isRemoteSyncReadOnly = useSelector(
PLUGIN_REMOTE_SYNC.getIsRemoteSyncReadOnly,
);
@@ -408,10 +437,22 @@ function ArchivedSnippets(props) {
);
}
-function Row(props) {
- const Component = {
- snippet: SnippetRow,
- ...PLUGIN_SNIPPET_SIDEBAR_ROW_RENDERERS,
- }[props.type];
- return Component ? <Component {...props} /> : null;
+interface RowProps {
+ item: RowItem;
+ type: CollectionItemModel;
+ canWrite: boolean;
+}
+
+function Row(props: RowProps) {
+ // `type` tells us what `item` is, but TS can't correlate independent props
+ if (props.type === "collection") {
+ const CollectionRow = PLUGIN_SNIPPET_SIDEBAR_ROW_RENDERERS.collection;
+ return CollectionRow ? (
+ <CollectionRow {...props} item={props.item as Collection} />
+ ) : null;
+ }
+ if (props.type === "snippet") {
+ return <SnippetRow {...props} item={props.item as NativeQuerySnippet} />;
+ }
+ return null;
}
diff --git a/frontend/src/metabase/querying/components/SnippetSidebar/tests/setup.tsx b/frontend/src/metabase/querying/components/SnippetSidebar/tests/setup.tsx
index 455b83dc688..3a277234484 100644
--- a/frontend/src/metabase/querying/components/SnippetSidebar/tests/setup.tsx
+++ b/frontend/src/metabase/querying/components/SnippetSidebar/tests/setup.tsx
@@ -45,6 +45,7 @@ export async function setup({
});
const state = createMockState({
+ currentUser: createMockUser(user),
settings: mockSettings({
"token-features": createMockTokenFeatures(tokenFeatures),
}),
@@ -61,7 +62,6 @@ export async function setup({
openSnippetModalWithSelectedText={() => null}
insertSnippet={() => null}
snippetCollectionId={null}
- user={createMockUser(user)}
/>,
{
storeInitialState: state,