From 9ad0b51b2978b934c3b1095efb81b073f5fbfd46 Mon Sep 17 00:00:00 2001
From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com>
Date: Sat, 16 Sep 2023 11:28:55 -0500
Subject: [PATCH] feat: finish search feature
---
convex/posts.ts | 1 -
src/components/Loader.tsx | 2 +-
src/components/Navbar.tsx | 5 +-
src/components/SearchResults.tsx | 95 +++++++++++++++++++++++++++-----
src/components/UserPosts.tsx | 2 +-
src/components/UserProfile.tsx | 2 +-
6 files changed, 87 insertions(+), 20 deletions(-)
diff --git a/convex/posts.ts b/convex/posts.ts
index 019e693..c93939f 100644
--- a/convex/posts.ts
+++ b/convex/posts.ts
@@ -201,7 +201,6 @@ export const getPostsBySearchTerm = query({
.withSearchIndex('search_by_content', q =>
q.search('contentText', args.term)
)
- .filter(q => q.eq(q.field('parentPostId'), undefined))
.paginate(args.paginationOpts);
const postsWithUser = await getPostsWithUsers({ ctx, posts: posts.page });
diff --git a/src/components/Loader.tsx b/src/components/Loader.tsx
index 0681dce..192eaa4 100644
--- a/src/components/Loader.tsx
+++ b/src/components/Loader.tsx
@@ -11,7 +11,7 @@ export default function Loader({
}) {
if (isLoading) {
return (
-
+
Loading...
diff --git a/src/components/Navbar.tsx b/src/components/Navbar.tsx
index 8cad95f..d9c646c 100644
--- a/src/components/Navbar.tsx
+++ b/src/components/Navbar.tsx
@@ -6,7 +6,7 @@ import Link from 'next/link';
import { useEffect, useState } from 'react';
import { AiFillHome } from 'react-icons/ai';
import { BiLogIn, BiPlus } from 'react-icons/bi';
-import { BsPersonPlusFill } from 'react-icons/bs';
+import { BsPersonPlusFill, BsSearch } from 'react-icons/bs';
import { ImProfile } from 'react-icons/im';
import {
RiMenuFoldLine,
@@ -75,10 +75,11 @@ export default function Navbar() {
className='w-full md:w-[unset] py-1 px-2 rounded-md border border-gray-600 dark:bg-primary-gray dark:border-0'
/>
diff --git a/src/components/SearchResults.tsx b/src/components/SearchResults.tsx
index 6cd133d..27b195a 100644
--- a/src/components/SearchResults.tsx
+++ b/src/components/SearchResults.tsx
@@ -1,13 +1,15 @@
'use client';
+import { PostWithUserDto, UserDto } from '@/app/types';
import { UsePaginatedQueryReturnType, usePaginatedQuery } from 'convex/react';
import { FunctionReference } from 'convex/server';
import { useState } from 'react';
import { api } from '../../convex/_generated/api';
-import { Id } from '../../convex/_generated/dataModel';
+import Loader from './Loader';
+import Post from './Post';
import UserProfile from './UserProfile';
-type UserSearchResultsPager = UsePaginatedQueryReturnType<
+type Pager
= UsePaginatedQueryReturnType<
FunctionReference<
'query',
'public',
@@ -20,29 +22,84 @@ type UserSearchResultsPager = UsePaginatedQueryReturnType<
};
},
{
- page: {
- _id: Id<'users'>;
- _creationTime: number;
- clerkUsername: string | null;
- clerkImageUrl: string;
- clerkUserId: string;
- }[];
+ page: T[];
isDone: boolean;
continueCursor: string;
}
>
>;
-function UserSearchResults({ pager }: { pager: UserSearchResultsPager }) {
+function NoResults() {
+ return (
+
+
+ Hmmm it doesn't look like we could find anything.
+
+
+ );
+}
+
+function UserSearchResults({
+ pager,
+ pageSize,
+}: {
+ pager: Pager;
+ pageSize: number;
+}) {
+ const { status, results, isLoading, loadMore } = pager;
+
+ if (isLoading === false && results.length == 0) {
+ return ;
+ }
+
return (
-
- {pager.results.map(user => (
+
+ {results.map(user => (
))}
+
loadMore(pageSize)}
+ />
+
+ );
+}
+
+function PostSearchResults({
+ pager,
+ pageSize,
+}: {
+ pager: Pager
;
+ pageSize: number;
+}) {
+ const { status, results, isLoading, loadMore } = pager;
+
+ if (isLoading === false && results.length == 0) {
+ return ;
+ }
+
+ return (
+
+
+ {results.map(post => (
+
+ ))}
+
+
loadMore(pageSize)}
+ />
);
}
@@ -63,7 +120,7 @@ export default function SearchResults({ term }: { term: string }) {
);
return (
-
+
- {current === 'posts' ? 'Posts' :
}
+ {current === 'posts' ? (
+
+ ) : (
+
+ )}
);
}
diff --git a/src/components/UserPosts.tsx b/src/components/UserPosts.tsx
index ef35256..d9546e3 100644
--- a/src/components/UserPosts.tsx
+++ b/src/components/UserPosts.tsx
@@ -47,7 +47,7 @@ export default function UserPosts({ user }: { user: UserDto }) {
{postsWithUser.map(post => (
diff --git a/src/components/UserProfile.tsx b/src/components/UserProfile.tsx
index 21e617e..00781ee 100644
--- a/src/components/UserProfile.tsx
+++ b/src/components/UserProfile.tsx
@@ -90,7 +90,7 @@ export default function UserProfile({ user }: { user: UserDto }) {
-