From 673d90c97206977d86b34038c0c2be1b05934331 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 21 Jan 2026 11:46:43 +0000 Subject: [PATCH] fix(post): preserve content when publishing fails to all relays MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Critical UX improvement: Only clear draft and show preview if at least one relay succeeded. **The Problem:** - When publishing failed to ALL relays, we still cleared the draft - User lost their content with no way to retry - Frustrating experience when all relays are down **The Solution:** - Track success/failure by returning result from each publish promise - Count successful publishes from Promise.allSettled results - Only clear draft and show preview if successCount > 0 - Keep editor visible with content if all relays fail **User Experience:** - All relays succeed → Clear draft, show preview, success toast - Some relays succeed → Clear draft, show preview, warning toast - All relays fail → Keep editor with content, error toast, can retry **Toast Messages:** - All succeeded: "Published to all X relays" - Partial success: "Published to X of Y relays" - Total failure: "Failed to publish to any relay. Please check..." --- src/components/PostViewer.tsx | 53 ++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/src/components/PostViewer.tsx b/src/components/PostViewer.tsx index 6605cfa..42e32da 100644 --- a/src/components/PostViewer.tsx +++ b/src/components/PostViewer.tsx @@ -460,6 +460,7 @@ export function PostViewer({ windowId }: PostViewerProps = {}) { : r, ), ); + return { success: true, relayUrl }; } catch (error) { console.error(`Failed to publish to ${relayUrl}:`, error); @@ -478,29 +479,49 @@ export function PostViewer({ windowId }: PostViewerProps = {}) { : r, ), ); + return { success: false, relayUrl }; } }); // Wait for all publishes to complete (settled = all finished, regardless of success/failure) - await Promise.allSettled(publishPromises); + const results = await Promise.allSettled(publishPromises); - // Add to event store for immediate local availability - eventStore.add(event); + // Check how many relays succeeded + const successCount = results.filter( + (r) => r.status === "fulfilled" && r.value.success, + ).length; - // Clear draft from localStorage - if (pubkey) { - const draftKey = windowId - ? `${DRAFT_STORAGE_KEY}-${pubkey}-${windowId}` - : `${DRAFT_STORAGE_KEY}-${pubkey}`; - localStorage.removeItem(draftKey); + if (successCount > 0) { + // At least one relay succeeded - add to event store + eventStore.add(event); + + // Clear draft from localStorage + if (pubkey) { + const draftKey = windowId + ? `${DRAFT_STORAGE_KEY}-${pubkey}-${windowId}` + : `${DRAFT_STORAGE_KEY}-${pubkey}`; + localStorage.removeItem(draftKey); + } + + // Show published preview + setShowPublishedPreview(true); + + // Show success toast + if (successCount === selected.length) { + toast.success( + `Published to all ${selected.length} relay${selected.length > 1 ? "s" : ""}`, + ); + } else { + toast.warning( + `Published to ${successCount} of ${selected.length} relays`, + ); + } + } else { + // All relays failed - keep the editor visible with content + toast.error( + "Failed to publish to any relay. Please check your relay connections and try again.", + ); } - - // Show published preview - setShowPublishedPreview(true); - - toast.success( - `Published to ${selected.length} relay${selected.length > 1 ? "s" : ""}`, - ); } catch (error) { console.error("Failed to publish:", error); toast.error(