@@ -2,7 +2,6 @@ import { useEffect, useMemo } from 'react'
22import { useLocation } from 'react-router-dom'
33import { TrendingUp , Sun , Calculator , Download , Lock , LayoutDashboard , Calendar , Zap , Users , AlertCircle , BookOpen , Settings as SettingsIcon , Key , Shield , FileText , Activity , Euro , Scale , UserCheck } from 'lucide-react'
44import { useQuery } from '@tanstack/react-query'
5- import toast from 'react-hot-toast'
65import { pdlApi } from '@/api/pdl'
76import { adminApi } from '@/api/admin'
87import { usePdlStore } from '@/stores/pdlStore'
@@ -73,7 +72,7 @@ export default function PageHeader() {
7372 } , [ isAdmin , impersonation , clearImpersonation ] )
7473
7574 // Récupérer la liste des PDLs de l'utilisateur
76- const { data : pdlsResponse } = useQuery ( {
75+ const { data : pdlsResponse , isLoading : isPdlsLoading } = useQuery ( {
7776 queryKey : [ 'pdls' ] ,
7877 queryFn : async ( ) => {
7978 const response = await pdlApi . list ( )
@@ -85,7 +84,7 @@ export default function PageHeader() {
8584 } )
8685
8786 // Récupérer les PDL partagés (admin only)
88- const { data : sharedPdlsResponse } = useQuery ( {
87+ const { data : sharedPdlsResponse , isLoading : isSharedPdlsLoading } = useQuery ( {
8988 queryKey : [ 'admin-shared-pdls' ] ,
9089 queryFn : async ( ) => {
9190 const response = await adminApi . getAllSharedPdls ( )
@@ -98,6 +97,9 @@ export default function PageHeader() {
9897 staleTime : 60000 , // Cache for 1 minute
9998 } )
10099
100+ // Determine if initial data is still loading
101+ const isInitialDataLoading = isPdlsLoading || ( isAdmin ( ) && isSharedPdlsLoading )
102+
101103 const userPdls : PDL [ ] = Array . isArray ( pdlsResponse ) ? pdlsResponse : [ ]
102104 const sharedPdls : SharedPDL [ ] = Array . isArray ( sharedPdlsResponse ) ? sharedPdlsResponse : [ ]
103105
@@ -156,10 +158,6 @@ export default function PageHeader() {
156158 if ( ! config ) return null
157159
158160 const Icon = config . icon
159- const activePdls = pdls . filter ( ( p : PDL ) => p . is_active )
160-
161- // Check if on a consumption page
162- const isConsumptionPage = location . pathname . startsWith ( '/consumption' )
163161
164162 // Get the set of production PDL IDs that are linked to consumption PDLs
165163 // These should be hidden from the selector (the consumption PDL will show instead)
@@ -169,47 +167,21 @@ export default function PageHeader() {
169167 . map ( ( pdl : PDL ) => pdl . linked_production_pdl_id )
170168 )
171169
172- // Filter PDLs based on page
173- const displayedPdls = location . pathname === '/production'
174- ? ( ( ) => {
175- // Show: consumption PDLs with linked production + standalone production PDLs
176- const consumptionWithProduction = pdls . filter ( ( pdl : PDL ) =>
177- pdl . has_consumption &&
178- pdl . is_active &&
179- pdl . linked_production_pdl_id
180- )
181-
182- const standaloneProduction = activePdls . filter ( ( pdl : PDL ) =>
183- pdl . has_production &&
184- ! linkedProductionIds . has ( pdl . id ) // Use pdl.id (UUID) not usage_point_id
185- )
170+ // Show all active PDLs, hiding only linked production PDLs (they appear via their consumption PDL)
171+ const displayedPdls = pdls . filter ( ( p : PDL ) => p . is_active && ! linkedProductionIds . has ( p . id ) )
186172
187- return [ ...consumptionWithProduction , ...standaloneProduction ]
188- } ) ( )
189- : isConsumptionPage
190- ? activePdls . filter ( ( pdl : PDL ) => pdl . has_consumption )
191- : activePdls . filter ( ( pdl : PDL ) => ! linkedProductionIds . has ( pdl . id ) ) // Hide linked production PDLs globally
192-
193- // Auto-select first PDL if none selected OR if current PDL is not in the displayed list
173+ // Auto-select first PDL only if none is selected (first load)
174+ // IMPORTANT: Wait for initial data to be loaded before auto-selecting to avoid
175+ // overriding the persisted selectedPdl from localStorage during hydration
194176 useEffect ( ( ) => {
195- if ( displayedPdls . length > 0 ) {
196- // Check if current PDL is in the displayed list for this page
197- const currentPdlInList = displayedPdls . some ( p => p . usage_point_id === selectedPdl )
177+ // Don't auto-select while data is still loading
178+ if ( isInitialDataLoading ) return
198179
199- if ( ! selectedPdl || ! currentPdlInList ) {
200- const newPdl = displayedPdls [ 0 ]
201- // Show toast only if we're switching from an incompatible PDL (not on first load)
202- if ( selectedPdl && ! currentPdlInList ) {
203- toast ( `PDL changé automatiquement vers "${ newPdl . name || newPdl . usage_point_id } "` , {
204- icon : '🔄' ,
205- duration : 4000 ,
206- } )
207- }
208- // Select the first available PDL for this page
209- setSelectedPdl ( newPdl . usage_point_id )
210- }
180+ // Only auto-select if no PDL is currently selected
181+ if ( ! selectedPdl && displayedPdls . length > 0 ) {
182+ setSelectedPdl ( displayedPdls [ 0 ] . usage_point_id )
211183 }
212- } , [ displayedPdls , selectedPdl , setSelectedPdl ] )
184+ } , [ displayedPdls , selectedPdl , setSelectedPdl , isInitialDataLoading ] )
213185
214186 return (
215187 < div className = "bg-white dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700" >
@@ -234,11 +206,7 @@ export default function PageHeader() {
234206 { showPdlSelector && (
235207 displayedPdls . length === 0 ? (
236208 < div className = "text-sm text-gray-600 dark:text-gray-400 italic" >
237- { location . pathname === '/production'
238- ? 'Aucun PDL de production non lié trouvé'
239- : isConsumptionPage
240- ? 'Aucun PDL avec l\'option consommation activée'
241- : 'Aucun point de livraison actif trouvé' }
209+ Aucun point de livraison actif trouvé
242210 </ div >
243211 ) : (
244212 < div className = "flex flex-col sm:flex-row items-stretch sm:items-center gap-2 sm:gap-3 w-full lg:w-auto" >
0 commit comments