Skip to content

Commit

Permalink
Create treatment page with meta-analysis
Browse files Browse the repository at this point in the history
Introduced a new treatment page to display meta-analysis on safety and efficacy. Updated related components and actions to support fetching the meta-analysis based on treatment and condition names. Enhanced existing components for better user input handling using autocomplete options.
  • Loading branch information
mikepsinn committed Sep 14, 2024
1 parent 3bba7e4 commit b4080c7
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 27 deletions.
15 changes: 6 additions & 9 deletions app/dfda/components/CostBenefitAnalysis.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { useState } from 'react'
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Card, CardHeader, CardTitle, CardDescription, CardContent } from "@/components/ui/card"
import { Stethoscope, Pill, BarChart } from "lucide-react"
import ConditionSearchAutocomplete from './ConditionSearchAutocomplete'
import TreatmentSearchAutocomplete from './TreatmentSearchAutocomplete'

export default function CostBenefitAnalysis() {
const [condition, setCondition] = useState('')
Expand All @@ -27,20 +28,16 @@ export default function CostBenefitAnalysis() {
<form onSubmit={handleAnalyze} className="space-y-4">
<div className="flex items-center space-x-2">
<Stethoscope className="text-gray-500" />
<Input
type="text"
<ConditionSearchAutocomplete
onConditionSelect={setCondition}
placeholder="Enter medical condition"
value={condition}
onChange={(e) => setCondition(e.target.value)}
/>
</div>
<div className="flex items-center space-x-2">
<Pill className="text-gray-500" />
<Input
type="text"
<TreatmentSearchAutocomplete
onTreatmentSelect={setTreatment}
placeholder="Enter treatment"
value={treatment}
onChange={(e) => setTreatment(e.target.value)}
/>
</div>
<div className="flex justify-center">
Expand Down
2 changes: 1 addition & 1 deletion app/dfda/components/TreatmentList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default function TreatmentList({ condition }: TreatmentListProps) {
<div className="flex-grow">
<div className="text-primary font-bold">#{index + 1}</div>
<Link
href={`/dfda/treatments/${encodeURIComponent(treatment.treatment.name)}`}
href={`/dfda/conditions/${encodeURIComponent(condition.name)}/treatments/${encodeURIComponent(treatment.treatment.name)}`}
className="hover:underline"
>
<h3 className="font-bold">{toTitleCase(treatment.treatment.name)}</h3>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use client"
import React, { useEffect, useState } from 'react'
import { useParams } from 'next/navigation'
import { getMetaAnalysis } from '@/app/dfda/dfdaActions'
import ArticleRenderer from '@/components/ArticleRenderer'
import { ArticleWithRelations } from '@/lib/agents/researcher/researcher'
import GlobalHealthOptimizationAgent from "@/components/landingPage/global-health-optimization-agent";
import { useSession } from 'next-auth/react'

export default function TreatmentForConditionPage() {
const { data: session, status } = useSession()
const params = useParams()
const [article, setArticle] = useState<ArticleWithRelations | null>(null)
const [loading, setLoading] = useState(status === "loading")

if (loading) {
return <div>Loading...</div>
}

useEffect(() => {
async function fetchMetaAnalysis() {
debugger
console.log("Fetching meta-analysis for", params.treatmentName, params.conditionName)
if (typeof params.treatmentName === 'string' && typeof params.conditionName === 'string') {
try {
const metaAnalysis = await getMetaAnalysis(params.treatmentName, params.conditionName)
setArticle(metaAnalysis)
} catch (error) {
console.error('Error fetching meta-analysis:', error)
} finally {
setLoading(false)
}
}
}

fetchMetaAnalysis()
}, [params.treatmentName, params.conditionName])

if (loading) {
return <div className="container mx-auto px-4 py-8">
<GlobalHealthOptimizationAgent />
</div>
}

if (!article) {
return <div className="container mx-auto px-4 py-8">Failed to load meta-analysis.</div>
}

return (
<div className="container mx-auto px-4 py-8">
<ArticleRenderer article={article} currentUserId={session?.user?.id} />
</div>
)
}
14 changes: 12 additions & 2 deletions app/dfda/dfdaActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import { Effectiveness } from "@prisma/client"

import { prisma } from "@/lib/db"
import {findArticleByTopic, writeArticle} from "@/lib/agents/researcher/researcher";

export async function fetchConditions() {
return prisma.dfdaCondition.findMany()
}



export async function addTreatment(
userId: string,
conditionName: string,
Expand Down Expand Up @@ -149,4 +148,15 @@ export async function getConditionByName(name: string) {
},
},
});
}

export async function getMetaAnalysis(treatmentName: string, conditionName: string) {
const topic = `Meta-analysis on the safety and effectiveness of ${treatmentName} for ${conditionName}`;
const article = await findArticleByTopic(topic);
debugger
if(article) {
return article;
}

return writeArticle(topic);
}
9 changes: 3 additions & 6 deletions app/dfda/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,19 @@ export default async function DashboardLayout({
const user = await getCurrentUser()

return (
<div className="flex min-h-screen flex-col space-y-6">
<div className="flex min-h-screen flex-col">
<TopNavbar
user={{
name: user?.name,
image: user?.image,
email: user?.email,
}}
/>
<div className="container grid flex-1 gap-12 md:grid-cols-[200px_1fr]">
<div className="flex flex-1">
<aside className="hidden w-[200px] flex-col md:flex">
<SidebarNav items={generalSidebarNav.data} />
</aside>
<main
className="flex w-full flex-1 flex-col"
style={{ maxWidth: "90%" }}
>
<main className="flex-1 px-4 py-6">
{children}
</main>
</div>
Expand Down
67 changes: 67 additions & 0 deletions app/dfda/treatments/[treatmentName]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"use client"
import React, { useEffect, useState } from 'react'
import { useParams } from 'next/navigation'
import { useSession } from 'next-auth/react'
import ArticleRenderer from '@/components/ArticleRenderer'
import { ArticleWithRelations } from '@/lib/agents/researcher/researcher'
import GlobalHealthOptimizationAgent from "@/components/landingPage/global-health-optimization-agent"
import { findOrCreateArticleByTopic } from "@/app/researcher/researcherActions"
import { UserAuthForm } from "@/components/user/user-auth-form"

export default function TreatmentPage() {
const { data: session, status } = useSession()
const params = useParams()
const [article, setArticle] = useState<ArticleWithRelations | null>(null)
const [loading, setLoading] = useState(true)
const treatmentName = typeof params.treatmentName === 'string' ? decodeURIComponent(params.treatmentName) : ''

useEffect(() => {
async function fetchMetaAnalysis() {
if (!session?.user?.id || !treatmentName) {
setLoading(false)
return
}

try {
const metaAnalysis = await findOrCreateArticleByTopic(
`Article on the Safety and Efficacy of ${treatmentName} for Various Conditions`,
session.user.id
)
setArticle(metaAnalysis)
} catch (error) {
console.error('Error fetching meta-analysis:', error)
} finally {
setLoading(false)
}
}

if (status === 'authenticated') {
fetchMetaAnalysis()
} else if (status === 'unauthenticated') {
setLoading(false)
}
}, [treatmentName, session?.user?.id, status])

if (status === 'loading' || loading) {
return (
<div className="container mx-auto px-4 py-8">
<GlobalHealthOptimizationAgent />
</div>
)
}

if (!session?.user?.id) {
return <UserAuthForm />
}

if (!article) {
return <div className="container mx-auto px-4 py-8">Failed to load meta-analysis.</div>
}

return (
<div className="container mx-auto px-4 py-8">
<h1 className="text-2xl font-bold mb-4">Treatment: {treatmentName}</h1>
<ArticleRenderer article={article} currentUserId={session.user.id} />
</div>
)
}
26 changes: 26 additions & 0 deletions app/dfda/treatments/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react'
import Link from 'next/link'
import { fetchConditions } from '../dfdaActions'

export default async function ConditionListPage() {
const conditions = await fetchConditions()
const sortedConditions = conditions.sort((a, b) => b.numberOfTreatments - a.numberOfTreatments)

return (
<div className="container mx-auto px-4 py-8 space-y-8">
<h1 className="text-2xl font-bold">Conditions</h1>
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
{sortedConditions.map((condition) => (
<Link href={`/dfda/conditions/${condition.name}`} key={condition.id}>
<div className="card p-4 rounded-lg shadow-md hover:shadow-lg transition-shadow duration-300">
<h2 className="text-lg font-semibold">{condition.name}</h2>
<span className="inline-block mt-2 text-xs font-semibold px-2 py-1 rounded-full bg-opacity-20">
{condition.numberOfTreatments} Treatments
</span>
</div>
</Link>
))}
</div>
</div>
)
}

0 comments on commit b4080c7

Please sign in to comment.