Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup header #191

Merged
merged 7 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions FU.SPA/src/components/Navbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ body {
justify-content: space-between;
align-items: stretch;
gap: 2rem;
padding: 0 1rem;
font-size: 1rem;
height: 50px;
}

.left-content,
.right-content {
display: flex;
display:flex;
}

.site-title {
Expand All @@ -40,13 +41,28 @@ body {
gap: 1rem;
}

.nav button {
color: white;
background: none;
border: none;
outline: none;
font-family: inherit;
font-size: inherit;
cursor: pointer;
display: flex;
align-items: center;
padding: 0.2rem;
height: 100%;
}

.nav a {
color: inherit;
text-decoration: none;
height: 100%;
display: flex;
align-items: center;
align-items:center;
padding: 0.2rem;

}

.nav li.active {
Expand Down
42 changes: 41 additions & 1 deletion FU.SPA/src/components/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@ import { Link, useMatch, useResolvedPath } from 'react-router-dom';
import UserContext from '../context/userContext';
import { useNavigate } from 'react-router-dom';
import { useContext } from 'react';
import Avatar from '@mui/material/Avatar';

export default function Navbar() {
const { user, logout } = useContext(UserContext);
const navigate = useNavigate();

const renderTabContent = () => {
if (user) {
// if user has a custom pfp then use that, if not then use MUI instead of default pfp
const defaultPFP = user.pfpUrl.includes('https://tr.rbxcdn.com/38c6edcb50633730ff4cf39ac8859840/420/420/Hat/Png');
const pfpComponent = !defaultPFP
? <Avatar src={user.pfpUrl} sx={{ width: 33, height: 33 }} />
: <Avatar {...stringAvatar(user.username)} sx={{ width: 33, height: 33, bgcolor: stringToColor(user.username) }} />;
return (
<>
<p>{user.username}</p>
<li style={{ display: 'flex', alignItems: 'center' }}>
{pfpComponent}
</li>
<li style={{ display: 'flex', alignItems: 'center' }}>
<button onClick={logout}>Logout</button>
</li>
</>
);
} else {
Expand Down Expand Up @@ -59,3 +69,33 @@ function CustomLink({ to, children, ...props }) {
</li>
);
}
function stringToColor(string) {
let hash = 0;
let i;
for (i = 0; i < string.length; i += 1) {
hash = string.charCodeAt(i) + ((hash << 5) - hash);
}
let color = '#';
for (i = 0; i < 3; i += 1) {
const value = (hash >> (i * 8)) & 0xff;
color += `00${value.toString(16)}`.slice(-2);
}
return color;
}

function stringAvatar(name) {
// split name by spaces and filter empty entries
let nameParts = name.split(' ').filter(Boolean);
// get the first letter of the first part
let initials = nameParts[0][0];
// if there is a second part to name
if (nameParts.length > 1) {
initials += nameParts[1][0];
}
return {
sx: {
bgcolor: stringToColor(name),
},
children: initials,
};
}
2 changes: 1 addition & 1 deletion FU.SPA/src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App.jsx';
//import './index.css'
import './index.css'
// add comment
import './components/Navbar.css';
import { BrowserRouter } from 'react-router-dom';
Expand Down
Loading