Skip to content

Commit

Permalink
feat: make a self-contained SPA using react-router-dom.
Browse files Browse the repository at this point in the history
  • Loading branch information
kovipu committed Aug 20, 2024
1 parent 906d326 commit bf3ff60
Show file tree
Hide file tree
Showing 18 changed files with 180 additions and 87 deletions.
42 changes: 42 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"framer-motion": "^11.3.17",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.26.1",
"tailwindcss": "^3.4.7",
"typescript": "^5.3.3"
},
Expand Down
39 changes: 39 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react';
import { Outlet, RouterProvider, createBrowserRouter } from 'react-router-dom';

import Nav from '@components/Nav';
import BorrowPage from '@pages/borrow/BorrowPage';
import LandingPage from '@pages/landing/LandingPage';
import LendPage from '@pages/lend/LendPage';
import LiquidatePage from '@pages/liquidate/LiquidatePage';

const PageWrapper = () => (
<>
<Nav />
<main className="max-w-screen w-256">
<Outlet />
</main>
</>
);

const router = createBrowserRouter([
{
element: <PageWrapper />,
children: [
{ path: '', element: <LandingPage /> },
{ path: 'lend', element: <LendPage /> },
{ path: 'borrow', element: <BorrowPage /> },
{ path: 'liquidate', element: <LiquidatePage /> },
],
},
]);

const App = () => {
return (
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
);
};

export default App;
11 changes: 6 additions & 5 deletions src/components/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PropsWithChildren } from 'react';
import { Link } from 'react-router-dom';

export interface ButtonProps {
onClick: () => void;
Expand All @@ -20,15 +21,15 @@ export const SelectButtonWrapper = ({ children }: PropsWithChildren) => (
);

export interface SelectLinkButtonProps {
href: string;
to: string;
selected: boolean;
}

export const SelectLinkButton = ({ href, selected, children }: PropsWithChildren<SelectLinkButtonProps>) => (
<a
export const SelectLinkButton = ({ to, selected, children }: PropsWithChildren<SelectLinkButtonProps>) => (
<Link
className={`font-bold px-6 py-1.5 rounded-full hover:text-black transition ${selected ? 'bg-white text-black' : 'text-white'}`}
href={href}
to={to}
>
{children}
</a>
</Link>
);
17 changes: 8 additions & 9 deletions src/components/Nav.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import { Link, useLocation } from 'react-router-dom';
import logo from '/public/laina_v3_shrinked.png';
import { SelectButtonWrapper, SelectLinkButton } from './Button';

interface NavProps {
pathname: 'index' | 'lend' | 'borrow' | 'liquidate';
}
export default function Nav() {
const { pathname } = useLocation();

export default function Nav({ pathname }: NavProps) {
return (
<nav className="relative max-w-screen-lg mx-auto mb-12 flex justify-between items-center pt-12 pb-6">
<div>
<a href="/">
<Link to="/">
<img src={logo.src} alt="logo" width={200} />
</a>
</Link>
</div>

<SelectButtonWrapper>
<SelectLinkButton href="/lend" selected={pathname === 'lend'}>
<SelectLinkButton to="/lend" selected={pathname === '/lend'}>
Lend
</SelectLinkButton>
<SelectLinkButton href="/borrow" selected={pathname === 'borrow'}>
<SelectLinkButton to="/borrow" selected={pathname === '/borrow'}>
Borrow
</SelectLinkButton>
<SelectLinkButton href="/liquidate" selected={pathname === 'liquidate'}>
<SelectLinkButton to="/liquidate" selected={pathname === '/liquidate'}>
Liquidate
</SelectLinkButton>
</SelectButtonWrapper>
Expand Down
1 change: 1 addition & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference types="astro/client" />

interface ImportMetaEnv {
Expand Down
10 changes: 2 additions & 8 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
import '@fontsource/inter';
import '@fontsource/inter/600.css';
import Nav from '../components/Nav';
interface Props {
title: string;
pathname: 'index' | 'lend' | 'borrow' | 'liquidate';
}
const { title, pathname } = Astro.props;
const { title } = Astro.props;
---

<!doctype html>
Expand All @@ -23,10 +20,7 @@ const { title, pathname } = Astro.props;
<title>{title}</title>
</head>
<body class="font-sans bg-grey-light min-h-screen">
<Nav pathname={pathname} />
<main class="max-w-full w-256">
<slot />
</main>
<slot />
</body>
</html>
<style is:global>
Expand Down
19 changes: 19 additions & 0 deletions src/pages/[...path].astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
import App from '../App.tsx';
import Layout from '../layouts/Layout.astro';
export function getStaticPaths() {
return [
{ params: { path: undefined }, props: { title: 'Laina – DeFi made simple.' } },
{ params: { path: 'lend' }, props: { title: 'Laina – Lend assets' } },
{ params: { path: 'borrow' }, props: { title: 'Laina – Borrow assets' } },
{ params: { path: 'liquidate' }, props: { title: 'Laina – Liquidate loans' } },
];
}
const { title } = Astro.props;
---

<Layout title={title}>
<App client:only />
</Layout>
10 changes: 0 additions & 10 deletions src/pages/borrow.astro

This file was deleted.

8 changes: 8 additions & 0 deletions src/pages/borrow/BorrowPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const BorrowPage = () => (
<div>
<h1 className="text-3xl font-bold mb-8">Borrow Assets</h1>
<p>Coming soon!</p>
</div>
);

export default BorrowPage;
12 changes: 0 additions & 12 deletions src/pages/index.astro

This file was deleted.

8 changes: 8 additions & 0 deletions src/pages/landing/LandingPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const LandingPage = () => (
<div>
<h1 className="text-3xl font-bold mb-8">Landing page</h1>
<p>Coming soon!</p>
</div>
);

export default LandingPage;
27 changes: 0 additions & 27 deletions src/pages/lend.astro

This file was deleted.

29 changes: 29 additions & 0 deletions src/pages/lend/LendPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import StellarIcon from '../../images/Stellar_Symbol.png';
import USDCIcon from '../../images/usdc.svg';
import { LendableAssetCard } from './LendableAssetCard';

const currencies = [
{
name: 'USD Coin',
symbol: 'USDC',
icon: USDCIcon.src,
},
{
name: 'Stellar Lumen',
symbol: 'XLM',
icon: StellarIcon.src,
},
];

const LendPage = () => {
return (
<>
<h1 className="text-3xl font-bold mb-8">Lend Assets</h1>
{currencies.map(({ name, symbol, icon }) => (
<LendableAssetCard key={name} name={name} symbol={symbol} icon={icon} />
))}
</>
);
};

export default LendPage;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button } from './Button';
import { Card } from './Card';
import { Button } from '@components/Button';
import { Card } from '@components/Card';

export interface LendableAssetCardProps {
name: string;
Expand Down
13 changes: 0 additions & 13 deletions src/pages/liquidate.astro

This file was deleted.

8 changes: 8 additions & 0 deletions src/pages/liquidate/LiquidatePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const LiquidatePage = () => (
<div>
<h1 className="text-3xl font-bold mb-8">Available for Liquidation</h1>
<p>Coming soon!</p>
</div>
);

export default LiquidatePage;
8 changes: 7 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
"exclude": ["packages"],
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "react"
"jsxImportSource": "react",
"baseUrl": ".",
"paths": {
"@images/*": ["src/images/*"],
"@components/*": ["src/components/*"],
"@pages/*": ["src/pages/*"]
}
}
}

0 comments on commit bf3ff60

Please sign in to comment.