Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(UI/UX): add a validation step before leaving scope #952

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion frontend/routes/@[scope]/(_islands)/ScopeInviteForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useCallback, useRef } from "preact/hooks";
import { JSX } from "preact/jsx-runtime";
import { ScopeInvite } from "../../../utils/api_types.ts";
import { api, path } from "../../../utils/api.ts";
import TbUsersPlus from "@preact-icons/tb/TbUsersPlus";

interface ScopeInviteFormProps {
scope: string;
Expand Down Expand Up @@ -53,7 +54,7 @@ export function ScopeInviteForm(props: ScopeInviteFormProps) {
class="contents"
onSubmit={onSubmit}
>
<div class="mt-4 flex gap-4">
<div class="mt-4 flex gap-4 justify-between">
<div class="flex">
<select
name="kind"
Expand Down Expand Up @@ -92,6 +93,7 @@ export function ScopeInviteForm(props: ScopeInviteFormProps) {
disabled={submitting}
>
Invite
<TbUsersPlus class="size-5 ml-2" />
</button>
</div>
{error && <p class="text-red-600 mt-2">{error}</p>}
Expand Down
85 changes: 85 additions & 0 deletions frontend/routes/@[scope]/(_islands)/ScopeMemberLeave.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2024 the JSR authors. All rights reserved. MIT license.
import TbArrowRightFromArc from "@preact-icons/tb/TbArrowRightFromArc";
import { useSignal } from "@preact/signals";
import { useEffect } from "preact/hooks";

export function ScopeMemberLeave({
userId,
isAdmin,
isLastAdmin,
scopeName = "",
}: {
userId: string;
isAdmin: boolean;
isLastAdmin: boolean;
scopeName?: string;
}) {
const scopeInput = useSignal("");
const isEmptyInput = useSignal(false);
const isInvalidInput = useSignal(false);

useEffect(() => {
const handler = setTimeout(() => {
validate();
}, 300);

return () => clearTimeout(handler);
}, [scopeInput.value]);

const validate = () => {
isEmptyInput.value = scopeInput.value.length === 0;
isInvalidInput.value = scopeInput.value !== scopeName &&
scopeInput.value.length > 0;
};

return (
<form
method="POST"
class="max-w-3xl border-t border-jsr-cyan-950/10 pt-8 mt-12"
>
<h2 class="text-lg font-semibold">Leave scope</h2>
<p class="mt-2 text-jsr-gray-600">
Leaving this scope will revoke your access to all packages in this
scope. You will no longer be able to publish packages to this
scope{isAdmin && " or manage members"}.
</p>
<input type="hidden" name="userId" value={userId} />
{(isLastAdmin || isInvalidInput.value) && (
<div class="mt-6 border rounded-md border-red-300 bg-red-50 p-6 text-red-600">
<span class="font-bold text-xl">Warning</span>
<p>
{isLastAdmin &&
"You are the last admin in this scope. You must promote another member to admin before leaving."}
{isInvalidInput.value &&
"The scope name you entered does not match the scope name."}
</p>
</div>
)}
<div class="mt-4 flex justify-between gap-4">
<input
type="text"
class="inline-block w-full max-w-sm px-3 input-container text-sm input"
value={scopeInput.value}
onInput={(e) => {
scopeInput.value = (e.target as HTMLInputElement).value;
}}
placeholder="Scope name"
disabled={isLastAdmin}
title={isLastAdmin
? "This is the last admin in this scope. Promote another member to admin before demoting this one."
: undefined}
/>
<button
class="button-danger"
type="submit"
name="action"
value="deleteMember"
disabled={isLastAdmin || isInvalidInput.value || isEmptyInput.value}
>
Leave
<TbArrowRightFromArc class="size-5 ml-2 rotate-180" />
</button>
</div>
</form>
);
}
41 changes: 3 additions & 38 deletions frontend/routes/@[scope]/~/members.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { scopeData } from "../../../utils/data.ts";
import TbTrash from "@preact-icons/tb/TbTrash";
import { scopeIAM } from "../../../utils/iam.ts";
import { ScopeIAM } from "../../../utils/iam.ts";
import { ScopeMemberLeave } from "../(_islands)/ScopeMemberLeave.tsx";

export default define.page<typeof handler>(function ScopeMembersPage(
{ params, data, state, url },
Expand Down Expand Up @@ -70,10 +71,11 @@ export default define.page<typeof handler>(function ScopeMembersPage(
</Table>
{iam.canAdmin && <MemberInvite scope={data.scope.scope} />}
{data.scopeMember && (
<MemberLeave
<ScopeMemberLeave
userId={data.scopeMember.user.id}
isAdmin={data.scopeMember.isAdmin}
isLastAdmin={isLastAdmin}
scopeName={data.scope.scope}
/>
)}
</div>
Expand Down Expand Up @@ -195,43 +197,6 @@ function MemberInvite({ scope }: { scope: string }) {
);
}

function MemberLeave(
props: { userId: string; isAdmin: boolean; isLastAdmin: boolean },
) {
return (
<form
method="POST"
class="max-w-3xl border-t border-jsr-cyan-950/10 pt-8 mt-12"
>
<h2 class="text-lg font-semibold">Leave scope</h2>
<p class="mt-2 text-jsr-gray-600">
Leaving this scope will revoke your access to all packages in this
scope. You will no longer be able to publish packages to this
scope{props.isAdmin && " or manage members"}.
</p>
<input type="hidden" name="userId" value={props.userId} />
{props.isLastAdmin && (
<div class="mt-6 border-1 rounded-md border-red-300 bg-red-50 p-6 text-red-600">
<span class="font-bold text-xl">Warning</span>
<p>
You are the last admin in this scope. You must promote another
member to admin before leaving.
</p>
</div>
)}
<button
class="button-danger mt-6"
type="submit"
name="action"
value="deleteMember"
disabled={props.isLastAdmin}
>
Leave
</button>
</form>
);
}

export const handler = define.handlers({
async GET(ctx) {
let [user, data, membersResp, invitesResp] = await Promise.all([
Expand Down
Loading