Skip to content

Commit

Permalink
fix: fix env variables issue (#157)
Browse files Browse the repository at this point in the history
Because

- When evaluating the env like
```
if (process.env.NEXT_PUBLIC_CONSOLE_BASE_URL === "hi"){}
```

it will affect by the webpack dead code elimination

This commit

- Use window.location.href to cope with this evaluation
  • Loading branch information
EiffelFly committed Jul 13, 2022
1 parent a3f7bff commit 58e2bb1
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, useState, useEffect, useCallback } from "react";
import { FC, useState, useEffect, useCallback, useMemo } from "react";
import {
BasicProgressMessageBox,
ProgressMessageBoxState,
Expand All @@ -18,6 +18,7 @@ import { sendAmplitudeData } from "@/lib/amplitude";
import { AxiosError } from "axios";
import { ErrorDetails } from "@/lib/instill/types";
import OutlineButton from "@/components/ui/Buttons/OutlineButton";
import useDeleteResourceGuard from "@/hooks/useDeleteResourceGuard";

export type ConfigureDestinationFormProps = {
destination: Nullable<DestinationWithDefinition>;
Expand Down Expand Up @@ -104,6 +105,8 @@ const ConfigureDestinationForm: FC<ConfigureDestinationFormProps> = ({
// # #
// ###################################################################

const { disableResourceDeletion } = useDeleteResourceGuard();

const [deleteDestinationModalIsOpen, setDeleteDestinationModalIsOpen] =
useState(false);

Expand Down Expand Up @@ -199,12 +202,7 @@ const ConfigureDestinationForm: FC<ConfigureDestinationFormProps> = ({
</div>
<div className="mb-10 flex flex-row">
<OutlineButton
disabled={
process.env.NEXT_PUBLIC_CONSOLE_BASE_URL ===
"https://demo.instill.tech"
? true
: false
}
disabled={disableResourceDeletion}
onClickHandler={() => setDeleteDestinationModalIsOpen(true)}
position="mr-auto my-auto"
type="button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { sendAmplitudeData } from "@/lib/amplitude";
import { AxiosError } from "axios";
import { ErrorDetails } from "@/lib/instill/types";
import OutlineButton from "@/components/ui/Buttons/OutlineButton";
import useDeleteResourceGuard from "@/hooks/useDeleteResourceGuard";

export type ConfigureSourceFormProps = {
source: Nullable<SourceWithPipelines>;
Expand Down Expand Up @@ -97,6 +98,8 @@ const ConfigureSourceForm: FC<ConfigureSourceFormProps> = ({ source }) => {
// # #
// ###################################################################

const { disableResourceDeletion } = useDeleteResourceGuard();

const [deleteSourceModalIsOpen, setDeleteSourceModalIsOpen] = useState(false);

const [messageBoxState, setMessageBoxState] =
Expand Down Expand Up @@ -191,12 +194,7 @@ const ConfigureSourceForm: FC<ConfigureSourceFormProps> = ({ source }) => {
</div>
<div className="mb-10 flex flex-row">
<OutlineButton
disabled={
process.env.NEXT_PUBLIC_CONSOLE_BASE_URL ===
"https://demo.instill.tech"
? true
: false
}
disabled={disableResourceDeletion}
onClickHandler={() => setDeleteSourceModalIsOpen(true)}
position="mr-auto my-auto"
type="button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import OutlineButton from "@/components/ui/Buttons/OutlineButton";
import { DeleteResourceModal } from "@/components/modals";
import { AxiosError } from "axios";
import { ErrorDetails } from "@/lib/instill/types";
import useDeleteResourceGuard from "@/hooks/useDeleteResourceGuard";

export type ConfigureModelFormProps = {
model: Nullable<Model>;
Expand Down Expand Up @@ -142,6 +143,8 @@ const ConfigureModelForm: FC<ConfigureModelFormProps> = ({
// # #
// ###################################################################

const { disableResourceDeletion } = useDeleteResourceGuard();

const modalState = useDeleteResourceModalState();

const deleteModel = useDeleteModel();
Expand Down Expand Up @@ -227,12 +230,7 @@ const ConfigureModelForm: FC<ConfigureModelFormProps> = ({
</div>
<div className="flex flex-row">
<OutlineButton
disabled={
process.env.NEXT_PUBLIC_CONSOLE_BASE_URL ===
"https://demo.instill.tech"
? true
: false
}
disabled={disableResourceDeletion}
onClickHandler={() => modalState.setModalIsOpen(true)}
position="mr-auto my-auto"
type="button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { sendAmplitudeData } from "@/lib/amplitude";
import { DeleteResourceModal } from "@/components/modals";
import { useRouter } from "next/router";
import OutlineButton from "@/components/ui/Buttons/OutlineButton";
import useDeleteResourceGuard from "@/hooks/useDeleteResourceGuard";

export type ConfigurePipelineFormProps = {
pipeline: Nullable<Pipeline>;
Expand Down Expand Up @@ -136,6 +137,8 @@ const ConfigurePipelineForm: FC<ConfigurePipelineFormProps> = ({
// # #
// ###################################################################

const { disableResourceDeletion } = useDeleteResourceGuard();

const [deletePipelineModalIsOpen, setDeletePipelineModalIsOpen] =
useState(false);

Expand Down Expand Up @@ -264,12 +267,7 @@ const ConfigurePipelineForm: FC<ConfigurePipelineFormProps> = ({
</div>
<div className="mb-10 flex flex-row">
<OutlineButton
disabled={
process.env.NEXT_PUBLIC_CONSOLE_BASE_URL ===
"https://demo.instill.tech"
? true
: false
}
disabled={disableResourceDeletion}
onClickHandler={() => setDeletePipelineModalIsOpen(true)}
position="mr-auto my-auto"
type="button"
Expand Down
20 changes: 20 additions & 0 deletions src/hooks/useDeleteResourceGuard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useEffect, useState } from "react";

const useDeleteResourceGuard = () => {
const [disableResourceDeletion, setDisableResourceDeletion] = useState(false);

useEffect(() => {
const hostname = new URL(window.location.href).hostname;

if (hostname === "demo.instill.tech") {
setDisableResourceDeletion(true);
return;
}

setDisableResourceDeletion(false);
}, []);

return { disableResourceDeletion };
};

export default useDeleteResourceGuard;
2 changes: 1 addition & 1 deletion src/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const Document: FC = () => {
property="twitter:image"
content={`${process.env.NEXT_PUBLIC_CONSOLE_BASE_URL}/images/instill-open-graph.png`}
/>
{process.env.NEXT_PUBLIC_CONSOLE_BASE_URL ===
{`${process.env.NEXT_PUBLIC_CONSOLE_BASE_URL}` ===
"https://demo.instill.tech" ? (
<>
<script
Expand Down

0 comments on commit 58e2bb1

Please sign in to comment.