Skip to content

Commit

Permalink
chore: extract into utils isHttpMode function
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Oct 21, 2024
1 parent 91fdbd2 commit 7ec580d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
7 changes: 3 additions & 4 deletions frontend/src/components/ExternalLink.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Link } from "react-router-dom";
import { isHttpMode } from "src/utils/isHttpMode";
import { openLink } from "src/utils/openLink";

type Props = {
Expand All @@ -8,11 +9,9 @@ type Props = {
};

export default function ExternalLink({ to, className, children }: Props) {
const isHttpMode =
window.location.protocol.startsWith("http") &&
!window.location.hostname.startsWith("wails");
const _isHttpMode = isHttpMode();

return isHttpMode ? (
return _isHttpMode ? (
<Link
to={to}
target="_blank"
Expand Down
16 changes: 6 additions & 10 deletions frontend/src/components/layouts/AppLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { useInfo } from "src/hooks/useInfo";
import { useRemoveSuccessfulChannelOrder } from "src/hooks/useRemoveSuccessfulChannelOrder";
import { deleteAuthToken } from "src/lib/auth";
import { cn } from "src/lib/utils";
import { isHttpMode } from "src/utils/isHttpMode";
import { openLink } from "src/utils/openLink";
import ExternalLink from "../ExternalLink";

Expand All @@ -65,6 +66,8 @@ export default function AppLayout() {
const navigate = useNavigate();
useRemoveSuccessfulChannelOrder();

const _isHttpMode = isHttpMode();

React.useEffect(() => {
setMobileMenuOpen(false);
}, [location]);
Expand All @@ -73,19 +76,12 @@ export default function AppLayout() {
deleteAuthToken();
await refetchInfo();

const isHttpMode =
window.location.protocol.startsWith("http") &&
!window.location.hostname.startsWith("wails");
if (isHttpMode) {
if (_isHttpMode) {
window.location.href = "/logout";
} else {
navigate("/", { replace: true });
}
}, [navigate, refetchInfo]);

const isHttpMode =
window.location.protocol.startsWith("http") &&
!window.location.hostname.startsWith("wails");
}, [_isHttpMode, navigate, refetchInfo]);

if (!info) {
return null;
Expand Down Expand Up @@ -119,7 +115,7 @@ export default function AppLayout() {
)}
</DropdownMenuGroup>
<DropdownMenuSeparator />
{isHttpMode && (
{_isHttpMode && (
<DropdownMenuItem
onClick={logout}
className="w-full flex flex-row items-center gap-2 cursor-pointer"
Expand Down
7 changes: 3 additions & 4 deletions frontend/src/screens/BackupNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { LoadingButton } from "src/components/ui/loading-button";
import { useToast } from "src/components/ui/use-toast";

import { handleRequestError } from "src/utils/handleRequestError";
import { isHttpMode } from "src/utils/isHttpMode";
import { request } from "src/utils/request";

export function BackupNode() {
Expand All @@ -27,14 +28,12 @@ export function BackupNode() {
const onSubmitPassword = async (e: React.FormEvent) => {
e.preventDefault();

const isHttpMode =
window.location.protocol.startsWith("http") &&
!window.location.hostname.startsWith("wails");
const _isHttpMode = isHttpMode();

try {
setLoading(true);

if (isHttpMode) {
if (_isHttpMode) {
const response = await fetch("/api/backup", {
method: "POST",
headers: {
Expand Down
9 changes: 4 additions & 5 deletions frontend/src/screens/setup/RestoreNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useToast } from "src/components/ui/use-toast";

import { useInfo } from "src/hooks/useInfo";
import { handleRequestError } from "src/utils/handleRequestError";
import { isHttpMode } from "src/utils/isHttpMode";
import { request } from "src/utils/request";

export function RestoreNode() {
Expand All @@ -34,9 +35,7 @@ export function RestoreNode() {
const [loading, setLoading] = useState(false);
const [restored, setRestored] = useState(false);
const { data: info } = useInfo(restored);
const isHttpMode =
window.location.protocol.startsWith("http") &&
!window.location.hostname.startsWith("wails");
const _isHttpMode = isHttpMode();

React.useEffect(() => {
if (restored && info?.setupCompleted) {
Expand Down Expand Up @@ -73,7 +72,7 @@ export function RestoreNode() {
try {
setLoading(true);

if (isHttpMode) {
if (_isHttpMode) {
const formData = new FormData();
formData.append("unlockPassword", unlockPassword);
if (file !== null) {
Expand Down Expand Up @@ -130,7 +129,7 @@ export function RestoreNode() {
placeholder="Unlock Password"
/>
</div>
{isHttpMode && (
{_isHttpMode && (
<div className="grid gap-2">
<Label htmlFor="backup">Backup File</Label>
<Input
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/utils/isHttpMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const isHttpMode = () => {
return (
window.location.protocol.startsWith("http") &&
!window.location.hostname.startsWith("wails")
);
};

0 comments on commit 7ec580d

Please sign in to comment.