page.tsx 11 KB

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