Skip to content

Commit

Permalink
Merge pull request #1 from FuelLabs/sophie/gh-auth
Browse files Browse the repository at this point in the history
Add login with github button
  • Loading branch information
sdankel authored Apr 5, 2024
2 parents ba5631a + 0799aa5 commit 26c31be
Show file tree
Hide file tree
Showing 18 changed files with 1,517 additions and 157 deletions.
884 changes: 806 additions & 78 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ edition = "2021"

[dependencies]
nanoid = "0.4.0"
fs_extra = "1.2.0"
hex = "0.4.3"
tokio = { version = "1", features = ["full"] }
serde_json = "1.0.91"
regex = "1.7.0"
rocket = { version = "0.5.0-rc.2", features = ["tls", "json"] }
serde = { version = "1.0", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
reqwest = { version = "0.12.2", features = ["json"] }
thiserror = "1.0.58"
54 changes: 54 additions & 0 deletions app/package-lock.json

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

2 changes: 2 additions & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
"@types/react-dom": "^18.2.22",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.22.3",
"react-scripts": "5.0.1",
"typescript": "^4.9.5",
"usehooks-ts": "^3.0.2",
"web-vitals": "^2.1.4"
},
"scripts": {
Expand Down
75 changes: 20 additions & 55 deletions app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import React from 'react';
import React, { ReactNode } from 'react';
import AppBar from '@mui/material/AppBar/AppBar';
import Toolbar from '@mui/material/Toolbar/Toolbar';
import SearchIcon from '@mui/icons-material/Search';
import MenuIcon from '@mui/icons-material/Menu';
import Input from '@mui/material/Input/Input';
import IconButton from '@mui/material/IconButton/IconButton';
import Box from '@mui/material/Box/Box';
import AccountCircle from '@mui/icons-material/AccountCircle';
import InputAdornment from '@mui/material/InputAdornment/InputAdornment';
import { useNavigate } from 'react-router-dom';
import UserButton from './features/toolbar/components/UserButton';
import { useIsMobile } from './features/toolbar/hooks/useIsMobile';
import SearchBar from './features/toolbar/components/SearchBar';

export const FUEL_GREEN = '#00f58c';

function App() {
interface AppProps {
children?: ReactNode;
}

function App({children}: AppProps) {
const navigate = useNavigate();
const isMobile = useIsMobile();

return (
<div
style={{
Expand All @@ -23,64 +27,25 @@ function App() {
height: '100vh',
}}>
<AppBar position='static'>
<Toolbar style={{ backgroundColor: 'black' }}>
<IconButton
size='large'
edge='start'
color='inherit'
aria-label='open drawer'
sx={{ mr: 2 }}>
<MenuIcon />
</IconButton>
<Toolbar style={{ backgroundColor: '#181818' }}>
<div
style={{
flexGrow: 1,
display: 'block',
color: FUEL_GREEN,
fontSize: '24px',
fontFamily: 'monospace',
}}>
cursor: 'pointer'
}} onClick={()=>navigate('/')}>
forc.pub
</div>

<div
style={{
marginLeft: '20px',
marginRight: '10px',
height: '70%',
width: '100%',
}}>
<Input
style={{
width: '100%',
height: '100%',
backgroundColor: 'lightGrey',
}}
startAdornment={
<InputAdornment position='start' style={{ marginLeft: '10px' }}>
<SearchIcon />
</InputAdornment>
}
placeholder='Search packages and plugins'
inputProps={{ 'aria-label': 'search' }}
/>
</div>
<IconButton
size='large'
edge='end'
aria-label='account of current user'
aria-haspopup='true'
onClick={() => {}}
color='inherit'>
<AccountCircle />
</IconButton>
{!isMobile && <SearchBar />}
<UserButton />
</Toolbar>
{isMobile && <SearchBar />}
</AppBar>

<div style={{ width: '100%' }}>
<h1>{"The Sway community's package registry"}</h1>
</div>
<div style={{ color: 'red' }}>{'Under construction'}</div>
{children}
</div>
);
}
Expand Down
58 changes: 58 additions & 0 deletions app/src/features/toolbar/components/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from 'react';
import { useLocation, useNavigate, useSearchParams } from 'react-router-dom';
import { useIsMobile } from '../hooks/useIsMobile';
import InputAdornment from '@mui/material/InputAdornment/InputAdornment';
import Input from '@mui/material/Input/Input';
import SearchIcon from '@mui/icons-material/Search';

function SearchBar() {
const navigate = useNavigate();
const isMobile = useIsMobile();
const [searchParams, setSearchParams] = useSearchParams();
const {pathname} = useLocation();


return (
<div
style={{
marginLeft: isMobile ? 0 : '25px',
marginRight: '15px',
height: '70%',
width: '100%',
}}>
<Input
style={{
width: '100%',
height: '100%',
backgroundColor: 'lightGrey',
}}
startAdornment={
<InputAdornment position='start' style={{ marginLeft: '10px' }}>
<SearchIcon />
</InputAdornment>
}
placeholder='Search packages and plugins'
inputProps={{ 'aria-label': 'search' }}
onChange={(e) => {
const newSearch = e.currentTarget.value;

if (!newSearch.length) {
if (pathname === '/search') {
navigate('/');
}
searchParams.delete('q');
setSearchParams(searchParams);
} else {
if (pathname !== '/search') {
navigate('/search');
}
searchParams.set('q', newSearch);
setSearchParams(searchParams);
}
}}
/>
</div>
);
}

export default SearchBar;
115 changes: 115 additions & 0 deletions app/src/features/toolbar/components/UserButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import React, { useCallback } from 'react';
import Lock from '@mui/icons-material/Lock';
import Button from '@mui/material/Button/Button';
import styled from '@emotion/styled';
import Menu from '@mui/material/Menu/Menu';
import MenuItem from '@mui/material/MenuItem/MenuItem';
import { useNavigate } from 'react-router-dom';
import { useGithubAuth } from '../hooks/useGithubAuth';
import { useLocalSession } from '../../../utils/localStorage';
import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown';

export const GITHUB_CLIENT_ID = 'Iv1.ebdf596c6c548759';

const StyledWrapper = styled.div`
text-wrap: nowrap;
color: inherit;
`;

function UserButton() {
const { clearSessionId, sessionId } = useLocalSession();
const navigate = useNavigate();
const [user, logout] = useGithubAuth();
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);

const handleMenu = useCallback(
(event: React.MouseEvent<HTMLElement>) => {
setAnchorEl(event.currentTarget);
},
[setAnchorEl]
);

const handleClose = useCallback(() => {
setAnchorEl(null);
}, [setAnchorEl]);

const handleNavigate = useCallback(
(route: string) => {
handleClose();
navigate(route);
},
[handleClose, navigate]
);

const handleLogout = useCallback(() => {
clearSessionId();
logout();
handleNavigate('/');
}, [handleNavigate, logout, clearSessionId]);

if (user && sessionId) {
return (
<StyledWrapper>
<Button color='inherit' onClick={handleMenu} endIcon={<ArrowDropDownIcon />}>
<img
src={user.avatar_url}
title={user.name}
alt={`${user.name} (TODO: login)`}
style={{ height: '30px', width: '30px', borderRadius: '50%' }}
/>
<div style={{ marginLeft: '10px' }}>{user.name}</div>
</Button>
<Menu
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={handleClose}>
<MenuItem key='profile' onClick={() => handleNavigate('/settings')}>
Account Settings
</MenuItem>
<MenuItem key='account' onClick={handleLogout}>
Log Out
</MenuItem>
</Menu>
</StyledWrapper>
);
}

return (
<StyledWrapper>
<Button
color='inherit'
onClick={() => {
const width = 800;
const height = 1000;
const left = window.screen.width / 2 - width / 2;
const top = window.screen.height / 2 - height / 2;
let windowDimensions = [
`width=${width}`,
`height=${height}`,
`left=${left}`,
`top=${top}`,
'toolbar=0',
'scrollbars=1',
'status=1',
'resizable=1',
'location=1',
'menuBar=0',
].join(',');

window.open(
`https://github.com/login/oauth/authorize?client_id=${GITHUB_CLIENT_ID}`,
undefined,
windowDimensions
);
}}>
<>
<Lock />
<div style={{ marginLeft: '10px' }}>Log in with GitHub</div>
</>
</Button>
</StyledWrapper>
);
}

export default UserButton;
Loading

0 comments on commit 26c31be

Please sign in to comment.