Skip to content

Commit

Permalink
feat: add message rule create modal
Browse files Browse the repository at this point in the history
  • Loading branch information
virtual-designer committed Oct 14, 2023
1 parent c8193f8 commit 52da848
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 86 deletions.
20 changes: 17 additions & 3 deletions src/components/MessageRuleManagement/MessageRuleCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const MessageRuleCard: FC<SettingCardProps> = ({
enabled: !!config?.message_rules?.enabled,
}));
const isDesktop = useIsDesktop();
const [isCreating, setIsCreating] = useState(false);

return (
<Card>
Expand All @@ -50,7 +51,12 @@ const MessageRuleCard: FC<SettingCardProps> = ({
</div>
<div className="flex items-center gap-3">
{state.enabled && isDesktop && (
<Button startIcon={<MdAdd />}>Create</Button>
<Button
onClick={() => setIsCreating(true)}
startIcon={<MdAdd />}
>
Create
</Button>
)}
<Switch
defaultChecked={!!config?.message_rules?.enabled}
Expand All @@ -72,11 +78,19 @@ const MessageRuleCard: FC<SettingCardProps> = ({
Control what messages can be sent in your server.
</p>

<MessageRules rules={config?.message_rules?.rules ?? []} />
<MessageRules
rules={config?.message_rules?.rules ?? []}
createFormOpenState={[isCreating, setIsCreating]}
/>

<div className="flex items-center justify-end mt-3">
{state.enabled && !isDesktop && (
<Button startIcon={<MdAdd />}>Create</Button>
<Button
onClick={() => setIsCreating(true)}
startIcon={<MdAdd />}
>
Create
</Button>
)}
</div>
</CardBody>
Expand Down
111 changes: 111 additions & 0 deletions src/components/MessageRuleManagement/MessageRuleCreateForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { APIMessageRule } from "@/types/APIMessageRule";
import { Button, Select, SelectItem } from "@nextui-org/react";
import { FC, useState } from "react";
import { useForm } from "react-hook-form";
import MessageRuleIcon from "./MessageRuleIcon";

interface MessageRuleCreateFormProps {
onSubmit?: () => any;
onCancel?: () => any;
}
type Type = APIMessageRule["type"];

const nameRecord: Record<Type, string> = {
anti_invite: "Anti Invite",
block_mass_mention: "Block Mass Mention",
block_repeated_text: "Block Repeated Text",
blocked_file_extension: "Block File with Specific Extensions",
blocked_mime_type: "Block Specific File Types",
domain: "Block Domains",
regex_filter: "Regex (Block)",
regex_must_match: "Regex (Must match)",
};

const names = (Object.entries(nameRecord) as [Type, string][]).map(
([type, name]) => ({ type, name })
);

const MessageRuleCreateForm: FC<MessageRuleCreateFormProps> = ({
onCancel,
onSubmit = () => null,
}) => {
const {
formState: { errors },
register,
handleSubmit,
} = useForm();
const [ruleType, setRuleType] = useState<Type>();

return (
<form noValidate onSubmit={handleSubmit(onSubmit)}>
<div>
<Select
items={names}
label="Rule Type"
placeholder="Select a message rule type"
labelPlacement="outside"
{...register("a", {
required: {
message: "Please enter a valid rule type!",
value: true,
},
onChange: e => {
if (e.target.value) {
setRuleType(e.target.value);
}
},
value: ruleType,
})}
selectedKeys={ruleType ? [ruleType] : []}
classNames={{
popover: "[overflow-wrap:break-word]",
innerWrapper: "[overflow-wrap:break-word]",
value: "[overflow-wrap:break-word]",
description: "[overflow-wrap:break-word]",
}}
renderValue={rules => {
console.log(rules.map(r => r));

return rules.map((rule, index) => (
<div
key={index}
className="flex items-center gap-3"
>
<div>{rule.props?.startContent}</div>
<div>{rule.textValue}</div>
</div>
));
}}
>
{names.map((rule, index) => (
<SelectItem
key={index}
title={rule.name}
className="relative"
description={rule.type}
startContent={
<MessageRuleIcon type={rule.type} size={25} />
}
/>
))}
</Select>
</div>

<div className="flex items-center justify-end gap-2 mt-3 pb-3">
<Button
variant="flat"
type="reset"
color="danger"
onPress={onCancel}
>
Cancel
</Button>
<Button variant="flat" type="submit" color="primary">
Create
</Button>
</div>
</form>
);
};

export default MessageRuleCreateForm;
36 changes: 17 additions & 19 deletions src/components/MessageRuleManagement/MessageRuleIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/*
* This file is part of SudoBot Dashboard.
*
* Copyright (C) 2021-2023 OSN Developers.
*
* SudoBot Dashboard is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SudoBot Dashboard is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with SudoBot Dashboard. If not, see <https://www.gnu.org/licenses/>.
*/
* This file is part of SudoBot Dashboard.
*
* Copyright (C) 2021-2023 OSN Developers.
*
* SudoBot Dashboard is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SudoBot Dashboard is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with SudoBot Dashboard. If not, see <https://www.gnu.org/licenses/>.
*/

import { APIMessageRule } from "@/types/APIMessageRule";
import { ComponentProps, FC } from "react";
Expand Down Expand Up @@ -51,8 +51,6 @@ const MessageRuleIcon: FC<MessageRuleIconProps> = ({ type, ...props }) => {
const Icon = icons[type];
const color = 0x0000ff * type.length;

console.log(color.toString(16));

return (
<Icon
className="p-1 rounded-lg [border:1px_solid]"
Expand Down
72 changes: 50 additions & 22 deletions src/components/MessageRuleManagement/MessageRules.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,64 @@
/*
* This file is part of SudoBot Dashboard.
*
* Copyright (C) 2021-2023 OSN Developers.
*
* SudoBot Dashboard is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SudoBot Dashboard is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with SudoBot Dashboard. If not, see <https://www.gnu.org/licenses/>.
*/
* This file is part of SudoBot Dashboard.
*
* Copyright (C) 2021-2023 OSN Developers.
*
* SudoBot Dashboard is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SudoBot Dashboard is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with SudoBot Dashboard. If not, see <https://www.gnu.org/licenses/>.
*/

import { APIMessageRule } from "@/types/APIMessageRule";
import { FC } from "react";
import { Modal, ModalBody, ModalContent, ModalHeader } from "@nextui-org/react";
import { Dispatch, FC, SetStateAction } from "react";
import MessageRule from "./MessageRule";
import MessageRuleCreateForm from "./MessageRuleCreateForm";

interface MessageRulesProps {
rules: APIMessageRule[];
createFormOpenState: [boolean, Dispatch<SetStateAction<boolean>>];
}

const MessageRules: FC<MessageRulesProps> = ({ rules }) => {
const MessageRules: FC<MessageRulesProps> = ({
rules,
createFormOpenState,
}) => {
const [modalOpen, setModalOpen] = createFormOpenState;

return (
<div>
{rules.map((rule, i) => (
<MessageRule key={`${i}_${rule.type}`} rule={rule} />
))}
<Modal
backdrop={"blur"}
isOpen={modalOpen}
onClose={() => setModalOpen(false)}
>
<ModalContent>
{onClose => (
<>
<ModalHeader className="flex flex-col gap-1">
Create Message Rule
</ModalHeader>
<ModalBody>
<MessageRuleCreateForm onCancel={onClose} />
</ModalBody>
</>
)}
</ModalContent>
</Modal>
<div>
{rules.map((rule, i) => (
<MessageRule key={`${i}_${rule.type}`} rule={rule} />
))}
</div>
</div>
);
};
Expand Down
38 changes: 18 additions & 20 deletions src/components/PermissionManagement/PermissionRoleEditModal.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
/*
* This file is part of SudoBot Dashboard.
*
* Copyright (C) 2021-2023 OSN Developers.
*
* SudoBot Dashboard is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SudoBot Dashboard is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with SudoBot Dashboard. If not, see <https://www.gnu.org/licenses/>.
*/
* This file is part of SudoBot Dashboard.
*
* Copyright (C) 2021-2023 OSN Developers.
*
* SudoBot Dashboard is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SudoBot Dashboard is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with SudoBot Dashboard. If not, see <https://www.gnu.org/licenses/>.
*/

import { APIPermissionRole } from "@/types/APIPermissionRole";
import { Modal, ModalBody, ModalContent, ModalHeader } from "@nextui-org/react";
import { FC, useRef } from "react";
import { FC } from "react";
import PermissionRoleEditForm from "./PermissionRoleEditForm";

interface PermissionRoleEditModalProps {
Expand All @@ -33,8 +33,6 @@ const PermissionRoleEditModal: FC<PermissionRoleEditModalProps> = ({
toggleEditing,
permission,
}) => {
const ref = useRef<{ submit: Function }>();

return (
<Modal backdrop={"blur"} isOpen={isEditing} onClose={toggleEditing}>
<ModalContent>
Expand Down
39 changes: 17 additions & 22 deletions src/contexts/RouterContext.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/*
* This file is part of SudoBot Dashboard.
*
* Copyright (C) 2021-2023 OSN Developers.
*
* SudoBot Dashboard is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SudoBot Dashboard is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with SudoBot Dashboard. If not, see <https://www.gnu.org/licenses/>.
*/
* This file is part of SudoBot Dashboard.
*
* Copyright (C) 2021-2023 OSN Developers.
*
* SudoBot Dashboard is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SudoBot Dashboard is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with SudoBot Dashboard. If not, see <https://www.gnu.org/licenses/>.
*/

"use client";

Expand Down Expand Up @@ -55,13 +55,8 @@ export function RouterContextProvider({ children }: PropsWithChildren) {

useEffect(() => {
setState(state => ({ ...state, loading: false }));
console.log("pathname", pathname);
}, [pathname]);

useEffect(() => {
console.log("Rerender!");
});

return (
<RouterContext.Provider
value={{
Expand Down

0 comments on commit 52da848

Please sign in to comment.