Skip to content

Let's use AI to Earn!

fix: Fix some problems (#335)

Gao7396b8e20745
diff --git a/project/web/src/components/PublishDialog/compoents/PlatParamsSetting/index.tsx b/project/web/src/components/PublishDialog/compoents/PlatParamsSetting/index.tsx
index 8deaa209..bcd3952a 100644
--- a/project/web/src/components/PublishDialog/compoents/PlatParamsSetting/index.tsx
+++ b/project/web/src/components/PublishDialog/compoents/PlatParamsSetting/index.tsx
@@ -69,10 +69,6 @@ const PlatParamsSetting = memo(
}
}, [pubItem, onImageToImage])
- useEffect(() => {
- console.log(pubItem, 1111)
- }, [pubItem])
-
// true=展开当前账号的参数设置 false=不展开
const isExpand = useMemo(() => {
if (step === 0)
diff --git a/project/web/src/components/PublishDialog/compoents/PlatParamsSetting/plats/FacebookParams.tsx b/project/web/src/components/PublishDialog/compoents/PlatParamsSetting/plats/FacebookParams.tsx
index 09b3fab8..036501c5 100644
--- a/project/web/src/components/PublishDialog/compoents/PlatParamsSetting/plats/FacebookParams.tsx
+++ b/project/web/src/components/PublishDialog/compoents/PlatParamsSetting/plats/FacebookParams.tsx
@@ -20,7 +20,7 @@ const FacebookParams = memo(
// 初始化Facebook参数
useEffect(() => {
const option = pubItem.params.option
- if (!option.facebook || !option.facebook.content_category) {
+ if (!option.facebook?.content_category) {
setOnePubParams(
{
option: {
diff --git a/project/web/src/components/PublishDialog/compoents/PlatParamsSetting/plats/InstagramParams.tsx b/project/web/src/components/PublishDialog/compoents/PlatParamsSetting/plats/InstagramParams.tsx
index 010e0fae..291e4e73 100644
--- a/project/web/src/components/PublishDialog/compoents/PlatParamsSetting/plats/InstagramParams.tsx
+++ b/project/web/src/components/PublishDialog/compoents/PlatParamsSetting/plats/InstagramParams.tsx
@@ -25,7 +25,7 @@ const InstagramParams = memo(
console.log('InstagramParams - Current option:', option)
console.log('InstagramParams - Current instagram:', option.instagram)
- if (!option.instagram || !option.instagram.content_category) {
+ if (!option.instagram?.content_category) {
console.log('InstagramParams - Setting default instagram option')
setOnePubParams(
{
diff --git a/project/web/src/components/PublishDialog/index.tsx b/project/web/src/components/PublishDialog/index.tsx
index 858873e8..507e7984 100644
--- a/project/web/src/components/PublishDialog/index.tsx
+++ b/project/web/src/components/PublishDialog/index.tsx
@@ -2,6 +2,8 @@ import type {
ForwardedRef,
} from 'react'
import type { SocialAccount } from '@/api/types/account.type'
+import type { AIAction, IPublishDialogAiRef } from '@/components/PublishDialog/compoents/PublishDialogAi'
+import type { IImgFile, IVideoFile } from '@/components/PublishDialog/publishDialog.type'
import {
ArrowRightOutlined,
CloseOutlined,
@@ -9,6 +11,7 @@ import {
InfoCircleOutlined,
SendOutlined,
} from '@ant-design/icons'
+
import { Button, message, Modal, Tooltip } from 'antd'
import {
forwardRef,
@@ -20,13 +23,12 @@ import {
useRef,
useState,
} from 'react'
-
import { CSSTransition } from 'react-transition-group'
import { useWindowSize } from 'react-use'
import { useShallow } from 'zustand/react/shallow'
+import { getChatModels } from '@/api/ai'
import { apiCreatePublish } from '@/api/plat/publish'
import { toolsApi } from '@/api/tools'
-import { getChatModels } from '@/api/ai'
import {
getDays,
getUtcDays,
@@ -52,16 +54,15 @@ import { useTransClient } from '@/app/i18n/client'
import AvatarPlat from '@/components/AvatarPlat'
import DownloadAppModal from '@/components/common/DownloadAppModal'
import PlatParamsSetting from '@/components/PublishDialog/compoents/PlatParamsSetting'
-import PublishDialogAi, { type AIAction, type IPublishDialogAiRef } from '@/components/PublishDialog/compoents/PublishDialogAi'
-import TextSelectionToolbar from '@/components/PublishDialog/compoents/TextSelectionToolbar'
import PublishDatePicker from '@/components/PublishDialog/compoents/PublishDatePicker'
+import PublishDialogAi from '@/components/PublishDialog/compoents/PublishDialogAi'
import PublishDialogPreview from '@/components/PublishDialog/compoents/PublishDialogPreview'
-import { usePublishManageUpload } from '@/components/PublishDialog/compoents/PublishManageUpload/usePublishManageUpload'
import { UploadTaskTypeEnum } from '@/components/PublishDialog/compoents/PublishManageUpload/publishManageUpload.enum'
+import { usePublishManageUpload } from '@/components/PublishDialog/compoents/PublishManageUpload/usePublishManageUpload'
import PubParmasTextarea from '@/components/PublishDialog/compoents/PubParmasTextarea'
+import TextSelectionToolbar from '@/components/PublishDialog/compoents/TextSelectionToolbar'
import usePubParamsVerify from '@/components/PublishDialog/hooks/usePubParamsVerify'
import { usePublishDialog } from '@/components/PublishDialog/usePublishDialog'
-import type { IImgFile, IVideoFile } from '@/components/PublishDialog/publishDialog.type'
import { usePublishDialogStorageStore } from '@/components/PublishDialog/usePublishDialogStorageStore'
import { useAccountStore } from '@/store/account'
import { generateUUID } from '@/utils'
@@ -102,11 +103,13 @@ const PublishDialog = memo(
setPubData,
restorePubData,
_hasHydrated,
+ setPubListData,
} = usePublishDialogStorageStore(
useShallow(state => ({
setPubData: state.setPubData,
restorePubData: state.restorePubData,
_hasHydrated: state._hasHydrated,
+ setPubListData: state.setPubListData,
})),
)
const {
@@ -183,7 +186,9 @@ const PublishDialog = memo(
})),
)
// 是否clear
- const isClear = useRef(false)
+ const isClear = useRef(true)
+ // 是否init
+ const isInit = useRef(false)
// 获取账户store
const { accountGroupList, getAccountList } = useAccountStore(
@@ -413,18 +418,26 @@ const PublishDialog = memo(
return false
}, [step, pubListChoosed, commonPubParams, expandedPubItem])
- useEffect(() => {
- isClear.current = true
- }, [])
-
// 实时保存数据
useEffect(() => {
+ if (!open)
+ return
if (isClear.current) {
isClear.current = false
return
}
setPubData(pubListChoosed)
}, [pubListChoosed])
+ useEffect(() => {
+ console.log(expandedPubItem)
+ if (!expandedPubItem || pubList.length === 0)
+ return
+ if (isInit.current) {
+ isInit.current = false
+ return
+ }
+ setPubListData(pubList)
+ }, [pubList])
// 检查选中的平台是否需要内容安全检测
const needsContentModeration = useMemo(() => {
@@ -462,24 +475,27 @@ const PublishDialog = memo(
useEffect(() => {
if (open) {
+ isInit.current = true
init(accounts, defaultAccountId)
-
+
// 获取聊天模型列表(使用 sessionStorage 缓存)
const cachedModels = sessionStorage.getItem('ai_chat_models')
if (cachedModels) {
try {
setChatModels(JSON.parse(cachedModels))
- } catch (error) {
+ }
+ catch (error) {
console.error(t('messages.parseCachedChatModelsFailed' as any), error)
}
- } else {
+ }
+ else {
// 如果没有缓存,则请求
getChatModels().then((res: any) => {
if (res?.code === 0 && res.data && Array.isArray(res.data)) {
setChatModels(res.data)
sessionStorage.setItem('ai_chat_models', JSON.stringify(res.data))
}
- }).catch(error => {
+ }).catch((error) => {
console.error(t('messages.getChatModelsFailed' as any), error)
})
}
@@ -490,9 +506,9 @@ const PublishDialog = memo(
setPubListChoosed([])
setStep(0)
setExpandedPubItem(undefined)
- // 注意:不要调用clear(),否则会清空pubList导致无法选择账户
+ clear()
}
- }, [accounts, open])
+ }, [open])
// 离线账号(status === 0)不可参与发布:如被默认选中则自动移除
useEffect(() => {
@@ -557,6 +573,7 @@ const PublishDialog = memo(
*/
const pubClick = useCallback(async () => {
setCreateLoading(true)
+
const publishTime = getUtcDays(
pubTime || getDays().add(5, 'second'),
).format()
@@ -596,7 +613,7 @@ const PublishDialog = memo(
onPubSuccess()
}
usePublishDialogStorageStore.getState().clearPubData()
- }, [pubListChoosed])
+ }, [pubListChoosed, pubTime])
// 处理划词操作
const handleTextSelection = useCallback((action: AIAction, selectedText: string) => {
@@ -609,7 +626,7 @@ const PublishDialog = memo(
aiAssistantRef.current?.processText(selectedText, action)
}, openLeft ? 100 : 500) // 如果已打开,减少延迟
}, [openLeft, setOpenLeft])
-
+
// 处理图生图
const handleImageToImage = useCallback((imageFile: IImgFile) => {
// 打开AI面板
@@ -624,37 +641,35 @@ const PublishDialog = memo(
// Sync AI content to editor
const handleSyncToEditor = useCallback(async (content: string, images?: IImgFile[], video?: IVideoFile, append?: boolean) => {
-
// Handle image upload
if (images && images.length > 0) {
const uploadsWithImages: Array<{ image: IImgFile, promise: Promise<any>, cancel: () => void }> = []
-
+
for (const image of images) {
const handle = enqueueUpload({
file: image.file,
fileName: image.filename,
type: UploadTaskTypeEnum.Image,
})
-
+
const imageWithTask: IImgFile = {
...image,
uploadTaskId: handle.taskId,
}
-
+
uploadsWithImages.push({
image: imageWithTask,
promise: handle.promise,
cancel: handle.cancel,
})
}
-
+
// Use images with uploadTaskId
images = uploadsWithImages.map(item => item.image)
}
-
+
// Handle video upload (AI-generated videos already have ossUrl, only need to upload cover)
if (video) {
-
// If video already has ossUrl (AI-generated), only upload cover
if (video.ossUrl && !video.cover.ossUrl) {
const coverHandle = enqueueUpload({
@@ -662,14 +677,14 @@ const PublishDialog = memo(
fileName: video.cover.filename,
type: UploadTaskTypeEnum.Image,
})
-
+
video = {
...video,
uploadTaskIds: {
cover: coverHandle.taskId,
},
}
- }
+ }
// If video doesn't have ossUrl (user uploaded), need to upload both video and cover
else if (!video.ossUrl) {
const videoHandle = enqueueUpload({
@@ -677,13 +692,13 @@ const PublishDialog = memo(
fileName: video.filename,
type: UploadTaskTypeEnum.Video,
})
-
+
const coverHandle = enqueueUpload({
file: video.cover.file,
fileName: video.cover.filename,
type: UploadTaskTypeEnum.Image,
})
-
+
video = {
...video,
uploadTaskIds: {
@@ -693,7 +708,7 @@ const PublishDialog = memo(
}
}
}
-
+
// If only one account, update directly
if (pubListChoosed.length === 1) {
const params: any = {}
@@ -701,8 +716,9 @@ const PublishDialog = memo(
if (content) {
// If append mode, append content to existing text
if (append && pubListChoosed[0].params.des) {
- params.des = pubListChoosed[0].params.des + '\n' + content
- } else {
+ params.des = `${pubListChoosed[0].params.des}\n${content}`
+ }
+ else {
params.des = content
}
}
@@ -711,11 +727,12 @@ const PublishDialog = memo(
params.video = video
// If has video, clear images
params.images = []
- } else if (images && images.length > 0) {
+ }
+ else if (images && images.length > 0) {
params.images = images
}
setOnePubParams(params, pubListChoosed[0].account.id)
- }
+ }
// If multiple accounts and in step 0, update common params
else if (pubListChoosed.length >= 2 && step === 0) {
const params: any = {}
@@ -723,8 +740,9 @@ const PublishDialog = memo(
if (content) {
// If append mode, append content to existing text
if (append && commonPubParams.des) {
- params.des = commonPubParams.des + '\n' + content
- } else {
+ params.des = `${commonPubParams.des}\n${content}`
+ }
+ else {
params.des = content
}
}
@@ -733,7 +751,8 @@ const PublishDialog = memo(
params.video = video
// If has video, clear images
params.images = []
- } else if (images && images.length > 0) {
+ }
+ else if (images && images.length > 0) {
params.images = images
}
setAccountAllParams(params)
@@ -745,8 +764,9 @@ const PublishDialog = memo(
if (content) {
// If append mode, append content to existing text
if (append && expandedPubItem.params.des) {
- params.des = expandedPubItem.params.des + '\n' + content
- } else {
+ params.des = `${expandedPubItem.params.des}\n${content}`
+ }
+ else {
params.des = content
}
}
@@ -755,7 +775,8 @@ const PublishDialog = memo(
params.video = video
// If has video, clear images
params.images = []
- } else if (images && images.length > 0) {
+ }
+ else if (images && images.length > 0) {
params.images = images
}
setOnePubParams(params, expandedPubItem.account.id)
@@ -784,7 +805,7 @@ const PublishDialog = memo(
classNames="left"
unmountOnExit
>
- <PublishDialogAi
+ <PublishDialogAi
ref={aiAssistantRef}
onClose={() => setOpenLeft(false)}
onSyncToEditor={handleSyncToEditor}
@@ -806,7 +827,7 @@ const PublishDialog = memo(
containerRef={contentAreaRef}
onAction={handleTextSelection}
/>
-
+
<div className="publishDialog-con" ref={contentAreaRef}>
<div className="publishDialog-con-head">
<span className="publishDialog-con-head-title">
@@ -1189,7 +1210,7 @@ const PublishDialog = memo(
classNames="left"
unmountOnExit
>
- <PublishDialogAi
+ <PublishDialogAi
ref={aiAssistantRef}
onClose={() => setOpenLeft(false)}
onSyncToEditor={handleSyncToEditor}
diff --git a/project/web/src/components/PublishDialog/usePublishDialog.ts b/project/web/src/components/PublishDialog/usePublishDialog.ts
index cd629aea..77fd9775 100644
--- a/project/web/src/components/PublishDialog/usePublishDialog.ts
+++ b/project/web/src/components/PublishDialog/usePublishDialog.ts
@@ -83,6 +83,8 @@ export const usePublishDialog = create(
})
},
setExpandedPubItem(expandedPubItem: PubItem | undefined) {
+ if (!expandedPubItem)
+ return
usePublishDialogStorageStore.getState().setExpandedPubItem(expandedPubItem)
set({
expandedPubItem,
diff --git a/project/web/src/components/PublishDialog/usePublishDialogStorageStore.tsx b/project/web/src/components/PublishDialog/usePublishDialogStorageStore.tsx
index 5076deae..4cb19788 100644
--- a/project/web/src/components/PublishDialog/usePublishDialogStorageStore.tsx
+++ b/project/web/src/components/PublishDialog/usePublishDialogStorageStore.tsx
@@ -1,22 +1,25 @@
import type { PubItem } from '@/components/PublishDialog/publishDialog.type'
import { ExclamationCircleFilled } from '@ant-design/icons'
import { Modal } from 'antd'
+import { directTrans } from '@/app/i18n/client'
import { usePublishDialog } from '@/components/PublishDialog/usePublishDialog'
import { useAccountStore } from '@/store/account'
import { modalApi, useConfigStore } from '@/store/config'
import { createPersistStore } from '@/utils/createPersistStore'
-import { directTrans } from '@/app/i18n/client'
export interface IPublishDialogStorageStore {
- pubData?: PubItem[]
+ pubListChoosed?: PubItem[]
expandedPubItem?: PubItem
+ pubList?: PubItem[]
}
const state: IPublishDialogStorageStore = {
// 实时保存的发布数据
- pubData: undefined,
+ pubListChoosed: undefined,
// 保存当前展开的数据
expandedPubItem: undefined,
+ // 实时保存的 所有发布列表
+ pubList: undefined,
}
export const usePublishDialogStorageStore = createPersistStore(
@@ -30,18 +33,21 @@ export const usePublishDialogStorageStore = createPersistStore(
expandedPubItem,
})
},
- setPubData(pubData: PubItem[] | undefined) {
- set({ pubData })
+ setPubData(pubListChoosed: PubItem[] | undefined) {
+ set({ pubListChoosed })
+ },
+ setPubListData(pubList?: PubItem[]) {
+ set({ pubList })
},
clearPubData() {
- set({ pubData: undefined, expandedPubItem: undefined })
+ set({ pubListChoosed: undefined, expandedPubItem: undefined, pubList: undefined })
},
// 恢复发布记录
restorePubData() {
- const { pubData } = _get()
- if (pubData === undefined || pubData.length === 0) {
+ const { pubListChoosed } = _get()
+ if (pubListChoosed === undefined || pubListChoosed.length === 0) {
return
}
// 提示用户是否恢复
@@ -63,48 +69,73 @@ export const usePublishDialogStorageStore = createPersistStore(
},
onCancel() {
set({
- pubData: undefined,
+ pubListChoosed: undefined,
})
},
})
},
- async restorePubDataCore() {
- let { pubData, expandedPubItem } = _get()
+ /**
+ * 处理单个发布项:更新账户信息、过滤无效媒体资源
+ * @param pubItem 发布项
+ * @returns 处理后的发布项,如果账户不存在则返回null
+ */
+ processPubItem(pubItem: PubItem): PubItem | null {
const accountMap = useAccountStore.getState().accountMap
- // 更新成最新的账户信息
- pubData = pubData?.map((v) => {
- const account = accountMap.get(v.account.id)
- // 过滤掉没有ossUrl的图片
- v.params.images = v.params.images?.filter(img => img.ossUrl)
- // 过滤掉没有ossUrl、或者封面ossUrl的视频
- if (v.params.video && (!v.params.video.ossUrl || !v.params.video.cover.ossUrl)) {
- v.params.video = undefined
- }
-
- if (v.params.video) {
- v.params.video.videoUrl = v.params.video.ossUrl!
- v.params.video.cover.imgUrl = v.params.video.cover.ossUrl!
- }
-
- v.params.images = v.params.images?.map((img) => {
- img.imgUrl = img.ossUrl!
- return img
- })
-
- if (account) {
- v.account = account
- }
- else {
- return null
- }
- return v
- }).filter(Boolean) as PubItem[]
+ const account = accountMap.get(pubItem.account.id)
+
+ // 过滤掉没有ossUrl的图片
+ pubItem.params.images = pubItem.params.images?.filter(img => img.ossUrl)
+
+ // 过滤掉没有ossUrl、或者封面ossUrl的视频
+ if (pubItem.params.video && (!pubItem.params.video.ossUrl || !pubItem.params.video.cover.ossUrl)) {
+ pubItem.params.video = undefined
+ }
+
+ // 更新视频URL
+ if (pubItem.params.video) {
+ pubItem.params.video.videoUrl = pubItem.params.video.ossUrl!
+ pubItem.params.video.cover.imgUrl = pubItem.params.video.cover.ossUrl!
+ }
+
+ // 更新图片URL
+ pubItem.params.images = pubItem.params.images?.map((img) => {
+ img.imgUrl = img.ossUrl!
+ return img
+ })
+
+ // 更新账户信息
+ if (account) {
+ pubItem.account = account
+ return pubItem
+ }
+
+ // 账户不存在,返回null
+ return null
+ },
+
+ async restorePubDataCore() {
+ let { pubListChoosed, expandedPubItem, pubList } = _get()
+
+ // 处理已选择的发布列表
+ pubListChoosed = pubListChoosed?.map(v => methods.processPubItem(v))
+ .filter(Boolean) as PubItem[]
+
+ // 处理所有发布列表
+ if (pubList && pubList.length > 0) {
+ pubList = pubList.map(v => methods.processPubItem(v))
+ .filter(Boolean) as PubItem[]
+ }
// 恢复数据
- usePublishDialog.getState().setPubListChoosed(pubData)
+ usePublishDialog.getState().setPubListChoosed(pubListChoosed)
usePublishDialog.getState().setStep(2)
- usePublishDialog.getState().setExpandedPubItem(expandedPubItem)
+ usePublishDialog.getState().setExpandedPubItem(pubListChoosed.find(v => v.account.id === expandedPubItem?.account.id))
+ if (pubList && pubList.length > 0) {
+ setTimeout(() => {
+ usePublishDialog.getState().setPubList(pubList)
+ }, 50)
+ }
},
}