From 1bf9c78e7f7b483a2785b72ecd590cc54905b383 Mon Sep 17 00:00:00 2001 From: mehditorabiv Date: Thu, 19 Sep 2024 13:30:51 +0300 Subject: [PATCH] integrate network graph --- .../memberInteraction/ForceGraphComponent.tsx | 15 +++++-- src/pages/membersInteraction.tsx | 44 ++++++++++--------- src/store/slices/memberInteractionSlice.ts | 13 ++++-- src/store/types/IMemberInteraction.ts | 5 ++- 4 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/components/pages/memberInteraction/ForceGraphComponent.tsx b/src/components/pages/memberInteraction/ForceGraphComponent.tsx index 7e94c6d0..f12c51f2 100644 --- a/src/components/pages/memberInteraction/ForceGraphComponent.tsx +++ b/src/components/pages/memberInteraction/ForceGraphComponent.tsx @@ -1,4 +1,4 @@ -import { Avatar, Box, Paper, Popover, Typography } from '@mui/material'; +import { Avatar, Box, Popover, Typography } from '@mui/material'; import clsx from 'clsx'; import React, { useRef, useState } from 'react'; import ForceGraph2D, { @@ -24,7 +24,12 @@ interface CustomNode { id?: string; } -const ForceGraphComponent = ({ nodes, links, numberOfnodes }: any) => { +const ForceGraphComponent = ({ + nodes, + links, + numberOfnodes, + platformType, +}: any) => { const [popOverOpen, setpopOverOpen] = useState(false); const [anchorEl, setAnchorEl] = useState(null); const [user, setUser] = useState(); @@ -199,7 +204,11 @@ const ForceGraphComponent = ({ nodes, links, numberOfnodes }: any) => {
{' '}
diff --git a/src/pages/membersInteraction.tsx b/src/pages/membersInteraction.tsx index bb04d48d..321b9431 100644 --- a/src/pages/membersInteraction.tsx +++ b/src/pages/membersInteraction.tsx @@ -10,7 +10,6 @@ import HintBox from '../components/pages/memberInteraction/HintBox'; import { useToken } from '../context/TokenContext'; import { defaultLayout } from '../layouts/defaultLayout'; import useAppStore from '../store/useStore'; -import { IUser } from '../utils/types'; import { withRoles } from '../utils/withRoles'; const ForceGraphComponent = dynamic( @@ -50,7 +49,7 @@ const transformApiResponseToMockData = (apiResponse: any[]) => { ngu: from.ngu, roles: from.roles, radius: from.radius, - discordId: from.discordId, + discordId: from.id, avatar: from.avatar, }; const targetNode = { @@ -67,7 +66,7 @@ const transformApiResponseToMockData = (apiResponse: any[]) => { ngu: to.ngu, roles: to.roles, radius: to.radius, - discordId: to.discordId, + discordId: to.id, avatar: to.avatar, }; const link = { source: from.id, target: to.id, width }; @@ -89,14 +88,13 @@ const transformApiResponseToMockData = (apiResponse: any[]) => { }; function MembersInteraction() { - const { community } = useToken(); + const { community, selectedPlatform } = useToken(); const [nodes, setNodes] = useState([]); const [links, setLinks] = useState([]); const [nodeSizes, setNodeSizes] = useState([]); - const [user, setUser] = useState(); const [popoverAnchorEl, setPopoverAnchorEl] = useState( null ); @@ -104,21 +102,22 @@ function MembersInteraction() { const { getMemberInteraction, isLoading } = useAppStore(); useEffect(() => { - const platformId = community?.platforms.find( - (platform) => - platform.disconnectedAt === null && platform.name === 'discord' - )?.id; - - if (platformId) { - getMemberInteraction(platformId).then((apiResponse: any[]) => { - const { nodes, links } = transformApiResponseToMockData(apiResponse); - const nodeSizes = nodes.map((node) => node.size); - setNodes(nodes); - setLinks(links); - setNodeSizes(nodeSizes); - }); + const platform = community?.platforms.find( + (platform) => platform.id === selectedPlatform + ); + + if (platform) { + getMemberInteraction(selectedPlatform, platform.name).then( + (apiResponse: any[]) => { + const { nodes, links } = transformApiResponseToMockData(apiResponse); + const nodeSizes = nodes.map((node) => node.size); + setNodes(nodes); + setLinks(links); + setNodeSizes(nodeSizes); + } + ); } - }, []); + }, [selectedPlatform]); const handlePopoverOpen = (event: React.MouseEvent) => { setPopoverAnchorEl(event.currentTarget); @@ -156,12 +155,17 @@ function MembersInteraction() {

Data from the last 7 days

-
+
platform.id === selectedPlatform + )?.name + } />
diff --git a/src/store/slices/memberInteractionSlice.ts b/src/store/slices/memberInteractionSlice.ts index 3a7b8a84..d2946e39 100644 --- a/src/store/slices/memberInteractionSlice.ts +++ b/src/store/slices/memberInteractionSlice.ts @@ -6,12 +6,17 @@ import { axiosInstance } from '../../axiosInstance'; const createHeatmapSlice: StateCreator = (set, get) => ({ isLoading: false, memberInteractionRecords: [], - getMemberInteraction: async (platformId: string) => { + getMemberInteraction: async ( + platformId: string, + platformType: 'discord' | 'discourse' + ) => { try { + const endpoint = + platformType === 'discourse' + ? `/discourse/member-activity/${platformId}/members-interactions-network-graph` + : `/member-activity/${platformId}/members-interactions-network-graph`; set(() => ({ isLoading: true })); - const { data } = await axiosInstance.post( - `/member-activity/${platformId}/members-interactions-network-graph` - ); + const { data } = await axiosInstance.post(endpoint); set({ memberInteractionRecords: [...data], isLoading: false }); return data; } catch (error) { diff --git a/src/store/types/IMemberInteraction.ts b/src/store/types/IMemberInteraction.ts index 49d2e275..796fff51 100644 --- a/src/store/types/IMemberInteraction.ts +++ b/src/store/types/IMemberInteraction.ts @@ -1,5 +1,8 @@ export default interface IMemberInteraction { isLoading: boolean; memberInteractionRecords: any[]; - getMemberInteraction: (platformId: string) => any; + getMemberInteraction: ( + platformId: string, + platformType: 'discord' | 'discourse' + ) => any; }