2026-03-05 19:30:50 +08:00
|
|
|
import { useCallback, useMemo, useRef } from 'react';
|
2026-03-09 17:01:35 +08:00
|
|
|
import { Comment, usePublishComment } from '@bitsocialnet/bitsocial-react-hooks';
|
2025-01-30 17:09:21 +01:00
|
|
|
import usePublishPostStore from '../stores/use-publish-post-store';
|
2026-03-05 19:30:50 +08:00
|
|
|
import useChallengesStore from '../stores/use-challenges-store';
|
2025-01-30 17:09:21 +01:00
|
|
|
|
2026-03-13 13:25:10 +08:00
|
|
|
type UsePublishPostOptions = {
|
|
|
|
|
communityAddress?: string;
|
|
|
|
|
/** legacy compatibility */
|
|
|
|
|
subplebbitAddress?: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const usePublishPost = ({ communityAddress: requestedCommunityAddress, subplebbitAddress }: UsePublishPostOptions) => {
|
2025-11-25 13:13:38 +01:00
|
|
|
const { author, title, content, link, spoiler, publishCommentOptions } = usePublishPostStore((state) => ({
|
2025-01-30 17:09:21 +01:00
|
|
|
author: state.author,
|
2025-01-30 17:27:58 +01:00
|
|
|
title: state.title || undefined,
|
|
|
|
|
content: state.content || undefined,
|
|
|
|
|
link: state.link || undefined,
|
2025-01-30 17:09:21 +01:00
|
|
|
spoiler: state.spoiler || false,
|
|
|
|
|
publishCommentOptions: state.publishCommentOptions,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const setPublishPostStore = usePublishPostStore((state) => state.setPublishPostStore);
|
|
|
|
|
const resetPublishPostStore = usePublishPostStore((state) => state.resetPublishPostStore);
|
2026-03-05 19:30:50 +08:00
|
|
|
const addChallenge = useChallengesStore((state) => state.addChallenge);
|
|
|
|
|
const abandonPublishRef = useRef<(() => Promise<void>) | undefined>();
|
2026-03-05 20:47:49 +08:00
|
|
|
const abandonCurrentPublish = useCallback(async () => {
|
|
|
|
|
await abandonPublishRef.current?.();
|
|
|
|
|
}, []);
|
2025-01-30 17:09:21 +01:00
|
|
|
|
2026-03-13 13:25:10 +08:00
|
|
|
const communityAddress = requestedCommunityAddress ?? subplebbitAddress;
|
|
|
|
|
|
2025-01-30 17:09:21 +01:00
|
|
|
const createBaseOptions = useCallback(() => {
|
|
|
|
|
const baseOptions: Comment = {
|
2026-03-13 13:25:10 +08:00
|
|
|
communityAddress,
|
2025-01-30 17:09:21 +01:00
|
|
|
title,
|
|
|
|
|
content,
|
|
|
|
|
link,
|
|
|
|
|
spoiler,
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-03 15:50:32 +08:00
|
|
|
const displayName = author?.displayName;
|
|
|
|
|
if (displayName) {
|
|
|
|
|
baseOptions.author = { displayName };
|
|
|
|
|
}
|
2025-01-30 17:09:21 +01:00
|
|
|
|
|
|
|
|
return baseOptions;
|
2026-03-13 13:25:10 +08:00
|
|
|
}, [author, content, link, spoiler, communityAddress, title]);
|
2025-01-30 17:09:21 +01:00
|
|
|
|
|
|
|
|
const setPublishPostOptions = useCallback(
|
|
|
|
|
(options: Partial<Comment>) => {
|
|
|
|
|
const baseOptions = createBaseOptions();
|
2025-12-27 16:35:51 +01:00
|
|
|
const sanitizedOptions = Object.entries(options).reduce(
|
|
|
|
|
(acc, [key, value]) => {
|
|
|
|
|
acc[key] = value === '' ? undefined : value;
|
|
|
|
|
return acc;
|
|
|
|
|
},
|
|
|
|
|
{} as Partial<Comment>,
|
|
|
|
|
);
|
2025-01-30 17:09:21 +01:00
|
|
|
|
2026-04-11 16:57:00 +07:00
|
|
|
const {
|
|
|
|
|
communityAddress: nextCommunityAddress,
|
|
|
|
|
subplebbitAddress: legacyCommunityAddress,
|
|
|
|
|
...restOptions
|
|
|
|
|
} = sanitizedOptions as Partial<Comment> & { subplebbitAddress?: string };
|
|
|
|
|
const resolvedCommunityAddress = nextCommunityAddress ?? legacyCommunityAddress ?? baseOptions.communityAddress;
|
|
|
|
|
const newOptions = {
|
|
|
|
|
...baseOptions,
|
|
|
|
|
...restOptions,
|
|
|
|
|
...(resolvedCommunityAddress ? { communityAddress: resolvedCommunityAddress } : {}),
|
|
|
|
|
};
|
2025-01-30 17:09:21 +01:00
|
|
|
setPublishPostStore(newOptions);
|
|
|
|
|
},
|
|
|
|
|
[createBaseOptions, setPublishPostStore],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const resetPublishPostOptions = useCallback(() => resetPublishPostStore(), [resetPublishPostStore]);
|
|
|
|
|
|
2026-03-05 19:30:50 +08:00
|
|
|
const publishOptionsWithAbandon = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
...publishCommentOptions,
|
|
|
|
|
onChallenge: async (...args: any[]) => {
|
2026-03-05 20:47:49 +08:00
|
|
|
addChallenge(args, abandonCurrentPublish);
|
2026-03-05 19:30:50 +08:00
|
|
|
},
|
|
|
|
|
}),
|
2026-03-05 20:47:49 +08:00
|
|
|
[abandonCurrentPublish, addChallenge, publishCommentOptions],
|
2026-03-05 19:30:50 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const { index, publishComment, abandonPublish } = usePublishComment(publishOptionsWithAbandon);
|
|
|
|
|
abandonPublishRef.current = abandonPublish;
|
2025-01-30 17:09:21 +01:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
setPublishPostOptions,
|
|
|
|
|
resetPublishPostOptions,
|
|
|
|
|
postIndex: index,
|
|
|
|
|
publishPost: publishComment,
|
|
|
|
|
publishPostOptions: publishCommentOptions,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default usePublishPost;
|