generated from nhcarrigan/template
feat: show version in nav
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { readFileSync } from "fs";
|
||||
import { join } from "path";
|
||||
|
||||
interface PackageJson {
|
||||
version: string;
|
||||
}
|
||||
|
||||
let cachedVersion: string | null = null;
|
||||
|
||||
function getVersion(): string {
|
||||
if (cachedVersion) {
|
||||
return cachedVersion;
|
||||
}
|
||||
|
||||
try {
|
||||
const packageJsonPath = join(process.cwd(), "package.json");
|
||||
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8")) as PackageJson;
|
||||
cachedVersion = packageJson.version;
|
||||
return cachedVersion;
|
||||
} catch {
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
export default async function (app: FastifyInstance): Promise<void> {
|
||||
app.get("/", async (_request, reply) => {
|
||||
reply.send({ version: getVersion() });
|
||||
});
|
||||
}
|
||||
@@ -4,10 +4,11 @@
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { Component, inject } from '@angular/core';
|
||||
import { Component, inject, signal, OnInit } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { RouterModule } from '@angular/router';
|
||||
import { AuthService } from '../../services/auth.service';
|
||||
import { ApiService } from '../../services/api.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-header',
|
||||
@@ -18,6 +19,9 @@ import { AuthService } from '../../services/auth.service';
|
||||
<nav class="navbar">
|
||||
<div class="nav-brand">
|
||||
<h1><a routerLink="/">Naomi's Library</a></h1>
|
||||
@if (version()) {
|
||||
<span class="version">v{{ version() }}</span>
|
||||
}
|
||||
</div>
|
||||
|
||||
<ul class="nav-links">
|
||||
@@ -77,6 +81,13 @@ import { AuthService } from '../../services/auth.service';
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.version {
|
||||
font-size: 0.7rem;
|
||||
color: var(--witch-lavender);
|
||||
opacity: 0.8;
|
||||
margin-left: 0.5rem;
|
||||
}
|
||||
|
||||
.nav-links {
|
||||
display: flex;
|
||||
list-style: none;
|
||||
@@ -175,8 +186,17 @@ import { AuthService } from '../../services/auth.service';
|
||||
}
|
||||
`]
|
||||
})
|
||||
export class HeaderComponent {
|
||||
export class HeaderComponent implements OnInit {
|
||||
authService = inject(AuthService);
|
||||
private apiService = inject(ApiService);
|
||||
version = signal<string | null>(null);
|
||||
|
||||
ngOnInit() {
|
||||
this.apiService.get<{ version: string }>('/version').subscribe({
|
||||
next: (response) => this.version.set(response.version),
|
||||
error: () => this.version.set(null)
|
||||
});
|
||||
}
|
||||
|
||||
login() {
|
||||
this.authService.login();
|
||||
|
||||
Reference in New Issue
Block a user