page.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. 'use client';
  2. import { useEffect, useState } from 'react';
  3. import Link from 'next/link';
  4. import { Header } from '@/components/header';
  5. import { AppLayout } from '@/components/layout';
  6. import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
  7. import { Button } from '@/components/ui/button';
  8. import { apiClient } from '@/lib/api';
  9. import { ProtectedRoute } from '@/components/auth/protected-route';
  10. import { useAuth } from '@/lib/auth-context';
  11. import { ImagePlus, Image, Sparkles, Settings, Activity, ArrowRight, CheckCircle2, XCircle, User, LogOut } from 'lucide-react';
  12. const features = [
  13. {
  14. title: 'Text to Image',
  15. description: 'Generate stunning images from text descriptions using Stable Diffusion',
  16. icon: ImagePlus,
  17. href: '/text2img',
  18. color: 'text-blue-500',
  19. },
  20. {
  21. title: 'Image to Image',
  22. description: 'Transform existing images with AI-powered modifications',
  23. icon: Image,
  24. href: '/img2img',
  25. color: 'text-purple-500',
  26. },
  27. {
  28. title: 'Upscaler',
  29. description: 'Enhance and upscale your images for higher quality results',
  30. icon: Sparkles,
  31. href: '/upscaler',
  32. color: 'text-green-500',
  33. },
  34. {
  35. title: 'Model Management',
  36. description: 'Load, unload, and manage your AI models efficiently',
  37. icon: Settings,
  38. href: '/models',
  39. color: 'text-orange-500',
  40. },
  41. {
  42. title: 'Queue Monitor',
  43. description: 'Track and manage your generation jobs in real-time',
  44. icon: Activity,
  45. href: '/queue',
  46. color: 'text-pink-500',
  47. },
  48. ];
  49. export default function HomePage() {
  50. const { user, logout, isAuthenticated, isLoading } = useAuth();
  51. const [health, setHealth] = useState<'checking' | 'healthy' | 'error'>('checking');
  52. const [systemInfo, setSystemInfo] = useState<{ version?: string; cuda_available?: boolean } | null>(null);
  53. const checkHealth = async () => {
  54. try {
  55. await apiClient.getHealth();
  56. setTimeout(() => setHealth('healthy'), 0);
  57. } catch (err) {
  58. console.error('Health check failed:', err);
  59. setTimeout(() => setHealth('error'), 0);
  60. }
  61. };
  62. const loadSystemInfo = async () => {
  63. try {
  64. const info = await apiClient.getSystemInfo();
  65. setTimeout(() => setSystemInfo(info), 0);
  66. } catch (err) {
  67. console.error('Failed to load system info:', err);
  68. }
  69. };
  70. useEffect(() => {
  71. // Check health and system info regardless of authentication status
  72. checkHealth();
  73. loadSystemInfo();
  74. // Set up periodic health checks
  75. const healthInterval = setInterval(checkHealth, 30000); // Check every 30 seconds
  76. const systemInfoInterval = setInterval(loadSystemInfo, 60000); // Check every minute
  77. return () => {
  78. clearInterval(healthInterval);
  79. clearInterval(systemInfoInterval);
  80. };
  81. }, []);
  82. // Show loading state while checking authentication
  83. if (isLoading) {
  84. return (
  85. <div className="flex items-center justify-center min-h-screen">
  86. <div className="text-center">
  87. <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary mx-auto mb-4"></div>
  88. <p className="text-muted-foreground">Loading...</p>
  89. </div>
  90. </div>
  91. );
  92. }
  93. return (
  94. <ProtectedRoute>
  95. <AppLayout>
  96. <Header
  97. title="Stable Diffusion REST"
  98. description="Modern web interface for AI image generation"
  99. actions={
  100. <div className="flex items-center gap-4">
  101. <div className="flex items-center gap-2 text-sm">
  102. <User className="h-4 w-4" />
  103. <span>{user?.username}</span>
  104. <span className="text-muted-foreground">({user?.role})</span>
  105. </div>
  106. <Button variant="outline" size="sm" onClick={logout}>
  107. <LogOut className="h-4 w-4 mr-2" />
  108. Sign Out
  109. </Button>
  110. <Link href="/settings">
  111. <Button variant="outline" size="sm">
  112. <Settings className="h-4 w-4 mr-2" />
  113. Settings
  114. </Button>
  115. </Link>
  116. </div>
  117. }
  118. />
  119. <div className="container mx-auto p-6">
  120. <div className="space-y-8">
  121. {/* Status Banner */}
  122. <Card>
  123. <CardContent className="flex items-center justify-between p-6">
  124. <div className="flex items-center gap-3">
  125. {health === 'checking' ? (
  126. <>
  127. <div className="h-3 w-3 animate-pulse rounded-full bg-yellow-500" />
  128. <span className="text-sm">Checking API status...</span>
  129. </>
  130. ) : health === 'healthy' ? (
  131. <>
  132. <CheckCircle2 className="h-5 w-5 text-green-500" />
  133. <span className="text-sm font-medium">API is running</span>
  134. </>
  135. ) : (
  136. <>
  137. <XCircle className="h-5 w-5 text-red-500" />
  138. <span className="text-sm font-medium text-destructive">
  139. Cannot connect to API
  140. </span>
  141. </>
  142. )}
  143. </div>
  144. {systemInfo && (
  145. <div className="flex gap-4 text-sm text-muted-foreground">
  146. {systemInfo.version && <span>v{systemInfo.version}</span>}
  147. {systemInfo.cuda_available !== undefined && (
  148. <span>CUDA: {systemInfo.cuda_available ? 'Available' : 'Not Available'}</span>
  149. )}
  150. </div>
  151. )}
  152. </CardContent>
  153. </Card>
  154. {/* Welcome Section */}
  155. <div className="text-center">
  156. <h1 className="text-4xl font-bold tracking-tight">Welcome to SD REST UI</h1>
  157. <p className="mt-4 text-lg text-muted-foreground">
  158. A modern, intuitive interface for Stable Diffusion image generation
  159. </p>
  160. </div>
  161. {/* Features Grid */}
  162. <div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
  163. {features.map((feature) => (
  164. <Card key={feature.href} className="group relative overflow-hidden transition-all hover:shadow-lg">
  165. <CardHeader>
  166. <div className="flex items-center gap-3">
  167. <div className={`rounded-lg bg-muted p-2 ${feature.color}`}>
  168. <feature.icon className="h-6 w-6" />
  169. </div>
  170. <CardTitle className="text-xl">{feature.title}</CardTitle>
  171. </div>
  172. <CardDescription className="mt-2">{feature.description}</CardDescription>
  173. </CardHeader>
  174. <CardContent>
  175. <Link href={feature.href}>
  176. <Button variant="ghost" className="w-full justify-between group-hover:bg-accent">
  177. Get Started
  178. <ArrowRight className="h-4 w-4 transition-transform group-hover:translate-x-1" />
  179. </Button>
  180. </Link>
  181. </CardContent>
  182. </Card>
  183. ))}
  184. </div>
  185. {/* Quick Start Guide */}
  186. <Card>
  187. <CardHeader>
  188. <CardTitle>Quick Start Guide</CardTitle>
  189. <CardDescription>Get started with image generation in a few simple steps</CardDescription>
  190. </CardHeader>
  191. <CardContent className="space-y-4">
  192. <div className="flex gap-4">
  193. <div className="flex h-8 w-8 items-center justify-center rounded-full bg-primary text-primary-foreground font-semibold">
  194. 1
  195. </div>
  196. <div className="flex-1">
  197. <h4 className="font-medium">Load a Model</h4>
  198. <p className="text-sm text-muted-foreground">
  199. Navigate to <Link href="/models" className="text-primary hover:underline">Model Management</Link> and load your preferred checkpoint
  200. </p>
  201. </div>
  202. </div>
  203. <div className="flex gap-4">
  204. <div className="flex h-8 w-8 items-center justify-center rounded-full bg-primary text-primary-foreground font-semibold">
  205. 2
  206. </div>
  207. <div className="flex-1">
  208. <h4 className="font-medium">Choose Generation Type</h4>
  209. <p className="text-sm text-muted-foreground">
  210. Select <Link href="/text2img" className="text-primary hover:underline">Text to Image</Link>, <Link href="/img2img" className="text-primary hover:underline">Image to Image</Link>, or <Link href="/upscaler" className="text-primary hover:underline">Upscaler</Link>
  211. </p>
  212. </div>
  213. </div>
  214. <div className="flex gap-4">
  215. <div className="flex h-8 w-8 items-center justify-center rounded-full bg-primary text-primary-foreground font-semibold">
  216. 3
  217. </div>
  218. <div className="flex-1">
  219. <h4 className="font-medium">Generate & Monitor</h4>
  220. <p className="text-sm text-muted-foreground">
  221. Start generation and track progress in the <Link href="/queue" className="text-primary hover:underline">Queue Monitor</Link>
  222. </p>
  223. </div>
  224. </div>
  225. </CardContent>
  226. </Card>
  227. {/* API Info */}
  228. <Card>
  229. <CardHeader>
  230. <CardTitle>API Configuration</CardTitle>
  231. <CardDescription>Backend API connection details</CardDescription>
  232. </CardHeader>
  233. <CardContent>
  234. <dl className="grid gap-3 text-sm">
  235. <div className="flex justify-between">
  236. <dt className="text-muted-foreground">API URL:</dt>
  237. <dd className="font-mono">{process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8080'}</dd>
  238. </div>
  239. <div className="flex justify-between">
  240. <dt className="text-muted-foreground">Base Path:</dt>
  241. <dd className="font-mono">{process.env.NEXT_PUBLIC_API_BASE_PATH || '/api/v1'}</dd>
  242. </div>
  243. <div className="flex justify-between">
  244. <dt className="text-muted-foreground">Status:</dt>
  245. <dd>
  246. {health === 'healthy' ? (
  247. <span className="text-green-600 dark:text-green-400">Connected</span>
  248. ) : health === 'error' ? (
  249. <span className="text-destructive">Disconnected</span>
  250. ) : (
  251. <span className="text-yellow-600 dark:text-yellow-400">Checking...</span>
  252. )}
  253. </dd>
  254. </div>
  255. </dl>
  256. </CardContent>
  257. </Card>
  258. </div>
  259. </div>
  260. </AppLayout>
  261. </ProtectedRoute>
  262. );
  263. }