diff --git a/src/lib/utils/pattern-utils.ts b/src/lib/utils/pattern-utils.ts index ae4dc3f6..5ed7a121 100644 --- a/src/lib/utils/pattern-utils.ts +++ b/src/lib/utils/pattern-utils.ts @@ -135,20 +135,15 @@ export const displayNameMatchesPattern = (comment: Comment, pattern: string): bo * * @param comment The comment to check * @param role The role to check for (without the #!# prefix) + * @param subplebbitRoles The roles object from the subplebbit * @returns True if the user has the specified role, false otherwise */ -export const userHasRole = (comment: Comment, role: string): boolean => { - if (!role || !comment?.author?.address || !comment?.subplebbitAddress) { +export const userHasRole = (comment: Comment, role: string, subplebbitRoles?: { [key: string]: { role: string } }): boolean => { + if (!role || !comment?.author?.address || !subplebbitRoles) { return false; } - const subplebbit = useSubplebbit({ subplebbitAddress: comment.subplebbitAddress, onlyIfCached: true }); - - if (!subplebbit?.roles) { - return false; - } - - const userRole = subplebbit.roles[comment.author.address]?.role; + const userRole = subplebbitRoles[comment.author.address]?.role; // Handle different role names (moderator/mod) if ((role.toLowerCase() === 'moderator' || role.toLowerCase() === 'mod') && userRole === 'moderator') { @@ -200,9 +195,10 @@ export const parsePattern = ( * * @param comment The comment to check * @param pattern The pattern to match + * @param subplebbitRoles Optional roles object from the subplebbit (required for role filters) * @returns True if the comment matches the pattern, false otherwise */ -export const commentMatchesPattern = (comment: Comment, pattern: string): boolean => { +export const commentMatchesPattern = (comment: Comment, pattern: string, subplebbitRoles?: { [key: string]: { role: string } }): boolean => { if (!pattern || !comment) return false; // Check if the pattern contains spaces, which might indicate combined filters @@ -218,7 +214,7 @@ export const commentMatchesPattern = (comment: Comment, pattern: string): boolea } else if (filter.type === 'displayName') { return displayNameMatchesPattern(comment, filter.value); } else if (filter.type === 'role') { - return userHasRole(comment, filter.value); + return userHasRole(comment, filter.value, subplebbitRoles); } return false; }); @@ -248,7 +244,7 @@ export const commentMatchesPattern = (comment: Comment, pattern: string): boolea // Check for role filter (starts with #!#) if (pattern.startsWith('#!#')) { const rolePattern = pattern.substring(3); - return userHasRole(comment, rolePattern); + return userHasRole(comment, rolePattern, subplebbitRoles); } // Regular content matching @@ -260,6 +256,23 @@ export const commentMatchesPattern = (comment: Comment, pattern: string): boolea return matchesPattern(textToMatch, pattern); }; +/** + * Custom hook version of commentMatchesPattern that can handle role filters + * by fetching subplebbit data when needed + * + * @param comment The comment to check + * @param pattern The pattern to match + * @returns True if the comment matches the pattern, false otherwise + */ +export const useCommentMatchesPattern = (comment: Comment, pattern: string): boolean => { + const subplebbit = useSubplebbit({ + subplebbitAddress: comment?.subplebbitAddress, + onlyIfCached: true, + }); + + return commentMatchesPattern(comment, pattern, subplebbit?.roles); +}; + /** * Highlights text that matches a search pattern by wrapping matches with tags *