Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/app/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
};

Expand Down
33 changes: 21 additions & 12 deletions src/app/create/[id]/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export const useCreateBlueprintStore = create<CreateBlueprintState>()(
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,
Expand All @@ -196,8 +196,8 @@ export const useCreateBlueprintStore = create<CreateBlueprintState>()(
}
}
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) {
Expand All @@ -214,19 +214,28 @@ export const useCreateBlueprintStore = create<CreateBlueprintState>()(
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);
Expand Down