Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
重写账户选择组件
daoyian0a026ab44c27
diff --git a/src/app/[lng]/accounts/components/AccountSidebar/MCPManagerModal.tsx b/src/app/[lng]/accounts/components/AccountSidebar/MCPManagerModal.tsxindex bd863bec..490d4338 100644--- a/src/app/[lng]/accounts/components/AccountSidebar/MCPManagerModal.tsx+++ b/src/app/[lng]/accounts/components/AccountSidebar/MCPManagerModal.tsx@@ -3,7 +3,6 @@ import { Modal, Button, List, Avatar, Tag, Space, message, Empty } from "antd";import { PlusOutlined, CopyOutlined, DeleteOutlined, KeyOutlined } from "@ant-design/icons";import ChooseAccountModule from "@/components/ChooseAccountModule/ChooseAccountModule";import { SocialAccount } from "@/api/types/account.type";-import { PubType } from "@/app/config/publishConfig";import styles from "./MCPManagerModal.module.scss";export interface IMCPManagerModalRef {@@ -184,9 +183,8 @@ const MCPManagerModal = memo(<ChooseAccountModuleopen={isCreateModalOpen}onClose={setIsCreateModalOpen}- onPlatConfirm={handleAccountConfirm}- platChooseProps={{- pubType: PubType.VIDEO, // 根据实际需要调整+ onAccountConfirm={handleAccountConfirm}+ simpleAccountChooseProps={{choosedAccounts: selectedAccounts,disableAllSelect: false,isCancelChooseAccount: true,diff --git a/src/components/ChooseAccountModule/ChooseAccountModule.tsx b/src/components/ChooseAccountModule/ChooseAccountModule.tsxindex b81626c7..1ae0e8a5 100644--- a/src/components/ChooseAccountModule/ChooseAccountModule.tsx+++ b/src/components/ChooseAccountModule/ChooseAccountModule.tsx@@ -7,26 +7,26 @@ import {useRef,useState,} from "react";-import { Modal, Tabs } from "antd";-import PlatChoose, {- IPlatChooseProps,- IPlatChooseRef,-} from "@/components/ChooseAccountModule/components/PlatChoose";+import { Modal } from "antd";+import SimpleAccountChoose, {+ ISimpleAccountChooseProps,+ ISimpleAccountChooseRef,+} from "@/components/ChooseAccountModule/components/SimpleAccountChoose";import { SocialAccount } from "@/api/types/account.type";export interface IChooseAccountModuleRef {- getPlatChooseRef: () => IPlatChooseRef | null;+ getSimpleAccountChooseRef: () => ISimpleAccountChooseRef | null;}export interface IChooseAccountModuleProps {open: boolean;onClose: (open: boolean) => void;- // 按平台props- platChooseProps?: IPlatChooseProps;- // 按平台选择确认- onPlatConfirm?: (accounts: SocialAccount[]) => void;- // 按平台选择change- onPlatChange?: (accounts: SocialAccount[], account: SocialAccount) => void;+ // 简化账户选择props+ simpleAccountChooseProps?: ISimpleAccountChooseProps;+ // 账户选择确认+ onAccountConfirm?: (accounts: SocialAccount[]) => void;+ // 账户选择change+ onAccountChange?: (accounts: SocialAccount[], account: SocialAccount) => void;}const ChooseAccountModule = memo(@@ -35,24 +35,24 @@ const ChooseAccountModule = memo({open,onClose,- onPlatConfirm,- onPlatChange,- platChooseProps,+ onAccountConfirm,+ onAccountChange,+ simpleAccountChooseProps,}: IChooseAccountModuleProps,ref: ForwardedRef<IChooseAccountModuleRef>,) => {const [newChoosedAccounts, setNewChoosedAccounts] = useState<SocialAccount[]>([]);- const platChooseRef = useRef<IPlatChooseRef>(null);+ const simpleAccountChooseRef = useRef<ISimpleAccountChooseRef>(null);const handleOk = () => {- if (onPlatConfirm) onPlatConfirm(newChoosedAccounts);+ if (onAccountConfirm) onAccountConfirm(newChoosedAccounts);close();};const handleCancel = () => {- setNewChoosedAccounts(platChooseProps?.choosedAccounts || []);+ setNewChoosedAccounts(simpleAccountChooseProps?.choosedAccounts || []);close();};@@ -61,16 +61,16 @@ const ChooseAccountModule = memo(};useEffect(() => {- setTimeout(() => platChooseRef.current?.recover(), 1);- }, [platChooseProps?.choosedAccounts]);+ setTimeout(() => simpleAccountChooseRef.current?.recover(), 1);+ }, [simpleAccountChooseProps?.choosedAccounts]);useEffect(() => {- platChooseRef.current?.init();+ simpleAccountChooseRef.current?.init();}, [open]);const ImperativeHandle: IChooseAccountModuleRef = {- getPlatChooseRef() {- return platChooseRef.current;+ getSimpleAccountChooseRef() {+ return simpleAccountChooseRef.current;},};useImperativeHandle(ref, () => ImperativeHandle);@@ -83,32 +83,19 @@ const ChooseAccountModule = memo(onOk={handleOk}onCancel={handleCancel}>- <Tabs- defaultActiveKey="1"- items={[- {- key: "1",- label: "按平台选择",- children: (- <>- {platChooseProps && (- <PlatChoose- {...platChooseProps}- disableAllSelect={- platChooseProps.disableAllSelect || false- }- ref={platChooseRef}- onChange={(aList, account) => {- setNewChoosedAccounts(aList);- if (onPlatChange) onPlatChange(aList, account);- }}- />- )}- </>- ),- },- ]}- />+ {simpleAccountChooseProps && (+ <SimpleAccountChoose+ {...simpleAccountChooseProps}+ disableAllSelect={+ simpleAccountChooseProps.disableAllSelect || false+ }+ ref={simpleAccountChooseRef}+ onChange={(accounts: SocialAccount[], account: SocialAccount) => {+ setNewChoosedAccounts(accounts);+ if (onAccountChange) onAccountChange(accounts, account);+ }}+ />+ )}</Modal>);},diff --git a/src/components/ChooseAccountModule/chooseAccountModule.module.scss b/src/components/ChooseAccountModule/chooseAccountModule.module.scssindex 1b8cafb5..7e8f4685 100644--- a/src/components/ChooseAccountModule/chooseAccountModule.module.scss+++ b/src/components/ChooseAccountModule/chooseAccountModule.module.scss@@ -99,4 +99,158 @@}}}+}++.simpleAccountChoose {+ display: flex;+ flex-direction: column;+ height: 100%;++ :global {+ .simpleAccountChoose-empty {+ display: flex;+ justify-content: center;+ width: 100%;+ padding: 40px 0;+ }++ .simpleAccountChoose-header {+ display: flex;+ justify-content: space-between;+ align-items: center;+ margin-bottom: 16px;+ padding: 0 4px;++ .ant-checkbox-wrapper {+ font-weight: 500;+ }++ .simpleAccountChoose-count {+ color: #666;+ font-size: 14px;+ }+ }++ .simpleAccountChoose-accounts {+ flex: 1;+ display: grid;+ grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));+ gap: 12px;+ max-height: 400px;+ overflow-y: auto;++ &-item {+ background: #f8f9fa;+ cursor: pointer;+ padding: 12px;+ border-radius: 8px;+ border: 1px solid #e9ecef;+ transition: all 0.2s ease;+ position: relative;+ display: flex;+ align-items: center;+ gap: 12px;++ &:hover {+ border-color: #625BF2;+ box-shadow: 0 2px 8px rgba(98, 91, 242, 0.1);+ }++ &-avatar {+ position: relative;+ flex-shrink: 0;++ .ant-avatar {+ border: 2px solid #fff;+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);+ }++ .simpleAccountChoose-accounts-item-platform {+ position: absolute;+ bottom: -2px;+ right: -2px;+ width: 20px;+ height: 20px;+ border-radius: 50%;+ background: #fff;+ border: 2px solid #fff;+ box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);+ display: flex;+ align-items: center;+ justify-content: center;+ overflow: hidden;++ img {+ width: 14px;+ height: 14px;+ object-fit: contain;+ }+ }+ }++ &-nickname {+ flex: 1;+ font-weight: 500;+ color: #333;+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+ }++ &-choose {+ position: absolute;+ top: 8px;+ right: 8px;+ width: 20px;+ height: 20px;+ border-radius: 50%;+ background: #e9ecef;+ color: #fff;+ display: flex;+ align-items: center;+ justify-content: center;+ font-size: 12px;+ opacity: 0;+ transition: all 0.2s ease;+ }++ &--active {+ background: linear-gradient(135deg, #625BF2 0%, #925BF2 100%);+ border-color: #625BF2;+ color: #fff;++ .simpleAccountChoose-accounts-item-nickname {+ color: #fff;+ }++ .simpleAccountChoose-accounts-item-choose {+ background: #fff;+ color: #625BF2;+ opacity: 1;+ }++ .simpleAccountChoose-accounts-item-avatar {+ .ant-avatar {+ border-color: #fff;+ }++ .simpleAccountChoose-accounts-item-platform {+ border-color: #fff;+ }+ }+ }++ &--disable {+ cursor: not-allowed;+ opacity: 0.5;+ background: #f5f5f5;++ &:hover {+ border-color: #e9ecef;+ box-shadow: none;+ }+ }+ }+ }+ }}\ No newline at end of filediff --git a/src/components/ChooseAccountModule/components/SimpleAccountChoose.tsx b/src/components/ChooseAccountModule/components/SimpleAccountChoose.tsxnew file mode 100644index 00000000..44789222--- /dev/null+++ b/src/components/ChooseAccountModule/components/SimpleAccountChoose.tsx@@ -0,0 +1,194 @@+import {+ ForwardedRef,+ forwardRef,+ memo,+ useEffect,+ useImperativeHandle,+ useMemo,+ useRef,+ useState,+} from "react";+import styles from "../chooseAccountModule.module.scss";+import {+ Avatar,+ Checkbox,+ Empty,+ Tooltip,+} from "antd";+import { CheckOutlined } from "@ant-design/icons";+import { useShallow } from "zustand/react/shallow";+import { AccountPlatInfoMap } from "@/app/config/platConfig";+import { useAccountStore } from "@/store/account";+import Link from "next/link";+import { SocialAccount } from "@/api/types/account.type";++export interface ISimpleAccountChooseRef {+ /**+ * 恢复选中状态+ */+ recover: () => void;+ // 每次关闭弹框的时候需要初始化一些状态+ init: () => void;+}++export interface ISimpleAccountChooseProps {+ onChange?: (+ choosedAcounts: SocialAccount[],+ choosedAcount: SocialAccount,+ ) => void;+ // 外部传入的已经选中的数据,这个值只有在确认更改才会更新+ choosedAccounts?: SocialAccount[];+ // 是否禁用多选,true=禁用,false=不禁用+ disableAllSelect?: boolean;+ // 是否可以取消已经选择的账户,默认为 false+ isCancelChooseAccount?: boolean;+}++const SimpleAccountChoose = memo(+ forwardRef(+ (+ {+ onChange,+ choosedAccounts,+ disableAllSelect = false,+ isCancelChooseAccount = false,+ }: ISimpleAccountChooseProps,+ ref: ForwardedRef<ISimpleAccountChooseRef>,+ ) => {+ // 当前选择的账户数据+ const [choosedAccountsList, setChoosedAccountsList] = useState<SocialAccount[]>([]);+ // 每次change操作的数据+ const recentData = useRef<SocialAccount>();+ const { accountList } = useAccountStore(+ useShallow((state) => ({+ accountList: state.accountList,+ })),+ );++ // change事件+ useEffect(() => {+ if (!recentData.current) return;+ if (onChange) onChange(choosedAccountsList, recentData.current!);+ }, [choosedAccountsList, onChange]);++ const init = () => {+ recentData.current = undefined;+ };++ useImperativeHandle(ref, () => ({+ // 恢复到本次操作之前的状态+ recover() {+ setChoosedAccountsList(choosedAccounts || []);+ },+ init,+ }));++ // 全选/取消全选+ const handleSelectAll = (checked: boolean) => {+ recentData.current = accountList[0];+ setChoosedAccountsList(checked ? [...accountList] : []);+ };++ // 选择/取消选择单个账户+ const handleSelectAccount = (account: SocialAccount) => {+ recentData.current = account;+ setChoosedAccountsList(prev => {+ const isSelected = prev.some(item => item.id === account.id);+ if (isSelected) {+ return prev.filter(item => item.id !== account.id);+ } else {+ return [...prev, account];+ }+ });+ };++ return (+ <div className={styles.simpleAccountChoose}>+ {accountList.length === 0 ? (+ <div className="simpleAccountChoose-empty">+ <Empty+ description={+ <>+ 无账户数据,请前往 <Link href="/accounts">账户</Link>+ 添加数据+ </>+ }+ />+ </div>+ ) : (+ <>+ <div className="simpleAccountChoose-header">+ {!disableAllSelect && (+ <Checkbox+ indeterminate={+ choosedAccountsList.length > 0 &&+ choosedAccountsList.length < accountList.length+ }+ onChange={(e) => handleSelectAll(e.target.checked)}+ checked={choosedAccountsList.length === accountList.length}+ >+ 选择所有账户+ </Checkbox>+ )}+ <span className="simpleAccountChoose-count">+ 已选择 {choosedAccountsList.length} 个+ </span>+ </div>++ <div className="simpleAccountChoose-accounts">+ {accountList.map((account) => {+ const platInfo = AccountPlatInfoMap.get(account.type);+ const isSelected = choosedAccountsList.some(item => item.id === account.id);+ const isDisable = choosedAccounts?.find((k) => k.id === account.id) && isCancelChooseAccount;++ return (+ <div+ key={account.id}+ className={[+ "simpleAccountChoose-accounts-item",+ isSelected && "simpleAccountChoose-accounts-item--active",+ isDisable && "simpleAccountChoose-accounts-item--disable",+ ].join(" ")}+ onClick={() => {+ if (isDisable) return;+ handleSelectAccount(account);+ }}+ >+ <Tooltip+ title={+ <>+ <p>昵称:{account.nickname}</p>+ <p>平台:{platInfo?.name}</p>+ </>+ }+ >+ <div className="simpleAccountChoose-accounts-item-avatar">+ <Avatar src={account.avatar} size="large" />+ {platInfo && (+ <div className="simpleAccountChoose-accounts-item-platform">+ <img src={platInfo.icon} alt={platInfo.name} />+ </div>+ )}+ </div>+ <span className="simpleAccountChoose-accounts-item-nickname">+ {account.nickname}+ </span>+ </Tooltip>++ <div className="simpleAccountChoose-accounts-item-choose">+ <CheckOutlined />+ </div>+ </div>+ );+ })}+ </div>+ </>+ )}+ </div>+ );+ },+ ),+);+SimpleAccountChoose.displayName = "SimpleAccountChoose";++export default SimpleAccountChoose;\ No newline at end of file