Improve navigation bar

This commit is contained in:
Aelita4 2023-12-25 14:26:43 +01:00
parent fe53185779
commit 2fdb719df8
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
5 changed files with 270 additions and 157 deletions

View File

@ -2,130 +2,198 @@
import { getHighestWeightedLanguage, getLocales } from '../lib/lang/langDriver'; import { getHighestWeightedLanguage, getLocales } from '../lib/lang/langDriver';
interface Props { interface Props {
loggedIn: string; loggedIn: string;
active: string;
} }
type ShowType = "loggedInOnly" | "notLoggedInOnly" | "always" | "never";
type Position = "top" | "bottom";
interface NavElement { interface NavElement {
id: string;
title: string; title: string;
type: "simple" | "dropdown"; type: "simple" | "dropdown";
url: string; url: string;
showForLoggedIn: boolean; show: ShowType;
position: Position;
dropdowns?: Array<NavElement>; dropdowns?: Array<NavElement>;
} }
const lang = await getLocales(getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'navbar'); const lang = await getLocales(getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'navbar');
const listOfElements: Array<NavElement> = [{ const listOfElements: Array<NavElement> = [{
id: "home",
title: lang["Link_home"], title: lang["Link_home"],
type: "simple", type: "simple",
url: "/", url: "/",
showForLoggedIn: true show: "always",
position: "top"
}, { }, {
id: "about",
title: lang["Link_about"],
type: "simple",
url: "#",
show: "always",
position: "top"
}, {
id: "api",
title: lang["Link_api"],
type: "simple",
url: "#",
show: "always",
position: "top"
}, {
id: "login",
title: lang["Link_login"], title: lang["Link_login"],
type: "simple", type: "simple",
url: "/login", url: "/login",
showForLoggedIn: false show: "notLoggedInOnly",
position: "bottom"
}, { }, {
id: "register",
title: lang["Link_register"], title: lang["Link_register"],
type: "simple", type: "simple",
url: "/register", url: "/register",
showForLoggedIn: false show: "notLoggedInOnly",
position: "bottom"
},{ },{
title: lang["Link_dropdown"], id: "overview",
type: "dropdown", title: lang["Link_overview"],
url: "about:blank", type: "simple",
showForLoggedIn: true, url: "/game",
dropdowns: [{ show: "loggedInOnly",
title: "drop1", position: "bottom"
type: "simple", }, {
url: "aaa", id: "buildings",
showForLoggedIn: true title: lang["Link_buildings"],
}, { type: "simple",
title: "drop2", url: "/game/buildings",
type: "simple", show: "loggedInOnly",
url: "aaa", position: "bottom"
showForLoggedIn: true }, {
}, { id: "research",
title: "drop3", title: lang["Link_research"],
type: "simple", type: "simple",
url: "aaa", url: "/game/research",
showForLoggedIn: true show: "loggedInOnly",
}] position: "bottom"
}, {
id: "ships",
title: lang["Link_ships"],
type: "simple",
url: "/game/ships",
show: "loggedInOnly",
position: "bottom"
}, {
id: "fleet",
title: lang["Link_fleet"],
type: "simple",
url: "/game/fleet",
show: "loggedInOnly",
position: "bottom"
}, {
id: "galaxyView",
title: lang["Link_galaxyView"],
type: "simple",
url: "/game/galaxyView",
show: "loggedInOnly",
position: "bottom"
}, {
id: "profile",
title: lang["Link_profile"],
type: "simple",
url: "/game/profile",
show: "loggedInOnly",
position: "bottom"
}]; }];
const { loggedIn } = Astro.props; const { loggedIn, active } = Astro.props;
const filteredList = listOfElements.filter(element => (element.showForLoggedIn && loggedIn === "true") || loggedIn === "false"); const filteredList = listOfElements.filter(element =>
((element.show === "always" || element.show === "loggedInOnly") && loggedIn === "true") ||
((element.show === "always" || element.show === "notLoggedInOnly") && loggedIn === "false")
);
const topRow = filteredList.filter(element => element.position === "top");
const bottomRow = filteredList.filter(element => element.position === "bottom");
const username = Astro.cookies.get('username')?.value ?? "";
--- ---
<nav> <nav>
<ul> <div class="row">
{filteredList.map(element => <ul>
element.type === "dropdown" ? {topRow.map(element =>
<div class="nav-item nav-item-dropdown"> element.type === "dropdown" ?
<li><a href={element.url} class="nav-url">{element.title}</a></li> <div class="nav-item nav-item-dropdown">
<div class="nav-item-dropdown-div"> <li><a href={element.url} class="nav-url">{element.title}</a></li>
<ul>{element.dropdowns?.map(drop => <li class="nav-item-dropdown-content"><a href={drop.url} class="nav-url">{drop.title}</a></li><br />)}</ul> <div class="nav-item-dropdown-div">
</div> <ul>{element.dropdowns?.map(drop => <li class="nav-item-dropdown-content"><a href={drop.url} class={`nav-url ${active === drop.id ? "active" : ""}`}>{drop.title}</a></li><br />)}</ul>
</div> : // else </div>
<li class="nav-item"><a href={element.url} class="nav-url">{element.title}</a></li> </div> : // else
)} <li class="nav-item"><a href={element.url} class={`nav-url ${active === element.id ? "active" : ""}`}>{element.title}</a></li>
</ul> )}
<li class="nav-item"><span class="nav-username nav-keep-right">{username}</span></li>
</ul>
</div>
<div class="row">
<ul>
{bottomRow.map(element =>
element.type === "dropdown" ?
<div class="nav-item nav-item-dropdown">
<li><a href={element.url} class="nav-url">{element.title}</a></li>
<div class="nav-item-dropdown-div">
<ul>{element.dropdowns?.map(drop => <li class="nav-item-dropdown-content"><a href={drop.url} class={`nav-url ${active === drop.id ? "active" : ""}`}>{drop.title}</a></li><br />)}</ul>
</div>
</div> : // else
<li class="nav-item"><a href={element.url} class={`nav-url ${element.id === "profile" ? "nav-keep-right" : ""} ${active === element.id ? "active" : ""}`}>{element.title}</a></li>
)}
</ul>
</div>
</nav> </nav>
<style> <style>
nav { nav {
color: yellow; background-color: rgb(56, 59, 99);
background-color: gray; border-radius: 10px;
border-radius: 15px;
} }
nav ul { nav ul {
list-style: none;
display: flex; display: flex;
flex-direction: row;
padding-left: 15px; padding-left: 15px;
padding-top: 5px;
padding-bottom: 5px;
margin: 0;
} }
.nav-item { .nav-item {
flex-direction: row; list-style-type: none;
font-size: xx-large;
} }
.nav-item:not(:first-child) { .nav-item:not(:first-child) {
margin-left: 50px; margin-left: 20px;
/* color: green; */
}
.nav-item-dropdown {
color: red;
font-size: xx-large;
position: relative;
display: inline-block;
}
.nav-item-dropdown-div {
display: none;
position: absolute;
background-color: green;
}
.nav-item-dropdown-div ul {
display: flex;
flex-direction: column;
}
.nav-item-dropdown:hover .nav-item-dropdown-div {
display: inline-block;
/* position: relative; */
/* padding-top: 100px; */
}
.nav-item-dropdown:hover .nav-item-dropdown-content {
z-index: 1;
padding-top: 5px;
} }
.nav-url { .nav-url {
text-decoration: none; text-decoration: none;
color: inherit; color: green;
}
.nav-url:hover {
color: lime;
}
.nav-username {
color: white;
}
.nav-keep-right {
position: absolute;
right: 20px;
}
.active {
color: lime;
} }
</style> </style>

View File

@ -1,8 +1,17 @@
{ {
"Link": { "Link": {
"home": "home", "home": "[home]",
"login": "login", "login": "[login]",
"register": "register", "register": "[register]",
"dropdown": "dropdown" "about": "[about]",
"api": "[api]",
"overview": "[overview]",
"buildings": "[buildings]",
"research": "[research]",
"ships": "[ships]",
"fleet": "[fleet]",
"galaxyView": "[galaxy view]",
"profile": "[profile]"
} }
} }

View File

@ -0,0 +1,94 @@
---
import Layout from '../../layouts/Layout.astro';
import NavBar from '../../components/NavBar.astro';
import { getAllUsers } from '../../lib/db/users';
import { getHighestWeightedLanguage, getLocales } from '../../lib/lang/langDriver';
import format from '../../lib/utils/format';
const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null;
const username = Astro.cookies.get('username')?.value ?? "";
if(loggedToken === null || username === "") return Astro.redirect('/');
const allUsers = await getAllUsers();
const langGame = await getLocales(getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'game');
---
<Layout title="Galaxy view">
<NavBar loggedIn="true" active="galaxyView" />
<h1>{format(langGame['Header_user'], username)}</h1>
<h1>Galaxy view</h1>
<ul>
{allUsers.map(user => <li>{user.username}</li>)}
</ul>
</Layout>
<style>
* {
color: white;
}
main {
margin: auto;
padding: 1rem;
width: 800px;
max-width: calc(100% - 2rem);
color: white;
font-size: 20px;
line-height: 1.6;
}
.astro-a {
position: absolute;
top: -32px;
left: 50%;
transform: translatex(-50%);
width: 220px;
height: auto;
z-index: -1;
}
h1 {
font-size: 4rem;
font-weight: 700;
line-height: 1;
text-align: center;
margin-bottom: 1em;
}
.text-gradient {
background-image: var(--accent-gradient);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-size: 400%;
background-position: 0%;
}
.instructions {
margin-bottom: 2rem;
border: 1px solid rgba(var(--accent-light), 25%);
background: linear-gradient(rgba(var(--accent-dark), 66%), rgba(var(--accent-dark), 33%));
padding: 1.5rem;
border-radius: 8px;
}
.instructions code {
font-size: 0.8em;
font-weight: bold;
background: rgba(var(--accent-light), 12%);
color: rgb(var(--accent-light));
border-radius: 4px;
padding: 0.3em 0.4em;
}
.instructions strong {
color: rgb(var(--accent-light));
}
.link-card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(24ch, 1fr));
gap: 2rem;
padding: 0;
}
</style>

View File

@ -4,10 +4,6 @@ import NavBar from '../../components/NavBar.astro';
import { getUserResources, updateUserResources } from '../../lib/db/users'; import { getUserResources, updateUserResources } from '../../lib/db/users';
import { getHighestWeightedLanguage, getLocales } from '../../lib/lang/langDriver'; import { getHighestWeightedLanguage, getLocales } from '../../lib/lang/langDriver';
String.prototype.format = function() {
return [...arguments].reduce((p,c) => p.replace(/{}/,c), this);
};
const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null; const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null;
const username = Astro.cookies.get('username')?.value ?? ""; const username = Astro.cookies.get('username')?.value ?? "";
@ -22,23 +18,15 @@ await updateUserResources(username, {
}); });
const langResources = await getLocales(getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'resources'); const langResources = await getLocales(getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'resources');
const langGame = await getLocales(getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'game');
--- ---
<Layout title="chujów sto"> <Layout title="chujów sto">
<NavBar loggedIn="true" /> <NavBar loggedIn="true" active="overview" />
<a href="/logout" style="color: pink;">{langGame['Link_logout']}</a>
<h1>{langGame['Header_user'].format(username)}</h1>
<ul> <ul>
<li>{langResources['Label_coal']}: <span id="coal">{resources.coal * 2}</span></li> <li>{langResources['Label_coal']}: <span id="coal">{resources.coal * 2}</span></li>
<li>{langResources['Label_iron']}: <span id="iron">{resources.iron * 3}</span></li> <li>{langResources['Label_iron']}: <span id="iron">{resources.iron * 3}</span></li>
<li>{langResources['Label_gold']}: <span id="gold">{resources.gold * 4}</span></li> <li>{langResources['Label_gold']}: <span id="gold">{resources.gold * 4}</span></li>
</ul> </ul>
<ul>
<li><a href="#">{langGame['Link_build'].format('iron mine')}</a></li>
<li><a href="#">{langGame['Link_build'].format('gold mine')}</a></li>
</ul>
</Layout> </Layout>
<style> <style>
@ -55,6 +43,7 @@ const langGame = await getLocales(getHighestWeightedLanguage(Astro.request.heade
font-size: 20px; font-size: 20px;
line-height: 1.6; line-height: 1.6;
} }
.astro-a { .astro-a {
position: absolute; position: absolute;
top: -32px; top: -32px;
@ -64,13 +53,15 @@ const langGame = await getLocales(getHighestWeightedLanguage(Astro.request.heade
height: auto; height: auto;
z-index: -1; z-index: -1;
} }
h1 {
font-size: 4rem; h3 {
font-size: 2rem;
font-weight: 700; font-weight: 700;
line-height: 1; line-height: 1;
text-align: center; text-align: center;
margin-bottom: 1em; margin-bottom: 1em;
} }
.text-gradient { .text-gradient {
background-image: var(--accent-gradient); background-image: var(--accent-gradient);
-webkit-background-clip: text; -webkit-background-clip: text;
@ -78,6 +69,7 @@ const langGame = await getLocales(getHighestWeightedLanguage(Astro.request.heade
background-size: 400%; background-size: 400%;
background-position: 0%; background-position: 0%;
} }
.instructions { .instructions {
margin-bottom: 2rem; margin-bottom: 2rem;
border: 1px solid rgba(var(--accent-light), 25%); border: 1px solid rgba(var(--accent-light), 25%);
@ -85,6 +77,7 @@ const langGame = await getLocales(getHighestWeightedLanguage(Astro.request.heade
padding: 1.5rem; padding: 1.5rem;
border-radius: 8px; border-radius: 8px;
} }
.instructions code { .instructions code {
font-size: 0.8em; font-size: 0.8em;
font-weight: bold; font-weight: bold;
@ -93,9 +86,11 @@ const langGame = await getLocales(getHighestWeightedLanguage(Astro.request.heade
border-radius: 4px; border-radius: 4px;
padding: 0.3em 0.4em; padding: 0.3em 0.4em;
} }
.instructions strong { .instructions strong {
color: rgb(var(--accent-light)); color: rgb(var(--accent-light));
} }
.link-card-grid { .link-card-grid {
display: grid; display: grid;
grid-template-columns: repeat(auto-fit, minmax(24ch, 1fr)); grid-template-columns: repeat(auto-fit, minmax(24ch, 1fr));
@ -104,7 +99,6 @@ const langGame = await getLocales(getHighestWeightedLanguage(Astro.request.heade
} }
</style> </style>
<script> <script>
setInterval(() => { setInterval(() => {
const coal = document.querySelector('#coal'); const coal = document.querySelector('#coal');
const iron = document.querySelector('#iron'); const iron = document.querySelector('#iron');

View File

@ -1,6 +1,5 @@
--- ---
import Layout from '../layouts/Layout.astro'; import Layout from '../layouts/Layout.astro';
// import Card from '../components/Card.astro';
import NavBar from '../components/NavBar.astro'; import NavBar from '../components/NavBar.astro';
const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null; const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null;
@ -10,66 +9,8 @@ if(loggedToken === null) return Astro.redirect('/login');
--- ---
<Layout title="Welcome to Astro."> <Layout title="Welcome to Astro.">
<NavBar loggedIn="true" /> <NavBar loggedIn="true" active="home" />
<!-- <main>
<svg
class="astro-a"
width="495"
height="623"
viewBox="0 0 495 623"
fill="none"
xmlns="http://www.w3.org/2000/svg"
aria-hidden="true"
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
d="M167.19 364.254C83.4786 364.254 0 404.819 0 404.819C0 404.819 141.781 19.4876 142.087 18.7291C146.434 7.33701 153.027 0 162.289 0H332.441C341.703 0 348.574 7.33701 352.643 18.7291C352.92 19.5022 494.716 404.819 494.716 404.819C494.716 404.819 426.67 364.254 327.525 364.254L264.41 169.408C262.047 159.985 255.147 153.581 247.358 153.581C239.569 153.581 232.669 159.985 230.306 169.408L167.19 364.254ZM160.869 530.172C160.877 530.18 160.885 530.187 160.894 530.195L160.867 530.181C160.868 530.178 160.868 530.175 160.869 530.172ZM136.218 411.348C124.476 450.467 132.698 504.458 160.869 530.172C160.997 529.696 161.125 529.242 161.248 528.804C161.502 527.907 161.737 527.073 161.917 526.233C165.446 509.895 178.754 499.52 195.577 500.01C211.969 500.487 220.67 508.765 223.202 527.254C224.141 534.12 224.23 541.131 224.319 548.105C224.328 548.834 224.337 549.563 224.347 550.291C224.563 566.098 228.657 580.707 237.264 593.914C245.413 606.426 256.108 615.943 270.749 622.478C270.593 621.952 270.463 621.508 270.35 621.126C270.045 620.086 269.872 619.499 269.685 618.911C258.909 585.935 266.668 563.266 295.344 543.933C298.254 541.971 301.187 540.041 304.12 538.112C310.591 533.854 317.059 529.599 323.279 525.007C345.88 508.329 360.09 486.327 363.431 457.844C364.805 446.148 363.781 434.657 359.848 423.275C358.176 424.287 356.587 425.295 355.042 426.275C351.744 428.366 348.647 430.33 345.382 431.934C303.466 452.507 259.152 455.053 214.03 448.245C184.802 443.834 156.584 436.019 136.218 411.348Z"
fill="url(#paint0_linear_1805_24383)"></path>
<defs>
<linearGradient
id="paint0_linear_1805_24383"
x1="247.358"
y1="0"
x2="247.358"
y2="622.479"
gradientUnits="userSpaceOnUse"
>
<stop stop-opacity="0.9"></stop>
<stop offset="1" stop-opacity="0.2"></stop>
</linearGradient>
</defs>
</svg>
<h1>Welcome to <span class="text-gradient">Astro</span></h1>
<p class="instructions">
To get started, open the directory <code>src/pages</code> in your project.<br />
<strong>Code Challenge:</strong> Tweak the "Welcome to Astro" message above.
</p>
<ul role="list" class="link-card-grid">
<Card
href="https://docs.astro.build/"
title="Documentation"
body="Learn how Astro works and explore the official API docs."
/>
<Card
href="https://astro.build/integrations/"
title="Integrations"
body="Supercharge your project with new frameworks and libraries."
/>
<Card
href="https://astro.build/themes/"
title="Themes"
body="Explore a galaxy of community-built starter themes."
/>
<Card
href="https://astro.build/chat/"
title="Community"
body="Come say hi to our amazing Discord community. ❤️"
/>
</ul>
</main> -->
</Layout> </Layout>
<style> <style>
main { main {
margin: auto; margin: auto;
@ -80,6 +21,7 @@ if(loggedToken === null) return Astro.redirect('/login');
font-size: 20px; font-size: 20px;
line-height: 1.6; line-height: 1.6;
} }
.astro-a { .astro-a {
position: absolute; position: absolute;
top: -32px; top: -32px;
@ -89,6 +31,7 @@ if(loggedToken === null) return Astro.redirect('/login');
height: auto; height: auto;
z-index: -1; z-index: -1;
} }
h1 { h1 {
font-size: 4rem; font-size: 4rem;
font-weight: 700; font-weight: 700;
@ -96,6 +39,7 @@ if(loggedToken === null) return Astro.redirect('/login');
text-align: center; text-align: center;
margin-bottom: 1em; margin-bottom: 1em;
} }
.text-gradient { .text-gradient {
background-image: var(--accent-gradient); background-image: var(--accent-gradient);
-webkit-background-clip: text; -webkit-background-clip: text;
@ -103,6 +47,7 @@ if(loggedToken === null) return Astro.redirect('/login');
background-size: 400%; background-size: 400%;
background-position: 0%; background-position: 0%;
} }
.instructions { .instructions {
margin-bottom: 2rem; margin-bottom: 2rem;
border: 1px solid rgba(var(--accent-light), 25%); border: 1px solid rgba(var(--accent-light), 25%);
@ -110,6 +55,7 @@ if(loggedToken === null) return Astro.redirect('/login');
padding: 1.5rem; padding: 1.5rem;
border-radius: 8px; border-radius: 8px;
} }
.instructions code { .instructions code {
font-size: 0.8em; font-size: 0.8em;
font-weight: bold; font-weight: bold;
@ -118,9 +64,11 @@ if(loggedToken === null) return Astro.redirect('/login');
border-radius: 4px; border-radius: 4px;
padding: 0.3em 0.4em; padding: 0.3em 0.4em;
} }
.instructions strong { .instructions strong {
color: rgb(var(--accent-light)); color: rgb(var(--accent-light));
} }
.link-card-grid { .link-card-grid {
display: grid; display: grid;
grid-template-columns: repeat(auto-fit, minmax(24ch, 1fr)); grid-template-columns: repeat(auto-fit, minmax(24ch, 1fr));