1- import { createContext , useContext , useEffect , useState } from 'react' ;
2- import _ from 'lodash' ;
1+ import { createContext , useContext , useEffect , useState } from 'react'
2+ import _ from 'lodash'
3+
4+ import { useConfig } from '@eplant/config'
5+ import GeneticElement from '@eplant/GeneticElement'
6+ import {
7+ useGeneticElements ,
8+ useSetActiveGeneId ,
9+ useSetActiveViewId ,
10+ useSpecies ,
11+ } from '@eplant/state'
12+ import { View } from '@eplant/View'
313
4- import { useConfig } from '@eplant/config' ;
5- import GeneticElement from '@eplant/GeneticElement' ;
6- import { useGeneticElements , useSetActiveGeneId , useSetActiveViewId , useSpecies } from '@eplant/state' ;
7- import { View } from '@eplant/View' ;
8-
9- /**
14+ /**
1015 * Interface defining the shape of the ViewSwitch context
1116 */
1217interface ViewSwitchContextType {
1318 /** Currently active view */
14- activeView : View | null ;
19+ activeView : View | null
1520 /** Currently active genetic element */
16- activeGene : GeneticElement | null ;
21+ activeGene : GeneticElement | null
1722 /** Function to switch view only */
18- switchViewOnly : ( viewId : string ) => Promise < void > ;
23+ switchViewOnly : ( viewId : string ) => Promise < void >
1924 /** Function to switch gene only */
20- switchGeneOnly : ( geneName : string ) => Promise < void > ;
25+ switchGeneOnly : ( geneName : string ) => Promise < void >
2126 /** Function to switch both view and gene */
22- switchViewAndGene : ( viewId : string , geneName : string , species ?: string ) => Promise < void > ;
27+ switchViewAndGene : (
28+ viewId : string ,
29+ geneName : string ,
30+ species ?: string
31+ ) => Promise < void >
2332}
2433
2534/** Default context value */
@@ -29,10 +38,10 @@ const ViewSwitchContext = createContext<ViewSwitchContextType>({
2938 switchViewOnly : async ( ) => { } ,
3039 switchGeneOnly : async ( ) => { } ,
3140 switchViewAndGene : async ( ) => { } ,
32- } ) ;
41+ } )
3342
3443/** Hook to access the ViewSwitch context */
35- export const useViewSwitch = ( ) => useContext ( ViewSwitchContext ) ;
44+ export const useViewSwitch = ( ) => useContext ( ViewSwitchContext )
3645
3746/**
3847 * Creates a ViewSwitch provider component
@@ -44,155 +53,164 @@ export const createViewSwitchProvider = () => {
4453 * @param props - Component props
4554 */
4655 const ViewSwitchProvider = ( { children } : { children : React . ReactNode } ) => {
47- const [ activeView , setActiveView ] = useState < View | null > ( null ) ;
48- const [ activeGene , setActiveGene ] = useState < GeneticElement | null > ( null ) ;
49- const [ geneticElements , setGeneticElements ] = useGeneticElements ( ) ;
50-
51- const { userViews } = useConfig ( ) ;
52- const setActiveViewId = useSetActiveViewId ( ) ;
53- const setActiveGeneId = useSetActiveGeneId ( ) ;
54- const [ speciesList ] = useSpecies ( ) ;
55- const species = speciesList . length ? speciesList [ 0 ] : undefined ;
56+ const [ activeView , setActiveView ] = useState < View | null > ( null )
57+ const [ activeGene , setActiveGene ] = useState < GeneticElement | null > ( null )
58+ const [ geneticElements , setGeneticElements ] = useGeneticElements ( )
59+
60+ const { userViews } = useConfig ( )
61+ const setActiveViewId = useSetActiveViewId ( )
62+ const setActiveGeneId = useSetActiveGeneId ( )
63+ const [ speciesList ] = useSpecies ( )
64+ const species = speciesList . length ? speciesList [ 0 ] : undefined
5665
5766 /**
5867 * Adds genetic elements to the state, ensuring uniqueness
5968 * @param genes - Array of genetic elements to add
6069 */
6170 const addGeneticElements = ( genes : GeneticElement [ ] ) => {
6271 setGeneticElements ( ( prev ) => {
63- const updatedGenes = _ . uniqBy ( [ ...prev , ...genes ] , ( gene ) => gene . id ) ;
64- return updatedGenes ;
65- } ) ;
72+ const updatedGenes = _ . uniqBy ( [ ...prev , ...genes ] , ( gene ) => gene . id )
73+ return updatedGenes
74+ } )
6675
6776 if ( genes . length > 0 ) {
68- setActiveGeneId ( genes [ 0 ] . id ) ;
77+ setActiveGeneId ( genes [ 0 ] . id )
6978 }
70- } ;
79+ }
7180
7281 // Ensure genetic elements remain unique
7382 useEffect ( ( ) => {
74- const uniqueGenes = _ . uniqBy ( geneticElements , ( gene ) => gene . id ) ;
83+ const uniqueGenes = _ . uniqBy ( geneticElements , ( gene ) => gene . id )
7584 if ( uniqueGenes . length !== geneticElements . length ) {
76- setGeneticElements ( uniqueGenes ) ;
85+ setGeneticElements ( uniqueGenes )
7786 }
78- } , [ geneticElements , setGeneticElements ] ) ;
87+ } , [ geneticElements , setGeneticElements ] )
7988
8089 /**
8190 * Validates and retrieves a view by ID
8291 * @param viewId - The ID of the view to validate
8392 * @returns The validated view or null
8493 */
8594 const validateView = ( viewId : string ) : View | null => {
86- const targetView = userViews . find ( ( view ) => view . id === viewId ) ;
95+ const targetView = userViews . find ( ( view ) => view . id === viewId )
8796 if ( ! targetView ) {
88- console . warn ( `View with ID ${ viewId } not found` ) ;
89- return null ;
97+ console . warn ( `View with ID ${ viewId } not found` )
98+ return null
9099 }
91- return targetView ;
92- } ;
100+ return targetView
101+ }
93102
94103 /**
95104 * Loads a gene by name
96105 * @param geneName - The name of the gene to load
97106 * @returns The loaded genetic element or null
98107 */
99- const loadGene = async ( geneName : string ) : Promise < GeneticElement | null > => {
108+ const loadGene = async (
109+ geneName : string
110+ ) : Promise < GeneticElement | null > => {
100111 if ( ! species ) {
101- console . error ( " Species configuration is missing." ) ;
102- return null ;
112+ console . error ( ' Species configuration is missing.' )
113+ return null
103114 }
104115
105116 try {
106- const loadedGene = await species . api . searchGene ( geneName ) ;
107- return loadedGene as GeneticElement ;
117+ const loadedGene = await species . api . searchGene ( geneName )
118+ return loadedGene as GeneticElement
108119 } catch ( error ) {
109- console . error ( `Error loading gene ${ geneName } :` , error ) ;
110- return null ;
120+ console . error ( `Error loading gene ${ geneName } :` , error )
121+ return null
111122 }
112- } ;
123+ }
113124
114125 /**
115126 * Handles navigation to external species links
116127 * @param speciesUrl - The URL to navigate to
117128 * @param geneName - The gene identifier
118129 */
119- const handleExternalSpecies = ( speciesUrl : string , geneName : string ) : void => {
120- /** const fullUrl = `${speciesUrl}${geneName}`; */
121- const fullUrl = `${ speciesUrl } ` ;
122- window . open ( fullUrl , '_blank' ) ;
123- } ;
130+ const handleExternalSpecies = (
131+ speciesUrl : string ,
132+ geneName : string
133+ ) : void => {
134+ /** const fullUrl = `${speciesUrl}${geneName}`; */
135+ const fullUrl = `${ speciesUrl } `
136+ window . open ( fullUrl , '_blank' )
137+ }
124138
125139 /**
126140 * Switches only the view, keeping the current gene
127141 * @param viewId - ID of the view to switch to
128142 */
129143 const switchViewOnly = async ( viewId : string ) : Promise < void > => {
130-
131- const targetView = validateView ( viewId ) ;
132-
144+ const targetView = validateView ( viewId )
145+
133146 if ( targetView ) {
134- setActiveViewId ( targetView . id ) ;
135- setActiveView ( targetView ) ;
147+ setActiveViewId ( targetView . id )
148+ setActiveView ( targetView )
136149 }
137- } ;
150+ }
138151
139152 /**
140153 * Switches only the gene, keeping the current view
141154 * @param geneName - Name of the gene to switch to
142155 */
143- const switchGeneOnly = async ( geneName : string , speciesUrl ?: string ) : Promise < void > => {
144-
156+ const switchGeneOnly = async (
157+ geneName : string ,
158+ speciesUrl ?: string
159+ ) : Promise < void > => {
145160 if ( speciesUrl ) {
146- handleExternalSpecies ( speciesUrl , geneName ) ;
161+ handleExternalSpecies ( speciesUrl , geneName )
147162 }
148-
149- const geneticElement = await loadGene ( geneName ) ;
150-
163+
164+ const geneticElement = await loadGene ( geneName )
165+
151166 if ( geneticElement ) {
152- addGeneticElements ( [ geneticElement ] ) ;
153- setActiveGeneId ( geneticElement . id ) ;
154- setActiveGene ( geneticElement ) ;
167+ addGeneticElements ( [ geneticElement ] )
168+ setActiveGeneId ( geneticElement . id )
169+ setActiveGene ( geneticElement )
155170 }
156- } ;
171+ }
157172
158173 /**
159174 * Switches both view and gene
160175 * @param viewId - ID of the view to switch to
161176 * @param geneName - Name of the gene to switch to
162177 */
163- const switchViewAndGene = async ( viewId : string , geneName : string , speciesUrl ?: string ) : Promise < void > => {
164-
178+ const switchViewAndGene = async (
179+ viewId : string ,
180+ geneName : string ,
181+ speciesUrl ?: string
182+ ) : Promise < void > => {
165183 if ( speciesUrl ) {
166- handleExternalSpecies ( speciesUrl , geneName ) ;
184+ handleExternalSpecies ( speciesUrl , geneName )
167185 }
168186
169- const targetView = validateView ( viewId ) ;
170- const geneticElement = await loadGene ( geneName ) ;
187+ const targetView = validateView ( viewId )
188+ const geneticElement = await loadGene ( geneName )
171189
172190 if ( targetView && geneticElement ) {
173- addGeneticElements ( [ geneticElement ] ) ;
174- setActiveViewId ( targetView . id ) ;
175- setActiveView ( targetView ) ;
176- setActiveGeneId ( geneticElement . id ) ;
177- setActiveGene ( geneticElement ) ;
191+ addGeneticElements ( [ geneticElement ] )
192+ setActiveViewId ( targetView . id )
193+ setActiveView ( targetView )
194+ setActiveGeneId ( geneticElement . id )
195+ setActiveGene ( geneticElement )
178196 }
179- } ;
197+ }
180198
181199 return (
182- < ViewSwitchContext . Provider
183- value = { {
184- activeView,
185- activeGene,
200+ < ViewSwitchContext . Provider
201+ value = { {
202+ activeView,
203+ activeGene,
186204 switchViewOnly,
187205 switchGeneOnly,
188- switchViewAndGene
206+ switchViewAndGene,
189207 } }
190208 >
191209 { children }
192210 </ ViewSwitchContext . Provider >
193- ) ;
194- } ;
211+ )
212+ }
195213
196- ViewSwitchProvider . displayName = 'ViewSwitchProvider' ;
197- return ViewSwitchProvider ;
198- } ;
214+ ViewSwitchProvider . displayName = 'ViewSwitchProvider'
215+ return ViewSwitchProvider
216+ }
0 commit comments