Skip to content

Commit 2967549

Browse files
committed
Refactor HomePage component and update related imports
Replaces HomePage component with DFDAHomePage and updates its structure. Adjusts related imports and incorporates a new VariableSearchAutocomplete with optimized caching. Took 35 minutes
1 parent db97d70 commit 2967549

File tree

4 files changed

+97
-33
lines changed

4 files changed

+97
-33
lines changed

app/dfda/components/AdvancedTrialSearch.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import React, { useState } from 'react'
44
import { Search, ArrowRight, ChevronDown } from 'lucide-react'
55
import { useRouter } from 'next/navigation'
6-
import { searchConditions, searchInterventions } from '@/lib/clinicaltables'
6+
import { searchConditions } from '@/lib/clinicaltables'
77
import { motion, AnimatePresence } from 'framer-motion'
88
interface SearchFilters {
99
// Basic Search
@@ -229,18 +229,14 @@ export default function AdvancedTrialSearch({ initialFilters }: AdvancedTrialSea
229229
setCondition(value)
230230

231231
// Removed the character length limitation
232-
if (true) {
233-
setLoading(true)
234-
try {
235-
const results = await searchConditions(value)
236-
setSuggestions(results.slice(0, 5))
237-
} catch (error) {
238-
console.error('Error fetching suggestions:', error)
239-
}
240-
setLoading(false)
241-
} else {
242-
setSuggestions([])
232+
setLoading(true)
233+
try {
234+
const results = await searchConditions(value)
235+
setSuggestions(results.slice(0, 5))
236+
} catch (error) {
237+
console.error('Error fetching suggestions:', error)
243238
}
239+
setLoading(false)
244240
}
245241

246242
const handleInputFocus = async () => {

app/dfda/components/VariableSearchAutocomplete.tsx

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,21 @@ function getCachedResults(key: string): GlobalVariable[] | null {
4040

4141
function setCachedResults(key: string, results: GlobalVariable[]) {
4242
try {
43+
// shrink the variables and just keep the name, url, and pngUrl
44+
const smallerResults = results.map(variable => ({
45+
name: variable.name,
46+
url: variable.url,
47+
pngUrl: variable.pngUrl,
48+
id: variable.id,
49+
userId: variable.userId,
50+
variableId: variable.variableId,
51+
displayName: variable.displayName,
52+
description: variable.description,
53+
variableCategoryName: variable.variableCategoryName
54+
}))
4355
const cacheData: SearchCache = {
4456
timestamp: Date.now(),
45-
results
57+
results: smallerResults
4658
}
4759
localStorage.setItem(key, JSON.stringify(cacheData))
4860
} catch (error) {
@@ -60,6 +72,14 @@ export default function VariableSearchAutocomplete({
6072
const [isLoading, setIsLoading] = useState(false)
6173
const [showDropdown, setShowDropdown] = useState(false)
6274
const componentRef = useRef<HTMLDivElement>(null)
75+
const [cachedVariables, setCachedVariables] = useState<GlobalVariable[]>([])
76+
// Create a cache key based on search term and params
77+
const cacheKey = `dfda-variable-search:${searchTerm}:${
78+
JSON.stringify(Object.keys(searchParams).sort().reduce<Record<string, string>>((obj, key) => {
79+
obj[key] = searchParams[key]
80+
return obj
81+
}, {}))
82+
}`
6383

6484
useEffect(() => {
6585
function handleClickOutside(event: MouseEvent) {
@@ -75,17 +95,16 @@ export default function VariableSearchAutocomplete({
7595
}, [])
7696

7797
useEffect(() => {
98+
const cachedVariables = getCachedResults(cacheKey)
99+
if (cachedVariables) {
100+
setCachedVariables(cachedVariables)
101+
}
102+
78103
const search = async () => {
79104
console.log('Searching for:', searchTerm ? `"${searchTerm}"` : '(empty string)')
80105
setIsLoading(true)
81106
try {
82-
// Create a cache key based on search term and params
83-
const cacheKey = `dfda-variable-search:${searchTerm}:${
84-
JSON.stringify(Object.keys(searchParams).sort().reduce<Record<string, string>>((obj, key) => {
85-
obj[key] = searchParams[key]
86-
return obj
87-
}, {}))
88-
}`
107+
89108
// Check cache first
90109
const cachedResults = getCachedResults(cacheKey)
91110
if (cachedResults) {
@@ -161,6 +180,26 @@ export default function VariableSearchAutocomplete({
161180
))}
162181
</div>
163182
)}
183+
184+
{/* Cached variables section */}
185+
<div className="mt-4 flex flex-wrap gap-2">
186+
{cachedVariables.map((variable, index) => (
187+
<button
188+
key={`${cacheKey}-${index}`}
189+
onClick={() => onVariableSelect(variable)}
190+
className="flex items-center gap-2 rounded-full border border-gray-200 bg-white px-3 py-1 text-sm hover:bg-gray-50"
191+
>
192+
{variable.pngUrl && (
193+
<img
194+
src={variable.pngUrl}
195+
alt=""
196+
className="h-4 w-4 object-contain"
197+
/>
198+
)}
199+
<span>{variable.displayName || variable.name}</span>
200+
</button>
201+
))}
202+
</div>
164203
</div>
165204
)
166205
}

app/dfda/components/home-page.tsx renamed to app/dfda/components/dfda-home-page.tsx

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import React from 'react'
44
import Link from 'next/link'
5-
import { ArrowRight, Dna, Pill, Users, Activity, Info, Scroll } from 'lucide-react'
5+
import { ArrowRight, Pill, Users, Activity, Info, Scroll } from 'lucide-react'
66
import { motion } from 'framer-motion'
77
import CostSavingsTable from './CostSavingsTable'
8-
import PredictorSearchAutocomplete from './PredictorSearchAutocomplete'
98
import { GlobalVariable } from '@/types/models/all'
10-
import OutcomeSearchAutocomplete from './OutcomeSearchAutocomplete'
119
import AdvancedTrialSearch from './AdvancedTrialSearch'
1210
import { Robot } from '@phosphor-icons/react'
1311
import { FeatureBox } from './FeatureBox'
@@ -26,7 +24,7 @@ const SquigglyPattern = () => (
2624
</svg>
2725
)
2826

29-
export default function HomePage() {
27+
export default function DFDAHomePage() {
3028
const router = useRouter()
3129
const [isLoading, setIsLoading] = useState(false)
3230

@@ -168,16 +166,52 @@ export default function HomePage() {
168166

169167
<main className="space-y-12">
170168
<section className="relative overflow-visible rounded-xl border-4 border-black bg-gradient-to-r from-pink-400 to-purple-400 p-6 shadow-[8px_8px_0px_0px_rgba(0,0,0,1)]">
171-
<h2 className="mb-6 text-4xl font-black uppercase">See Effects of Foods🍟 or Drugs💊</h2>
169+
<h2 className="mb-6 text-4xl font-black uppercase">See Effects of Foods🍟</h2>
172170
<div className="flex flex-col gap-4 md:flex-row">
173-
<PredictorSearchAutocomplete onVariableSelect={onVariableSelect} />
171+
<VariableSearchAutocomplete
172+
onVariableSelect={onVariableSelect}
173+
searchParams={{
174+
sort: '-numberOfCorrelationsAsCause',
175+
isPublic: '1',
176+
variableCategoryName: 'Foods',
177+
limit: '50'
178+
}}
179+
placeholder="Enter Foods🍟"
180+
/>
181+
182+
</div>
183+
</section>
184+
185+
<section className="relative overflow-visible rounded-xl border-4 border-black bg-gradient-to-r from-pink-400 to-purple-400 p-6 shadow-[8px_8px_0px_0px_rgba(0,0,0,1)]">
186+
<h2 className="mb-6 text-4xl font-black uppercase">See Effects of Treatments💊</h2>
187+
<div className="flex flex-col gap-4 md:flex-row">
188+
<VariableSearchAutocomplete
189+
onVariableSelect={onVariableSelect}
190+
searchParams={{
191+
sort: '-numberOfCorrelationsAsCause',
192+
isPublic: '1',
193+
variableCategoryName: 'Treatments',
194+
limit: '50'
195+
}}
196+
placeholder="Enter treatment 💊"
197+
/>
198+
174199
</div>
175200
</section>
176201

177202
<section className="relative overflow-visible rounded-xl border-4 border-black bg-gradient-to-r from-pink-400 to-purple-400 p-6 shadow-[8px_8px_0px_0px_rgba(0,0,0,1)]">
178203
<h2 className="mb-6 text-4xl font-black uppercase">See Most Effective Treatments for your Condition</h2>
179204
<div className="flex flex-col gap-4 md:flex-row">
180-
<OutcomeSearchAutocomplete onVariableSelect={onVariableSelect} />
205+
<VariableSearchAutocomplete
206+
onVariableSelect={onVariableSelect}
207+
searchParams={{
208+
sort: '-numberOfCorrelationsAsCause',
209+
isPublic: '1',
210+
variableCategoryName: 'Symptoms',
211+
limit: '50'
212+
}}
213+
placeholder="Enter treatment 💊"
214+
/>
181215
</div>
182216
</section>
183217

app/dfda/page.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
"use client"
22
import React from 'react'
3-
import FindTreatments from './components/FindTreatments'
4-
import HealthTrackingLinkBoxes from './components/HealthTrackingLinkBoxes'
5-
import CostBenefitAnalysis from './components/CostBenefitAnalysis'
6-
import TopTreatments from './components/TopTreatments'
7-
import TreatmentConditionSearchBox from "@/app/dfda/components/TreatmentConditionSearchBox";
8-
import HomePage from './components/home-page'
3+
import HomePage from './components/dfda-home-page'
94

105
export default function DFDAPage() {
116
return (

0 commit comments

Comments
 (0)