diff --git a/src/app/components/Navbar.tsx b/src/app/components/Navbar.tsx index e93f578..bf110b2 100644 --- a/src/app/components/Navbar.tsx +++ b/src/app/components/Navbar.tsx @@ -7,6 +7,8 @@ import { Button } from '@/components/ui/button'; import { useAuthStore } from '@/lib/stores/useAuthStore'; import { usePathname, useRouter, useSearchParams } from 'next/navigation'; import { useBlueprintFiltersStore } from '@/lib/stores/useBlueprintFiltersStore'; +import { useCreateBlueprintStore } from '@/app/create/[id]/store'; +import { set as setIdb } from 'idb-keyval'; const Navbar = () => { const token = useAuthStore((state) => state.token); @@ -40,7 +42,23 @@ const Navbar = () => { }; const handleCreateBlueprint = () => { - localStorage.removeItem('create-blueprint'); + // Clear persisted create-blueprint store in IndexedDB + try { + // Fire and forget – if this fails we still reset in-memory state below + setIdb('create-blueprint', null).catch((err) => { + console.error('Failed to clear persisted create-blueprint store', err); + }); + } catch (err) { + console.error('Error while scheduling create-blueprint store clear', err); + } + + // Reset in-memory create blueprint store so we always start from a clean slate + try { + useCreateBlueprintStore.getState().reset(); + } catch (err) { + console.error('Failed to reset create blueprint store', err); + } + router.push('/create'); }; diff --git a/src/app/create/[id]/store.ts b/src/app/create/[id]/store.ts index f474c35..f346250 100644 --- a/src/app/create/[id]/store.ts +++ b/src/app/create/[id]/store.ts @@ -183,7 +183,7 @@ export const useCreateBlueprintStore = create()( let emlStr = ''; // Always use savedEmls for email content // File objects cannot be properly serialized/deserialized from storage - const blueprintId = state.id ?? 'new'; + const blueprintId = state.blueprint?.props.id ?? state.id ?? 'new'; emlStr = savedEmls[blueprintId] || ''; // If we don't have the email content in savedEmls and have a valid File object, @@ -196,8 +196,8 @@ export const useCreateBlueprintStore = create()( } } console.log('got email content'); - // Create a new blueprint - if (!state.id || state.id === 'new') { + // If we don't have a blueprint instance yet, always create a new one + if (!state.blueprint) { console.log('creating a new blueprint'); const blueprint = sdk.createBlueprint(data); if (emlStr) { @@ -214,19 +214,28 @@ export const useCreateBlueprintStore = create()( return blueprint.props.id!; } - // Update an existing blueprint - if (state.blueprint && state.blueprint.canUpdate(data)) { - await state.blueprint.update(data); - return state.blueprint.props.id!; - } + // We have an existing blueprint instance at this point + const blueprint = state.blueprint; + const canUpdateFn = (blueprint as any)?.canUpdate; + + // Prefer SDK's canUpdate logic when available + if (typeof canUpdateFn === 'function') { + if (canUpdateFn.call(blueprint, data)) { + await blueprint.update(data); + return blueprint.props.id!; + } - // Create a new version of an blueprint - if (state.blueprint && !state.blueprint.canUpdate(data)) { + // Create a new version of the blueprint when it can't be updated in-place console.log('creating new version'); - await state.blueprint.submitNewVersionDraft(data); - return state.blueprint.props.id!; + await blueprint.submitNewVersionDraft(data); + return blueprint.props.id!; } + // Fallback: if canUpdate is not available (or not a function), always create a new version + console.warn('Blueprint.canUpdate is not available, submitting new version draft instead'); + await blueprint.submitNewVersionDraft(data); + return blueprint.props.id!; + throw new Error('Unknown error saving blueprint'); } catch (err) { console.error('Failed to submit blueprint: ', err);