mirror of
https://github.com/purrgrammer/grimoire.git
synced 2026-04-11 16:07:15 +02:00
fix(post): preserve content when publishing fails to all relays
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..."
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user