You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

146 lines
4.9 KiB

<script lang="ts">
import Header from '../../lib/components/layout/Header.svelte';
import CreateEventForm from '../../lib/components/write/CreateEventForm.svelte';
import { nostrClient } from '../../lib/services/nostr/nostr-client.js';
import { sessionManager } from '../../lib/services/auth/session-manager.js';
import { onMount } from 'svelte';
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
const kindParam = $derived($page.url.searchParams.get('kind'));
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
// Explicitly preserve content (important for kind 0 which has stringified JSON)
initialEvent = {
id: '',
pubkey: '',
created_at: Math.floor(Date.now() / 1000),
kind: cloneData.kind !== undefined && cloneData.kind !== null ? cloneData.kind : 1,
content: cloneData.content !== undefined ? cloneData.content : '', // Preserve content even if empty string
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 (only if no event from sessionStorage)
$effect(() => {
if (kindParam && !initialEvent) {
const kind = parseInt(kindParam, 10);
if (!isNaN(kind)) {
initialEvent = {
id: '',
pubkey: '',
created_at: Math.floor(Date.now() / 1000),
kind: kind,
content: '',
tags: [],
sig: ''
};
}
}
});
// Subscribe to session changes to reactively update login status
let currentSession = $state(sessionManager.session.value);
const isLoggedIn = $derived(currentSession !== null);
// Subscribe to session changes
$effect(() => {
const unsubscribe = sessionManager.session.subscribe((session) => {
currentSession = session;
});
return unsubscribe;
});
onMount(async () => {
await nostrClient.initialize();
// Ensure session is restored (fallback in case layout restoration didn't complete)
if (!sessionManager.isLoggedIn()) {
try {
await sessionManager.restoreSession(); // Ignore return value - just try to restore
} catch (error) {
console.error('Failed to restore session in write page:', error);
}
}
});
</script>
<Header />
<main class="container mx-auto px-4 py-8">
<div class="write-page">
<h1 class="font-bold mb-6 text-fog-text dark:text-fog-dark-text font-mono" style="font-size: 1.5em;">
{isCloneMode ? '/Write: Edit or Clone an event' : '/Write'}
</h1>
{#if !isLoggedIn}
<div class="login-prompt">
<p class="text-fog-text dark:text-fog-dark-text mb-4">You must be logged in to write or edit events.</p>
<a href="/login" class="text-fog-accent dark:text-fog-dark-accent hover:underline">Login here</a>
</div>
{:else}
<div class="form-container">
<CreateEventForm
initialEvent={initialEvent}
/>
</div>
{/if}
</div>
</main>
<style>
.write-page {
max-width: var(--content-width);
margin: 0 auto;
padding: 0 1rem;
}
.form-container {
display: flex;
flex-direction: column;
gap: 1rem;
}
</style>