chore(forum): decouple toasts from lib

This commit is contained in:
Danish Arora 2025-09-01 16:23:06 +05:30
parent 55ba3f374e
commit 34279ff8b1
No known key found for this signature in database
GPG Key ID: 1C6EF37CDAE1426E
2 changed files with 395 additions and 298 deletions

View File

@ -284,17 +284,32 @@ export function ForumProvider({ children }: { children: React.ReactNode }) {
content: string content: string
): Promise<Post | null> => { ): Promise<Post | null> => {
setIsPostingPost(true); setIsPostingPost(true);
toast({
title: 'Creating post',
description: 'Sending your post to the network...',
});
const result = await createPost( const result = await createPost(
cellId, { cellId, title, content, currentUser, isAuthenticated },
title,
content,
currentUser,
isAuthenticated,
toast,
updateStateFromCache updateStateFromCache
); );
setIsPostingPost(false); setIsPostingPost(false);
return result;
if (result.success) {
toast({
title: 'Post Created',
description: 'Your post has been published successfully.',
});
return result.data || null;
} else {
toast({
title: 'Post Failed',
description: result.error || 'Failed to create post. Please try again.',
variant: 'destructive',
});
return null;
}
}; };
const handleCreateComment = async ( const handleCreateComment = async (
@ -302,16 +317,35 @@ export function ForumProvider({ children }: { children: React.ReactNode }) {
content: string content: string
): Promise<Comment | null> => { ): Promise<Comment | null> => {
setIsPostingComment(true); setIsPostingComment(true);
toast({
title: 'Posting comment',
description: 'Sending your comment to the network...',
});
const result = await createComment( const result = await createComment(
postId, postId,
content, content,
currentUser, currentUser,
isAuthenticated, isAuthenticated,
toast,
updateStateFromCache updateStateFromCache
); );
setIsPostingComment(false); setIsPostingComment(false);
return result;
if (result.success) {
toast({
title: 'Comment Added',
description: 'Your comment has been published.',
});
return result.data || null;
} else {
toast({
title: 'Comment Failed',
description: result.error || 'Failed to add comment. Please try again.',
variant: 'destructive',
});
return null;
}
}; };
const handleVotePost = async ( const handleVotePost = async (
@ -319,16 +353,37 @@ export function ForumProvider({ children }: { children: React.ReactNode }) {
isUpvote: boolean isUpvote: boolean
): Promise<boolean> => { ): Promise<boolean> => {
setIsVoting(true); setIsVoting(true);
const voteType = isUpvote ? 'upvote' : 'downvote';
toast({
title: `Sending ${voteType}`,
description: 'Recording your vote on the network...',
});
const result = await vote( const result = await vote(
postId, postId,
isUpvote, isUpvote,
currentUser, currentUser,
isAuthenticated, isAuthenticated,
toast,
updateStateFromCache updateStateFromCache
); );
setIsVoting(false); setIsVoting(false);
return result;
if (result.success) {
toast({
title: 'Vote Recorded',
description: `Your ${voteType} has been registered.`,
});
return result.data || false;
} else {
toast({
title: 'Vote Failed',
description:
result.error || 'Failed to register your vote. Please try again.',
variant: 'destructive',
});
return false;
}
}; };
const handleVoteComment = async ( const handleVoteComment = async (
@ -336,16 +391,37 @@ export function ForumProvider({ children }: { children: React.ReactNode }) {
isUpvote: boolean isUpvote: boolean
): Promise<boolean> => { ): Promise<boolean> => {
setIsVoting(true); setIsVoting(true);
const voteType = isUpvote ? 'upvote' : 'downvote';
toast({
title: `Sending ${voteType}`,
description: 'Recording your vote on the network...',
});
const result = await vote( const result = await vote(
commentId, commentId,
isUpvote, isUpvote,
currentUser, currentUser,
isAuthenticated, isAuthenticated,
toast,
updateStateFromCache updateStateFromCache
); );
setIsVoting(false); setIsVoting(false);
return result;
if (result.success) {
toast({
title: 'Vote Recorded',
description: `Your ${voteType} has been registered.`,
});
return result.data || false;
} else {
toast({
title: 'Vote Failed',
description:
result.error || 'Failed to register your vote. Please try again.',
variant: 'destructive',
});
return false;
}
}; };
const handleCreateCell = async ( const handleCreateCell = async (
@ -354,17 +430,36 @@ export function ForumProvider({ children }: { children: React.ReactNode }) {
icon?: string icon?: string
): Promise<Cell | null> => { ): Promise<Cell | null> => {
setIsPostingCell(true); setIsPostingCell(true);
toast({
title: 'Creating cell',
description: 'Sending your cell to the network...',
});
const result = await createCell( const result = await createCell(
name, name,
description, description,
icon, icon,
currentUser, currentUser,
isAuthenticated, isAuthenticated,
toast,
updateStateFromCache updateStateFromCache
); );
setIsPostingCell(false); setIsPostingCell(false);
return result;
if (result.success) {
toast({
title: 'Cell Created',
description: 'Your cell has been published.',
});
return result.data || null;
} else {
toast({
title: 'Cell Failed',
description: result.error || 'Failed to create cell. Please try again.',
variant: 'destructive',
});
return null;
}
}; };
const handleModeratePost = async ( const handleModeratePost = async (
@ -373,16 +468,36 @@ export function ForumProvider({ children }: { children: React.ReactNode }) {
reason: string | undefined, reason: string | undefined,
cellOwner: string cellOwner: string
) => { ) => {
return moderatePost( toast({
title: 'Moderating Post',
description: 'Sending moderation message to the network...',
});
const result = await moderatePost(
cellId, cellId,
postId, postId,
reason, reason,
currentUser, currentUser,
isAuthenticated, isAuthenticated,
cellOwner, cellOwner,
toast,
updateStateFromCache updateStateFromCache
); );
if (result.success) {
toast({
title: 'Post Moderated',
description: 'The post has been marked as moderated.',
});
return result.data || false;
} else {
toast({
title: 'Moderation Failed',
description:
result.error || 'Failed to moderate post. Please try again.',
variant: 'destructive',
});
return false;
}
}; };
const handleModerateComment = async ( const handleModerateComment = async (
@ -391,16 +506,36 @@ export function ForumProvider({ children }: { children: React.ReactNode }) {
reason: string | undefined, reason: string | undefined,
cellOwner: string cellOwner: string
) => { ) => {
return moderateComment( toast({
title: 'Moderating Comment',
description: 'Sending moderation message to the network...',
});
const result = await moderateComment(
cellId, cellId,
commentId, commentId,
reason, reason,
currentUser, currentUser,
isAuthenticated, isAuthenticated,
cellOwner, cellOwner,
toast,
updateStateFromCache updateStateFromCache
); );
if (result.success) {
toast({
title: 'Comment Moderated',
description: 'The comment has been marked as moderated.',
});
return result.data || false;
} else {
toast({
title: 'Moderation Failed',
description:
result.error || 'Failed to moderate comment. Please try again.',
variant: 'destructive',
});
return false;
}
}; };
const handleModerateUser = async ( const handleModerateUser = async (
@ -409,16 +544,31 @@ export function ForumProvider({ children }: { children: React.ReactNode }) {
reason: string | undefined, reason: string | undefined,
cellOwner: string cellOwner: string
) => { ) => {
return moderateUser( const result = await moderateUser(
cellId, cellId,
userAddress, userAddress,
reason, reason,
currentUser, currentUser,
isAuthenticated, isAuthenticated,
cellOwner, cellOwner,
toast,
updateStateFromCache updateStateFromCache
); );
if (result.success) {
toast({
title: 'User Moderated',
description: `User ${userAddress} has been moderated in this cell.`,
});
return result.data || false;
} else {
toast({
title: 'Moderation Failed',
description:
result.error || 'Failed to moderate user. Please try again.',
variant: 'destructive',
});
return false;
}
}; };
return ( return (

View File

@ -11,36 +11,39 @@ import {
PostMessage, PostMessage,
} from '@/types/waku'; } from '@/types/waku';
import { Cell, Comment, Post } from '@/types/forum'; import { Cell, Comment, Post } from '@/types/forum';
import { User } from '@/types/identity'; import { EVerificationStatus, User } from '@/types/identity';
import { transformCell, transformComment, transformPost } from './transformers'; import { transformCell, transformComment, transformPost } from './transformers';
import { MessageService, CryptoService } from '@/lib/services'; import { MessageService, CryptoService } from '@/lib/services';
type ToastFunction = (props: { // Result types for action functions
title: string; type ActionResult<T> = {
description: string; success: boolean;
variant?: 'default' | 'destructive'; data?: T;
}) => void; error?: string;
};
/* ------------------------------------------------------------------ /* ------------------------------------------------------------------
POST / COMMENT / CELL CREATION POST / COMMENT / CELL CREATION
-------------------------------------------------------------------*/ -------------------------------------------------------------------*/
interface PostCreationParams {
cellId: string;
title: string;
content: string;
currentUser: User | null;
isAuthenticated: boolean;
}
export const createPost = async ( export const createPost = async (
cellId: string, { cellId, title, content, currentUser, isAuthenticated }: PostCreationParams,
title: string,
content: string,
currentUser: User | null,
isAuthenticated: boolean,
toast: ToastFunction,
updateStateFromCache: () => void updateStateFromCache: () => void
): Promise<Post | null> => { ): Promise<ActionResult<Post>> => {
if (!isAuthenticated || !currentUser) { if (!isAuthenticated || !currentUser) {
toast({ return {
title: 'Authentication Required', success: false,
description: 'You need to connect your wallet to post.', error:
variant: 'destructive', 'Authentication required. You need to connect your wallet to post.',
}); };
return null;
} }
// Check if user has basic verification or better, or owns ENS/Ordinal // Check if user has basic verification or better, or owns ENS/Ordinal
@ -48,29 +51,23 @@ export const createPost = async (
currentUser.ensDetails || currentUser.ordinalDetails currentUser.ensDetails || currentUser.ordinalDetails
); );
const isVerified = const isVerified =
currentUser.verificationStatus === 'verified-owner' || currentUser.verificationStatus === EVerificationStatus.VERIFIED_OWNER ||
currentUser.verificationStatus === 'verified-basic' || currentUser.verificationStatus === EVerificationStatus.VERIFIED_BASIC ||
hasENSOrOrdinal; hasENSOrOrdinal;
if ( if (
!isVerified && !isVerified &&
(currentUser.verificationStatus === 'unverified' || (currentUser.verificationStatus === EVerificationStatus.UNVERIFIED ||
currentUser.verificationStatus === 'verifying') currentUser.verificationStatus === EVerificationStatus.VERIFYING)
) { ) {
toast({ return {
title: 'Verification Required', success: false,
description: 'Please complete wallet verification to post.', error:
variant: 'destructive', 'Verification required. Please complete wallet verification to post.',
}); };
return null;
} }
try { try {
toast({
title: 'Creating post',
description: 'Sending your post to the network...',
});
const postId = uuidv4(); const postId = uuidv4();
const postMessage: UnsignedPostMessage = { const postMessage: UnsignedPostMessage = {
type: MessageType.POST, type: MessageType.POST,
@ -86,28 +83,30 @@ export const createPost = async (
const messageService = new MessageService(cryptoService); const messageService = new MessageService(cryptoService);
const result = await messageService.sendMessage(postMessage); const result = await messageService.sendMessage(postMessage);
if (!result.success) { if (!result.success) {
toast({ return {
title: 'Post Failed', success: false,
description: result.error || 'Failed to create post. Please try again.', error: result.error || 'Failed to create post. Please try again.',
variant: 'destructive', };
});
return null;
} }
updateStateFromCache(); updateStateFromCache();
toast({ const transformedPost = transformPost(result.message! as PostMessage);
title: 'Post Created', if (!transformedPost) {
description: 'Your post has been published successfully.', return {
}); success: false,
return transformPost(result.message! as PostMessage); error: 'Failed to transform post data.',
};
}
return {
success: true,
data: transformedPost,
};
} catch (error) { } catch (error) {
console.error('Error creating post:', error); console.error('Error creating post:', error);
toast({ return {
title: 'Post Failed', success: false,
description: 'Failed to create post. Please try again.', error: 'Failed to create post. Please try again.',
variant: 'destructive', };
});
return null;
} }
}; };
@ -116,16 +115,14 @@ export const createComment = async (
content: string, content: string,
currentUser: User | null, currentUser: User | null,
isAuthenticated: boolean, isAuthenticated: boolean,
toast: ToastFunction,
updateStateFromCache: () => void updateStateFromCache: () => void
): Promise<Comment | null> => { ): Promise<ActionResult<Comment>> => {
if (!isAuthenticated || !currentUser) { if (!isAuthenticated || !currentUser) {
toast({ return {
title: 'Authentication Required', success: false,
description: 'You need to connect your wallet to comment.', error:
variant: 'destructive', 'Authentication required. You need to connect your wallet to comment.',
}); };
return null;
} }
// Check if user has basic verification or better, or owns ENS/Ordinal // Check if user has basic verification or better, or owns ENS/Ordinal
@ -133,29 +130,23 @@ export const createComment = async (
currentUser.ensDetails || currentUser.ordinalDetails currentUser.ensDetails || currentUser.ordinalDetails
); );
const isVerified = const isVerified =
currentUser.verificationStatus === 'verified-owner' || currentUser.verificationStatus === EVerificationStatus.VERIFIED_OWNER ||
currentUser.verificationStatus === 'verified-basic' || currentUser.verificationStatus === EVerificationStatus.VERIFIED_BASIC ||
hasENSOrOrdinal; hasENSOrOrdinal;
if ( if (
!isVerified && !isVerified &&
(currentUser.verificationStatus === 'unverified' || (currentUser.verificationStatus === EVerificationStatus.UNVERIFIED ||
currentUser.verificationStatus === 'verifying') currentUser.verificationStatus === EVerificationStatus.VERIFYING)
) { ) {
toast({ return {
title: 'Verification Required', success: false,
description: 'Please complete wallet verification to comment.', error:
variant: 'destructive', 'Verification required. Please complete wallet verification to comment.',
}); };
return null;
} }
try { try {
toast({
title: 'Posting comment',
description: 'Sending your comment to the network...',
});
const commentId = uuidv4(); const commentId = uuidv4();
const commentMessage: UnsignedCommentMessage = { const commentMessage: UnsignedCommentMessage = {
type: MessageType.COMMENT, type: MessageType.COMMENT,
@ -170,28 +161,32 @@ export const createComment = async (
const messageService = new MessageService(cryptoService); const messageService = new MessageService(cryptoService);
const result = await messageService.sendMessage(commentMessage); const result = await messageService.sendMessage(commentMessage);
if (!result.success) { if (!result.success) {
toast({ return {
title: 'Comment Failed', success: false,
description: result.error || 'Failed to add comment. Please try again.', error: result.error || 'Failed to add comment. Please try again.',
variant: 'destructive', };
});
return null;
} }
updateStateFromCache(); updateStateFromCache();
toast({ const transformedComment = transformComment(
title: 'Comment Added', result.message! as CommentMessage
description: 'Your comment has been published.', );
}); if (!transformedComment) {
return transformComment(result.message! as CommentMessage); return {
success: false,
error: 'Failed to transform comment data.',
};
}
return {
success: true,
data: transformedComment,
};
} catch (error) { } catch (error) {
console.error('Error creating comment:', error); console.error('Error creating comment:', error);
toast({ return {
title: 'Comment Failed', success: false,
description: 'Failed to add comment. Please try again.', error: 'Failed to add comment. Please try again.',
variant: 'destructive', };
});
return null;
} }
}; };
@ -201,24 +196,17 @@ export const createCell = async (
icon: string | undefined, icon: string | undefined,
currentUser: User | null, currentUser: User | null,
isAuthenticated: boolean, isAuthenticated: boolean,
toast: ToastFunction,
updateStateFromCache: () => void updateStateFromCache: () => void
): Promise<Cell | null> => { ): Promise<ActionResult<Cell>> => {
if (!isAuthenticated || !currentUser) { if (!isAuthenticated || !currentUser) {
toast({ return {
title: 'Authentication Required', success: false,
description: 'You need to verify Ordinal ownership to create a cell.', error:
variant: 'destructive', 'Authentication required. You need to verify Ordinal ownership to create a cell.',
}); };
return null;
} }
try { try {
toast({
title: 'Creating cell',
description: 'Sending your cell to the network...',
});
const cellId = uuidv4(); const cellId = uuidv4();
const cellMessage: UnsignedCellMessage = { const cellMessage: UnsignedCellMessage = {
type: MessageType.CELL, type: MessageType.CELL,
@ -234,28 +222,30 @@ export const createCell = async (
const messageService = new MessageService(cryptoService); const messageService = new MessageService(cryptoService);
const result = await messageService.sendMessage(cellMessage); const result = await messageService.sendMessage(cellMessage);
if (!result.success) { if (!result.success) {
toast({ return {
title: 'Cell Failed', success: false,
description: result.error || 'Failed to create cell. Please try again.', error: result.error || 'Failed to create cell. Please try again.',
variant: 'destructive', };
});
return null;
} }
updateStateFromCache(); updateStateFromCache();
toast({ const transformedCell = transformCell(result.message! as CellMessage);
title: 'Cell Created', if (!transformedCell) {
description: 'Your cell has been published.', return {
}); success: false,
return transformCell(result.message! as CellMessage); error: 'Failed to transform cell data.',
};
}
return {
success: true,
data: transformedCell,
};
} catch (error) { } catch (error) {
console.error('Error creating cell:', error); console.error('Error creating cell:', error);
toast({ return {
title: 'Cell Failed', success: false,
description: 'Failed to create cell. Please try again.', error: 'Failed to create cell. Please try again.',
variant: 'destructive', };
});
return null;
} }
}; };
@ -268,16 +258,14 @@ export const vote = async (
isUpvote: boolean, isUpvote: boolean,
currentUser: User | null, currentUser: User | null,
isAuthenticated: boolean, isAuthenticated: boolean,
toast: ToastFunction,
updateStateFromCache: () => void updateStateFromCache: () => void
): Promise<boolean> => { ): Promise<ActionResult<boolean>> => {
if (!isAuthenticated || !currentUser) { if (!isAuthenticated || !currentUser) {
toast({ return {
title: 'Authentication Required', success: false,
description: 'You need to connect your wallet to vote.', error:
variant: 'destructive', 'Authentication required. You need to connect your wallet to vote.',
}); };
return false;
} }
// Check if user has basic verification or better, or owns ENS/Ordinal // Check if user has basic verification or better, or owns ENS/Ordinal
@ -285,30 +273,23 @@ export const vote = async (
currentUser.ensDetails || currentUser.ordinalDetails currentUser.ensDetails || currentUser.ordinalDetails
); );
const isVerified = const isVerified =
currentUser.verificationStatus === 'verified-owner' || currentUser.verificationStatus === EVerificationStatus.VERIFIED_OWNER ||
currentUser.verificationStatus === 'verified-basic' || currentUser.verificationStatus === EVerificationStatus.VERIFIED_BASIC ||
hasENSOrOrdinal; hasENSOrOrdinal;
if ( if (
!isVerified && !isVerified &&
(currentUser.verificationStatus === 'unverified' || (currentUser.verificationStatus === EVerificationStatus.UNVERIFIED ||
currentUser.verificationStatus === 'verifying') currentUser.verificationStatus === EVerificationStatus.VERIFYING)
) { ) {
toast({ return {
title: 'Verification Required', success: false,
description: 'Please complete wallet verification to vote.', error:
variant: 'destructive', 'Verification required. Please complete wallet verification to vote.',
}); };
return false;
} }
try { try {
const voteType = isUpvote ? 'upvote' : 'downvote';
toast({
title: `Sending ${voteType}`,
description: 'Recording your vote on the network...',
});
const voteId = uuidv4(); const voteId = uuidv4();
const voteMessage: UnsignedVoteMessage = { const voteMessage: UnsignedVoteMessage = {
type: MessageType.VOTE, type: MessageType.VOTE,
@ -323,29 +304,24 @@ export const vote = async (
const messageService = new MessageService(cryptoService); const messageService = new MessageService(cryptoService);
const result = await messageService.sendMessage(voteMessage); const result = await messageService.sendMessage(voteMessage);
if (!result.success) { if (!result.success) {
toast({ return {
title: 'Vote Failed', success: false,
description: error:
result.error || 'Failed to register your vote. Please try again.', result.error || 'Failed to register your vote. Please try again.',
variant: 'destructive', };
});
return false;
} }
updateStateFromCache(); updateStateFromCache();
toast({ return {
title: 'Vote Recorded', success: true,
description: `Your ${voteType} has been registered.`, data: true,
}); };
return true;
} catch (error) { } catch (error) {
console.error('Error voting:', error); console.error('Error voting:', error);
toast({ return {
title: 'Vote Failed', success: false,
description: 'Failed to register your vote. Please try again.', error: 'Failed to register your vote. Please try again.',
variant: 'destructive', };
});
return false;
} }
}; };
@ -360,32 +336,23 @@ export const moderatePost = async (
currentUser: User | null, currentUser: User | null,
isAuthenticated: boolean, isAuthenticated: boolean,
cellOwner: string, cellOwner: string,
toast: ToastFunction,
updateStateFromCache: () => void updateStateFromCache: () => void
): Promise<boolean> => { ): Promise<ActionResult<boolean>> => {
if (!isAuthenticated || !currentUser) { if (!isAuthenticated || !currentUser) {
toast({ return {
title: 'Authentication Required', success: false,
description: 'You need to verify Ordinal ownership to moderate posts.', error:
variant: 'destructive', 'Authentication required. You need to verify Ordinal ownership to moderate posts.',
}); };
return false;
} }
if (currentUser.address !== cellOwner) { if (currentUser.address !== cellOwner) {
toast({ return {
title: 'Not Authorized', success: false,
description: 'Only the cell admin can moderate posts.', error: 'Not authorized. Only the cell admin can moderate posts.',
variant: 'destructive', };
});
return false;
} }
try { try {
toast({
title: 'Moderating Post',
description: 'Sending moderation message to the network...',
});
const modMsg: UnsignedModerateMessage = { const modMsg: UnsignedModerateMessage = {
type: MessageType.MODERATE, type: MessageType.MODERATE,
id: uuidv4(), id: uuidv4(),
@ -400,29 +367,23 @@ export const moderatePost = async (
const messageService = new MessageService(cryptoService); const messageService = new MessageService(cryptoService);
const result = await messageService.sendMessage(modMsg); const result = await messageService.sendMessage(modMsg);
if (!result.success) { if (!result.success) {
toast({ return {
title: 'Moderation Failed', success: false,
description: error: result.error || 'Failed to moderate post. Please try again.',
result.error || 'Failed to moderate post. Please try again.', };
variant: 'destructive',
});
return false;
} }
updateStateFromCache(); updateStateFromCache();
toast({ return {
title: 'Post Moderated', success: true,
description: 'The post has been marked as moderated.', data: true,
}); };
return true;
} catch (error) { } catch (error) {
console.error('Error moderating post:', error); console.error('Error moderating post:', error);
toast({ return {
title: 'Moderation Failed', success: false,
description: 'Failed to moderate post. Please try again.', error: 'Failed to moderate post. Please try again.',
variant: 'destructive', };
});
return false;
} }
}; };
@ -433,32 +394,23 @@ export const moderateComment = async (
currentUser: User | null, currentUser: User | null,
isAuthenticated: boolean, isAuthenticated: boolean,
cellOwner: string, cellOwner: string,
toast: ToastFunction,
updateStateFromCache: () => void updateStateFromCache: () => void
): Promise<boolean> => { ): Promise<ActionResult<boolean>> => {
if (!isAuthenticated || !currentUser) { if (!isAuthenticated || !currentUser) {
toast({ return {
title: 'Authentication Required', success: false,
description: 'You need to verify Ordinal ownership to moderate comments.', error:
variant: 'destructive', 'Authentication required. You need to verify Ordinal ownership to moderate comments.',
}); };
return false;
} }
if (currentUser.address !== cellOwner) { if (currentUser.address !== cellOwner) {
toast({ return {
title: 'Not Authorized', success: false,
description: 'Only the cell admin can moderate comments.', error: 'Not authorized. Only the cell admin can moderate comments.',
variant: 'destructive', };
});
return false;
} }
try { try {
toast({
title: 'Moderating Comment',
description: 'Sending moderation message to the network...',
});
const modMsg: UnsignedModerateMessage = { const modMsg: UnsignedModerateMessage = {
type: MessageType.MODERATE, type: MessageType.MODERATE,
id: uuidv4(), id: uuidv4(),
@ -473,29 +425,23 @@ export const moderateComment = async (
const messageService = new MessageService(cryptoService); const messageService = new MessageService(cryptoService);
const result = await messageService.sendMessage(modMsg); const result = await messageService.sendMessage(modMsg);
if (!result.success) { if (!result.success) {
toast({ return {
title: 'Moderation Failed', success: false,
description: error: result.error || 'Failed to moderate comment. Please try again.',
result.error || 'Failed to moderate comment. Please try again.', };
variant: 'destructive',
});
return false;
} }
updateStateFromCache(); updateStateFromCache();
toast({ return {
title: 'Comment Moderated', success: true,
description: 'The comment has been marked as moderated.', data: true,
}); };
return true;
} catch (error) { } catch (error) {
console.error('Error moderating comment:', error); console.error('Error moderating comment:', error);
toast({ return {
title: 'Moderation Failed', success: false,
description: 'Failed to moderate comment. Please try again.', error: 'Failed to moderate comment. Please try again.',
variant: 'destructive', };
});
return false;
} }
}; };
@ -506,26 +452,23 @@ export const moderateUser = async (
currentUser: User | null, currentUser: User | null,
isAuthenticated: boolean, isAuthenticated: boolean,
cellOwner: string, cellOwner: string,
toast: ToastFunction,
updateStateFromCache: () => void updateStateFromCache: () => void
): Promise<boolean> => { ): Promise<ActionResult<boolean>> => {
if (!isAuthenticated || !currentUser) { if (!isAuthenticated || !currentUser) {
toast({ return {
title: 'Authentication Required', success: false,
description: 'You need to verify Ordinal ownership to moderate users.', error:
variant: 'destructive', 'Authentication required. You need to verify Ordinal ownership to moderate users.',
}); };
return false;
} }
if (currentUser.address !== cellOwner) { if (currentUser.address !== cellOwner) {
toast({ return {
title: 'Not Authorized', success: false,
description: 'Only the cell admin can moderate users.', error: 'Not authorized. Only the cell admin can moderate users.',
variant: 'destructive', };
});
return false;
} }
try {
const modMsg: UnsignedModerateMessage = { const modMsg: UnsignedModerateMessage = {
type: MessageType.MODERATE, type: MessageType.MODERATE,
id: uuidv4(), id: uuidv4(),
@ -540,18 +483,22 @@ export const moderateUser = async (
const messageService = new MessageService(cryptoService); const messageService = new MessageService(cryptoService);
const result = await messageService.sendMessage(modMsg); const result = await messageService.sendMessage(modMsg);
if (!result.success) { if (!result.success) {
toast({ return {
title: 'Moderation Failed', success: false,
description: result.error || 'Failed to moderate user. Please try again.', error: result.error || 'Failed to moderate user. Please try again.',
variant: 'destructive', };
});
return false;
} }
updateStateFromCache(); updateStateFromCache();
toast({ return {
title: 'User Moderated', success: true,
description: `User ${userAddress} has been moderated in this cell.`, data: true,
}); };
return true; } catch (error) {
console.error('Error moderating user:', error);
return {
success: false,
error: 'Failed to moderate user. Please try again.',
};
}
}; };