@ -6,22 +6,80 @@
import { onMount } from 'svelte';
import { onMount } from 'svelte';
import { page } from '$app/stores';
import { page } from '$app/stores';
import type { NostrEvent } from '../../lib/types/nostr.js';
// Read kind from URL synchronously so it's available on first render
// Read kind from URL synchronously so it's available on first render
const kindParam = $derived($page.url.searchParams.get('kind'));
const kindParam = $derived($page.url.searchParams.get('kind'));
let initialKind = $state< number | null > (null);
let initialEvent = $state< NostrEvent | null > (null);
let isCloneMode = $state(false);
// Read from sessionStorage synchronously (runs immediately, not in onMount)
// This ensures initialEvent is set before CreateEventForm renders
if (typeof window !== 'undefined') {
// Check for clone/edit event data in sessionStorage (takes priority)
const cloneDataStr = sessionStorage.getItem('aitherboard_cloneEvent');
if (cloneDataStr) {
try {
const cloneData = JSON.parse(cloneDataStr);
// Construct event from clone data
// Use nullish coalescing to properly handle kind 0
initialEvent = {
id: '',
pubkey: '',
created_at: Math.floor(Date.now() / 1000),
kind: cloneData.kind !== undefined & & cloneData.kind !== null ? cloneData.kind : 1,
content: cloneData.content || '',
tags: cloneData.tags || [],
sig: ''
};
isCloneMode = cloneData.isClone === true;
// Clear sessionStorage after reading
sessionStorage.removeItem('aitherboard_cloneEvent');
} catch (error) {
console.error('Error parsing clone event data:', error);
}
} else {
// Check for highlight data in sessionStorage (fallback)
const highlightDataStr = sessionStorage.getItem('aitherboard_highlightData');
if (highlightDataStr) {
try {
const highlightData = JSON.parse(highlightDataStr);
// Construct event from highlight data (default to kind 1)
initialEvent = {
id: '',
pubkey: '',
created_at: Math.floor(Date.now() / 1000),
kind: 1,
content: highlightData.content || '',
tags: highlightData.tags || [],
sig: ''
};
// Clear sessionStorage after reading
sessionStorage.removeItem('aitherboard_highlightData');
} catch (error) {
console.error('Error parsing highlight data:', error);
}
}
}
}
// Set initial kind from URL if available
// Set initial kind from URL if available (only if no event from sessionStorage)
$effect(() => {
$effect(() => {
if (kindParam) {
if (kindParam && !initialEvent ) {
const kind = parseInt(kindParam, 10);
const kind = parseInt(kindParam, 10);
if (!isNaN(kind)) {
if (!isNaN(kind)) {
initialKind = kind;
initialEvent = {
id: '',
pubkey: '',
created_at: Math.floor(Date.now() / 1000),
kind: kind,
content: '',
tags: [],
sig: ''
};
}
}
}
}
});
});
let initialContent = $state< string | null > (null);
let initialTags = $state< string [ ] [ ] | null > (null);
let isCloneMode = $state(false);
// Subscribe to session changes to reactively update login status
// Subscribe to session changes to reactively update login status
let currentSession = $state(sessionManager.session.value);
let currentSession = $state(sessionManager.session.value);
@ -46,36 +104,6 @@
console.error('Failed to restore session in write page:', error);
console.error('Failed to restore session in write page:', error);
}
}
}
}
// Check for clone/edit event data in sessionStorage (takes priority)
const cloneDataStr = sessionStorage.getItem('aitherboard_cloneEvent');
if (cloneDataStr) {
try {
const cloneData = JSON.parse(cloneDataStr);
initialKind = cloneData.kind || null;
initialContent = cloneData.content || null;
initialTags = cloneData.tags || null;
isCloneMode = cloneData.isClone === true;
// Clear sessionStorage after reading
sessionStorage.removeItem('aitherboard_cloneEvent');
} catch (error) {
console.error('Error parsing clone event data:', error);
}
} else {
// Check for highlight data in sessionStorage (fallback)
const highlightDataStr = sessionStorage.getItem('aitherboard_highlightData');
if (highlightDataStr) {
try {
const highlightData = JSON.parse(highlightDataStr);
initialContent = highlightData.content || null;
initialTags = highlightData.tags || null;
// Clear sessionStorage after reading
sessionStorage.removeItem('aitherboard_highlightData');
} catch (error) {
console.error('Error parsing highlight data:', error);
}
}
}
});
});
< / script >
< / script >
@ -95,9 +123,7 @@
{ : else }
{ : else }
< div class = "form-container" >
< div class = "form-container" >
< CreateEventForm
< CreateEventForm
initialKind={ initialKind }
initialEvent={ initialEvent }
initialContent={ initialContent }
initialTags={ initialTags }
/>
/>
< / div >
< / div >
{ /if }
{ /if }