Skip to content

Commit

Permalink
feat: extension window navigation (#21)
Browse files Browse the repository at this point in the history
* feat: extension window navigation

* remove unnecessary async fn

* remove redundant bool return

Co-authored-by: Anush <[email protected]>

---------

Co-authored-by: Anush <[email protected]>
  • Loading branch information
diivi and Anush008 authored Apr 19, 2023
1 parent 3e4dd69 commit b5ae9a6
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 107 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"matches": ["https://github.com/*"]
}
],
"permissions": ["chrome"]
"permissions": ["storage"]
}
43 changes: 0 additions & 43 deletions src/App.css

This file was deleted.

79 changes: 41 additions & 38 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
import { useState } from 'react'
import logo from './logo.svg'
import './App.css'
import { useState, useEffect } from "react";

import Start from "./pages/start";
import SignIn from "./pages/signin";
import Home from "./pages/home";
import Loading from "./pages/loading";

import { checkTokenValidity } from "./utils/fetchOpenSaucedApiData";

function App() {
const [count, setCount] = useState(0)
const [osAccessToken, setOsAccessToken] = useState("");
// renderedPage can be either "start", "home", "signin" or "loading"
const [renderedPage, setRenderedPage] = useState("loading");

useEffect(() => {
chrome.storage.sync.get(["os-access-token"], (result) => {
if (result["os-access-token"]) {
checkTokenValidity(result["os-access-token"]).then((valid) => {
if (!valid) {
setOsAccessToken("");
setRenderedPage("signin");
} else {
setOsAccessToken(result["os-access-token"]);
setRenderedPage("home");
}
});
} else {
setRenderedPage("start");
}
});
}, []);

return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>Hello Vite + React!</p>
<p>
<button type="button" onClick={() => setCount((count) => count + 1)}>
count is: {count}
</button>
</p>
<p>
Edit <code>App.tsx</code> and save to test HMR updates.
</p>
<p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
{' | '}
<a
className="App-link"
href="https://vitejs.dev/guide/features.html"
target="_blank"
rel="noopener noreferrer"
>
Vite Docs
</a>
</p>
</header>
<div className="p-4">
{renderedPage === "start" ? (
<Start setRenderedPage={setRenderedPage} />
) : renderedPage === "home" ? (
<Home osAccessToken={osAccessToken} setRenderedPage={setRenderedPage} />
) : renderedPage === "signin" ? (
<SignIn setRenderedPage={setRenderedPage} />
) : (
<Loading />
)}
</div>
)
);
}

export default App
export default App;
15 changes: 0 additions & 15 deletions src/favicon.svg

This file was deleted.

12 changes: 9 additions & 3 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
@tailwind components;
@tailwind utilities;

* {
box-sizing: border-box;
margin: 0px;
padding: 0px;
}

body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}
7 changes: 0 additions & 7 deletions src/logo.svg

This file was deleted.

12 changes: 12 additions & 0 deletions src/pages/home.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from "react";

interface HomeProps {
osAccessToken: string;
setRenderedPage: (page: string) => void;
}

function Home({ osAccessToken, setRenderedPage }: HomeProps) {
return <div>Home</div>;
}

export default Home;
7 changes: 7 additions & 0 deletions src/pages/loading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react";

function Loading() {
return <div>Loading</div>;
}

export default Loading;
16 changes: 16 additions & 0 deletions src/pages/signin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from "react";

interface SignInProps {
setRenderedPage: (page: string) => void;
}

function SignIn({ setRenderedPage }: SignInProps) {
return <div>
<h1>Sign In</h1>
<p>Sign in with your PAT</p>
<input type="text" />
<button onClick={() => setRenderedPage("home")}>Sign In</button>
</div>;
}

export default SignIn;
17 changes: 17 additions & 0 deletions src/pages/start.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react";

interface StartProps {
setRenderedPage: (page: string) => void;
}

function Start({ setRenderedPage }: StartProps) {
return (
<div>
<h1>OpenSauced</h1>
<p>Welcome Text</p>
<button onClick={() => setRenderedPage("signin")}>Get Started</button>
</div>
);
}

export default Start;
11 changes: 11 additions & 0 deletions src/utils/fetchOpenSaucedApiData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,14 @@ export const getOpenSaucedUser = async (username: string) => {
}
return await response.json();
};

export const checkTokenValidity = async (token: string) => {
const url = "https://api.opensauced.pizza/v1/auth/session";
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: `Bearer ${token}`,
},
});
return response.status === 200;
};

0 comments on commit b5ae9a6

Please sign in to comment.