Skip to content

Commit

Permalink
Add server transfer ownership modal
Browse files Browse the repository at this point in the history
  • Loading branch information
SupertigerDev committed Jan 22, 2025
1 parent 8baf167 commit 5e24216
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/chat-api/services/ServerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ export async function getInvites(serverId: string): Promise<any> {
useToken: true,
});
}
export async function transferOwnership(serverId: string, password: string, newOwnerUserId: string): Promise<any> {
return request({
method: "POST",
url: env.SERVER_URL + "/api" + ServiceEndpoints.server(serverId) + "/transfer-ownership",
body: { password, newOwnerUserId },
useToken: true,
});
}

export async function updateServer(
serverId: string,
Expand Down
109 changes: 109 additions & 0 deletions src/components/member-context-menu/MemberContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Checkbox from "@/components/ui/Checkbox";
import {
BanServerMember,
kickServerMember,
transferOwnership,
updateServerMember,
updateServerMemberProfile,
} from "@/chat-api/services/ServerService";
Expand All @@ -30,6 +31,9 @@ import Input from "../ui/input/Input";
import { Notice } from "../ui/Notice/Notice";
import Text from "../ui/Text";
import Avatar from "../ui/Avatar";
import { Modal } from "../ui/modal";
import { User } from "@/chat-api/store/useUsers";
import { Server } from "@/chat-api/store/useServers";
type Props = Omit<ContextMenuProps, "items"> & {
serverId?: string;
userId: string;
Expand Down Expand Up @@ -79,6 +83,12 @@ export default function MemberContextMenu(props: Props) {
icon: "edit",
onClick: onNicknameClick,
};
const transferOwnership = {
label: t("userContextMenu.transferOwnership"),
icon: "next_week",
onClick: onTransferOwnershipClick,
alert: true,
};

const isCurrentUserCreator = server()?.isCurrentUserCreator();
const clickedOnMyself = props.userId === account.user()?.id;
Expand Down Expand Up @@ -110,6 +120,8 @@ export default function MemberContextMenu(props: Props) {
separator,
...(member() ? [kick] : []),
ban,
separator,
transferOwnership,
];
}

Expand Down Expand Up @@ -152,6 +164,11 @@ export default function MemberContextMenu(props: Props) {
<ServerNicknameModal close={close} member={member()!} />
));
};
const onTransferOwnershipClick = () => {
createPortal?.((close) => (
<TransferOwnershipModal close={close} member={member()!} />
));
};

return (
<>
Expand Down Expand Up @@ -356,6 +373,98 @@ function ServerNicknameModal(props: {
);
}

function TransferOwnershipModal(props: {
member: ServerMember;
close: () => void;
}) {
const [requestSent, setRequestSent] = createSignal(false);
const [error, setError] = createSignal<string | null>(null);
const [password, setPassword] = createSignal(props.member.nickname || "");

const store = useStore();

const server = () => store.servers.get(props.member.serverId!);

const onUpdate = async () => {
if (requestSent()) return;
setRequestSent(true);
await transferOwnership(
props.member.serverId!,
password(),
props.member.userId
)
.then(() => {
props.close();
})
.catch((err) => setError(err.message))
.finally(() => {
setRequestSent(false);
});
};

return (
<Modal.Root close={props.close} doNotCloseOnBackgroundClick>
<Modal.Header title="Transfer Ownership" icon="next_week" alert />
<Modal.Body>
<Notice
style={{ "margin-bottom": "10px" }}
type="error"
description="You will not be able to undo this action."
/>
<Show when={server()?.verified}>
<Notice
style={{ "margin-bottom": "10px" }}
type="error"
description="This server will be unverified."
/>
</Show>
<div>Server:</div>
<TransferOwnershipOwnerBox server={server()} />
<div>New Owner:</div>
<TransferOwnershipOwnerBox user={props.member.user()} />
<Input
label="Confirm Password"
type="password"
value={password()}
onText={setPassword}
primaryColor="var(--alert-color)"
/>
<Show when={error()}>
<Text color="var(--alert-color)">{error()}</Text>
</Show>
</Modal.Body>
<Modal.Footer>
<Modal.Button
label={"Don't Transfer"}
iconName="arrow_back"
onClick={props.close}
/>
<Modal.Button
label={requestSent() ? "Transferring..." : "Transfer"}
alert
iconName="next_week"
primary
onClick={onUpdate}
/>
</Modal.Footer>
</Modal.Root>
);
}

function TransferOwnershipOwnerBox(props: { user?: User; server?: Server }) {
return (
<div class={styles.transferOwnershipOwnerBox}>
<Avatar user={props.user} server={props.server} size={38} />
<Text>
{props.server?.name || props.user.username}
<Show when={props.user}>
<span class={styles.tag}>:{props.user?.tag}</span>
</Show>
</Text>
</div>
);
}

function BanModal(props: {
user: RawUser;
serverId: string;
Expand Down
15 changes: 15 additions & 0 deletions src/components/member-context-menu/styles.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,18 @@
margin-left: 4px;
}
}


.transferOwnershipOwnerBox {
display: flex;
align-items: center;
gap: 12px;
background-color: rgb(22, 22, 22);
border-radius: 6px;
padding: 6px;
margin-bottom: 8px;
margin-top: 4px;
.tag {
opacity: 0.6;
}
}
2 changes: 2 additions & 0 deletions src/components/ui/modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface RootProps {
@default true
*/
closeOnEscape?: boolean;
doNotCloseOnBackgroundClick?: boolean;
}
const Root = (props: RootProps) => {
const { isMobileWidth } = useWindowProperties();
Expand All @@ -33,6 +34,7 @@ const Root = (props: RootProps) => {
let textSelected = false;

const onBackgroundClick = (event: MouseEvent) => {
if (props.doNotCloseOnBackgroundClick) return;
if (event.target !== event.currentTarget) return;

const xDistance = Math.abs(startClick.x - event.clientX);
Expand Down
1 change: 1 addition & 0 deletions src/locales/list/en-gb.json
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@
"kick": "Kick",
"ban": "Ban",
"changeNickname": "Change Nickname",
"transferOwnership": "Transfer Ownership",
"callVolume": "Call Volume"
},
"kickServerMemberModal": {
Expand Down

0 comments on commit 5e24216

Please sign in to comment.