reputation.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. import { View, Row } from 'app/design/view'
  2. import { Text } from 'app/design/typography'
  3. import Time from 'app/ui/atoms/time'
  4. import Profile from 'app/ui/molecules/profile/profile'
  5. import { Icon } from 'app/ui/atoms/icon'
  6. import { Svg, Path } from 'react-native-svg'
  7. import { Modal, NeoButton } from 'app/design/controls'
  8. import { useState, useCallback, useMemo } from 'react'
  9. import { useQueries } from '@tanstack/react-query'
  10. import { useFetch, fetchQueryOptions } from 'app/lib/hooks/use-fetch'
  11. import Tabs from 'app/ui/molecules/tabs/tabs'
  12. import {
  13. Table,
  14. TableHeader,
  15. TableBody,
  16. TableRow,
  17. TableHead,
  18. TableCell,
  19. TableHeaderText,
  20. TableCellText,
  21. } from 'app/ui/molecules/page/table'
  22. import Badge from 'app/ui/molecules/profile/badge'
  23. import { Loading } from 'app/customization/loading'
  24. import { renderForm } from 'app/components/elements/form'
  25. import { BlockWrapper } from 'app/components/block-wrapper'
  26. import { useTranslation } from 'react-i18next'
  27. export function ReputationActions({ data, blockWrapperProps }) {
  28. return (
  29. <BlockWrapper {...blockWrapperProps}>
  30. <Table className="w-full">
  31. <TableHeader>
  32. <TableRow>
  33. <TableHead className="flex-3">
  34. <TableHeaderText>Action</TableHeaderText>
  35. </TableHead>
  36. <TableHead className="flex-1 justify-center">
  37. <TableHeaderText>Points (active)</TableHeaderText>
  38. </TableHead>
  39. <TableHead className="flex-1 justify-center">
  40. <TableHeaderText>Points (passive)</TableHeaderText>
  41. </TableHead>
  42. </TableRow>
  43. </TableHeader>
  44. <TableBody>
  45. {data.map((item, index) => (
  46. <TableRow key={index}>
  47. <TableCell className="flex-3">
  48. <TableCellText>
  49. {item.unit} {item.action}
  50. </TableCellText>
  51. </TableCell>
  52. <TableCell className="flex-1 justify-center">
  53. <TableCellText className="font-medium">
  54. {item.points_active}
  55. </TableCellText>
  56. </TableCell>
  57. <TableCell className="flex-1 justify-center">
  58. <TableCellText className="font-medium">
  59. {item.points_passive}
  60. </TableCellText>
  61. </TableCell>
  62. </TableRow>
  63. ))}
  64. </TableBody>
  65. </Table>
  66. </BlockWrapper >
  67. )
  68. }
  69. export function ReputationSummary({ data }) {
  70. if (data.mode === 'simple') {
  71. return <ReputationSummarySimple data={data} />
  72. }
  73. return (
  74. <View className="flex-auto">
  75. <ReputationSummarySimple data={data} />
  76. </View>
  77. )
  78. }
  79. export function ReputationWidget({ data }) {
  80. // Tabs without inline `data` load their own; each shows <Loading /> until then.
  81. const tabQueries = useQueries({
  82. queries: data.tabs.map((tab) => fetchQueryOptions(tab.data ? null : tab.url)),
  83. })
  84. const tabsData = data.tabs.map(({ url, title, data: inline }, index) => ({
  85. title,
  86. url,
  87. index,
  88. data: inline ?? (tabQueries[index]?.isSuccess ? (tabQueries[index].data?.data?.[0]?.data ?? []) : undefined),
  89. }))
  90. // Reorder tabs: leaderboard tabs first, then "You" (summary) tab last
  91. const reorderedTabs = useMemo(() => {
  92. const leaderboardTabs = tabsData.filter((item) =>
  93. item.url.includes('leaderboard')
  94. )
  95. const summaryTabs = tabsData.filter(
  96. (item) => !item.url.includes('leaderboard')
  97. )
  98. return [...leaderboardTabs, ...summaryTabs]
  99. }, [tabsData])
  100. const preparedTabs = reorderedTabs.map((item) => ({
  101. ...item,
  102. key: item.url,
  103. content: item.url.includes('leaderboard') ? (
  104. item.data ? (
  105. <ReputationLeaderboard data={item.data} />
  106. ) : (
  107. <View className="h-12">
  108. <Loading />
  109. </View>
  110. )
  111. ) : item.data ? (
  112. <ReputationSummary data={item.data} />
  113. ) : (
  114. <View className="h-12">
  115. <Loading />
  116. </View>
  117. ),
  118. }))
  119. return (
  120. <Tabs
  121. equalWidth
  122. tabs={preparedTabs}
  123. size="sm"
  124. activeTab={reorderedTabs[0]?.url}
  125. />
  126. )
  127. }
  128. function ReputationSummarySimple({ data }) {
  129. const { t } = useTranslation()
  130. const [isModal, setIsModal] = useState(false)
  131. const [isModal2, setIsModal2] = useState(false)
  132. return (
  133. <View className="w-full">
  134. <Modal
  135. scrollable
  136. title={t('Score rules')}
  137. onVisible={isModal}
  138. onClose={() => setIsModal(false)}
  139. >
  140. <View className="w-full lg:min-w-md">
  141. <ReputationActions data={data.actions_list} />
  142. </View>
  143. </Modal>
  144. <Modal
  145. scrollable
  146. title={t('Levels')}
  147. onVisible={isModal2}
  148. onClose={() => setIsModal2(false)}
  149. >
  150. <View className="w-full lg:min-w-md">
  151. <ReputationLevels data={data.levels_list} />
  152. </View>
  153. </Modal>
  154. <View className="flex-auto flex-row gap-3 p-4 w-full ">
  155. <Profile
  156. {...data.author_data}
  157. displayType="unit_wo_info"
  158. displaySize="xl"
  159. />
  160. <View className="flex-auto gap-y-2 justify-center">
  161. <Text className="font-semibold text-lg text-foreground">
  162. {data.author_data.display_name}
  163. </Text>
  164. {data.levels.map((item, index) => (
  165. <View
  166. key={index}
  167. className="flex-row gap-3 items-center"
  168. >
  169. <Badge
  170. variant="secondary"
  171. data={{ text: item.title, icon: item.icon }}
  172. />
  173. <NeoButton
  174. style="borderless"
  175. borderShape="circle"
  176. controlSize="mini"
  177. image="ListPlus"
  178. accessibilityLabel={t('Levels')}
  179. classNames={{ root: 'self-center' }}
  180. onPress={() => setIsModal2(true)}
  181. />
  182. <NeoButton
  183. style="borderless"
  184. borderShape="circle"
  185. controlSize="mini"
  186. image="Info"
  187. accessibilityLabel={t('Score rules')}
  188. classNames={{ root: 'self-center' }}
  189. onPress={() => setIsModal(true)}
  190. />
  191. </View>
  192. ))}
  193. </View>
  194. </View>
  195. <View className="flex-row gap-3 flex-auto flex-wrap px-2">
  196. <View className="flex-1 border border-border/60 rounded-xl px-4 py-3">
  197. <Text className="text-sm text-muted-foreground mb-1">
  198. Total
  199. </Text>
  200. <Text className="text-2xl font-bold text-foreground">
  201. {data.points || 0}
  202. </Text>
  203. </View>
  204. <View className="flex-1 border border-border/60 rounded-xl px-4 py-3">
  205. <Text className="text-sm text-muted-foreground mb-1">
  206. 7 Days
  207. </Text>
  208. <Text className="text-2xl font-bold text-foreground">
  209. {data.points_7d || 0}
  210. </Text>
  211. </View>
  212. <View className="flex-1 border border-border/60 rounded-xl px-4 py-3">
  213. <Text className="text-sm text-muted-foreground mb-1">
  214. 30 Days
  215. </Text>
  216. <Text className="text-2xl font-bold text-foreground">
  217. {data.points_30d || 0}
  218. </Text>
  219. </View>
  220. </View>
  221. </View>
  222. )
  223. }
  224. const getPositionColors = (index) => {
  225. switch (index) {
  226. case 1:
  227. return 'bg-yellow-500' // Gold for 1st place
  228. case 2:
  229. return 'bg-secondary-foreground' // Silver for 2nd place
  230. case 3:
  231. return 'bg-amber-600' // Bronze for 3rd place
  232. default:
  233. return 'bg-transparent border border-border/60'
  234. }
  235. }
  236. const getStarColor = (index) => {
  237. switch (index) {
  238. case 1:
  239. return '#EAB308' // Gold
  240. case 2:
  241. return '#9CA3AF' // Silver
  242. case 3:
  243. return '#D97706' // Bronze
  244. default:
  245. return 'transparent'
  246. }
  247. }
  248. const getTextColor = (index) => {
  249. return index <= 3 ? 'text-white' : 'text-muted-foreground'
  250. }
  251. const StarIcon = ({ color, size = 28 }) => (
  252. <Svg
  253. width={size}
  254. height={size}
  255. viewBox="0 0 24 24"
  256. fill={color}
  257. stroke={color}
  258. >
  259. <Path d="M11.525 2.295a.53.53 0 0 1 .95 0l2.31 4.679a2.123 2.123 0 0 0 1.595 1.16l5.166.756a.53.53 0 0 1 .294.904l-3.736 3.638a2.123 2.123 0 0 0-.611 1.878l.882 5.14a.53.53 0 0 1-.771.56l-4.618-2.428a2.122 2.122 0 0 0-1.973 0L6.396 21.01a.53.53 0 0 1-.77-.56l.881-5.139a2.122 2.122 0 0 0-.611-1.879L2.16 9.795a.53.53 0 0 1 .294-.906l5.165-.755a2.122 2.122 0 0 0 1.597-1.16z" />
  260. </Svg>
  261. )
  262. export function ReputationLeaderboard({ data }) {
  263. const { t } = useTranslation()
  264. const formProps = data?.filter_form
  265. ? { ...data?.filter_form, layout: 'hor', name: 'reputaion_filter_form' }
  266. : null
  267. if (formProps?.data?.inputs?.days) {
  268. formProps.data.inputs.days.mode = 'buttons'
  269. formProps.data.inputs.days.caption = ''
  270. }
  271. if (formProps?.data?.inputs?.username) {
  272. formProps.data.inputs.username.placeholder = t('Search by name')
  273. formProps.data.inputs.username.caption = ''
  274. }
  275. // Filtered list: the latest search wins even if an older response lands later;
  276. // the previous result stays on screen while the next one loads.
  277. const [searchUrl, setSearchUrl] = useState(null)
  278. const { data: searchResponse } = useFetch(searchUrl, { keepPreviousData: true })
  279. const profilesList = (searchUrl && searchResponse?.data?.profiles) || data.profiles
  280. const onFormChange = useCallback((values) => {
  281. const transformedValues = Object.fromEntries(
  282. Object.entries(values).map(([key, value]) => [
  283. key,
  284. Array.isArray(value) ? value.join(',') : value,
  285. ])
  286. )
  287. setSearchUrl(data?.request_url + JSON.stringify(transformedValues))
  288. }, [])
  289. const searchForm = useMemo(
  290. () => formProps ? renderForm(formProps, onFormChange) : null,
  291. [formProps, onFormChange]
  292. )
  293. return (
  294. <>
  295. {!!searchForm && (
  296. <View className="mb-8 max-w-xl mx-auto w-full">
  297. {searchForm}
  298. </View>
  299. )}
  300. <View className="items-center w-full flex-col gap-3 p-3 max-w-xl mx-auto">
  301. {profilesList.map((item, index) => (
  302. <Row
  303. className="w-full justify-between items-center "
  304. key={index}
  305. >
  306. <Row className="items-center gap-2 ">
  307. {item.position > 0 && (
  308. <View className="w-7 h-7 items-center justify-center relative">
  309. {item.position <= 3 ? (
  310. <>
  311. <StarIcon
  312. color={getStarColor(
  313. item.position
  314. )}
  315. size={28}
  316. />
  317. <Text
  318. className={`${getTextColor(
  319. item.position
  320. )} text-xs font-bold absolute`}
  321. >
  322. {item.position}
  323. </Text>
  324. </>
  325. ) : (
  326. <View
  327. className={`w-6 h-6 rounded-full ${getPositionColors(
  328. item.position
  329. )} items-center justify-center`}
  330. >
  331. <Text
  332. className={`${getTextColor(
  333. item.position
  334. )} text-sm font-bold absolute`}
  335. >
  336. {item.position}
  337. </Text>
  338. </View>
  339. )}
  340. </View>
  341. )}
  342. <Row className="items-center gap-2">
  343. <Profile
  344. {...item.unit}
  345. displayType="unit_wo_info"
  346. displaySize="sm"
  347. />
  348. <Text className="text-sm text-muted-foreground">
  349. {item.unit.display_name}
  350. </Text>
  351. </Row>
  352. </Row>
  353. <View>
  354. <Text className=" text-base font-bold text-muted-foreground">
  355. {item.sign}
  356. {item.points}
  357. </Text>
  358. </View>
  359. </Row>
  360. ))}
  361. </View>
  362. </>
  363. )
  364. }
  365. export function ReputationHistory({ data }) {
  366. return (
  367. <Table className="w-full">
  368. <TableHeader>
  369. <TableRow>
  370. <TableHead className="flex-1">
  371. <TableHeaderText>Date</TableHeaderText>
  372. </TableHead>
  373. <TableHead className="flex-3">
  374. <TableHeaderText>Action</TableHeaderText>
  375. </TableHead>
  376. <TableHead className="flex-1 ">
  377. <TableHeaderText>Points</TableHeaderText>
  378. </TableHead>
  379. </TableRow>
  380. </TableHeader>
  381. <TableBody>
  382. {data.map((item, index) => (
  383. <TableRow key={index}>
  384. <TableCell className="flex-1 ">
  385. <Time
  386. stylesName=""
  387. ts={item.date}
  388. format="datetime"
  389. />
  390. </TableCell>
  391. <TableCell className="flex-3">
  392. <TableCellText>
  393. {item.unit} {item.action}
  394. </TableCellText>
  395. </TableCell>
  396. <TableCell className="flex-1 ">
  397. <TableCellText className="font-medium">
  398. {item.points}
  399. </TableCellText>
  400. </TableCell>
  401. </TableRow>
  402. ))}
  403. </TableBody>
  404. </Table>
  405. )
  406. }
  407. export function ReputationLevels({ data }) {
  408. return (
  409. <Table className="w-full">
  410. <TableBody>
  411. {data.map((item, index) => (
  412. <TableRow key={index} className="border-b-0">
  413. <TableCell className="flex-auto">
  414. <Row className="gap-x-2 items-center">
  415. <Icon icon={item.icon} size={24} />
  416. <TableCellText>{item.title}</TableCellText>
  417. </Row>
  418. </TableCell>
  419. <TableCell className="flex-none">
  420. <TableCellText className="text-muted-foreground font-bold">
  421. {item.points_in}
  422. </TableCellText>
  423. </TableCell>
  424. </TableRow>
  425. ))}
  426. </TableBody>
  427. </Table>
  428. )
  429. }