|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, {ReactNode} from "react"; |
| 4 | +import {useRouter} from "next/navigation"; |
| 5 | +import {useTranslation} from "react-i18next"; |
| 6 | + |
| 7 | +import {Badge, Button, Dropdown} from "antd"; |
| 8 | +import {DownOutlined} from "@ant-design/icons"; |
| 9 | +import {FiArrowLeft, FiRefreshCw} from "react-icons/fi"; |
| 10 | +import {Globe} from "lucide-react"; |
| 11 | +import {languageOptions} from "@/const/constants"; |
| 12 | +import {useLanguageSwitch} from "@/lib/language"; |
| 13 | +import {HEADER_CONFIG} from "@/const/layoutConstants"; |
| 14 | +import {CONNECTION_STATUS, ConnectionStatus,} from "@/const/modelConfig"; |
| 15 | + |
| 16 | +// ================ Header ================ |
| 17 | +interface HeaderProps { |
| 18 | + connectionStatus: ConnectionStatus; |
| 19 | + isCheckingConnection: boolean; |
| 20 | + onCheckConnection: () => void; |
| 21 | + title: string; |
| 22 | + description: string; |
| 23 | +} |
| 24 | + |
| 25 | +function Header({ |
| 26 | + connectionStatus, |
| 27 | + isCheckingConnection, |
| 28 | + onCheckConnection, |
| 29 | + title, |
| 30 | + description, |
| 31 | +}: HeaderProps) { |
| 32 | + const router = useRouter(); |
| 33 | + const { t } = useTranslation(); |
| 34 | + const { currentLanguage, handleLanguageChange } = useLanguageSwitch(); |
| 35 | + |
| 36 | + // Get status text |
| 37 | + const getStatusText = () => { |
| 38 | + switch (connectionStatus) { |
| 39 | + case CONNECTION_STATUS.SUCCESS: |
| 40 | + return t("setup.header.status.connected"); |
| 41 | + case CONNECTION_STATUS.ERROR: |
| 42 | + return t("setup.header.status.disconnected"); |
| 43 | + case CONNECTION_STATUS.PROCESSING: |
| 44 | + return t("setup.header.status.checking"); |
| 45 | + default: |
| 46 | + return t("setup.header.status.unknown"); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + return ( |
| 51 | + <header |
| 52 | + className="w-full py-4 px-6 flex items-center justify-between border-b border-slate-200 dark:border-slate-700 bg-white/80 dark:bg-slate-900/80 backdrop-blur-sm" |
| 53 | + style={{ height: HEADER_CONFIG.HEIGHT }} |
| 54 | + > |
| 55 | + <div className="flex items-center"> |
| 56 | + <button |
| 57 | + onClick={() => router.push("/")} |
| 58 | + className="mr-3 p-2 rounded-full hover:bg-gray-100 dark:hover:bg-slate-800 transition-colors" |
| 59 | + aria-label={t("setup.header.button.back")} |
| 60 | + > |
| 61 | + <FiArrowLeft className="text-slate-600 dark:text-slate-300 text-xl" /> |
| 62 | + </button> |
| 63 | + <h1 className="text-xl font-bold text-blue-600 dark:text-blue-500"> |
| 64 | + {title} |
| 65 | + </h1> |
| 66 | + <div className="mx-2 h-6 border-l border-slate-300 dark:border-slate-600"></div> |
| 67 | + <span className="text-slate-600 dark:text-slate-400 text-sm"> |
| 68 | + {description} |
| 69 | + </span> |
| 70 | + </div> |
| 71 | + {/* Language switch */} |
| 72 | + <div className="flex items-center gap-3"> |
| 73 | + <Dropdown |
| 74 | + menu={{ |
| 75 | + items: languageOptions.map((opt) => ({ |
| 76 | + key: opt.value, |
| 77 | + label: opt.label, |
| 78 | + })), |
| 79 | + onClick: ({ key }) => handleLanguageChange(key as string), |
| 80 | + }} |
| 81 | + > |
| 82 | + <a className="ant-dropdown-link text-sm !font-medium text-slate-600 hover:text-slate-900 dark:text-slate-300 dark:hover:text-white transition-colors flex items-center gap-2 cursor-pointer w-[110px] border-0 shadow-none bg-transparent text-left"> |
| 83 | + <Globe className="h-4 w-4" /> |
| 84 | + {languageOptions.find((o) => o.value === currentLanguage)?.label || |
| 85 | + currentLanguage} |
| 86 | + <DownOutlined className="text-[10px]" /> |
| 87 | + </a> |
| 88 | + </Dropdown> |
| 89 | + {/* ModelEngine connectivity status */} |
| 90 | + <div className="flex items-center px-3 py-1.5 rounded-md border border-slate-200 dark:border-slate-700"> |
| 91 | + <Badge |
| 92 | + status={connectionStatus} |
| 93 | + text={getStatusText()} |
| 94 | + className="[&>.ant-badge-status-dot]:w-[8px] [&>.ant-badge-status-dot]:h-[8px] [&>.ant-badge-status-text]:text-base [&>.ant-badge-status-text]:ml-2 [&>.ant-badge-status-text]:font-medium" |
| 95 | + /> |
| 96 | + <Button |
| 97 | + icon={ |
| 98 | + <FiRefreshCw |
| 99 | + className={isCheckingConnection ? "animate-spin" : ""} |
| 100 | + /> |
| 101 | + } |
| 102 | + size="small" |
| 103 | + type="text" |
| 104 | + onClick={onCheckConnection} |
| 105 | + disabled={isCheckingConnection} |
| 106 | + className="ml-2" |
| 107 | + /> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + </header> |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +// ================ Navigation ================ |
| 115 | +interface NavigationProps { |
| 116 | + onBack?: () => void; |
| 117 | + onNext?: () => void; |
| 118 | + onComplete?: () => void; |
| 119 | + isSaving?: boolean; |
| 120 | + showBack?: boolean; |
| 121 | + showNext?: boolean; |
| 122 | + showComplete?: boolean; |
| 123 | + nextText?: string; |
| 124 | + completeText?: string; |
| 125 | +} |
| 126 | + |
| 127 | +function Navigation({ |
| 128 | + onBack, |
| 129 | + onNext, |
| 130 | + onComplete, |
| 131 | + isSaving = false, |
| 132 | + showBack = false, |
| 133 | + showNext = false, |
| 134 | + showComplete = false, |
| 135 | + nextText, |
| 136 | + completeText, |
| 137 | +}: NavigationProps) { |
| 138 | + const { t } = useTranslation(); |
| 139 | + |
| 140 | + const handleClick = () => { |
| 141 | + if (showComplete && onComplete) { |
| 142 | + onComplete(); |
| 143 | + } else if (showNext && onNext) { |
| 144 | + onNext(); |
| 145 | + } |
| 146 | + }; |
| 147 | + |
| 148 | + const buttonText = () => { |
| 149 | + if (showComplete) { |
| 150 | + return isSaving |
| 151 | + ? t("setup.navigation.button.saving") |
| 152 | + : completeText || t("setup.navigation.button.complete"); |
| 153 | + } |
| 154 | + if (showNext) { |
| 155 | + return nextText || t("setup.navigation.button.next"); |
| 156 | + } |
| 157 | + return ""; |
| 158 | + }; |
| 159 | + |
| 160 | + return ( |
| 161 | + <div className="mt-3 flex justify-between px-6"> |
| 162 | + <div className="flex gap-2"> |
| 163 | + {showBack && onBack && ( |
| 164 | + <button |
| 165 | + onClick={onBack} |
| 166 | + className="px-6 py-2.5 rounded-md flex items-center text-sm font-medium bg-slate-100 dark:bg-slate-800 text-slate-700 dark:text-slate-300 hover:bg-slate-200 dark:hover:bg-slate-700 cursor-pointer transition-colors" |
| 167 | + > |
| 168 | + {t("setup.navigation.button.previous")} |
| 169 | + </button> |
| 170 | + )} |
| 171 | + </div> |
| 172 | + |
| 173 | + <div className="flex gap-2"> |
| 174 | + {(showNext || showComplete) && ( |
| 175 | + <button |
| 176 | + onClick={handleClick} |
| 177 | + disabled={isSaving} |
| 178 | + className="px-6 py-2.5 rounded-md flex items-center text-sm font-medium bg-blue-600 dark:bg-blue-600 text-white hover:bg-blue-700 dark:hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors" |
| 179 | + style={{ |
| 180 | + border: "none", |
| 181 | + marginLeft: !showBack ? "auto" : undefined, |
| 182 | + }} |
| 183 | + > |
| 184 | + {buttonText()} |
| 185 | + </button> |
| 186 | + )} |
| 187 | + </div> |
| 188 | + </div> |
| 189 | + ); |
| 190 | +} |
| 191 | + |
| 192 | +// ================ Layout ================ |
| 193 | +interface SetupLayoutProps { |
| 194 | + children: ReactNode; |
| 195 | + connectionStatus: ConnectionStatus; |
| 196 | + isCheckingConnection: boolean; |
| 197 | + onCheckConnection: () => void; |
| 198 | + title: string; |
| 199 | + description: string; |
| 200 | + onBack?: () => void; |
| 201 | + onNext?: () => void; |
| 202 | + onComplete?: () => void; |
| 203 | + isSaving?: boolean; |
| 204 | + showBack?: boolean; |
| 205 | + showNext?: boolean; |
| 206 | + showComplete?: boolean; |
| 207 | + nextText?: string; |
| 208 | + completeText?: string; |
| 209 | +} |
| 210 | + |
| 211 | +export default function SetupLayout({ |
| 212 | + children, |
| 213 | + connectionStatus, |
| 214 | + isCheckingConnection, |
| 215 | + onCheckConnection, |
| 216 | + title, |
| 217 | + description, |
| 218 | + onBack, |
| 219 | + onNext, |
| 220 | + onComplete, |
| 221 | + isSaving = false, |
| 222 | + showBack = false, |
| 223 | + showNext = false, |
| 224 | + showComplete = false, |
| 225 | + nextText, |
| 226 | + completeText, |
| 227 | +}: SetupLayoutProps) { |
| 228 | + return ( |
| 229 | + <div className="min-h-screen bg-slate-50 dark:bg-slate-900 font-sans"> |
| 230 | + <Header |
| 231 | + connectionStatus={connectionStatus} |
| 232 | + isCheckingConnection={isCheckingConnection} |
| 233 | + onCheckConnection={onCheckConnection} |
| 234 | + title={title} |
| 235 | + description={description} |
| 236 | + /> |
| 237 | + |
| 238 | + {/* Main content */} |
| 239 | + <div className="max-w-[1800px] mx-auto px-8 pb-4 mt-6 bg-transparent"> |
| 240 | + {children} |
| 241 | + <Navigation |
| 242 | + onBack={onBack} |
| 243 | + onNext={onNext} |
| 244 | + onComplete={onComplete} |
| 245 | + isSaving={isSaving} |
| 246 | + showBack={showBack} |
| 247 | + showNext={showNext} |
| 248 | + showComplete={showComplete} |
| 249 | + nextText={nextText} |
| 250 | + completeText={completeText} |
| 251 | + /> |
| 252 | + </div> |
| 253 | + </div> |
| 254 | + ); |
| 255 | +} |
0 commit comments