Skip to content

Commit

Permalink
show api access screen in projet settings (#3056)
Browse files Browse the repository at this point in the history
Signed-off-by: andreas-unleash <andreas@getunleash.ai>

<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->

## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Closes #
[1-612](https://linear.app/unleash/issue/1-612/first-iteration-of-project-token-management)

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
  • Loading branch information
andreas-unleash committed Feb 7, 2023
1 parent 377e0e9 commit d14072f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 2 deletions.
Expand Up @@ -28,12 +28,38 @@ import { useConditionallyHiddenColumns } from 'hooks/useConditionallyHiddenColum
import { TimeAgoCell } from 'component/common/Table/cells/TimeAgoCell/TimeAgoCell';

const hiddenColumnsSmall = ['Icon', 'createdAt'];
const hiddenColumnsCompact = ['Icon', 'project', 'seenAt'];

export const ApiTokenTable = () => {
interface IApiTokenTableProps {
compact?: boolean;
filterForProject?: string;
}
export const ApiTokenTable = ({
compact = false,
filterForProject,
}: IApiTokenTableProps) => {
const { tokens, loading } = useApiTokens();
const initialState = useMemo(() => ({ sortBy: [{ id: 'createdAt' }] }), []);
const isSmallScreen = useMediaQuery(theme.breakpoints.down('md'));

const filteredTokens = useMemo(() => {
if (Boolean(filterForProject)) {
return tokens.filter(token => {
if (token.projects) {
if (token.projects?.length > 1) return false;
if (
token.projects?.length === 1 &&
token.projects[0] === filterForProject
)
return true;
}

return token.project === filterForProject;
});
}
return tokens;
}, [tokens, filterForProject]);

const {
getTableProps,
getTableBodyProps,
Expand All @@ -46,7 +72,7 @@ export const ApiTokenTable = () => {
} = useTable(
{
columns: COLUMNS as any,
data: tokens as any,
data: filteredTokens as any,
initialState,
sortTypes,
autoResetHiddenColumns: false,
Expand All @@ -62,6 +88,10 @@ export const ApiTokenTable = () => {
condition: isSmallScreen,
columns: hiddenColumnsSmall,
},
{
condition: compact,
columns: hiddenColumnsCompact,
},
],
setHiddenColumns,
COLUMNS
Expand Down
@@ -0,0 +1,35 @@
import { useContext } from 'react';
import { PageContent } from 'component/common/PageContent/PageContent';
import { Alert } from '@mui/material';
import { PageHeader } from 'component/common/PageHeader/PageHeader';
import AccessContext from 'contexts/AccessContext';
import { UPDATE_PROJECT } from 'component/providers/AccessProvider/permissions';
import { useRequiredPathParam } from 'hooks/useRequiredPathParam';
import { usePageTitle } from 'hooks/usePageTitle';
import { useProjectNameOrId } from 'hooks/api/getters/useProject/useProject';
import { ApiTokenTable } from '../../../admin/apiToken/ApiTokenTable/ApiTokenTable';

export const ProjectApiAccess = () => {
const projectId = useRequiredPathParam('projectId');
const projectName = useProjectNameOrId(projectId);
const { hasAccess } = useContext(AccessContext);

usePageTitle(`Project api access – ${projectName}`);

if (!hasAccess(UPDATE_PROJECT, projectId)) {
return (
<PageContent header={<PageHeader title="Api access" />}>
<Alert severity="error">
You need project owner or admin permissions to access this
section.
</Alert>
</PageContent>
);
}

return (
<div style={{ width: '100%', overflow: 'hidden' }}>
<ApiTokenTable compact filterForProject={projectId} />
</div>
);
};
Expand Up @@ -9,10 +9,14 @@ import { ITab, VerticalTabs } from 'component/common/VerticalTabs/VerticalTabs';
import { ProjectAccess } from 'component/project/ProjectAccess/ProjectAccess';
import ProjectEnvironmentList from 'component/project/ProjectEnvironment/ProjectEnvironment';
import { ChangeRequestConfiguration } from './ChangeRequestConfiguration/ChangeRequestConfiguration';
import { ProjectApiAccess } from './ProjectApiAccess';
import useUiConfig from '../../../../hooks/api/getters/useUiConfig/useUiConfig';

export const ProjectSettings = () => {
const location = useLocation();
const navigate = useNavigate();
const { uiConfig } = useUiConfig();
const { showProjectApiAccess } = uiConfig.flags;

const tabs: ITab[] = [
{
Expand All @@ -29,6 +33,13 @@ export const ProjectSettings = () => {
},
];

if (Boolean(showProjectApiAccess)) {
tabs.push({
id: 'api-access',
label: 'API access',
});
}

const onChange = (tab: ITab) => {
navigate(tab.id);
};
Expand All @@ -53,6 +64,9 @@ export const ProjectSettings = () => {
path="change-requests/*"
element={<ChangeRequestConfiguration />}
/>
{Boolean(showProjectApiAccess) && (
<Route path="api-access/*" element={<ProjectApiAccess />} />
)}
<Route
path="*"
element={<Navigate replace to={tabs[0].id} />}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/interfaces/uiConfig.ts
Expand Up @@ -48,6 +48,7 @@ export interface IFlags {
newProjectOverview?: boolean;
caseInsensitiveInOperators?: boolean;
crOnVariants?: boolean;
showProjectApiAccess?: boolean;
}

export interface IVersionInfo {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/__snapshots__/create-config.test.ts.snap
Expand Up @@ -83,6 +83,7 @@ exports[`should create default config 1`] = `
"proxyReturnAllToggles": false,
"responseTimeWithAppName": false,
"serviceAccounts": false,
"showProjectApiAccess": false,
"variantsPerEnvironment": false,
},
},
Expand All @@ -104,6 +105,7 @@ exports[`should create default config 1`] = `
"proxyReturnAllToggles": false,
"responseTimeWithAppName": false,
"serviceAccounts": false,
"showProjectApiAccess": false,
"variantsPerEnvironment": false,
},
"externalResolver": {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/types/experimental.ts
Expand Up @@ -66,6 +66,10 @@ const flags = {
process.env.UNLEASH_EXPERIMENTAL_CR_ON_VARIANTS,
false,
),
showProjectApiAccess: parseEnvVarBoolean(
process.env.UNLEASH_EXPERIMENTAL_PROJECT_API_ACCESS,
false,
),
};

export const defaultExperimentalOptions: IExperimentalOptions = {
Expand Down

0 comments on commit d14072f

Please sign in to comment.