version-checker.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use client';
  2. import { useState, useEffect } from 'react';
  3. import { AlertCircle, RefreshCw } from 'lucide-react';
  4. import { Button } from './ui/button';
  5. interface VersionInfo {
  6. version: string;
  7. buildTime: string;
  8. }
  9. export function VersionChecker() {
  10. const [currentVersion, setCurrentVersion] = useState<string | null>(null);
  11. const [serverVersion, setServerVersion] = useState<string | null>(null);
  12. const [showUpdate, setShowUpdate] = useState(false);
  13. useEffect(() => {
  14. // Get initial version from version.json
  15. const loadCurrentVersion = async () => {
  16. try {
  17. const response = await fetch('/ui/version.json', {
  18. cache: 'no-store' // Always fetch fresh version
  19. });
  20. if (response.ok) {
  21. const data: VersionInfo = await response.json();
  22. setCurrentVersion(data.version);
  23. }
  24. } catch (error) {
  25. console.warn('Failed to load UI version:', error);
  26. }
  27. };
  28. loadCurrentVersion();
  29. }, []);
  30. useEffect(() => {
  31. // Check server version periodically (every 5 minutes)
  32. const checkVersion = async () => {
  33. try {
  34. // Check if __SERVER_CONFIG__ is available
  35. if (typeof window !== 'undefined' && (window as any).__SERVER_CONFIG__) {
  36. const config = (window as any).__SERVER_CONFIG__;
  37. const version = config.uiVersion;
  38. if (version && version !== 'unknown') {
  39. setServerVersion(version);
  40. // If we have both versions and they don't match, show update notification
  41. if (currentVersion && version !== currentVersion) {
  42. setShowUpdate(true);
  43. }
  44. }
  45. }
  46. } catch (error) {
  47. console.warn('Failed to check server version:', error);
  48. }
  49. };
  50. // Initial check after 2 seconds
  51. const initialTimeout = setTimeout(checkVersion, 2000);
  52. // Periodic check every 5 minutes
  53. const interval = setInterval(checkVersion, 5 * 60 * 1000);
  54. return () => {
  55. clearTimeout(initialTimeout);
  56. clearInterval(interval);
  57. };
  58. }, [currentVersion]);
  59. const handleRefresh = () => {
  60. // Force reload to get new version
  61. window.location.reload();
  62. };
  63. if (!showUpdate) {
  64. return null;
  65. }
  66. return (
  67. <div className="fixed top-4 left-1/2 transform -translate-x-1/2 z-50 animate-in slide-in-from-top duration-300">
  68. <div className="bg-amber-500 dark:bg-amber-600 text-white px-4 py-3 rounded-lg shadow-lg flex items-center gap-3 max-w-md">
  69. <AlertCircle className="h-5 w-5 flex-shrink-0" />
  70. <div className="flex-1">
  71. <p className="font-semibold">New UI Version Available</p>
  72. <p className="text-sm opacity-90">
  73. A new version of the UI has been deployed. Refresh to get the latest updates.
  74. </p>
  75. </div>
  76. <Button
  77. size="sm"
  78. variant="secondary"
  79. onClick={handleRefresh}
  80. className="flex-shrink-0"
  81. >
  82. <RefreshCw className="h-4 w-4 mr-1" />
  83. Refresh
  84. </Button>
  85. </div>
  86. </div>
  87. );
  88. }