Skip to content

Commit

Permalink
feat: add getEnvironmentDependentUrl function to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
amalv committed Jan 15, 2024
1 parent 8a8edb8 commit 3089926
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
11 changes: 2 additions & 9 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@ import { useTheme } from "./hooks";
import { AuthProvider } from "./contexts";
import { ApolloProvider } from "@apollo/client";
import { client } from "./apolloClient";
import { getEnvironmentDependentUrl } from "./utils/";

const getRedirectUri = () => {
const isProduction = process.env.NODE_ENV === "production";
const isStaging = import.meta.env.VITE_ENV === "staging";
if (isProduction && !isStaging) {
return `${window.location.origin}/bukie`;
}
return window.location.origin;
};
const App = () => {
const theme = useTheme();
const redirectUri = getRedirectUri();
const redirectUri = getEnvironmentDependentUrl();

return (
<Auth0Provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState, useCallback } from "react";
import { Avatar, CircularProgress, Menu, MenuItem } from "@mui/material";
import { User, useAuth0 } from "@auth0/auth0-react";
import { LoginButton } from "../LoginButton";
import { getEnvironmentDependentUrl } from "@/utils/";

const UserAvatar = ({
user,
Expand Down Expand Up @@ -67,22 +68,13 @@ const UserMenu = ({
);
};

const getReturnToUrl = () => {
const isProduction = process.env.NODE_ENV === "production";
const isStaging = import.meta.env.VITE_ENV === "staging";
if (isProduction && !isStaging) {
return `${window.location.origin}/bukie`;
}
return window.location.origin;
};

const useLogout = () => {
const { logout } = useAuth0();

return useCallback(
(event: React.MouseEvent<HTMLLIElement>) => {
event.preventDefault();
const returnTo = getReturnToUrl();
const returnTo = getEnvironmentDependentUrl();
logout({ logoutParams: { returnTo } });
localStorage.removeItem("auth0.token");
},
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./urlUtils";
7 changes: 7 additions & 0 deletions src/utils/urlUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const getEnvironmentDependentUrl = () => {
const isProduction = import.meta.env.VITE_ENV === "production";
if (isProduction) {
return `${window.location.origin}/bukie`;
}
return window.location.origin;
};

1 comment on commit 3089926

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.