Skip to content

Commit 97fb006

Browse files
authored
Merge pull request wishonia#143 from mikepsinn/main
dFDA variable search stuff
2 parents daed7fd + 05d5029 commit 97fb006

File tree

5 files changed

+99
-33
lines changed

5 files changed

+99
-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: 42 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'
@@ -16,6 +14,7 @@ import CitizenScienceSection from './CitizenScienceSection'
1614
import { useRouter } from 'next/navigation'
1715
import { useState } from 'react'
1816
import { getSafeUrlWithToken } from '../dfdaActions'
17+
import VariableSearchAutocomplete from "@/app/dfda/components/VariableSearchAutocomplete";
1918

2019
const SquigglyPattern = () => (
2120
<svg className="absolute inset-0 h-full w-full" xmlns="http://www.w3.org/2000/svg">
@@ -26,7 +25,7 @@ const SquigglyPattern = () => (
2625
</svg>
2726
)
2827

29-
export default function HomePage() {
28+
export default function DFDAHomePage() {
3029
const router = useRouter()
3130
const [isLoading, setIsLoading] = useState(false)
3231

@@ -168,16 +167,52 @@ export default function HomePage() {
168167

169168
<main className="space-y-12">
170169
<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>
170+
<h2 className="mb-6 text-4xl font-black uppercase">See Effects of Foods🍟</h2>
172171
<div className="flex flex-col gap-4 md:flex-row">
173-
<PredictorSearchAutocomplete onVariableSelect={onVariableSelect} />
172+
<VariableSearchAutocomplete
173+
onVariableSelect={onVariableSelect}
174+
searchParams={{
175+
sort: '-numberOfCorrelationsAsCause',
176+
isPublic: '1',
177+
variableCategoryName: 'Foods',
178+
limit: '50'
179+
}}
180+
placeholder="Enter Foods🍟"
181+
/>
182+
183+
</div>
184+
</section>
185+
186+
<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)]">
187+
<h2 className="mb-6 text-4xl font-black uppercase">See Effects of Treatments💊</h2>
188+
<div className="flex flex-col gap-4 md:flex-row">
189+
<VariableSearchAutocomplete
190+
onVariableSelect={onVariableSelect}
191+
searchParams={{
192+
sort: '-numberOfCorrelationsAsCause',
193+
isPublic: '1',
194+
variableCategoryName: 'Treatments',
195+
limit: '50'
196+
}}
197+
placeholder="Enter treatment 💊"
198+
/>
199+
174200
</div>
175201
</section>
176202

177203
<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)]">
178204
<h2 className="mb-6 text-4xl font-black uppercase">See Most Effective Treatments for your Condition</h2>
179205
<div className="flex flex-col gap-4 md:flex-row">
180-
<OutcomeSearchAutocomplete onVariableSelect={onVariableSelect} />
206+
<VariableSearchAutocomplete
207+
onVariableSelect={onVariableSelect}
208+
searchParams={{
209+
sort: '-numberOfCorrelationsAsCause',
210+
isPublic: '1',
211+
variableCategoryName: 'Symptoms',
212+
limit: '50'
213+
}}
214+
placeholder="Enter treatment 💊"
215+
/>
181216
</div>
182217
</section>
183218

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 (

types/models/GlobalVariable.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export class GlobalVariable {
173173
*/
174174
"id": number
175175
"imageUrl"?: string
176+
"url"?: string
176177
/**
177178
* Ex: https://google.com
178179
*/

0 commit comments

Comments
 (0)