|
1 | 1 | import React, { useState, useRef } from 'react'; |
2 | 2 | import { useNavigate } from 'react-router-dom'; |
3 | 3 | import { ArrowLeft, Camera, Save, User as UserIcon, Calendar, Globe } from 'lucide-react'; |
4 | | -import { useAuth } from '../lib/auth'; |
| 4 | +import { useTheme } from '../lib/theme'; |
5 | 5 | import { useTranslation } from '../lib/i18n'; |
6 | 6 | import { putFile, getFile } from '../lib/github'; |
7 | 7 | import { stringifyFrontmatter, generateGravatarUrl, safeLogError } from '../lib/utils'; |
8 | 8 | import { ColorPicker } from '../components/ColorPicker'; |
9 | 9 |
|
10 | 10 | export function ProfilePage() { |
11 | 11 | const { user, logout } = useAuth(); |
| 12 | + const { theme, setTheme } = useTheme(); |
12 | 13 | const { t } = useTranslation(); |
13 | 14 | const navigate = useNavigate(); |
14 | 15 | const fileInputRef = useRef<HTMLInputElement>(null); |
@@ -281,17 +282,62 @@ export function ProfilePage() { |
281 | 282 | <label className="block text-sm font-medium mb-2"> |
282 | 283 | {t('language')} |
283 | 284 | </label> |
284 | | - <div className="flex items-center gap-2"> |
285 | | - <Globe className="w-4 h-4 text-gray-400" /> |
286 | | - <span className="text-gray-600 dark:text-gray-400 capitalize">{user.lang}</span> |
287 | | - </div> |
| 285 | + <select |
| 286 | + value={user?.lang || 'en'} |
| 287 | + onChange={async (e) => { |
| 288 | + const newLang = e.target.value; |
| 289 | + setIsSaving(true); |
| 290 | + try { |
| 291 | + const updatedUser = { ...user!, lang: newLang }; |
| 292 | + const content = stringifyFrontmatter(updatedUser, ''); |
| 293 | + const existingFile = await getFile(`users/${user!.username}.md`); |
| 294 | + await putFile(`users/${user!.username}.md`, content, `Update language for ${user!.username}`, false, existingFile?.sha); |
| 295 | + localStorage.setItem('user', JSON.stringify(updatedUser)); |
| 296 | + localStorage.setItem('lang', newLang); |
| 297 | + window.location.reload(); // Reload to apply language |
| 298 | + } catch (error) { |
| 299 | + safeLogError('Error saving language:', error); |
| 300 | + alert('Failed to save language. Please try again.'); |
| 301 | + } finally { |
| 302 | + setIsSaving(false); |
| 303 | + } |
| 304 | + }} |
| 305 | + className="px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700" |
| 306 | + > |
| 307 | + <option value="en">English</option> |
| 308 | + <option value="ru">Русский</option> |
| 309 | + </select> |
288 | 310 | </div> |
289 | 311 |
|
290 | 312 | <div> |
291 | 313 | <label className="block text-sm font-medium mb-2"> |
292 | 314 | {t('theme')} |
293 | 315 | </label> |
294 | | - <span className="text-gray-600 dark:text-gray-400 capitalize">{user.theme}</span> |
| 316 | + <select |
| 317 | + value={theme} |
| 318 | + onChange={(e) => { |
| 319 | + const newTheme = e.target.value as 'light' | 'dark'; |
| 320 | + setTheme(newTheme); |
| 321 | + // Update user file |
| 322 | + const updateUserTheme = async () => { |
| 323 | + try { |
| 324 | + const updatedUser = { ...user!, theme: newTheme }; |
| 325 | + const content = stringifyFrontmatter(updatedUser, ''); |
| 326 | + const existingFile = await getFile(`users/${user!.username}.md`); |
| 327 | + await putFile(`users/${user!.username}.md`, content, `Update theme for ${user!.username}`, false, existingFile?.sha); |
| 328 | + localStorage.setItem('user', JSON.stringify(updatedUser)); |
| 329 | + } catch (error) { |
| 330 | + safeLogError('Error saving theme:', error); |
| 331 | + alert('Failed to save theme. Please try again.'); |
| 332 | + } |
| 333 | + }; |
| 334 | + updateUserTheme(); |
| 335 | + }} |
| 336 | + className="px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700" |
| 337 | + > |
| 338 | + <option value="light">Light</option> |
| 339 | + <option value="dark">Dark</option> |
| 340 | + </select> |
295 | 341 | </div> |
296 | 342 | </div> |
297 | 343 |
|
|
0 commit comments