{"version":3,"file":"Comments-031301df.js","sources":["../src/hooks/useComments.js","../src/hooks/useCommentPost.js","../src/components/Comments.jsx"],"sourcesContent":["import { useRef, useState, useCallback, useEffect } from 'react';\nimport { GetToken } from '@utils/storage';\nimport * as config from '@config';\nimport PropTypes from 'prop-types';\nimport { useWs } from '../contexts/WsContext';\nimport { PubSubDataType, PubSubCMD } from '../wsManager';\n\nfunction useComments() {\n const [fetching, setFetching] = useState(false);\n const [comments, setComments] = useState([]);\n const abortControllerRef = useRef(null);\n const fetchParamsRef = useRef({ commentOnID: null, selfOnly: false }); // Store the current query params\n const { wsManager } = useWs();\n\n useEffect(() => {\n if (!wsManager) return;\n const handleMessage = (data) => {\n // console.log('useComments received message:', data);\n try {\n const message = JSON.parse(data);\n switch (message.type) {\n case PubSubDataType.CMD:\n if (message.data === PubSubCMD.REFRESH_COMMENTS) {\n console.log('cmd to refresh comments');\n const { commentOnID, selfOnly } = fetchParamsRef.current;\n fetchComments(commentOnID, { selfOnly }).catch((err) => {\n console.error('Error refreshing comments:', err);\n });\n }\n break;\n default:\n console.log('dont know what to do with this message');\n break;\n }\n } catch (error) {\n console.err('Error parsing message');\n }\n };\n wsManager.addMessageListener(handleMessage);\n // Cleanup function to remove the listener when the component unmounts\n return () => {\n wsManager.removeMessageListener(handleMessage);\n };\n }, [wsManager]);\n\n const fetchComments = useCallback(async (commentOnID, { selfOnly = false }) => {\n setFetching(true);\n abortControllerRef.current = new AbortController();\n const query = {\n id: commentOnID,\n selfOnly: selfOnly,\n };\n const queryString = new URLSearchParams(query).toString();\n fetchParamsRef.current = { commentOnID, selfOnly };\n try {\n const response = await fetch(`${config.apiURL}/comments?${queryString}`, {\n method: 'GET',\n headers: { Authorization: `Bearer ${GetToken()}` },\n signal: abortControllerRef.current.signal,\n });\n\n //Uses response.ok to check for successful responses, which covers all 2xx status codes.\n if (!response.ok) {\n const data = await response.json();\n throw new Error(data.error || 'An error occurred while updating the order');\n }\n const data = await response.json();\n setComments(data.comments || []);\n return data;\n } catch (error) {\n if (error.name === 'AbortError') {\n console.log('Request was aborted');\n throw new Error('Request was aborted');\n } else {\n console.error('Error fetch comments:', error);\n }\n throw error;\n } finally {\n setFetching(false);\n }\n }, []);\n\n fetchComments.propTypes = {\n commentOnID: PropTypes.string.isRequired,\n options: PropTypes.shape({\n selfOnly: PropTypes.bool,\n }),\n };\n\n const abort = useCallback(() => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort();\n }\n }, []);\n\n return { fetching, comments, fetchComments, abort };\n}\n\nexport default useComments;\n","import { useState, useRef, useCallback } from 'react';\nimport PropTypes from 'prop-types';\nimport { GetToken } from '@utils/storage';\nimport * as config from '@config';\n\nfunction useConmmentPost() {\n const [posting, setPosting] = useState(false);\n const abortControllerRef = useRef(null);\n\n const postComment = useCallback(async ({ commentOnID, comment }) => {\n if (!comment) return;\n //use fetch to post comment\n setPosting(true);\n const req = {\n comment: comment,\n commentOnID: commentOnID,\n };\n console.log('req', req);\n abortControllerRef.current = new AbortController();\n try {\n const response = await fetch(`${config.apiURL}/comments`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${GetToken()}`,\n },\n body: JSON.stringify(req),\n signal: abortControllerRef.current.signal,\n });\n if (!response.ok) {\n const data = await response.json();\n throw new Error(data.error || 'An error occurred post a comment');\n }\n const result = await response.json();\n return result;\n } catch (error) {\n if (error.name === 'AbortError') {\n console.log('Request was aborted');\n throw new Error('Request was aborted');\n } else {\n console.error('Error posting a comment:', error);\n }\n throw error;\n } finally {\n setPosting(false);\n }\n }, []);\n\n postComment.propTypes = {\n commentOnID: PropTypes.string.isRequired,\n comment: PropTypes.string.isRequired,\n };\n\n const abort = useCallback(() => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort();\n }\n }, []);\n\n return { posting, postComment, abort };\n}\n\nuseConmmentPost.propTypes = {};\n\nexport default useConmmentPost;\n","import React from 'react';\nimport PropTypes from 'prop-types';\nimport { useState, useEffect } from 'react';\nimport { Timeago } from '@utils/utils';\nimport { GetUserInfo } from '../utils/storage';\nimport useComments from '../hooks/useComments';\nimport { App, Button, Space } from 'antd';\nimport useConmmentPost from '../hooks/useCommentPost';\nimport { List, Input } from 'antd';\n\nconst { TextArea } = Input;\n\nfunction Comments({ commentOnID = '' }) {\n const loginInfo = GetUserInfo();\n const { fetching, comments, fetchComments, abort: abortFetch } = useComments();\n const { posting, postComment, abort: abortPost } = useConmmentPost();\n const { message } = App.useApp();\n const [addedComment, setAddedComment] = useState('');\n\n useEffect(() => {\n if (commentOnID) {\n fetchData();\n }\n return () => {\n abortFetch();\n abortPost();\n };\n }, [commentOnID]);\n\n const fetchData = () => {\n if (commentOnID) {\n fetchComments(commentOnID, { selfOnly: false }).catch((err) => {\n message.error(err.toString());\n });\n }\n };\n\n const handleSubmit = () => {\n if (addedComment) {\n const req = {\n comment: addedComment,\n commentOnID: commentOnID,\n };\n postComment(req)\n .then((res) => {\n setAddedComment('');\n fetchData();\n message.success(res.reply);\n })\n .catch((err) => {\n let msg = err.toString();\n if (err.response?.data?.error) {\n console.log('***', err.response.data.error);\n msg = err.response.data.error;\n }\n window.alert(msg);\n });\n }\n };\n\n return (\n
{comment.content}\n