2023-11-27 06:44:06 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { useRouter } from "next/router";
|
2023-11-28 11:35:42 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
2023-11-27 06:44:06 +00:00
|
|
|
import useSWR from "swr";
|
2023-11-28 11:35:42 +00:00
|
|
|
// mobx store
|
|
|
|
import { useMobxStore } from "lib/mobx/store-provider";
|
2023-11-27 06:44:06 +00:00
|
|
|
// layouts
|
|
|
|
import { AppLayout } from "layouts/app-layout";
|
|
|
|
import { WorkspaceSettingLayout } from "layouts/settings-layout";
|
|
|
|
// component
|
|
|
|
import { WorkspaceSettingHeader } from "components/headers";
|
|
|
|
import { ApiTokenEmptyState, ApiTokenListItem, CreateApiTokenModal } from "components/api-token";
|
|
|
|
// ui
|
|
|
|
import { Button, Spinner } from "@plane/ui";
|
|
|
|
// services
|
|
|
|
import { APITokenService } from "services/api_token.service";
|
|
|
|
// types
|
|
|
|
import { NextPageWithLayout } from "types/app";
|
|
|
|
// constants
|
|
|
|
import { API_TOKENS_LIST } from "constants/fetch-keys";
|
2023-11-28 11:35:42 +00:00
|
|
|
import { EUserWorkspaceRoles } from "constants/workspace";
|
2023-11-27 06:44:06 +00:00
|
|
|
|
|
|
|
const apiTokenService = new APITokenService();
|
|
|
|
|
|
|
|
const ApiTokensPage: NextPageWithLayout = observer(() => {
|
|
|
|
// states
|
|
|
|
const [isCreateTokenModalOpen, setIsCreateTokenModalOpen] = useState(false);
|
|
|
|
// router
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug } = router.query;
|
|
|
|
// mobx store
|
|
|
|
const {
|
|
|
|
user: { currentWorkspaceRole },
|
|
|
|
} = useMobxStore();
|
|
|
|
|
2023-11-28 11:35:42 +00:00
|
|
|
const isAdmin = currentWorkspaceRole === EUserWorkspaceRoles.ADMIN;
|
2023-11-27 11:45:48 +00:00
|
|
|
|
|
|
|
const { data: tokens } = useSWR(workspaceSlug && isAdmin ? API_TOKENS_LIST(workspaceSlug.toString()) : null, () =>
|
|
|
|
workspaceSlug && isAdmin ? apiTokenService.getApiTokens(workspaceSlug.toString()) : null
|
2023-11-27 06:44:06 +00:00
|
|
|
);
|
|
|
|
|
2023-11-27 11:45:48 +00:00
|
|
|
if (!isAdmin)
|
2023-11-27 06:44:06 +00:00
|
|
|
return (
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="mt-10 flex h-full w-full justify-center p-4">
|
|
|
|
<p className="text-sm text-custom-text-300">You are not authorized to access this page.</p>
|
2023-11-27 06:44:06 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<CreateApiTokenModal isOpen={isCreateTokenModalOpen} onClose={() => setIsCreateTokenModalOpen(false)} />
|
|
|
|
{tokens ? (
|
2023-12-10 10:18:10 +00:00
|
|
|
<section className="w-full overflow-y-auto py-8 pr-9">
|
2023-11-28 05:59:01 +00:00
|
|
|
{tokens.length > 0 ? (
|
|
|
|
<>
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="mb-2 flex items-center justify-between border-b border-custom-border-200 py-3.5">
|
2023-11-28 05:59:01 +00:00
|
|
|
<h3 className="text-xl font-medium">API tokens</h3>
|
|
|
|
<Button variant="primary" onClick={() => setIsCreateTokenModalOpen(true)}>
|
|
|
|
Add API token
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
{tokens.map((token) => (
|
|
|
|
<ApiTokenListItem key={token.id} token={token} />
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<div className="mx-auto">
|
|
|
|
<ApiTokenEmptyState onClick={() => setIsCreateTokenModalOpen(true)} />
|
2023-11-27 06:44:06 +00:00
|
|
|
</div>
|
2023-11-28 05:59:01 +00:00
|
|
|
)}
|
|
|
|
</section>
|
2023-11-27 06:44:06 +00:00
|
|
|
) : (
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="grid h-full w-full place-items-center p-4">
|
2023-11-27 06:44:06 +00:00
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
ApiTokensPage.getLayout = function getLayout(page: React.ReactElement) {
|
|
|
|
return (
|
|
|
|
<AppLayout header={<WorkspaceSettingHeader title="API Tokens" />}>
|
|
|
|
<WorkspaceSettingLayout>{page}</WorkspaceSettingLayout>
|
|
|
|
</AppLayout>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ApiTokensPage;
|