comments.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. import { useMemo, memo, useCallback, useState, useEffect } from 'react'
  2. import { Platform, Animated } from 'react-native'
  3. import { menuItemsByName, FeedbackHaptics, appSetting, getDataForMenu, attachmentKindByName } from 'app/lib/util';
  4. import { Text } from 'app/design/typography'
  5. import { View, Row } from 'app/design/view'
  6. import { NeoButton, NeoButtonLink, Modal } from 'app/design/controls'
  7. import Time from 'app/ui/atoms/time'
  8. import Profile from 'app/ui/molecules/profile/profile'
  9. import { ContentMore } from 'app/ui/molecules/content/content-more'
  10. import Embed from 'app/ui/molecules/content/embed'
  11. import Menu from 'app/components/menu'
  12. import { useCurrentUser } from 'app/context/user'
  13. import DropdownMenu from 'app/ui/atoms/dropdown-menu'
  14. import { fetcher } from 'app/lib/fetcher'
  15. import Form from 'app/components/elements/form'
  16. import useFetchForm from 'app/lib/hooks/use-fetch-form'
  17. import { useTranslation } from 'react-i18next'
  18. import Link from 'app/ui/atoms/link'
  19. import { toUnaDisplayImageItem } from 'app/lib/image-helpers'
  20. import Carousel from 'app/ui/molecules/content/carousel'
  21. import FileCard from 'app/ui/molecules/content/file-card'
  22. import { components } from 'app/components/registry'
  23. import { StarsView } from 'app/ui/atoms/stars'
  24. import Loading from 'app/ui/atoms/loading'
  25. import { Icon } from 'app/ui/atoms/icon'
  26. import { usePulseOne } from 'app/lib/hooks/use-pulse-once'
  27. import emitter, { EVENTS } from 'app/context/emitter';
  28. import { CardList } from 'app/ui/molecules/page/card'
  29. import { getPageData } from 'app/lib/util';
  30. import FormModal from 'app/ui/molecules/dialogs/form-modal';
  31. function isSystemRootComment(item) {
  32. return item?.data?.author_data?.module === 'system' && item?.data?.cmt_level === 0
  33. }
  34. export default function UnitComments(props) {
  35. const UnitView = props.mode == 'search'
  36. ? UnitCommentsSearch
  37. : (isSystemRootComment(props) && components['unit']['activity']) || UnitCommentsDefault
  38. return <UnitView {...props} />
  39. }
  40. function UnitCommentsSearch(props) {
  41. const module = props.module.replace("_cmts", '');
  42. const [pageData, setPageData] = useState(false);
  43. const keys = Object.keys(props.data);
  44. const url = props?.data[keys[0]].data.cmt_url;
  45. const isCommentsModal = appSetting('browse', 'show_in_modal', module)
  46. const showCommentsModal = async (initFormData) => {
  47. const url2 = url;
  48. setPageData({ data: 'loading', url: url, url2: url2 });
  49. const sResponse = await getPageData(url2, false);
  50. if (sResponse.data !== pageData.data) {
  51. setPageData({ data: sResponse.data, url: url, url2: url2 });
  52. }
  53. }
  54. return (
  55. <>
  56. <FormModal pageData={pageData.data} setPageData={setPageData} modalView='content_page' url={pageData.url2} />
  57. <CardList
  58. border="border-y border-x-none sm:border-x"
  59. className="mb-0.5 sm:mb-3"
  60. >
  61. <UnitComments contentUrl={url} handleReply={isCommentsModal ? showCommentsModal : 'link'} {...props.data[keys[0]]} module={props.module} />
  62. </CardList>
  63. </>
  64. )
  65. }
  66. function UnitCommentsDefault(props) {
  67. const { t } = useTranslation()
  68. const { currentUser } = useCurrentUser()
  69. const [viewState, setViewState] = useState({ view: '' })
  70. const [postData, setPostData] = useState(null)
  71. const level = props.level || 0
  72. const lvls = props.lvls || []
  73. const hasData = !!props.data
  74. let data = props.data || {}
  75. const items = props.items
  76. const view = props.view
  77. let files = props.files
  78. const maxLevel = props.max_level
  79. const parent = props.parent
  80. const module = props.module;
  81. const objectId = data.cmt_object_id;
  82. const commentId = data.cmt_id;
  83. const cmtUrl = data.cmt_url
  84. const handleReply = useCallback(
  85. (data, isNoReaction) => {
  86. if (!isNoReaction) {
  87. FeedbackHaptics('Medium')
  88. }
  89. if (props.handleReply) {
  90. props.handleReply(data)
  91. }
  92. else {
  93. emitter.emit(EVENTS.commentThread(module, objectId), { action: 'reply_comment', data: data });
  94. }
  95. },
  96. [props.handleReply]
  97. )
  98. useEffect(() => {
  99. if (props.replyId == 'cmt_id=' + data.cmt_id) {
  100. handleReply(data, true)
  101. }
  102. }, [props.replyId])
  103. const isSelected = props.selectedId == data.cmt_id
  104. const { animatedStyle } = usePulseOne({ pulseDurationMs: 500, pulses: 3, autoStart: true, minOpacity: 0.2 });
  105. const editFormUrl = hasData
  106. ? '/api.php?r=' +
  107. appSetting('urls', 'cmts') +
  108. '/&params[]={"module":"' +
  109. props.module +
  110. '","object_id":' +
  111. objectId +
  112. ',"action":"edit","id":' +
  113. commentId +
  114. '}'
  115. : ''
  116. const { data: dynamicData } = useFetchForm(editFormUrl, postData)
  117. const onFormSubmit = useCallback((formData) => {
  118. setViewState({ view: '' })
  119. setPostData(formData)
  120. }, [])
  121. if (hasData && dynamicData?.data?.browse?.data?.data[0]['i' + commentId]) {
  122. data =
  123. dynamicData?.data?.browse?.data?.data[0]['i' + commentId]
  124. .data
  125. files =
  126. dynamicData?.data?.browse?.data?.data[0]['i' + commentId]
  127. .files
  128. }
  129. const cells = useMemo(() => {
  130. const cellsArray = []
  131. const effectiveLevel = Math.min(level, maxLevel)
  132. for (let i = 0; i < effectiveLevel; i++) {
  133. cellsArray.push(
  134. <View key={`sp-${level}-${i}`} className="w-8">
  135. {lvls[i + 1] && (
  136. <View className="mx-auto w-0.5 gap-0.5 -mt-3 flex-auto">
  137. <View className="mx-auto w-0.5 flex-auto bg-muted rounded-b-full" />
  138. <View className="mx-auto w-0.5 h-0.5 flex-none bg-muted rounded-full" />
  139. <View className="mx-auto w-0.5 h-0.5 flex-none bg-muted rounded-full" />
  140. <View className="mx-auto w-0.5 h-0.5 flex-none bg-muted rounded-full" />
  141. </View>
  142. )}
  143. {i === level - 1 && (
  144. <View className=" ml-4 -inset-s-px h-7 w-6 border-muted mt-px border-l-2 border-b-2 absolute top-0 rounded-bl-lg flex-auto" />
  145. )}
  146. </View>
  147. )
  148. }
  149. return cellsArray
  150. }, [level, maxLevel, lvls])
  151. // UNA sends every comment file here: images and videos go to the carousel, the rest to a file list.
  152. const { imageList, fileList } = useMemo(() => {
  153. const media = []
  154. const other = []
  155. for (const file of files || []) {
  156. const kind = file?.is_image ? 'image' : attachmentKindByName(file?.file_name)
  157. if (kind === 'image') {
  158. const item = toUnaDisplayImageItem(file)
  159. if (item) media.push(item)
  160. } else if (kind === 'video' && file?.file) {
  161. media.push({ src: file.file, type: 'video' })
  162. } else if (file?.file) {
  163. other.push(file)
  164. }
  165. }
  166. return { imageList: media, fileList: other }
  167. }, [files])
  168. if (!hasData) return null
  169. if (viewState.view == 'deleted') return <></>
  170. if (viewState.view == 'edited')
  171. return (
  172. <Modal
  173. title={t('Edit comment')}
  174. onVisible={true}
  175. onClose={() => {
  176. setViewState({ view: '' })
  177. }}
  178. transparent={true}
  179. headerBorder={true}
  180. >
  181. <View className="p-2">
  182. <Form
  183. {...viewState.data}
  184. classContainerName="flex-row flex-wrap w-full items-start justify-between"
  185. onFormSubmit={onFormSubmit}
  186. />
  187. </View>
  188. </Modal>
  189. )
  190. const Badges = components['molecule']['badges'];
  191. const isShowReply = data?.menu_actions?.items?.find(
  192. (x) => x.name === 'item-reply'
  193. )
  194. return (
  195. <Animated.View style={isSelected ? animatedStyle : {}}>
  196. <Row className="gap-2">
  197. {cells}
  198. <View className="w-8 min-h-8 z-50 mt-2.5">
  199. <View className="w-8 my-0.5 shadow-xs rounded-full">
  200. <Profile
  201. {...data.author_data}
  202. displayType="unit_wo_info"
  203. displaySize="sm"
  204. showInfo="false"
  205. /></View>
  206. {items?.length != 0 && view != 'flat' && (
  207. <View className="w-0.5 mx-auto top-0.5 rounded-full flex-auto bg-muted"></View>
  208. )}
  209. </View>
  210. <View className=" flex-col mt-2 flex-1">
  211. <View className="bg-bubble rounded-xl py-2 px-3 gap-0.5 me-auto max-w-full">
  212. <View className="flex-row items-center gap-3 justify-between ">
  213. <Row className="gap-3 items-center pr-8">
  214. <Profile
  215. {...data.author_data}
  216. displayType="unit_wo_image"
  217. displaySize="sm"
  218. showInfo="false"
  219. showInfo2={<Badges badges={data.author_badges} size="3xs" />}
  220. />
  221. {maxLevel < data.cmt_level &&
  222. appSetting('comments', 'in_reply') &&
  223. parent?.data && (
  224. <Row className="items-center gap-1">
  225. <Text className="text-secondary-foreground -ml-2 text-xs ">
  226. in reply to
  227. </Text>
  228. <Profile
  229. {...parent.data.author_data}
  230. displayType="unit_wo_image"
  231. displaySize="xs"
  232. showInfo="false"
  233. />
  234. </Row>
  235. )}
  236. <Link href={cmtUrl} size="xs" variant="secondary" >
  237. <Time ts={data.cmt_time} className="text-secondary-foreground"></Time>
  238. </Link>
  239. </Row>
  240. {!!data.cmt_mood && (
  241. <>
  242. <View className="text-muted-foreground">
  243. <Icon icon='Dot' size={14} />
  244. </View>
  245. <StarsView
  246. rating={data.cmt_mood}
  247. starSize={20}
  248. />
  249. </>
  250. )}
  251. <View className="flex-none absolute -inset-e-1">
  252. <MenuManage
  253. id={data.id}
  254. menu={data?.menu_manage}
  255. setViewState={setViewState}
  256. module={props.module}
  257. cmt_object_id={objectId}
  258. cmt_id={commentId}
  259. /></View>
  260. </View>
  261. {view == 'flat' && data.cmt_parent_id > 0 && (
  262. <View className=" border border-border/60 rounded-md p-2 my-1">
  263. <View className="flex-row items-baseline">
  264. <View>
  265. <Text className="text-xs text-muted-foreground">
  266. In Reply to{' '}
  267. </Text>
  268. </View>
  269. <View className=" "></View>
  270. </View>
  271. <ContentMore
  272. content={data.cmt_parent.data.cmt_text}
  273. numberOfLines={2}
  274. numberOfSymbols={200}
  275. openSmall={false}
  276. customClassName="u-vanilla-html-small"
  277. />
  278. </View>
  279. )}
  280. <View className="max-w-full">
  281. <ContentMore
  282. content={data.cmt_text}
  283. numberOfLines={3}
  284. numberOfSymbols={360}
  285. openSmall={false}
  286. showLess={true}
  287. customClassName="u-vanilla-html-small"
  288. />
  289. {!!data.embed && (
  290. <View>
  291. <Embed data={data.embed} size="small" />
  292. </View>
  293. )}
  294. </View>
  295. {viewState.view != 'edited' && imageList?.length > 0 && (
  296. <View className="max-w-xs w-full">
  297. {/* max-w-xs: tiles never need more than 320px of image */}
  298. <Carousel data={imageList} maxWidth={320} />
  299. </View>
  300. )}
  301. {viewState.view != 'edited' && fileList.length > 0 && (
  302. <View className="max-w-xs w-full gap-1">
  303. {fileList.map((file) => (
  304. <FileCard key={file.file_id} href={file.file} name={file.file_name} size={file.file_size} compact />
  305. ))}
  306. </View>
  307. )}
  308. </View>
  309. {(viewState.view != 'edited' && !data.disabled) && (
  310. <Row className="gap-2">
  311. {(!!currentUser && isShowReply) && (
  312. props.handleReply === 'link' ? (
  313. <View>
  314. <NeoButtonLink
  315. href={`${props.contentUrl}#cmt_id=${data.cmt_id}`}
  316. style="borderless"
  317. controlSize="mini"
  318. borderShape="capsule"
  319. align="start"
  320. image="MessageCircle"
  321. label={t('Reply')}
  322. />
  323. </View>
  324. ) : (
  325. <NeoButton
  326. style="borderless"
  327. controlSize="mini"
  328. borderShape="capsule"
  329. align="start"
  330. image="MessageCircle"
  331. label={t('Reply')}
  332. haptics={false}
  333. onPress={() => handleReply(data)}
  334. />
  335. )
  336. )}
  337. <View className="flex-row flex-auto gap-2 ">
  338. {data.menu_actions?.items?.length > 0 && <Menu
  339. {...data.menu_actions}
  340. displayType="element"
  341. showMatched={true}
  342. params={{
  343. show_action: true,
  344. show_counter: false,
  345. show_combined: false,
  346. button_size: 'xs',
  347. button_variant: 'link',
  348. button_rounded: true,
  349. }}
  350. />}
  351. <View className="ml-auto flex-row items-center gap-1">
  352. {data.menu_actions?.items?.length > 0 && <Menu
  353. {...data.menu_actions}
  354. displayType="element"
  355. showMatched={true}
  356. params={{
  357. show_action: false,
  358. show_counter: true,
  359. show_combined: false,
  360. button_size: 'xs',
  361. button_variant: 'link',
  362. button_rounded: true,
  363. }}
  364. />}
  365. </View>
  366. </View>
  367. </Row>
  368. )}
  369. </View>
  370. </Row>
  371. </Animated.View>
  372. )
  373. }
  374. const MenuManage = ({
  375. id,
  376. menu,
  377. setViewState,
  378. module,
  379. cmt_object_id,
  380. cmt_id,
  381. }) => {
  382. const [menuData, setMenuData] = useState(false)
  383. const { t } = useTranslation()
  384. if (!menu?.object) return null
  385. if (menu.items)
  386. return (
  387. <MenuManage_
  388. id={id}
  389. menu={menu}
  390. setViewState={setViewState}
  391. module={module}
  392. cmt_object_id={cmt_object_id}
  393. cmt_id={cmt_id}
  394. />
  395. )
  396. if (!menuData)
  397. return (
  398. <NeoButton
  399. style="borderless"
  400. controlSize="mini"
  401. borderShape="circle"
  402. image="Ellipsis"
  403. accessibilityLabel={t('More options')}
  404. onPress={() => {
  405. if (Platform.OS === 'web')
  406. setMenuData({ ...menu, items: [{ name: 'loader' }] })
  407. getDataForMenu(menu, setMenuData)
  408. }}
  409. />
  410. )
  411. return (
  412. <MenuManage_
  413. id={id}
  414. menu={menuData}
  415. defaultOpen={true}
  416. setViewState={setViewState}
  417. module={module}
  418. cmt_object_id={cmt_object_id}
  419. cmt_id={cmt_id}
  420. />
  421. )
  422. }
  423. const MenuManage_ = memo(
  424. ({
  425. id,
  426. menu,
  427. setViewState,
  428. defaultOpen,
  429. module,
  430. cmt_object_id,
  431. cmt_id,
  432. }) => {
  433. let { currentUser, setCurrentUser } = useCurrentUser()
  434. const { t } = useTranslation()
  435. //const refReport = useRef(null);
  436. //const [reportTitle, setReportTitle] = useState(null);
  437. const handleManageMenuSelect = async (oItem, event) => {
  438. switch (oItem.name) {
  439. case 'item-edit':
  440. const result1 = await fetcher(
  441. '/api.php?r=' +
  442. appSetting('urls', 'cmts') +
  443. '/&params[]={"module":"' +
  444. module +
  445. '","object_id":' +
  446. cmt_object_id +
  447. ',"action":"edit","id":' +
  448. cmt_id +
  449. '}'
  450. )
  451. setViewState({ view: 'edited', data: result1.data.form })
  452. break
  453. case 'item-delete':
  454. const result = await fetcher(
  455. '/api.php?r=' +
  456. appSetting('urls', 'cmts') +
  457. '/&params[]={"module":"' +
  458. module +
  459. '","object_id":' +
  460. cmt_object_id +
  461. ',"action":"remove","id":' +
  462. cmt_id +
  463. '}'
  464. )
  465. emitter.emit(EVENTS.commentThread(module, cmt_object_id), { action: 'remove_content', data: { id: cmt_id } });
  466. //setViewState({ view: 'deleted' })
  467. // props.handleDelete()
  468. break
  469. /* case 'item-report':
  470. refReport.current.report(event);
  471. break;*/
  472. }
  473. }
  474. let oReport = undefined
  475. // const aMenuManageItems = menuItemsByName('comments_manage_menu', data.menu_manage.items, currentUser).map(
  476. const aMenuManageItems = !!currentUser
  477. ? menu &&
  478. menuItemsByName(
  479. 'comments_manage_menu',
  480. menu?.items,
  481. currentUser
  482. ).map((aItem) => {
  483. let sTitle = aItem.title
  484. if (!!aItem.display_type && aItem.display_type == 'element') {
  485. const Element = components['molecule'][String(aItem.data.type)]
  486. if (!!Element) {
  487. sTitle = (
  488. <Element
  489. mode="dropdown-menu"
  490. key={aItem.id ? aItem.id : aItem.name}
  491. {...aItem.data}
  492. />
  493. )
  494. }
  495. }
  496. if (aItem.name == 'loader') {
  497. sTitle = <Loading size="small" />
  498. }
  499. return {
  500. id: aItem.id ? aItem.id : aItem.name,
  501. name: aItem.name,
  502. link: aItem.link,
  503. title: sTitle,
  504. icon: aItem.icon,
  505. image: aItem.image,
  506. }
  507. })
  508. : []
  509. return (
  510. aMenuManageItems?.length > 0 && (
  511. <>
  512. <DropdownMenu
  513. mode="popup"
  514. defaultOpen={defaultOpen}
  515. items={aMenuManageItems}
  516. onSelect={handleManageMenuSelect}
  517. buttonProps={{
  518. style: 'borderless',
  519. controlSize: 'mini',
  520. borderShape: 'circle',
  521. image: 'Ellipsis',
  522. accessibilityLabel: t('More options'),
  523. }}
  524. />
  525. {!!oReport && oReport}
  526. </>
  527. )
  528. )
  529. }
  530. )