/* * Copyright 2025 Commonwealth Scientific and Industrial Research * Organisation (CSIRO) ABN 41 687 119 230. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import type { Coding, QuestionnaireItem, ValueSet } from 'fhir/r4'; import { getValueSetCodings, getValueSetPromise } from '../utils/valueSet'; import type { AlertColor } from '@mui/material/Alert'; import { useQuestionnaireStore, useRendererConfigStore, useTerminologyServerStore } from '../stores'; import { useQuery } from '@tanstack/react-query'; import { getItemTerminologyServerToUse } from '../utils/preferredTerminologyServer'; function useTerminologyServerQuery( qItem: QuestionnaireItem, maxList: number, input: string, searchTerm: string ): { options: Coding[]; loading: boolean; feedback?: { message: string; color: AlertColor } } { const processedValueSets = useQuestionnaireStore.use.processedValueSets(); const itemPreferredTerminologyServers = useQuestionnaireStore.use.itemPreferredTerminologyServers(); const defaultTerminologyServerUrl = useTerminologyServerStore.use.url(); const rendererStrings = useRendererConfigStore.use.rendererStrings(); let fullUrl = ''; let options: Coding[] = []; let feedback: { message: string; color: AlertColor } | undefined; if (input.length === 0) { feedback = undefined; } if (searchTerm.length === 1) { feedback = { message: rendererStrings.terminologyMinCharacters, color: 'info' }; } // Restructure url to include filter and count parameters let answerValueSetUrl = qItem.answerValueSet; if (answerValueSetUrl) { if (answerValueSetUrl.startsWith('#')) { answerValueSetUrl = answerValueSetUrl.slice(1); } // attempt to get url from contained value sets when loading questionnaire if (processedValueSets[answerValueSetUrl]?.updatableValueSetUrl) { answerValueSetUrl = processedValueSets[answerValueSetUrl].updatableValueSetUrl; } const urlWithTrailingAmpersand = answerValueSetUrl + (answerValueSetUrl[answerValueSetUrl.length - 1] !== '&' ? '&' : ''); fullUrl = urlWithTrailingAmpersand + 'filter=' + searchTerm + '&count=' + maxList; } // Perform query const terminologyServerUrl = getItemTerminologyServerToUse( qItem, itemPreferredTerminologyServers, defaultTerminologyServerUrl ); const { isFetching, error, data } = useQuery({ queryKey: ['expandValueSet', fullUrl], queryFn: () => getValueSetPromise(fullUrl, terminologyServerUrl), enabled: searchTerm.length >= 2 && answerValueSetUrl !== undefined }); if (error) { console.warn('Ontoserver query failed. Details below: \n' + error); feedback = { message: rendererStrings.terminologyError, color: 'error' }; } if (data) { if (data.expansion?.total !== 0) { options = getValueSetCodings(data); } else { feedback = { message: rendererStrings.terminologyNoResults, color: 'warning' }; } } return { options, loading: isFetching, feedback }; } export default useTerminologyServerQuery;