page.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. // Only check health and system info if authenticated
  72. if (isAuthenticated) {
  73. checkHealth();
  74. loadSystemInfo();
  75. }
  76. }, [isAuthenticated]);
  77. // Show loading state while checking authentication
  78. if (isLoading) {
  79. return (
  80. <div className="flex items-center justify-center min-h-screen">
  81. <div className="text-center">
  82. <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary mx-auto mb-4"></div>
  83. <p className="text-muted-foreground">Loading...</p>
  84. </div>
  85. </div>
  86. );
  87. }
  88. return (
  89. <ProtectedRoute>
  90. <AppLayout>
  91. <Header
  92. title="Stable Diffusion REST"
  93. description="Modern web interface for AI image generation"
  94. actions={
  95. <div className="flex items-center gap-4">
  96. <div className="flex items-center gap-2 text-sm">
  97. <User className="h-4 w-4" />
  98. <span>{user?.username}</span>
  99. <span className="text-muted-foreground">({user?.role})</span>
  100. </div>
  101. <Button variant="outline" size="sm" onClick={logout}>
  102. <LogOut className="h-4 w-4 mr-2" />
  103. Sign Out
  104. </Button>
  105. <Link href="/settings">
  106. <Button variant="outline" size="sm">
  107. <Settings className="h-4 w-4 mr-2" />
  108. Settings
  109. </Button>
  110. </Link>
  111. </div>
  112. }
  113. />
  114. <div className="container mx-auto p-6">
  115. <div className="space-y-8">
  116. {/* Status Banner */}
  117. <Card>
  118. <CardContent className="flex items-center justify-between p-6">
  119. <div className="flex items-center gap-3">
  120. {health === 'checking' ? (
  121. <>
  122. <div className="h-3 w-3 animate-pulse rounded-full bg-yellow-500" />
  123. <span className="text-sm">Checking API status...</span>
  124. </>
  125. ) : health === 'healthy' ? (
  126. <>
  127. <CheckCircle2 className="h-5 w-5 text-green-500" />
  128. <span className="text-sm font-medium">API is running</span>
  129. </>
  130. ) : (
  131. <>
  132. <XCircle className="h-5 w-5 text-red-500" />
  133. <span className="text-sm font-medium text-destructive">
  134. Cannot connect to API
  135. </span>
  136. </>
  137. )}
  138. </div>
  139. {systemInfo && (
  140. <div className="flex gap-4 text-sm text-muted-foreground">
  141. {systemInfo.version && <span>v{systemInfo.version}</span>}
  142. {systemInfo.cuda_available !== undefined && (
  143. <span>CUDA: {systemInfo.cuda_available ? 'Available' : 'Not Available'}</span>
  144. )}
  145. </div>
  146. )}
  147. </CardContent>
  148. </Card>
  149. {/* Welcome Section */}
  150. <div className="text-center">
  151. <h1 className="text-4xl font-bold tracking-tight">Welcome to SD REST UI</h1>
  152. <p className="mt-4 text-lg text-muted-foreground">
  153. A modern, intuitive interface for Stable Diffusion image generation
  154. </p>
  155. </div>
  156. {/* Features Grid */}
  157. <div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
  158. {features.map((feature) => (
  159. <Card key={feature.href} className="group relative overflow-hidden transition-all hover:shadow-lg">
  160. <CardHeader>
  161. <div className="flex items-center gap-3">
  162. <div className={`rounded-lg bg-muted p-2 ${feature.color}`}>
  163. <feature.icon className="h-6 w-6" />
  164. </div>
  165. <CardTitle className="text-xl">{feature.title}</CardTitle>
  166. </div>
  167. <CardDescription className="mt-2">{feature.description}</CardDescription>
  168. </CardHeader>
  169. <CardContent>
  170. <Link href={feature.href}>
  171. <Button variant="ghost" className="w-full justify-between group-hover:bg-accent">
  172. Get Started
  173. <ArrowRight className="h-4 w-4 transition-transform group-hover:translate-x-1" />
  174. </Button>
  175. </Link>
  176. </CardContent>
  177. </Card>
  178. ))}
  179. </div>
  180. {/* Quick Start Guide */}
  181. <Card>
  182. <CardHeader>
  183. <CardTitle>Quick Start Guide</CardTitle>
  184. <CardDescription>Get started with image generation in a few simple steps</CardDescription>
  185. </CardHeader>
  186. <CardContent className="space-y-4">
  187. <div className="flex gap-4">
  188. <div className="flex h-8 w-8 items-center justify-center rounded-full bg-primary text-primary-foreground font-semibold">
  189. 1
  190. </div>
  191. <div className="flex-1">
  192. <h4 className="font-medium">Load a Model</h4>
  193. <p className="text-sm text-muted-foreground">
  194. Navigate to <Link href="/models" className="text-primary hover:underline">Model Management</Link> and load your preferred checkpoint
  195. </p>
  196. </div>
  197. </div>
  198. <div className="flex gap-4">
  199. <div className="flex h-8 w-8 items-center justify-center rounded-full bg-primary text-primary-foreground font-semibold">
  200. 2
  201. </div>
  202. <div className="flex-1">
  203. <h4 className="font-medium">Choose Generation Type</h4>
  204. <p className="text-sm text-muted-foreground">
  205. 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>
  206. </p>
  207. </div>
  208. </div>
  209. <div className="flex gap-4">
  210. <div className="flex h-8 w-8 items-center justify-center rounded-full bg-primary text-primary-foreground font-semibold">
  211. 3
  212. </div>
  213. <div className="flex-1">
  214. <h4 className="font-medium">Generate & Monitor</h4>
  215. <p className="text-sm text-muted-foreground">
  216. Start generation and track progress in the <Link href="/queue" className="text-primary hover:underline">Queue Monitor</Link>
  217. </p>
  218. </div>
  219. </div>
  220. </CardContent>
  221. </Card>
  222. {/* API Info */}
  223. <Card>
  224. <CardHeader>
  225. <CardTitle>API Configuration</CardTitle>
  226. <CardDescription>Backend API connection details</CardDescription>
  227. </CardHeader>
  228. <CardContent>
  229. <dl className="grid gap-3 text-sm">
  230. <div className="flex justify-between">
  231. <dt className="text-muted-foreground">API URL:</dt>
  232. <dd className="font-mono">{process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8080'}</dd>
  233. </div>
  234. <div className="flex justify-between">
  235. <dt className="text-muted-foreground">Base Path:</dt>
  236. <dd className="font-mono">{process.env.NEXT_PUBLIC_API_BASE_PATH || '/api/v1'}</dd>
  237. </div>
  238. <div className="flex justify-between">
  239. <dt className="text-muted-foreground">Status:</dt>
  240. <dd>
  241. {health === 'healthy' ? (
  242. <span className="text-green-600 dark:text-green-400">Connected</span>
  243. ) : health === 'error' ? (
  244. <span className="text-destructive">Disconnected</span>
  245. ) : (
  246. <span className="text-yellow-600 dark:text-yellow-400">Checking...</span>
  247. )}
  248. </dd>
  249. </div>
  250. </dl>
  251. </CardContent>
  252. </Card>
  253. </div>
  254. </div>
  255. </AppLayout>
  256. </ProtectedRoute>
  257. );
  258. }