Skip to content

Commit

Permalink
Merge pull request #529 from Kodylow/current-service-in-header
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodylow authored Sep 23, 2024
2 parents c7ec89f + bc26137 commit b3d207e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
28 changes: 20 additions & 8 deletions apps/router/src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React from 'react';
import { Flex } from '@chakra-ui/react';
import React, { useMemo } from 'react';
import { Flex, Text } from '@chakra-ui/react';
import { Logo } from './Logo';
import { ServiceMenu } from './ServiceMenu';
import { useActiveService } from '../hooks';
import { useAppContext } from '../context/hooks';

export const Header = React.memo(function Header() {
const { guardians, gateways } = useAppContext();
const { activeServiceId } = useActiveService();
const { type, id } = useActiveService();
const activeService = useMemo(() => {
const serviceMap = { guardian: guardians, gateway: gateways };
return serviceMap[type]?.[id] ?? null;
}, [type, id, guardians, gateways]);

const hasServices =
Object.keys(guardians).length > 0 || Object.keys(gateways).length > 0;
Expand All @@ -20,11 +24,19 @@ export const Header = React.memo(function Header() {
>
<Logo />
{hasServices && (
<ServiceMenu
guardians={guardians}
gateways={gateways}
activeServiceId={activeServiceId}
/>
<Flex alignItems='center'>
{activeService && (
<Text mr={2} fontSize='sm' color='gray.600'>
{type === 'guardian' ? 'Guardian' : 'Gateway'}:{' '}
{activeService.config.baseUrl}
</Text>
)}
<ServiceMenu
guardians={guardians}
gateways={gateways}
activeServiceId={id}
/>
</Flex>
)}
</Flex>
);
Expand Down
14 changes: 9 additions & 5 deletions apps/router/src/hooks/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { useLocation } from 'react-router-dom';

export const useActiveService = () => {
export const useActiveService = (): {
type: 'guardian' | 'gateway';
id: string;
} => {
const location = useLocation();
const [type, id] = location.pathname.split('/').filter(Boolean);

if (type !== 'guardian' && type !== 'gateway') {
throw new Error('Invalid service type');
}
return {
activeServiceId: type && id ? `${type}/${id}` : null,
serviceType: type as 'guardian' | 'gateway' | null,
serviceId: id || null,
type,
id,
};
};

0 comments on commit b3d207e

Please sign in to comment.