diff --git a/app/dashboard/page.tsx b/app/dashboard/page.tsx index 349af06..895bdce 100644 --- a/app/dashboard/page.tsx +++ b/app/dashboard/page.tsx @@ -2,8 +2,9 @@ import Dashboard from "@/components/dashboard/dashboard"; import Content from "@/components/dashboard/content"; import { fetchServer } from '@/lib/server-service'; import { performance } from 'perf_hooks'; -import { Info } from 'lucide-react'; +import { Info, GitCommit } from 'lucide-react'; import packageJson from '../../package.json'; +import { getGitCommitInfo } from '@/lib/git-utils'; // ✅ Corrected Vendor Type interface Vendor { @@ -28,14 +29,16 @@ interface OrderStats { export default async function DashboardPage() { const startTime = performance.now(); - const [userResponse, orderStats] = await Promise.all([ + const [userResponse, orderStats, gitInfo] = await Promise.all([ fetchServer("/auth/me"), fetchServer("/orders/stats"), + getGitCommitInfo() ]); const endTime = performance.now(); const generationTime = (endTime - startTime).toFixed(2); const panelVersion = packageJson.version; + const commitHash = gitInfo.commitHash; const vendor = userResponse.vendor; @@ -46,6 +49,9 @@ export default async function DashboardPage() { v{panelVersion} | + + {commitHash} + | Generated in {generationTime}ms diff --git a/lib/git-utils.ts b/lib/git-utils.ts new file mode 100644 index 0000000..72fc3d0 --- /dev/null +++ b/lib/git-utils.ts @@ -0,0 +1,71 @@ +/** + * Git utility to load commit hash in both development and production environments + */ + +interface GitInfo { + commitHash: string; + buildTime: string; +} + +let cachedGitInfo: GitInfo | null = null; + +/** + * Gets the current git commit hash from various sources + * - First tries environment variable (from Docker) + * - Then tries the JSON file created by our build script + * - Returns "dev" if nothing is available + */ +export async function getGitCommitInfo(): Promise { + // Return cached value if available + if (cachedGitInfo) { + return cachedGitInfo; + } + + // First, check if we have the env variable (from Docker) + if (process.env.GIT_COMMIT_SHA) { + const gitInfo: GitInfo = { + commitHash: process.env.GIT_COMMIT_SHA, + buildTime: new Date().toISOString(), + }; + cachedGitInfo = gitInfo; + return gitInfo; + } + + // In the browser, fetch from the public directory + if (typeof window !== 'undefined') { + try { + const response = await fetch('/git-info.json'); + if (response.ok) { + const gitInfo: GitInfo = await response.json(); + cachedGitInfo = gitInfo; + return gitInfo; + } + } catch (error) { + console.warn('Could not fetch git info from public directory', error); + } + } else { + // On the server (during local development), try to load from the static directory + try { + // We use dynamic import to avoid bundling this in the client build + const fs = require('fs'); + const path = require('path'); + + const gitInfoPath = path.join(process.cwd(), 'public', 'git-info.json'); + + if (fs.existsSync(gitInfoPath)) { + const gitInfo: GitInfo = JSON.parse(fs.readFileSync(gitInfoPath, 'utf8')); + cachedGitInfo = gitInfo; + return gitInfo; + } + } catch (error) { + console.warn('Could not read git info from file system', error); + } + } + + // Default if nothing else works + const defaultGitInfo: GitInfo = { + commitHash: 'development', + buildTime: new Date().toISOString(), + }; + return defaultGitInfo; +} \ No newline at end of file diff --git a/package.json b/package.json index f59225d..834c4bd 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "gitCommit": "1.1.7", "private": true, "scripts": { + "predev": "node scripts/get-git-hash.js", "dev": "next dev", + "prebuild": "node scripts/get-git-hash.js", "build": "cross-env NODE_OPTIONS='--max_old_space_size=4096' NEXT_TELEMETRY_DISABLED=1 next build", "build:fast": "cross-env NODE_OPTIONS='--max_old_space_size=4096' NEXT_TELEMETRY_DISABLED=1 NEXT_SKIP_LINT=1 NEXT_SKIP_TS_CHECK=1 next build", "build:optimized": "node scripts/optimize-build.js", diff --git a/public/git-info.json b/public/git-info.json new file mode 100644 index 0000000..5063632 --- /dev/null +++ b/public/git-info.json @@ -0,0 +1,4 @@ +{ + "commitHash": "a651bd3", + "buildTime": "2025-04-06T14:47:46.087Z" +} \ No newline at end of file diff --git a/scripts/get-git-hash.js b/scripts/get-git-hash.js new file mode 100755 index 0000000..1eaa95e --- /dev/null +++ b/scripts/get-git-hash.js @@ -0,0 +1,45 @@ +#!/usr/bin/env node + +const { execSync } = require('child_process'); +const fs = require('fs'); +const path = require('path'); + +// Ensure the directory exists +const gitDataDir = path.resolve(__dirname, '../.next/static/git'); +if (!fs.existsSync(gitDataDir)) { + fs.mkdirSync(gitDataDir, { recursive: true }); +} + +let gitHash = 'unknown'; + +try { + // Try to get the git hash + gitHash = execSync('git rev-parse --short HEAD').toString().trim(); + console.log(`✅ Git commit hash: ${gitHash}`); +} catch (error) { + console.error('⚠️ Could not get git commit hash:', error.message); +} + +// Write the git hash to a file that can be imported +const gitInfoFilePath = path.resolve(gitDataDir, 'git-info.json'); +fs.writeFileSync( + gitInfoFilePath, + JSON.stringify({ + commitHash: gitHash, + buildTime: new Date().toISOString() + }, null, 2) +); + +console.log(`📝 Git info written to: ${gitInfoFilePath}`); + +// Also write to a location that can be imported during development +const gitInfoDevPath = path.resolve(__dirname, '../public/git-info.json'); +fs.writeFileSync( + gitInfoDevPath, + JSON.stringify({ + commitHash: gitHash, + buildTime: new Date().toISOString() + }, null, 2) +); + +console.log(`📝 Git info written to: ${gitInfoDevPath}`); \ No newline at end of file