Skip to content

Commit

Permalink
refactor: clean up code PE-5161
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Dec 8, 2023
1 parent 1274b4b commit f40ddc5
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 75 deletions.
22 changes: 5 additions & 17 deletions src/Router.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
import { BrowserRouter, Route, Routes } from "react-router-dom";
import { GiftForm } from "./GiftForm";
import { Page } from "./components/RouterPage";
import { GiftPage } from "./pages/GiftPage";
import { Page } from "./pages/Page";

export function Router() {
return (
<BrowserRouter basename={`${import.meta.env.BASE_URL}`}>
<Routes>
<Route
path="/gift"
element={
<Page
page={(errorCallback) => (
<GiftForm errorCallback={errorCallback} />
)}
/>
}
/>
<Route path="/gift" element={<GiftPage />} />
<Route path="/redeem" element={<Page page={() => <p>TODO</p>} />} />
<Route
path="/"
element={
/* Put gift element at baseUrl for now */
<Page
page={(errorCallback) => (
<GiftForm errorCallback={errorCallback} />
)}
/>
<GiftPage />
}
/>
<Route path="*" element={<p>404</p>} />
</Routes>
</BrowserRouter>
);
Expand Down
28 changes: 5 additions & 23 deletions src/ArDriveLogo.tsx → src/components/ArDriveLogo.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
import { JSX, useEffect, useState } from "react";
import { useIsDarkMode } from "../hooks/useIsDarkMode";

export function ArDriveLogo(): JSX.Element {
const [isDarkMode, setIsDarkMode] = useState<boolean>(() => {
// Check the current theme using matchMedia
return window.matchMedia("(prefers-color-scheme: dark)").matches;
});

useEffect(() => {
// Update the theme on changes
const mediaQueryList = window.matchMedia("(prefers-color-scheme: dark)");
const handleChange = (e: MediaQueryListEvent) => {
setIsDarkMode(e.matches);
};

mediaQueryList.addEventListener("change", handleChange);

// Cleanup event listener on component unmount
return () => {
mediaQueryList.removeEventListener("change", handleChange);
};
}, []);
export function ArDriveLogo() {
const isDarkMode = useIsDarkMode();

return (
<a href="https://ardrive.io">
Expand All @@ -28,7 +10,7 @@ export function ArDriveLogo(): JSX.Element {
);
}

function ArDriveLogoLight(): JSX.Element {
function ArDriveLogoLight() {
return (
<svg
className="ardrive-logo"
Expand Down Expand Up @@ -95,7 +77,7 @@ function ArDriveLogoLight(): JSX.Element {
);
}

function ArDriveLogoDark(): JSX.Element {
function ArDriveLogoDark() {
return (
<svg
className="ardrive-logo"
Expand Down
9 changes: 9 additions & 0 deletions src/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { termsOfServiceUrl } from "../constants";

export function Footer() {
return (
<span id="version-footer">
<a href={termsOfServiceUrl}>Terms</a> | v{import.meta.env.PACKAGE_VERSION}
</span>
);
}
2 changes: 1 addition & 1 deletion src/components/LogoHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArDriveLogo } from "../ArDriveLogo";
import { ArDriveLogo } from "./ArDriveLogo";

interface LogoHeaderProps {
errorMessage?: string;
Expand Down
23 changes: 0 additions & 23 deletions src/components/RouterPage.tsx

This file was deleted.

25 changes: 25 additions & 0 deletions src/hooks/useIsDarkMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useState, useEffect } from "react";

export function useIsDarkMode(): boolean {
const [isDarkMode, setIsDarkMode] = useState<boolean>(() => {
// Check the current theme using matchMedia
return window.matchMedia("(prefers-color-scheme: dark)").matches;
});

useEffect(() => {
// Update the theme on changes
const mediaQueryList = window.matchMedia("(prefers-color-scheme: dark)");
const handleChange = (e: MediaQueryListEvent) => {
setIsDarkMode(e.matches);
};

mediaQueryList.addEventListener("change", handleChange);

// Cleanup event listener on component unmount
return () => {
mediaQueryList.removeEventListener("change", handleChange);
};
}, []);

return isDarkMode;
}
File renamed without changes.
24 changes: 13 additions & 11 deletions src/GiftForm.tsx → src/pages/GiftPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ import {
defaultUSDAmount,
termsOfServiceUrl,
wincPerCredit,
} from "./constants";
import useDebounce from "./hooks/useDebounce";
import "./GiftForm.css";
import { useWincForOneGiB } from "./hooks/useWincForOneGiB";
import { useCreditsForFiat } from "./hooks/useCreditsForFiat";
import { getCheckoutSessionUrl } from "./utils/getCheckoutSessionUrl";

interface GiftFormProps {
errorCallback: (message: string) => void;
}
} from "../constants";
import useDebounce from "../hooks/useDebounce";
import "./GiftPage.css";
import { useWincForOneGiB } from "../hooks/useWincForOneGiB";
import { useCreditsForFiat } from "../hooks/useCreditsForFiat";
import { getCheckoutSessionUrl } from "../utils/getCheckoutSessionUrl";
import { Page } from "./Page";
import { ErrMsgCallbackAsProps } from "../types";

const maxUSDAmount = 10000;
const minUSDAmount = 5;

export function GiftForm({ errorCallback }: GiftFormProps) {
function GiftForm({ errorCallback }: ErrMsgCallbackAsProps) {
const [usdAmount, setUsdAmount] = useState<number>(defaultUSDAmount);
const [recipientEmail, setRecipientEmail] = useState<string>("");
const [isTermsAccepted, setTermsAccepted] = useState<boolean>(false);
Expand Down Expand Up @@ -206,3 +204,7 @@ export function GiftForm({ errorCallback }: GiftFormProps) {
</form>
);
}

export const GiftPage = () => (
<Page page={(e) => <GiftForm errorCallback={e} />} />
);
File renamed without changes.
21 changes: 21 additions & 0 deletions src/pages/Page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useErrorMessage } from "../hooks/useErrorMessage";
import { LogoHeader } from "../components/LogoHeader";
import "./Page.css";
import { ErrMsgCallback } from "../types";
import { Footer } from "../components/Footer";

interface PageProps {
page: (errorCallback: ErrMsgCallback) => JSX.Element;
}

export function Page({ page }: PageProps) {
const [errorMessage, errorCallback] = useErrorMessage();

return (
<>
<LogoHeader errorMessage={errorMessage} />
{page(errorCallback)}
<Footer />
</>
);
}
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type ErrMsgCallback = (error: string) => void;
export type ErrMsgCallbackAsProps = {
errorCallback: ErrMsgCallback;
};

0 comments on commit f40ddc5

Please sign in to comment.