Skip to content

Commit

Permalink
Add tag-based article filtering and new tag page
Browse files Browse the repository at this point in the history
Expanded article search to include filtering by tags. Introduced a new page for viewing articles under specific tags. Updated article grid to link to the new tag-specific pages.
  • Loading branch information
mikepsinn committed Oct 2, 2024
1 parent e9cbdfa commit e1004be
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
20 changes: 20 additions & 0 deletions app/researcher/articles/tags/[tagSlug]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use client'

import { useParams } from 'next/navigation'
import ArticleSearchAndGrid from "@/components/article/ArticleSearchAndGrid";

type Params = {
tagSlug: string
}

export default function TagArticles() {
const params = useParams<Params>()
const tagSlug = params?.tagSlug ?? ''

return (
<div className="container mx-auto p-4">
<h1 className="text-3xl font-bold mb-6">Articles in {tagSlug}</h1>
<ArticleSearchAndGrid tagSlug={tagSlug} />
</div>
)
}
9 changes: 8 additions & 1 deletion app/researcher/researcherActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const openai = new OpenAI({
})
const prisma = new PrismaClient()

export async function searchArticles(query: string, categorySlug?: string) {
export async function searchArticles(query: string, categorySlug?: string, tagSlug?: string) {
try {
const whereClause: any = {
OR: [
Expand All @@ -28,6 +28,13 @@ export async function searchArticles(query: string, categorySlug?: string) {
whereClause.category = { slug: categorySlug };
}

if (tagSlug) {
const tag = await prisma.articleTag.findUnique({ where: { slug: tagSlug } });
if (tag) {
whereClause.tags = { some: { id: tag.id } };
}
}

return await prisma.article.findMany({
where: whereClause,
select: {
Expand Down
2 changes: 1 addition & 1 deletion components/ArticleGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function ArticleGrid({ articles }: ArticleGridProps) {
<CardFooter className="mt-auto pt-4 border-t">
<div className="flex flex-wrap gap-2">
{article.tags.map((tag) => (
<Link key={tag.name} href={`/research/tags/${tag.name}`}>
<Link key={tag.name} href={`/researcher/articles/tags/${tag.slug}`}>
<Badge variant="outline" className="text-xs">{tag.name}</Badge>
</Link>
))}
Expand Down
7 changes: 4 additions & 3 deletions components/article/ArticleSearchAndGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import { searchArticles } from '@/app/researcher/researcherActions'

type ArticleSearchAndGridProps = {
categorySlug?: string
tagSlug?: string
}

export default function ArticleSearchAndGrid({ categorySlug = '' }: ArticleSearchAndGridProps) {
export default function ArticleSearchAndGrid({ categorySlug = '', tagSlug = '' }: ArticleSearchAndGridProps) {
const [articles, setArticles] = useState<ArticleWithRelations[]>([])
const [isLoading, setIsLoading] = useState(true)
const [searchQuery, setSearchQuery] = useState('')
Expand All @@ -18,7 +19,7 @@ export default function ArticleSearchAndGrid({ categorySlug = '' }: ArticleSearc
const fetchArticles = async () => {
setIsLoading(true)
try {
const results = await searchArticles(searchQuery, categorySlug)
const results = await searchArticles(searchQuery, categorySlug, tagSlug)
setArticles(results as ArticleWithRelations[])
} catch (error) {
console.error('Error fetching articles:', error)
Expand All @@ -27,7 +28,7 @@ export default function ArticleSearchAndGrid({ categorySlug = '' }: ArticleSearc
}
}
fetchArticles()
}, [categorySlug, searchQuery])
}, [categorySlug, tagSlug, searchQuery])

const handleSearch = (query: string) => {
setSearchQuery(query)
Expand Down
2 changes: 1 addition & 1 deletion tests/researcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { findOrCreateArticleByPromptedTopic } from '@/lib/agents/researcher/rese

const prisma = new PrismaClient();

describe("Database-seeder tests", () => {
describe("Research Agent tests", () => {
jest.setTimeout(6000000)

it("Generates article by URL", async () => {
Expand Down

0 comments on commit e1004be

Please sign in to comment.