Skip to content

Commit d98b66d

Browse files
committed
fix(media-tab): restore wizard step when changing organizations
1 parent e6ce54b commit d98b66d

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

apps/pro-web/components/routes/workspace/media-tab/ui/brand-kit/wizard/brand-kit-wizard.tsx

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

apps/pro-web/lib/hooks/use-workspace-media.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type {
1212
import type { BrandKitData, ReferenceImagePayload } from '@/types/media.types'
1313
import type { OrganizationData } from '@/types/thread.types'
1414
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
15-
import { useLocalStorage } from 'mb-lib'
1615
import { useSession } from 'next-auth/react'
1716
import {
1817
type ReactNode,
@@ -111,6 +110,7 @@ interface MediaWorkspaceActions {
111110
organizationId: string,
112111
) => Promise<unknown>
113112
saveBrandKitStep: (step: number, organizationId: string) => void
113+
loadBrandKitStep: (organizationId: string) => number
114114
clearBrandKitStep: (organizationId: string) => void
115115
}
116116

@@ -267,7 +267,6 @@ export function WorkspaceMediaProvider({ children }: { children: ReactNode }) {
267267

268268
// Extract aspect ratio and name
269269
const aspectRatio: AspectRatio = size.ratio
270-
const targetFormat = size.name
271270

272271
// Prompt for resizing/reframing
273272
const prompt = `
@@ -407,6 +406,14 @@ export function WorkspaceMediaProvider({ children }: { children: ReactNode }) {
407406
[],
408407
)
409408

409+
const loadBrandKitStep = useCallback((organizationId: string) => {
410+
const key = `brandkit_step_${organizationId}`
411+
const savedStep = localStorage.getItem(key)
412+
const step = savedStep ? Number.parseInt(savedStep, 10) : 0
413+
setBrandKitCurrentStep(step)
414+
return step
415+
}, [])
416+
410417
const clearBrandKitStep = useCallback((organizationId: string) => {
411418
setBrandKitCurrentStep(0)
412419
const key = `brandkit_step_${organizationId}`
@@ -472,6 +479,7 @@ export function WorkspaceMediaProvider({ children }: { children: ReactNode }) {
472479
setSelectedFonts,
473480
setSelectedPalette,
474481
saveBrandKit,
482+
loadBrandKitStep,
475483
saveBrandKitStep,
476484
clearBrandKitStep,
477485
}}

0 commit comments

Comments
 (0)