From 7135fcf19d0847648935c358a1e85db86dea80a8 Mon Sep 17 00:00:00 2001 From: Luke Berndt Date: Wed, 18 Jan 2023 10:10:20 -0500 Subject: [PATCH] moved the links stuff to a component --- frontend/src/Call/CallInfo.js | 84 ++++++++++++++++++--------------- frontend/src/Call/CallLinks.js | 36 ++++++++++++++ frontend/src/Call/CallPlayer.js | 47 ++---------------- 3 files changed, 86 insertions(+), 81 deletions(-) create mode 100644 frontend/src/Call/CallLinks.js diff --git a/frontend/src/Call/CallInfo.js b/frontend/src/Call/CallInfo.js index 71ae3c6..7f75a35 100644 --- a/frontend/src/Call/CallInfo.js +++ b/frontend/src/Call/CallInfo.js @@ -1,4 +1,7 @@ import React from "react"; +import { useCallLink } from "./CallLinks"; +import { useGetGroupsQuery, useGetTalkgroupsQuery } from '../features/api/apiSlice' +import { Link, useLocation, useParams, useNavigate } from 'react-router-dom'; import { Header, Divider, @@ -11,39 +14,46 @@ import { // ---------------------------------------------------- function CallInfo(props) { - let srcList = ""; - let callLength = "-"; - let callFreq = "-"; - let callDate = "-"; - let callTime = "-"; - let talkgroupNum = "-"; - let callDownload = ""; - let callTweet = ""; - if (props.call) { - const currentCall = props.call; - var time = new Date(currentCall.time); - callTime = time.toLocaleTimeString(); - callDate = time.toLocaleDateString(); - if (currentCall.freq) { - var freq = currentCall.freq / 1000000; - callFreq = Math.round(freq * 1000) / 1000; - } + const { shortName } = useParams(); + const { data: talkgroupsData, isSuccess: isTalkgroupsSuccess } = useGetTalkgroupsQuery(shortName); - srcList = currentCall.srcList.map((source, index) => {source.src}[{source.pos}]); - callLength = currentCall.len; - talkgroupNum = currentCall.talkgroupNum; - callDownload = currentCall.url; - callTweet = "https://twitter.com/intent/tweet?url="+encodeURIComponent(document.location.origin+props.link)+"&via="+encodeURIComponent("OpenMHz"); + let srcList = ""; + let callLength = "-"; + let callFreq = "-"; + let callDate = "-"; + let callTime = "-"; + let talkgroupNum = "-"; + let header = "Call Info" + + if (props.call) { + const currentCall = props.call; + if ((talkgroupsData) && talkgroupsData.talkgroups[currentCall.talkgroupNum]) { + header = talkgroupsData.talkgroups[currentCall.talkgroupNum].description; + } + var time = new Date(currentCall.time); + callTime = time.toLocaleTimeString(); + callDate = time.toLocaleDateString(); + if (currentCall.freq) { + var freq = currentCall.freq / 1000000; + callFreq = Math.round(freq * 1000) / 1000; } - return ( -
+ + srcList = currentCall.srcList.map((source, index) => {source.src}[{source.pos}]); + callLength = currentCall.len; + talkgroupNum = currentCall.talkgroupNum; + + } + const {callLink,callDownload,callTweet} = useCallLink(props.call) + + return ( +
{props.header}
{srcList} - + Seconds {callLength} @@ -55,32 +65,32 @@ function CallInfo(props) { - + {callTime} - + {callDate} - + {callFreq} MHz
- - Tweet - Download - Link - -
- ); - } + + Tweet + Download + Link + +
+ ); +} - export default CallInfo; +export default CallInfo; diff --git a/frontend/src/Call/CallLinks.js b/frontend/src/Call/CallLinks.js new file mode 100644 index 0000000..23a5276 --- /dev/null +++ b/frontend/src/Call/CallLinks.js @@ -0,0 +1,36 @@ +import { useState, useEffect } from 'react'; +import { useSelector, useDispatch } from 'react-redux'; + +export function useCallLink(currentCall) { + const filterType = useSelector((state) => state.callPlayer.filterType); + const filterGroupId = useSelector((state) => state.callPlayer.filterGroupId); + const filterTalkgroups = useSelector((state) => state.callPlayer.filterTalkgroups); + const shortName = useSelector((state) => state.callPlayer.shortName); + if (currentCall) { + let search = "" + const time = new Date(currentCall.time); + const callTime = time.toLocaleTimeString(); + const callDate = time.toLocaleDateString(); + + switch (filterType) { + + case 1: + search = `filter-type=group&filter-code=${filterGroupId}&`; + break; + case 2: + search = `filter-type=talkgroup&filter-code=${filterTalkgroups}&`; + break; + default: + case 0: + break; + + } + const callLink = "/system/" + shortName + "?" + search + "call-id=" + currentCall._id + "&time=" + (time.getTime() + 1); + const callDownload = currentCall.url; + const callTweet = "https://twitter.com/intent/tweet?url=" + encodeURIComponent(document.location.origin + callLink) + "&via=" + encodeURIComponent("OpenMHz"); + + return { callLink, callDownload, callTweet } + } else { + return { callLink: "", callDownload: "", callTweet: "" } + } +} \ No newline at end of file diff --git a/frontend/src/Call/CallPlayer.js b/frontend/src/Call/CallPlayer.js index 1c87395..a670a9c 100644 --- a/frontend/src/Call/CallPlayer.js +++ b/frontend/src/Call/CallPlayer.js @@ -24,7 +24,7 @@ import { import "./CallPlayer.css"; import queryString from '../query-string'; import io from 'socket.io-client'; - +import { useCallLink } from "./CallLinks"; const socket = io(process.env.REACT_APP_BACKEND_SERVER); @@ -55,9 +55,7 @@ function CallPlayer(props) { const [isPlaying, setIsPlaying] = useState(false); - const filterType = useSelector((state) => state.callPlayer.filterType); - const filterGroupId = useSelector((state) => state.callPlayer.filterGroupId); - const filterTalkgroups = useSelector((state) => state.callPlayer.filterTalkgroups); + const {callLink,callDownload,callTweet} = useCallLink(props.call) @@ -109,9 +107,6 @@ function CallPlayer(props) { } } - - - useEffect(() => { if (loadNewerInView && callsData && (callsData.ids.length > 0)) { dispatch(getNewerCalls({})); @@ -142,40 +137,6 @@ function CallPlayer(props) { } }, [callsData]) -/* - useEffect(() => { - - }, [callsData]); -*/ - - - var callInfoHeader = "Call Info"; - var callLink = "" - var callDownload = "" - if (currentCall) { - if ((talkgroupsData) && talkgroupsData[currentCall.talkgroupNum]) { - callInfoHeader = talkgroupsData[currentCall.talkgroupNum].description; - } - const callDate = new Date(currentCall.time); - var search = "" - switch (filterType) { - - case 1: - search = `filter-type=group&filter-code=${filterGroupId}&`; - break; - case 2: - search = `filter-type=talkgroup&filter-code=${filterTalkgroups}&`; - break; - default: - case 0: - break; - - } - callLink = "/system/" + shortName + "?" + search + "call-id=" + currentCall._id + "&time=" + (callDate.getTime() + 1); - callDownload = currentCall.url; - } - - return (
@@ -187,13 +148,11 @@ function CallPlayer(props) {
- - - +