dashboard-stat-old.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import { Icon } from 'app/ui/atoms/icon'
  2. import Card from 'app/ui/molecules/page/card'
  3. import { useState } from 'react';
  4. import { useTranslation } from 'react-i18next';
  5. import { View, Row } from 'app/design/view'
  6. import { Text } from 'app/design/typography'
  7. import { NeoButton, NeoButtonLink } from 'app/design/controls'
  8. import Link from 'app/ui/atoms/link'
  9. import { useCurrentUser } from 'app/context/user'
  10. import Profile from 'app/ui/molecules/profile/profile'
  11. import ProfileSwitcher from 'app/components/elements/profile-switcher';
  12. import { Modal } from 'app/design/controls'
  13. import { appSetting } from 'app/lib/util'
  14. import DropdownMenu from 'app/ui/atoms/dropdown-menu';
  15. import i18n from 'i18next';
  16. import { useLayoutSettings } from 'app/context/layout-settings';
  17. import { useFetch } from 'app/lib/hooks/use-fetch';
  18. import { useBottomSheetData } from 'app/context/bottomsheet';
  19. import { useTheme } from 'app/design/theme';
  20. function getCounter(num, icon = '', add = '', color = '') {
  21. if (!num) num = 0;
  22. let sColor = 'gray'
  23. if (num > 0) {
  24. sColor = 'green'
  25. if (icon == '')
  26. icon = 'ArrowBigUp';
  27. }
  28. if (num < 0) {
  29. sColor = 'red'
  30. if (icon == '')
  31. icon = 'ArrowBigDown';
  32. }
  33. return (
  34. <Row className={'mb-auto text-' + sColor + '-800 bg-' + sColor + '-200 dark:bg-' + sColor + '-950 gap-x-1 py-1 px-2 rounded-full mb-auto dark:text-' + sColor + '-200 '}>
  35. <Icon color={color} className={"text-" + sColor + "-600 dark:text-" + sColor + "-400"} icon={icon} size={16} />
  36. <Text className={"flex-none text-" + sColor + "-800 dark:text-" + sColor + "-200 text-xs"}>{num}{add}</Text>
  37. </Row>
  38. )
  39. }
  40. export default function PageLayout(props) {
  41. const { setBottomSheetData } = useBottomSheetData();
  42. const { t } = useTranslation();
  43. let { currentUser, setCurrentUser } = useCurrentUser()
  44. const [showImage2, setShowImage2] = useState(false);
  45. // Same preference store as the remote dashboard (theme / language / layout).
  46. // Called before the guest early-return below — hook order must be stable.
  47. const { themeName, setThemeName, layoutName, setLayoutName, setLang } = useLayoutSettings();
  48. let profile = null
  49. if (currentUser) {
  50. let dUser = Object.assign({}, currentUser)
  51. dUser.url_avatar = dUser.avatar
  52. profile = <Profile {...dUser} displayType="unit_wo_info" size="lg" />
  53. }
  54. if (!currentUser)
  55. return <></>
  56. const handleLang = (item) => setLang(item);
  57. const handleTheme = (item) => setThemeName(item);
  58. const handleFormat = (item) => setLayoutName(item);
  59. const currentTheme = themeName || 'auto';
  60. const currentFormat = layoutName || appSetting('layout', 'default_layout');
  61. const langItems = [
  62. { id: 'auto', key: 'auto', name: 'auto', title: t('lang_auto') },
  63. ...appSetting('layout', 'avaliable_langs').map(lang => ({
  64. id: lang,
  65. key: lang,
  66. name: lang,
  67. title: t('lang_' + lang)
  68. }))
  69. ];
  70. return (
  71. <View className='w-full'>
  72. { /*<Button title="Test bottomsheet"
  73. onPress={() => {setBottomSheetData({ title: 'Choose labels', showClose: true, content: <View className='h-24 w-full'><Text>TextTextTextText</Text></View> })}}
  74. ></Button>*/}
  75. <Modal id='file-preview2' title="VideoChat" onVisible={!!showImage2} onClose={() => {setShowImage2(null)}}>
  76. </Modal>
  77. <View className={appSetting('layout', 'max_width') +" w-full mx-auto"}>
  78. <View className={"u-max-width-block w-full px-2 pb-1 pt-2 sm:p-2 mx-auto"}>
  79. <Card rounded=" rounded-2xl " addClassName=" w-full p-4 flex-row ">
  80. <View className="justify-center sm:justify-between flex-auto my-auto w-full items-center">
  81. <View className="flex-row w-full items-center ">
  82. <View className="flex-auto ">
  83. <Link href={currentUser.url}>
  84. <View className="flex-row items-center ">
  85. {profile}
  86. <Text className="my-auto ml-3 text-secondary-foreground web:hover:text-foreground web:hover: text-lg font-semibold ">
  87. {currentUser.display_name}
  88. </Text>
  89. </View>
  90. </Link>
  91. </View>
  92. <View className="flex-none">
  93. <ProfileSwitcher hideTitle={true} className="u-neo-btn-link" accessibilityLabel={t('Switch profile')}>
  94. <NeoButton borderShape="circle" image="RefreshCw" interactive />
  95. </ProfileSwitcher>
  96. </View>
  97. </View>
  98. <View className="flex-row items-center gap-x-2 my-auto hidden">
  99. <ProfileSwitcher hideTitle={true} className="u-neo-btn-link" accessibilityLabel={t('Switch profile')}>
  100. <NeoButton borderShape="circle" image="RefreshCw" interactive />
  101. </ProfileSwitcher>
  102. {
  103. appSetting('layout', 'avaliable_langs').length > 1 && (
  104. <View><DropdownMenu
  105. items={langItems}
  106. onSelect={(oItem) => {handleLang(oItem.id)}}
  107. buttonProps={{ borderShape: 'circle', image: 'Languages', accessibilityLabel: t('Language') }}
  108. /></View>)
  109. }
  110. {
  111. appSetting('dashboard', 'switch_theme') && (
  112. <View><DropdownMenu items={['dark', 'light','auto'].map(theme => ({
  113. key: theme,
  114. id: theme,
  115. name: theme,
  116. title: t('theme_' + theme)
  117. }))}
  118. onSelect={(oItem) => {handleTheme(oItem.id)}}
  119. buttonProps={{ borderShape: 'circle', image: 'Moon', accessibilityLabel: t('Theme') }}
  120. /></View>)
  121. }
  122. <NeoButtonLink href="/logout" borderShape="circle" image="LogOut" accessibilityLabel={t('Sign out')} />
  123. </View>
  124. </View>
  125. </Card>
  126. </View>
  127. <View className=" w-full ">
  128. <ElementDashboardStat {...props}/>
  129. <Card addClassName=" shadow flex-col m-2 p-3 sm:p-4">
  130. {
  131. appSetting('layout', 'avaliable_langs').length > 1 && (
  132. <View className="mb-2">
  133. <DropdownMenu
  134. items={langItems}
  135. onSelect={(oItem) => {handleLang(oItem.id)}}
  136. buttonProps={{ label: t('lang_' + i18n.language), image: 'Languages', width: 'fill', align: 'start' }}
  137. />
  138. </View>
  139. )
  140. }
  141. {
  142. appSetting('dashboard', 'switch_theme') && (
  143. <View className="mb-2">
  144. <DropdownMenu items={['dark', 'light','auto'].map(theme => ({
  145. key: theme,
  146. id: theme,
  147. name: theme,
  148. title: t('theme_' + theme)
  149. }))}
  150. onSelect={(oItem) => {handleTheme(oItem.id)}}
  151. buttonProps={{ label: t('theme_' + currentTheme), image: 'Moon', width: 'fill', align: 'start' }}
  152. />
  153. </View>)
  154. }
  155. {
  156. appSetting('layout', 'avaliable_layouts').length > 1 && (
  157. <View className='hidden sm:flex mb-2'>
  158. <DropdownMenu
  159. items={appSetting('layout', 'avaliable_layouts').map(lang => ({
  160. id: lang,
  161. key: lang,
  162. name: lang,
  163. title: t('format_' + lang)
  164. }))}
  165. onSelect={(oItem) => {handleFormat(oItem.id)}}
  166. buttonProps={{ label: t('format_' + currentFormat), image: 'Layout', width: 'fill', align: 'start' }}
  167. /></View>)
  168. }
  169. <NeoButtonLink
  170. href="/logout"
  171. label={t("Sign out")}
  172. image="LogOut"
  173. width="fill"
  174. align="start"
  175. />
  176. </Card>
  177. </View>
  178. </View>
  179. </View>
  180. )
  181. }
  182. function ElementDashboardStat(props) {
  183. const { colors } = useTheme();
  184. const { t } = useTranslation();
  185. let { currentUser, setCurrentUser } = useCurrentUser()
  186. // Same request as the remote dashboard — they share one cache entry.
  187. const { data: sResponse } = useFetch('/api.php?r=system/get_stat_block/TemplDashboardServices');
  188. const data = sResponse?.data?.[0]?.data ?? props.data;
  189. let menu = appSetting('menu_items', 'menu_dashboard')
  190. let menu_manage = appSetting('menu_items', 'menu_dashboard_manage')
  191. if (!currentUser?.moderator)
  192. menu_manage = [];
  193. return (
  194. <>
  195. <Row className="flex-wrap flex-auto mb-auto px-1 sm:px-0">
  196. {menu.map((item2, index) => {
  197. let item = data[item2.key];
  198. if (item) {
  199. if (item?.type != 'growth') {
  200. return <View className=" w-1/2 lg:w-1/3 xl:w-1/4 p-1 sm:p-2 web:duration-300 " key={index}>
  201. <Link href={item2.link}>
  202. <Card rounded=" rounded-2xl " addClassName=" sm:web:hover:scale-105 web:duration-300 w-full p-4 " >
  203. <Row className='space-x-1 w-full justify-between'>
  204. {
  205. item.count > 0 ? <Text className=" text-3xl -translate-y-1 font-semibold flex-auto text-secondary-foreground web:group-hover:text-foreground ">
  206. {item.count}
  207. </Text> : <View><NeoButtonLink href={item2.link2} emulate={true} controlSize="small" borderShape="circle" image="Plus" accessibilityLabel={t('Add')} /></View>
  208. }
  209. <View className="flex-none text-secondary-foreground web:group-hover:text-foreground font-semibold ">
  210. <Icon icon={item2.icon} width={24} height={24} color={colors.default}/>
  211. </View>
  212. </Row>
  213. <Row className="w-full my-auto gap-x-2 ">
  214. <Text className=" text-lg flex-auto text-secondary-foreground web:group-hover:text-foreground font-semibold ">
  215. {t(item2.title)}
  216. </Text>
  217. <View className='my-auto' ><Text>{getCounter(item[item2.action], item2.action_icon, '', colors.default)}</Text></View>
  218. </Row>
  219. </Card>
  220. </Link>
  221. </View>;
  222. }
  223. return (
  224. <View className=" w-1/2 lg:w-1/3 xl:w-1/4 p-1 sm:p-2 " key={index}>
  225. <Link href={item2.link} key={index}>
  226. <Card rounded=" rounded-2xl " addClassName="w-full p-4 web:duration-300 sm:web:hover:scale-105 " margin="a">
  227. <Row className=''>
  228. <Text className=" text-3xl -translate-y-1 font-semibold flex-auto text-secondary-foreground web:group-hover:text-foreground ">
  229. {item.current}
  230. </Text>
  231. <View className="flex-none text-secondary-foreground web:group-hover:text-foreground font-semibold ">
  232. <Icon icon={item2.icon} width={24} height={24} color={colors.default}/>
  233. </View>
  234. </Row>
  235. <Row className="w-full gap-x-2">
  236. <Text className=" text-lg flex-auto text-secondary-foreground web:group-hover:text-foreground font-semibold ">
  237. {t(item2.title)}
  238. </Text>
  239. <View className='my-auto' ><Text>{getCounter(item[item2.action], item2.action_icon, '', colors.default)}</Text></View>
  240. </Row>
  241. </Card>
  242. </Link>
  243. </View>
  244. )
  245. }
  246. })}
  247. </Row>
  248. {menu_manage.length > 0 && <Card addClassName='m-2 mb-1 p-2 sm:p-3'>
  249. <Text className="text-xl mx-1 mb-1 text-secondary-foreground font-semibold">Admin Tools</Text>
  250. <View className="flex-row flex-wrap ">
  251. {menu_manage.map((item2, index) => {
  252. return <View className=" w-1/2 lg:w-1/3 xl:w-1/4 p-1 " key={index}>
  253. <Link href={item2.link}>
  254. <Row className="w-full bg-muted p-2 rounded-lg my-auto items-center text-muted-foreground web:hover:text-foreground ">
  255. <View className="flex-none px-1 font-semibold ">
  256. <Icon icon={item2.icon} width={24} height={24} color={colors.default}/>
  257. </View>
  258. <Text className="ml-3 sm: text-base flex-auto text-muted-foreground web:hover:text-foreground font-medium ">
  259. {t(item2.title)}
  260. </Text>
  261. </Row>
  262. </Link>
  263. </View>;
  264. })}
  265. </View></Card>}
  266. </>
  267. )
  268. }