Skip to content

Commit

Permalink
Update frontend/resourceadm/pages/PageLayout/ResourceadmHeader/Resour…
Browse files Browse the repository at this point in the history
…ceadmHeader.tsx

Co-authored-by: Martin Gunnerud <mgunnerud@gmail.com>
  • Loading branch information
wrt95 and mgunnerud authored Sep 5, 2024
1 parent 46f9e69 commit 542f898
Showing 1 changed file with 13 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useContext } from 'react';
import classes from './ResourceadmHeader.module.css';
import { useNavigate } from 'react-router-dom';
import { getOrgNameByUsername, getOrgUsernameByUsername } from '../../../utils/userUtils';
import { getOrgNameByUsername } from '../../../utils/userUtils';
import { GiteaHeader } from 'app-shared/components/GiteaHeader';
import { useUrlParams } from '../../../hooks/useUrlParams';
import {
Expand All @@ -11,29 +11,23 @@ import {
useMediaQuery,
StudioAvatar,
} from '@studio/components';
import { repositoryBasePath, repositoryOwnerPath } from 'app-shared/api/paths';
import { repositoryOwnerPath } from 'app-shared/api/paths';
import { type Organization } from 'app-shared/types/Organization';
import { useTranslation } from 'react-i18next';
import { SelectedContextType, HeaderContext } from 'resourceadm/context/HeaderContext';
import { type SelectedContextType, HeaderContext } from 'resourceadm/context/HeaderContext';
import { MEDIA_QUERY_MAX_WIDTH } from 'app-shared/constants';
import { useLogoutMutation } from 'app-shared/hooks/mutations/useLogoutMutation';

export const ResourceadmHeader = () => {
const { org = SelectedContextType.Self } = useUrlParams();
const { org } = useUrlParams();
const selectedContext = org;

const { selectableOrgs } = useContext(HeaderContext);

return (
<StudioPageHeader>
<StudioPageHeader.Main>
<StudioPageHeader.Left
title={
selectedContext !== SelectedContextType.All &&
selectedContext !== SelectedContextType.Self &&
getOrgNameByUsername(selectedContext, selectableOrgs)
}
/>
<StudioPageHeader.Left title={getOrgNameByUsername(selectedContext, selectableOrgs)} />
<StudioPageHeader.Right>
<ResourceadmHeaderMenu />
</StudioPageHeader.Right>
Expand All @@ -48,7 +42,7 @@ export const ResourceadmHeader = () => {
const ResourceadmHeaderMenu = () => {
const { t } = useTranslation();
const shouldHideButtonText = useMediaQuery(MEDIA_QUERY_MAX_WIDTH);
const { org: selectedContext = SelectedContextType.Self } = useUrlParams();
const { org: selectedContext } = useUrlParams();
const { mutate: logout } = useLogoutMutation();
const { user, selectableOrgs } = useContext(HeaderContext);
const navigate = useNavigate();
Expand All @@ -58,36 +52,18 @@ const ResourceadmHeaderMenu = () => {

const username = user.full_name || user.login;

if (
selectedContext !== SelectedContextType.All &&
selectedContext !== SelectedContextType.Self
) {
return t('shared.header_user_for_org', {
user: username,
org: getOrgNameByUsername(selectedContext, selectableOrgs),
});
}
return username;
return t('shared.header_user_for_org', {
user: username,
org: getOrgNameByUsername(selectedContext, selectableOrgs),
});
};

const handleSetSelectedContext = (context: string | SelectedContextType) => {
navigate('/' + context + location.search);
navigate(`/${context}/${context}-resources`);
};

const org = getOrgUsernameByUsername(selectedContext, selectableOrgs);

const getRepoPath = () => {
const owner = org || user?.login;
if (owner) {
return repositoryOwnerPath(owner);
}
return repositoryBasePath();
};

const allMenuItem: StudioProfileMenuItem = {
action: { type: 'button', onClick: () => handleSetSelectedContext(SelectedContextType.All) },
itemName: t('shared.header_all'),
isActive: selectedContext === SelectedContextType.All,
return repositoryOwnerPath(selectedContext);
};

const selectableOrgMenuItems: StudioProfileMenuItem[] =
Expand All @@ -97,13 +73,6 @@ const ResourceadmHeaderMenu = () => {
isActive: selectedContext === selectableOrg.username,
})) ?? [];

const selfMenuItem: StudioProfileMenuItem = {
action: { type: 'button', onClick: () => handleSetSelectedContext(SelectedContextType.Self) },
itemName: user?.full_name || user?.login,
hasDivider: true,
isActive: selectedContext === SelectedContextType.Self,
};

const giteaMenuItem: StudioProfileMenuItem = {
action: { type: 'link', href: getRepoPath() },
itemName: t('shared.header_go_to_gitea'),
Expand Down Expand Up @@ -131,13 +100,7 @@ const ResourceadmHeaderMenu = () => {
}
/>
}
profileMenuItems={[
allMenuItem,
...selectableOrgMenuItems,
selfMenuItem,
giteaMenuItem,
logOutMenuItem,
]}
profileMenuItems={[...selectableOrgMenuItems, giteaMenuItem, logOutMenuItem]}
/>
);
};

0 comments on commit 542f898

Please sign in to comment.