page.tsx 10 KB

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