Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🪟🔧 Refactor ConnectorsView #21531

Merged
merged 14 commits into from
Jan 26, 2023
12 changes: 12 additions & 0 deletions airbyte-webapp/src/components/Indicator/Indicator.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@use "scss/colors";

.indicator {
height: 10px;
width: 10px;
border-radius: 50%;
background: colors.$red;
}

.hidden {
background-color: transparent;
}
21 changes: 14 additions & 7 deletions airbyte-webapp/src/components/Indicator/Indicator.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import styled from "styled-components";
import classNames from "classnames";

export const Indicator = styled.div`
height: 10px;
width: 10px;
border-radius: 50%;
background: ${({ theme }) => theme.dangerColor};
`;
import styles from "./Indicator.module.scss";

export interface IndicatorProps {
/**
* Set to true to render an invisible indicator so reserve the space in the UI
*/
hidden?: boolean;
className?: string;
}

export const Indicator: React.FC<IndicatorProps> = ({ hidden, className }) => (
<div className={classNames(className, styles.indicator, { [styles.hidden]: hidden })} />
);
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const ReleaseStageBadge: React.FC<ReleaseStageBadgeProps> = ({
}) => {
const { formatMessage } = useIntl();

if (!stage || stage === ReleaseStage.custom) {
if (!stage) {
return null;
}

Expand Down
1 change: 0 additions & 1 deletion airbyte-webapp/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@
"admin.downloadSchedulerLogs": "Download Scheduler Logs",
"admin.upgradeAll": "Upgrade all",
"admin.upgraded": "Upgraded!",
"admin.customImage": "custom",

"settings.accountSettings": "Account Settings",
"settings.webhook": "Connection status Webhook",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useMemo, useState } from "react";
import React, { useCallback, useMemo, useRef, useState } from "react";
import { useIntl } from "react-intl";
import { useAsyncFn } from "react-use";

Expand All @@ -22,28 +22,34 @@ const DestinationsPage: React.FC = () => {
const { destinations } = useDestinationList();

const [feedbackList, setFeedbackList] = useState<Record<string, string>>({});
const feedbackListRef = useRef(feedbackList);
feedbackListRef.current = feedbackList;

const { mutateAsync: updateDestinationDefinition } = useUpdateDestinationDefinition();
const [updatingDefinitionId, setUpdatingDefinitionId] = useState<string>();

const { hasNewDestinationVersion } = useGetConnectorsOutOfDate();

const onUpdateVersion = useCallback(
async ({ id, version }: { id: string; version: string }) => {
try {
setUpdatingDefinitionId(id);
await updateDestinationDefinition({
destinationDefinitionId: id,
dockerImageTag: version,
});
setFeedbackList({ ...feedbackList, [id]: "success" });
setFeedbackList({ ...feedbackListRef.current, [id]: "success" });
} catch (e) {
const messageId = e.status === 422 ? "form.imageCannotFound" : "form.someError";
setFeedbackList({
...feedbackList,
...feedbackListRef.current,
[id]: formatMessage({ id: messageId }),
});
} finally {
setUpdatingDefinitionId(undefined);
}
},
[feedbackList, formatMessage, updateDestinationDefinition]
[formatMessage, updateDestinationDefinition]
);

const usedDestinationDefinitions = useMemo<DestinationDefinitionRead[]>(() => {
Expand Down Expand Up @@ -80,6 +86,7 @@ const DestinationsPage: React.FC = () => {
onUpdateVersion={onUpdateVersion}
usedConnectorsDefinitions={usedDestinationDefinitions}
connectorsDefinitions={destinationDefinitions}
updatingDefinitionId={updatingDefinitionId}
loading={loading}
error={error}
onUpdate={onUpdate}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useMemo, useState } from "react";
import React, { useCallback, useMemo, useRef, useState } from "react";
import { useIntl } from "react-intl";
import { useAsyncFn } from "react-use";

Expand All @@ -15,33 +15,39 @@ const SourcesPage: React.FC = () => {

const [isUpdateSuccess, setIsUpdateSuccess] = useState(false);
const [feedbackList, setFeedbackList] = useState<Record<string, string>>({});
const feedbackListRef = useRef(feedbackList);
feedbackListRef.current = feedbackList;
Comment on lines +18 to +19
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this change made? And is it intentional that this change was only made to SourcesPage and not also to DestinationsPage?

Side note: it seems like the logic in those two components is almost exactly the same, so a lot of it could be moved into the ConnectorsView component, with props to differentiate between the source- or destination-specific methods. What do you think? Could also be done in a separate PR if you'd prefer - just thought I'd call it out here since we are already refactoring in this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to do the same change for the destination, thanks for catching this. This is done to prevent a full re-render of the table on state change of the feedback list. If onUpdateVersion changes, then renderColumns changes here

[allowUpdateConnectors, allowUploadCustomImage, onUpdateVersion]

which in turn means the columns object passed to the table changes here:

which should be avoided (the docs say that the columns should be memoized: https://react-table-v7.tanstack.com/docs/api/useTable which means if they change reference it's pretty expensive)


const { formatMessage } = useIntl();
const { sources } = useSourceList();
const { sourceDefinitions } = useSourceDefinitionList();

const { mutateAsync: updateSourceDefinition } = useUpdateSourceDefinition();
const [updatingDefinitionId, setUpdatingDefinitionId] = useState<string>();

const { hasNewSourceVersion } = useGetConnectorsOutOfDate();
const { updateAllSourceVersions } = useUpdateSourceDefinitions();

const onUpdateVersion = useCallback(
async ({ id, version }: { id: string; version: string }) => {
try {
setUpdatingDefinitionId(id);
await updateSourceDefinition({
sourceDefinitionId: id,
dockerImageTag: version,
});
setFeedbackList({ ...feedbackList, [id]: "success" });
setFeedbackList({ ...feedbackListRef.current, [id]: "success" });
} catch (e) {
const messageId = e.status === 422 ? "form.imageCannotFound" : "form.someError";
setFeedbackList({
...feedbackList,
...feedbackListRef.current,
[id]: formatMessage({ id: messageId }),
});
} finally {
setUpdatingDefinitionId(undefined);
}
},
[feedbackList, formatMessage, updateSourceDefinition]
[formatMessage, updateSourceDefinition]
);

const usedSourcesDefinitions: SourceDefinitionRead[] = useMemo(() => {
Expand Down Expand Up @@ -72,6 +78,7 @@ const SourcesPage: React.FC = () => {
<ConnectorsView
type="sources"
loading={loading}
updatingDefinitionId={updatingDefinitionId}
error={error}
isUpdateSuccess={isUpdateSuccess}
hasNewConnectorVersion={hasNewSourceVersion}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.iconContainer {
height: 25px;
}
Original file line number Diff line number Diff line change
@@ -1,57 +1,29 @@
import React from "react";
import { FormattedMessage } from "react-intl";
import styled from "styled-components";

import Indicator from "components/Indicator";
import { ReleaseStageBadge } from "components/ReleaseStageBadge";
import { FlexContainer } from "components/ui/Flex";

import { ReleaseStage } from "core/request/AirbyteClient";
import { getIcon } from "utils/imageUtils";

import styles from "./ConnectorCell.module.scss";

interface ConnectorCellProps {
connectorName: string;
img?: string;
hasUpdate?: boolean;
releaseStage?: ReleaseStage;
}

const Content = styled.div<{ enabled?: boolean }>`
display: flex;
align-items: center;
padding-left: 30px;
position: relative;
margin: -5px 0;
min-width: 290px;
gap: 8px;
`;

const Image = styled.div`
height: 25px;
width: 17px;
`;

const Notification = styled(Indicator)`
position: absolute;
left: 8px;
`;

const CustomAnnotation = styled.span`
color: ${({ theme }) => theme.greyColor40};
`;

const ConnectorCell: React.FC<ConnectorCellProps> = ({ connectorName, img, hasUpdate, releaseStage }) => {
return (
<Content>
{hasUpdate && <Notification />}
<Image>{getIcon(img)}</Image>
<span>{connectorName}</span>
<FlexContainer alignItems="center" gap="lg">
<Indicator hidden={!hasUpdate} />
<div className={styles.iconContainer}>{getIcon(img)}</div>
<div>{connectorName}</div>
<ReleaseStageBadge small tooltip={false} stage={releaseStage} />
{releaseStage === "custom" && (
<CustomAnnotation>
(<FormattedMessage id="admin.customImage" />)
</CustomAnnotation>
)}
</Content>
</FlexContainer>
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@use "scss/variables";
@use "scss/colors";

.changeToHeader {
margin-left: 200px;
}

.connectorsTable {
td {
padding-top: variables.$spacing-sm;
padding-bottom: variables.$spacing-sm;
}
}
Loading