Skip to content

Commit

Permalink
Hide exit node indicator for regular users
Browse files Browse the repository at this point in the history
  • Loading branch information
heisbrot committed Apr 17, 2024
1 parent 3d0c51d commit 5633273
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/modules/exit-node/useHasExitNodes.tsx
Original file line number Diff line number Diff line change
@@ -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<Route[]>(`/routes`);
const { isOwnerOrAdmin } = useLoggedInUser();
const { data: routes } = useFetchApi<Route[]>(
`/routes`,
false,
true,
isOwnerOrAdmin,
);
return peer
? routes?.some(
(route) =>
Expand Down
7 changes: 5 additions & 2 deletions src/modules/peers/PeerNameCell.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
Expand All @@ -29,7 +30,9 @@ export default function PeerNameCell({ peer }: Props) {
<ActiveInactiveRow
active={peer.connected}
text={peer.name}
additionalInfo={<ExitNodePeerIndicator peer={peer} />}
additionalInfo={
isOwnerOrAdmin && <ExitNodePeerIndicator peer={peer} />
}
>
<div className={"text-nb-gray-400 font-light truncate"}>
{userOfPeer?.email}
Expand Down
2 changes: 2 additions & 0 deletions src/utils/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ export default function useFetchApi<T>(
url: string,
ignoreError = false,
revalidate = true,
allowFetch = true,
) {
const { fetch } = useNetBirdFetch(ignoreError);
const handleErrors = useApiErrorHandling(ignoreError);

const { data, error, isLoading, isValidating, mutate } = useSWR(
url,
async (url) => {
if (!allowFetch) return;
return apiRequest<T>(fetch, "GET", url).catch((err) =>
handleErrors(err as ErrorResponse),
);
Expand Down

0 comments on commit 5633273

Please # to comment.