diff --git a/convex/posts.ts b/convex/posts.ts index c9331fa..019e693 100644 --- a/convex/posts.ts +++ b/convex/posts.ts @@ -28,8 +28,10 @@ export const createOrUpdatePost = mutation({ return 'CANNOT_POST_ON_BEHALF_OF_ANOTHER_USER'; } + const contentText = args.content.join('\n'); + if (args.id) { - await ctx.db.patch(args.id, { content: args.content }); + await ctx.db.patch(args.id, { content: args.content, contentText }); return; } @@ -37,6 +39,7 @@ export const createOrUpdatePost = mutation({ userId: user._id, content: args.content, parentPostId: args.parentPostId, + contentText, }); }, }); @@ -44,12 +47,16 @@ export const createOrUpdatePost = mutation({ export const getUsersPostById = query({ args: { userId: v.id('users'), paginationOpts: paginationOptsValidator }, handler: async (ctx, args) => { - return await ctx.db + const posts = await ctx.db .query('posts') .withIndex('by_user_id', q => q.eq('userId', args.userId)) .filter(q => q.eq(q.field('parentPostId'), undefined)) .order('desc') .paginate(args.paginationOpts); + + const postDtos = posts.page.map(createPostDto); + + return { ...posts, page: postDtos }; }, }); @@ -81,7 +88,7 @@ export const getPostById = query({ } return { - ...post, + ...createPostDto(post), user: createUserDto(user), }; }, @@ -100,6 +107,7 @@ export const getRepliesByParentId = query({ ctx, posts: replies.page, }); + return { ...replies, page: repliesWithUserData }; }, }); @@ -191,8 +199,9 @@ export const getPostsBySearchTerm = query({ const posts = await ctx.db .query('posts') .withSearchIndex('search_by_content', q => - q.search('content', args.term).eq('parentPostId', undefined) + q.search('contentText', args.term) ) + .filter(q => q.eq(q.field('parentPostId'), undefined)) .paginate(args.paginationOpts); const postsWithUser = await getPostsWithUsers({ ctx, posts: posts.page }); @@ -210,19 +219,25 @@ async function getPostsWithUsers({ }) { return await Promise.all( posts.map(async post => { + const postDto = createPostDto(post); const user = await ctx.db.get(post.userId); if (user === null) { return { - ...post, + ...postDto, user: createUserDto(user), }; } return { - ...post, + ...postDto, user: createUserDto(user), }; }) ); } + +function createPostDto(post: Doc<'posts'>) { + const { contentText, ...postDto } = post; + return postDto; +} diff --git a/convex/schema.ts b/convex/schema.ts index bb58827..335ae1a 100644 --- a/convex/schema.ts +++ b/convex/schema.ts @@ -44,11 +44,12 @@ export default defineSchema( parentPostId: v.optional(v.id('posts')), userId: v.id('users'), content: v.array(v.string()), + contentText: v.string(), }) .index('by_user_id', ['userId']) .index('by_parent_id', ['parentPostId']) .searchIndex('search_by_content', { - searchField: 'content', + searchField: 'contentText', filterFields: ['parentPostId'], }), likes: defineTable({ diff --git a/src/app/search/[term]/page.tsx b/src/app/search/[term]/page.tsx deleted file mode 100644 index 7a04868..0000000 --- a/src/app/search/[term]/page.tsx +++ /dev/null @@ -1,12 +0,0 @@ -import SearchResults from '@/components/SearchResults'; - -export default function SearchPage({ params }: { params: { term: string } }) { - return ( -
-
-

Here's what we found

- -
-
- ); -} diff --git a/src/app/search/page.tsx b/src/app/search/page.tsx new file mode 100644 index 0000000..9b09de3 --- /dev/null +++ b/src/app/search/page.tsx @@ -0,0 +1,26 @@ +import SearchResults from '@/components/SearchResults'; + +export default function SearchPage({ + searchParams, +}: { + searchParams: { [key: string]: string | string[] | undefined }; +}) { + let term = searchParams.term; + + if (term === undefined) { + term = ''; + } + + if (Array.isArray(term)) { + term = term.length > 0 ? term[0] : ''; + } + + return ( +
+
+

Here's what we found

+ +
+
+ ); +} diff --git a/src/app/types/index.ts b/src/app/types/index.ts index 7fc5b34..4a6b278 100644 --- a/src/app/types/index.ts +++ b/src/app/types/index.ts @@ -8,4 +8,6 @@ export type UserDto = { clerkUserId: string; }; -export type PostWithUserDto = Doc<'posts'> & { user: UserDto }; +export type PostWithUserDto = Omit, 'contentText'> & { + user: UserDto; +}; diff --git a/src/components/Navbar.tsx b/src/components/Navbar.tsx index 3eb50fe..8cad95f 100644 --- a/src/components/Navbar.tsx +++ b/src/components/Navbar.tsx @@ -63,7 +63,7 @@ export default function Navbar() {
{ e.preventDefault(); - router.push(`/search/${term}`); + router.push(`/search?term=${term}`); }} className='flex w-full relative' >