Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import "./ServiceAccounts.css";
import { NewAlert, NewButton, Modal, NewIcon } from "@thunderstore/cyberstorm";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faTrash } from "@fortawesome/free-solid-svg-icons";
import { useToast } from "@thunderstore/cyberstorm";
import { teamServiceAccountRemove } from "@thunderstore/thunderstore-api";
import { ApiAction } from "@thunderstore/ts-api-react-actions";
import { type OutletContextShape } from "~/root";
import { type TeamServiceAccount } from "@thunderstore/thunderstore-api";

interface ServiceAccountRemoveModalProps {
serviceAccount: TeamServiceAccount;
teamName: string;
outletContext: OutletContextShape;
revalidate: () => void;
}

export function ServiceAccountRemoveModal({
serviceAccount,
teamName,
outletContext,
revalidate,
}: ServiceAccountRemoveModalProps) {
const toast = useToast();

const canDelete = () => {
const teams = outletContext.currentUser?.teams_full || [];
const belongingTeam = teams.find((team) => team.name === teamName);
if (!belongingTeam) {
return false;
}
return belongingTeam.role === "owner";
};

const removeServiceAccountAction = ApiAction({
endpoint: teamServiceAccountRemove,
onSubmitSuccess: () => {
toast.addToast({
csVariant: "success",
children: "Service account removed",
duration: 4000,
});
revalidate();
},
onSubmitError: (error) => {
toast.addToast({
csVariant: "danger",
children: `Error occurred: ${error.message || "Unknown error"}`,
duration: 8000,
});
},
});

return (
<Modal
titleContent="Confirm service account removal"
trigger={
canDelete() && ( // Only show trigger if user can delete
<NewButton csVariant="danger" csSize="xsmall">
<NewIcon csMode="inline" noWrapper>
<FontAwesomeIcon icon={faTrash} />
</NewIcon>
Remove
</NewButton>
)
}
csSize="small"
>
<Modal.Body>
<NewAlert csVariant="warning">
This cannot be undone! Related API token will stop working immediately
if the service account is removed.
</NewAlert>
<div>
You are about to remove service account{" "}
<span className="team-service-accounts__highlight">
{serviceAccount.name}
</span>
.
</div>
</Modal.Body>
<Modal.Footer>
<NewButton
onClick={() => {
removeServiceAccountAction({
config: outletContext.requestConfig,
params: {
uuid: serviceAccount.identifier,
},
queryParams: {},
data: {},
});
}}
csVariant="danger"
>
<NewIcon csMode="inline" noWrapper>
<FontAwesomeIcon icon={faTrash} />
</NewIcon>
Remove service account
</NewButton>
</Modal.Footer>
</Modal>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@ import {
useToast,
} from "@thunderstore/cyberstorm";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faPlus, faTrash } from "@fortawesome/free-solid-svg-icons";
import { faPlus } from "@fortawesome/free-solid-svg-icons";
import { type LoaderFunctionArgs } from "react-router";
import { useLoaderData, useOutletContext, useRevalidator } from "react-router";
import {
ApiError,
type RequestConfig,
teamAddServiceAccount,
type TeamServiceAccountAddRequestData,
teamServiceAccountRemove,
} from "@thunderstore/thunderstore-api";
import { TableSort } from "@thunderstore/cyberstorm/src/newComponents/Table/Table";
import { type OutletContextShape } from "../../../../../root";
import { useReducer, useState } from "react";
import { DapperTs } from "@thunderstore/dapper-ts";
import { getSessionTools } from "cyberstorm/security/publicEnvVariables";
import { ApiAction } from "@thunderstore/ts-api-react-actions";
import { useStrongForm } from "cyberstorm/utils/StrongForm/useStrongForm";
import { ServiceAccountRemoveModal } from "./ServiceAccountRemoveModal";

// REMIX TODO: Add check for "user has permission to see this page"
export async function clientLoader({ params }: LoaderFunctionArgs) {
Expand Down Expand Up @@ -74,33 +73,11 @@ export default function ServiceAccounts() {

const revalidator = useRevalidator();

const toast = useToast();

async function serviceAccountRevalidate() {
revalidator.revalidate();
}

// Remove service account stuff
const removeServiceAccountAction = ApiAction({
endpoint: teamServiceAccountRemove,
onSubmitSuccess: () => {
toast.addToast({
csVariant: "success",
children: `Service account removed`,
duration: 4000,
});
serviceAccountRevalidate();
},
onSubmitError: (error) => {
toast.addToast({
csVariant: "danger",
children: `Error occurred: ${error.message || "Unknown error"}`,
duration: 8000,
});
},
});

const tableData = serviceAccounts.map((serviceAccount, index) => {
const tableData = serviceAccounts.map((serviceAccount) => {
return [
{
value: (
Expand All @@ -120,54 +97,13 @@ export default function ServiceAccounts() {
},
{
value: (
<Modal
key={`${serviceAccount.name}_${index}`}
titleContent="Confirm service account removal"
trigger={
<NewButton csVariant="danger" csSize="xsmall">
<NewIcon csMode="inline" noWrapper>
<FontAwesomeIcon icon={faTrash} />
</NewIcon>
Remove
</NewButton>
}
csSize="small"
>
<Modal.Body>
<NewAlert csVariant="warning">
This cannot be undone! Related API token will stop working
immediately if the service account is removed.
</NewAlert>
<div>
You are about to remove service account{" "}
<span className="team-service-accounts__highlight">
{serviceAccount.name}
</span>
.
</div>
</Modal.Body>
<Modal.Footer>
<NewButton
onClick={() => {
removeServiceAccountAction({
config: outletContext.requestConfig,
params: {
team_name: teamName,
uuid: serviceAccount.identifier,
},
queryParams: {},
data: {},
});
}}
csVariant="danger"
>
<NewIcon csMode="inline" noWrapper>
<FontAwesomeIcon icon={faTrash} />
</NewIcon>
Remove service account
</NewButton>
</Modal.Footer>
</Modal>
<ServiceAccountRemoveModal
key={serviceAccount.identifier}
serviceAccount={serviceAccount}
teamName={teamName}
outletContext={outletContext}
revalidate={serviceAccountRevalidate}
/>
),
sortValue: 0,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function teamServiceAccountRemove(
props: ApiEndpointProps<TeamServiceAccountRemoveRequestParams, object, object>
): Promise<undefined> {
const { config, params } = props;
const path = `/api/cyberstorm/team/${params.team_name}/service-account/delete/${params.uuid}/`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sibling PR for creating a service account still uses this URL structure. Do you think it should also be also updated to not reside under the team path? It feels a bit odd that the URLs now differ in this regard. Not asking it to be done within this PR.

const path = `/api/cyberstorm/service-account/${params.uuid}/delete/`;

return apiFetch({
args: {
Expand Down
1 change: 0 additions & 1 deletion packages/thunderstore-api/src/schemas/requestSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,6 @@ export type TeamServiceAccountAddRequestData = z.infer<

// TeamServiceAccountRemoveRequest
export const teamServiceAccountRemoveRequestParamsSchema = z.object({
team_name: z.string(),
uuid: z.string(),
});

Expand Down
Loading