@@ -29,8 +29,8 @@ export function BrandKitWizard({ onFinish, onCancel }: BrandKitWizardProps) {
2929 setSelectedVibes,
3030 setSelectedFonts,
3131 setSelectedPalette,
32- brandKitCurrentStep,
3332 saveBrandKitStep,
33+ loadBrandKitStep,
3434 } = useWorkspaceMedia ( )
3535 const { activeOrganization, initialState } = useWorkspace ( )
3636
@@ -41,15 +41,45 @@ export function BrandKitWizard({ onFinish, onCancel }: BrandKitWizardProps) {
4141
4242 const [ isInitialized , setIsInitialized ] = useState ( false )
4343 const previousBrandKitRef = useRef < string > ( '' )
44+ const previousOrgRef = useRef < string | null > ( null )
4445
4546 const { currentStep, Next, Prev, close, goTo, lastStep } = useWizard (
4647 steps ,
4748 true ,
4849 )
4950
51+ // Detect organization change and reload step + brand kit
52+ useEffect ( ( ) => {
53+ if ( ! currentOrg ?. id ) return
54+
55+ // If organization changed, reload everything for that org
56+ if ( previousOrgRef . current !== currentOrg . id ) {
57+ console . log ( `🔄 Organization changed to: ${ currentOrg . name } ` )
58+
59+ // Load step from localStorage for this specific org
60+ const stepNumber = loadBrandKitStep ( currentOrg . id )
61+ goTo ( stepNumber )
62+ console . log ( `✅ Loaded step ${ stepNumber } for org ${ currentOrg . name } ` )
63+
64+ previousOrgRef . current = currentOrg . id
65+ }
66+
67+ // Always mark as initialized after org is set
68+ if ( ! isInitialized ) {
69+ setIsInitialized ( true )
70+ }
71+ } , [ currentOrg ?. id , currentOrg ?. name , goTo , loadBrandKitStep , isInitialized ] )
72+
5073 // Restore Brand Kit data when it changes
5174 useEffect ( ( ) => {
52- if ( ! brandKit ) return
75+ if ( ! brandKit ) {
76+ // Clear selections if no brand kit
77+ setSelectedVibes ( [ ] )
78+ setSelectedFonts ( [ ] )
79+ setSelectedPalette ( null )
80+ previousBrandKitRef . current = ''
81+ return
82+ }
5383
5484 // Prevent unnecessary updates by comparing serialized brandKit
5585 const brandKitStr = JSON . stringify ( brandKit )
@@ -60,6 +90,8 @@ export function BrandKitWizard({ onFinish, onCancel }: BrandKitWizardProps) {
6090 // Update vibes
6191 if ( brandKit . vibes ?. length > 0 ) {
6292 setSelectedVibes ( brandKit . vibes )
93+ } else {
94+ setSelectedVibes ( [ ] )
6395 }
6496
6597 // Update fonts
@@ -72,32 +104,21 @@ export function BrandKitWizard({ onFinish, onCancel }: BrandKitWizardProps) {
72104 fonts . push ( { family : brandKit . fonts . secondary , role : 'secondary' } )
73105 }
74106 setSelectedFonts ( fonts )
107+ } else {
108+ setSelectedFonts ( [ ] )
75109 }
76110
77111 // Update palette
78112 if ( brandKit . palette ) {
79113 setSelectedPalette ( brandKit . palette )
114+ } else {
115+ setSelectedPalette ( null )
80116 }
81117
82118 previousBrandKitRef . current = brandKitStr
83119 console . log ( '✅ Brand Kit selections updated' )
84120 } , [ brandKit , setSelectedVibes , setSelectedFonts , setSelectedPalette ] )
85121
86- // Restore step from LocalStorage/State ONCE on mount
87- useEffect ( ( ) => {
88- if ( isInitialized ) return
89-
90- // Restore saved step if available
91- if ( brandKitCurrentStep && brandKitCurrentStep > 0 ) {
92- goTo ( brandKitCurrentStep )
93- console . log (
94- `✅ Restored to step ${ brandKitCurrentStep } from LocalStorage` ,
95- )
96- }
97-
98- setIsInitialized ( true )
99- } , [ brandKitCurrentStep , goTo , isInitialized ] )
100-
101122 // Save step to LocalStorage on every change
102123 useEffect ( ( ) => {
103124 if ( ! isInitialized || ! currentOrg ?. id ) return
0 commit comments