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

feat: navbar active links #122

Merged
merged 1 commit into from
Dec 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
9 changes: 8 additions & 1 deletion src/html/components/games-link.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { requestContext } from '@fastify/request-context'
import { collections } from '../../database/collections'
import { GameState } from '../../database/models/game.model'
import { GameLiveIndicator } from './game-live-indicator'
Expand All @@ -8,8 +9,14 @@ export async function GamesLink() {
$in: [GameState.created, GameState.configuring, GameState.launching, GameState.started],
},
})
const url = requestContext.get('url')
return (
<a href="/games" class={['menu-item', gamesLiveCount > 0 && 'accent']} id="navbar-games-link">
<a
href="/games"
class={['menu-item', gamesLiveCount > 0 && 'accent']}
id="navbar-games-link"
aria-current={url === '/games' ? 'page' : undefined}
>
{gamesLiveCount > 0 ? <GameLiveIndicator /> : <></>}
Games
</a>
Expand Down
4 changes: 3 additions & 1 deletion src/html/components/navigation-bar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { requestContext } from '@fastify/request-context'
import type { User } from '../../auth/types/user'
import { GamesLink } from './games-link'
import { IconBrandDiscord, IconChartPie, IconCrown, IconHeart } from './icons'
Expand Down Expand Up @@ -71,8 +72,9 @@ function Menu(props: Html.PropsWithChildren<{ user?: User | undefined }>) {
}

function MenuItem({ href, children }: Html.PropsWithChildren<{ href: string }>) {
const url = requestContext.get('url')
return (
<a href={href} class="menu-item">
<a href={href} class="menu-item" aria-current={url === href ? 'page' : undefined}>
{children}
</a>
)
Expand Down
5 changes: 4 additions & 1 deletion src/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import fp from 'fastify-plugin'
import { z } from 'zod'
import type { ZodTypeProvider } from 'fastify-type-provider-zod'
import mime from 'mime'
import { resolve } from 'node:path'

export const csses = new Map<string, string>()

export default fp(
async app => {
await app.register((await import('./middleware/htmx')).default)
await app.register((await import('@fastify/autoload')).default, {
dir: resolve(import.meta.dirname, 'middleware'),
})

app.withTypeProvider<ZodTypeProvider>().get(
'/css/:fileName',
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions src/html/middleware/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import fp from 'fastify-plugin'

declare module '@fastify/request-context' {
interface RequestContextData {
// Current request url (e.g. /games/1234)
url: string | undefined
}
}

// eslint-disable-next-line @typescript-eslint/require-await
export default fp(async app => {
app.addHook('preHandler', async request => {
request.requestContext.set('url', request.url)
})
})
2 changes: 1 addition & 1 deletion src/html/styles/navigation-bar.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
padding: 4px 10px;
}

&.active {
&[aria-current='page'] {
background-color: theme(colors.abru.dark.20);
}

Expand Down
1 change: 0 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ await app.register(await import('@fastify/static'), {
await app.register((await import('@kitajs/fastify-html-plugin')).default)

await app.register((await import('./html')).default)
await app.register((await import('./messages')).default)
await app.register((await import('./websocket')).default)
await app.register((await import('./auth')).default)
await app.register((await import('./queue')).default)
Expand Down
Loading