From 56332738d845a975c81471e53b187aaa9a381246 Mon Sep 17 00:00:00 2001 From: Eduard Gert Date: Wed, 17 Apr 2024 10:44:58 +0200 Subject: [PATCH] Hide exit node indicator for regular users --- src/modules/exit-node/useHasExitNodes.tsx | 9 ++++++++- src/modules/peers/PeerNameCell.tsx | 7 +++++-- src/utils/api.tsx | 2 ++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/modules/exit-node/useHasExitNodes.tsx b/src/modules/exit-node/useHasExitNodes.tsx index 0337c5ca..288fab2c 100644 --- a/src/modules/exit-node/useHasExitNodes.tsx +++ b/src/modules/exit-node/useHasExitNodes.tsx @@ -1,9 +1,16 @@ import useFetchApi from "@utils/api"; +import { useLoggedInUser } from "@/contexts/UsersProvider"; import { Peer } from "@/interfaces/Peer"; import { Route } from "@/interfaces/Route"; export const useHasExitNodes = (peer?: Peer) => { - const { data: routes } = useFetchApi(`/routes`); + const { isOwnerOrAdmin } = useLoggedInUser(); + const { data: routes } = useFetchApi( + `/routes`, + false, + true, + isOwnerOrAdmin, + ); return peer ? routes?.some( (route) => diff --git a/src/modules/peers/PeerNameCell.tsx b/src/modules/peers/PeerNameCell.tsx index 7ddcfc52..b9fe70b8 100644 --- a/src/modules/peers/PeerNameCell.tsx +++ b/src/modules/peers/PeerNameCell.tsx @@ -1,7 +1,7 @@ import { useRouter } from "next/navigation"; import * as React from "react"; import { useMemo } from "react"; -import { useUsers } from "@/contexts/UsersProvider"; +import { useLoggedInUser, useUsers } from "@/contexts/UsersProvider"; import { Peer } from "@/interfaces/Peer"; import ActiveInactiveRow from "@/modules/common-table-rows/ActiveInactiveRow"; import { ExitNodePeerIndicator } from "@/modules/exit-node/ExitNodePeerIndicator"; @@ -12,6 +12,7 @@ type Props = { export default function PeerNameCell({ peer }: Props) { const { users } = useUsers(); const router = useRouter(); + const { isOwnerOrAdmin } = useLoggedInUser(); const userOfPeer = useMemo(() => { return users?.find((user) => user.id === peer.user_id); @@ -29,7 +30,9 @@ export default function PeerNameCell({ peer }: Props) { } + additionalInfo={ + isOwnerOrAdmin && + } >
{userOfPeer?.email} diff --git a/src/utils/api.tsx b/src/utils/api.tsx index c2495a24..2bd6b13c 100644 --- a/src/utils/api.tsx +++ b/src/utils/api.tsx @@ -93,6 +93,7 @@ export default function useFetchApi( url: string, ignoreError = false, revalidate = true, + allowFetch = true, ) { const { fetch } = useNetBirdFetch(ignoreError); const handleErrors = useApiErrorHandling(ignoreError); @@ -100,6 +101,7 @@ export default function useFetchApi( const { data, error, isLoading, isValidating, mutate } = useSWR( url, async (url) => { + if (!allowFetch) return; return apiRequest(fetch, "GET", url).catch((err) => handleErrors(err as ErrorResponse), );