Skip to content

Commit

Permalink
useCallback fix & use of safeParseJSON (#8396)
Browse files Browse the repository at this point in the history
* remove extraConfig from useCallback dep

* safeParseJSON

* fix
  • Loading branch information
spolu authored Oct 31, 2024
1 parent 010987b commit 6fda99a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
19 changes: 11 additions & 8 deletions front/components/data_source/CreateConnectionConfirmationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ export function CreateConnectionConfirmationModal({
const [isLoading, setIsLoading] = useState(false);
const [extraConfig, setExtraConfig] = useState<Record<string, string>>({});

const isExtraConfigValid = useCallback(() => {
if (connectorProviderConfiguration.connectorProvider === "zendesk") {
return isValidZendeskSubdomain(extraConfig.zendesk_subdomain);
} else {
return true;
}
}, [connectorProviderConfiguration.connectorProvider, extraConfig]);
const isExtraConfigValid = useCallback(
(extraConfig: Record<string, string>) => {
if (connectorProviderConfiguration.connectorProvider === "zendesk") {
return isValidZendeskSubdomain(extraConfig.zendesk_subdomain);
} else {
return true;
}
},
[connectorProviderConfiguration.connectorProvider]
);

useEffect(() => {
if (isOpen) {
Expand Down Expand Up @@ -143,7 +146,7 @@ export function CreateConnectionConfirmationModal({
setIsLoading(true);
onConfirm(extraConfig);
}}
disabled={!isExtraConfigValid() || isLoading}
disabled={!isExtraConfigValid(extraConfig) || isLoading}
label={
isLoading
? "Connecting..."
Expand Down
22 changes: 10 additions & 12 deletions front/pages/w/[wId]/oauth/[provider]/setup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isOAuthProvider, isOAuthUseCase } from "@dust-tt/types";
import { isOAuthProvider, isOAuthUseCase, safeParseJSON } from "@dust-tt/types";
import { isLeft } from "fp-ts/lib/Either";
import * as t from "io-ts";

Expand Down Expand Up @@ -30,21 +30,19 @@ export const getServerSideProps = withDefaultUserAuthRequirements<object>(
}

let parsedExtraConfig: Record<string, string> = {};
try {
const bodyValidation = ExtraConfigTypeSchema.decode(
JSON.parse(extraConfig as string)
);
if (isLeft(bodyValidation)) {
return {
notFound: true,
};
}
parsedExtraConfig = bodyValidation.right;
} catch (e) {
const parseRes = safeParseJSON(extraConfig as string);
if (parseRes.isErr()) {
return {
notFound: true,
};
}
const bodyValidation = ExtraConfigTypeSchema.decode(parseRes.value);
if (isLeft(bodyValidation)) {
return {
notFound: true,
};
}
parsedExtraConfig = bodyValidation.right;

const urlRes = await createConnectionAndGetSetupUrl(
auth,
Expand Down

0 comments on commit 6fda99a

Please sign in to comment.