| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 'use client' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { useState } from 'react' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import { useContext } from 'use-context-selector' | 
					
						
							|  |  |  | import { useAppContext } from '@/context/app-context' | 
					
						
							|  |  |  | import { SimpleSelect } from '@/app/components/base/select' | 
					
						
							|  |  |  | import type { Item } from '@/app/components/base/select' | 
					
						
							|  |  |  | import { updateUserProfile } from '@/service/common' | 
					
						
							|  |  |  | import { ToastContext } from '@/app/components/base/toast' | 
					
						
							|  |  |  | import I18n from '@/context/i18n' | 
					
						
							|  |  |  | import { timezones } from '@/utils/timezone' | 
					
						
							| 
									
										
										
										
											2024-02-23 14:31:06 +08:00
										 |  |  | import { languages } from '@/i18n/language' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const titleClassName = `
 | 
					
						
							| 
									
										
										
										
											2024-12-23 11:17:49 +08:00
										 |  |  |   mb-2 system-sm-semibold text-text-secondary | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | `
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default function LanguagePage() { | 
					
						
							|  |  |  |   const { locale, setLocaleOnClient } = useContext(I18n) | 
					
						
							|  |  |  |   const { userProfile, mutateUserProfile } = useAppContext() | 
					
						
							|  |  |  |   const { notify } = useContext(ToastContext) | 
					
						
							|  |  |  |   const [editing, setEditing] = useState(false) | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							| 
									
										
										
										
											2024-06-07 20:02:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const handleSelectLanguage = async (item: Item) => { | 
					
						
							|  |  |  |     const url = '/account/interface-language' | 
					
						
							|  |  |  |     const bodyKey = 'interface_language' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setEditing(true) | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       await updateUserProfile({ url, body: { [bodyKey]: item.value } }) | 
					
						
							|  |  |  |       notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 21:14:53 +08:00
										 |  |  |       setLocaleOnClient(item.value.toString()) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-06-07 20:02:08 +08:00
										 |  |  |     catch (e) { | 
					
						
							|  |  |  |       notify({ type: 'error', message: (e as Error).message }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     finally { | 
					
						
							|  |  |  |       setEditing(false) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-06-07 20:02:08 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleSelectTimezone = async (item: Item) => { | 
					
						
							|  |  |  |     const url = '/account/timezone' | 
					
						
							|  |  |  |     const bodyKey = 'timezone' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setEditing(true) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     try { | 
					
						
							|  |  |  |       await updateUserProfile({ url, body: { [bodyKey]: item.value } }) | 
					
						
							|  |  |  |       notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') }) | 
					
						
							| 
									
										
										
										
											2024-06-07 20:02:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       mutateUserProfile() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							|  |  |  |       notify({ type: 'error', message: (e as Error).message }) | 
					
						
							| 
									
										
										
										
											2024-06-07 20:02:08 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     finally { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       setEditing(false) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <> | 
					
						
							|  |  |  |       <div className='mb-8'> | 
					
						
							|  |  |  |         <div className={titleClassName}>{t('common.language.displayLanguage')}</div> | 
					
						
							|  |  |  |         <SimpleSelect | 
					
						
							| 
									
										
										
										
											2024-01-23 21:14:53 +08:00
										 |  |  |           defaultValue={locale || userProfile.interface_language} | 
					
						
							| 
									
										
										
										
											2024-02-23 14:31:06 +08:00
										 |  |  |           items={languages.filter(item => item.supported)} | 
					
						
							| 
									
										
										
										
											2024-06-07 20:02:08 +08:00
										 |  |  |           onSelect={item => handleSelectLanguage(item)} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |           disabled={editing} | 
					
						
							|  |  |  |         /> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |       <div className='mb-8'> | 
					
						
							|  |  |  |         <div className={titleClassName}>{t('common.language.timezone')}</div> | 
					
						
							|  |  |  |         <SimpleSelect | 
					
						
							|  |  |  |           defaultValue={userProfile.timezone} | 
					
						
							|  |  |  |           items={timezones} | 
					
						
							| 
									
										
										
										
											2024-06-07 20:02:08 +08:00
										 |  |  |           onSelect={item => handleSelectTimezone(item)} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |           disabled={editing} | 
					
						
							|  |  |  |         /> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } |