Skip to content
Merged
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
101 changes: 101 additions & 0 deletions app/components/layout/load-boundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import * as React from "react";
import { Button } from "@/components/ui/button";

const reloadStorageKey = "hqbase:load-failure-reload";
const reloadWindowMs = 60_000;

export class LoadFailure extends Error {
constructor(cause: unknown) {
super("A part of HQBase could not load.", { cause });
this.name = "LoadFailure";
}
}

export const lazyComponent: typeof React.lazy = (load) =>
React.lazy(() =>
load().catch((error: unknown) => {
throw new LoadFailure(error);
})
);

type LoadBoundaryProps = {
children: React.ReactNode;
message: string;
resetKey?: string;
variant: "notice" | "page";
};

type LoadBoundaryState = {
failure: "reload" | "show" | null;
};

export class LoadBoundary extends React.Component<LoadBoundaryProps, LoadBoundaryState> {
override state: LoadBoundaryState = { failure: null };

static getDerivedStateFromError(error: unknown): LoadBoundaryState {
return { failure: error instanceof LoadFailure && mayReload(Date.now()) ? "reload" : "show" };
}

override componentDidCatch(): void {
if (this.state.failure !== "reload") return;
// A new release can remove the files of the running page. One reload gets the current files.
if (recordReload(Date.now())) window.location.reload();
else this.setState({ failure: "show" });
}

override componentDidUpdate(previous: LoadBoundaryProps): void {
if (this.state.failure !== null && previous.resetKey !== this.props.resetKey) {
this.setState({ failure: null });
}
}

override render(): React.ReactNode {
if (this.state.failure === null) return this.props.children;
if (this.state.failure === "reload") return null;
const reload = (
<Button
className="shrink-0 rounded-lg px-3.5"
onClick={() => window.location.reload()}
size="sm"
type="button"
>
Reload
</Button>
);
return this.props.variant === "page" ? (
<div
className="flex h-full flex-col items-center justify-center gap-3 p-6 text-center text-sm text-muted-foreground"
role="alert"
>
<p>{this.props.message}</p>
{reload}
</div>
) : (
<aside
className="fixed right-4 bottom-4 left-4 z-[100] mx-auto flex max-w-lg items-center justify-between gap-3 rounded-xl border border-border bg-popover px-3.5 py-2.5 text-sm text-popover-foreground shadow-[0_14px_38px_rgb(0_0_0/0.32)]"
role="alert"
>
<span className="text-xs font-semibold">{this.props.message}</span>
{reload}
</aside>
);
}
}

function mayReload(now: number): boolean {
try {
const previous = Number(window.sessionStorage.getItem(reloadStorageKey));
return !(previous > 0 && now - previous < reloadWindowMs);
} catch {
return false;
}
}

function recordReload(now: number): boolean {
try {
window.sessionStorage.setItem(reloadStorageKey, String(now));
return true;
} catch {
return false;
}
}
187 changes: 97 additions & 90 deletions app/features/compose/composer-host.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from "react";
import { LoadBoundary, lazyComponent } from "@/components/layout/load-boundary";

import type { Mailbox } from "@/features/mailboxes/types";
import { ConversationMessages } from "@/features/messages/conversation-messages";
Expand All @@ -22,7 +23,7 @@ export type {
} from "./composer-context";
export { ComposerInlineTarget, useComposer } from "./composer-context";

const ComposeDialog = React.lazy(() =>
const ComposeDialog = lazyComponent(() =>
import("./compose-dialog").then((module) => ({ default: module.ComposeDialog }))
);

Expand Down Expand Up @@ -272,95 +273,101 @@ export function ComposerHost({
const inlineTarget = session.detached ? null : (inlineTargets[session.id] ?? parkingTarget);
const presentation = session.detached ? "window" : "thread";
return (
<React.Suspense fallback={null} key={session.id}>
<ComposeDialog
defaultFromMailboxId={defaultFromMailboxId}
dockIndex={Math.max(
0,
minimizedSessions.findIndex((item) => item.id === session.id)
)}
dockTarget={dockTarget}
draftId={session.initialDraftId}
initialTo={session.initialTo}
inlineTarget={inlineTarget}
mailboxes={mailboxes}
message={session.message}
minimized={session.minimized}
mode={session.mode}
open
presentation={presentation}
threadContext={
session.messages.length > 0 ? (
<ConversationMessages compact messages={session.messages} />
) : undefined
}
threadMessages={session.messages}
windowSlot={Math.max(
0,
windowSessions.length -
1 -
windowSessions.findIndex((item) => item.id === session.id)
)}
onDetach={
presentation === "thread"
? () => {
updateSessions((current) =>
current.map((candidate) =>
candidate.id === session.id
? { ...candidate, detached: true, minimized: false }
: candidate
)
);
}
: undefined
}
onDraftReady={(draftId) => {
updateSessions((current) =>
current.map((candidate) =>
candidate.id === session.id ? { ...candidate, draftId } : candidate
)
);
}}
onDraftsChange={() => onDraftsChangeRef.current()}
onManageSignatures={() => onManageSignaturesRef.current()}
onMinimizedChange={(minimized) => {
updateSessions((current) =>
current.map((candidate) =>
candidate.id === session.id ? { ...candidate, minimized } : candidate
)
);
}}
onOpenChange={(open) => {
if (!open) closeSession(session.id);
}}
onReturnToThread={
origin
? () => {
updateSessions((current) =>
current.map((candidate) =>
candidate.id === session.id
? {
...candidate,
detached: false,
minimized: false
}
: candidate
)
);
navigateRef.current({
kind: "mail",
folder: origin.folder,
messageId: origin.messageId
});
}
: undefined
}
onSent={() => {
onSentRef.current();
session.onSent?.();
}}
/>
</React.Suspense>
<LoadBoundary
key={session.id}
message="HQBase could not open the message editor."
variant="notice"
>
<React.Suspense fallback={null}>
<ComposeDialog
defaultFromMailboxId={defaultFromMailboxId}
dockIndex={Math.max(
0,
minimizedSessions.findIndex((item) => item.id === session.id)
)}
dockTarget={dockTarget}
draftId={session.initialDraftId}
initialTo={session.initialTo}
inlineTarget={inlineTarget}
mailboxes={mailboxes}
message={session.message}
minimized={session.minimized}
mode={session.mode}
open
presentation={presentation}
threadContext={
session.messages.length > 0 ? (
<ConversationMessages compact messages={session.messages} />
) : undefined
}
threadMessages={session.messages}
windowSlot={Math.max(
0,
windowSessions.length -
1 -
windowSessions.findIndex((item) => item.id === session.id)
)}
onDetach={
presentation === "thread"
? () => {
updateSessions((current) =>
current.map((candidate) =>
candidate.id === session.id
? { ...candidate, detached: true, minimized: false }
: candidate
)
);
}
: undefined
}
onDraftReady={(draftId) => {
updateSessions((current) =>
current.map((candidate) =>
candidate.id === session.id ? { ...candidate, draftId } : candidate
)
);
}}
onDraftsChange={() => onDraftsChangeRef.current()}
onManageSignatures={() => onManageSignaturesRef.current()}
onMinimizedChange={(minimized) => {
updateSessions((current) =>
current.map((candidate) =>
candidate.id === session.id ? { ...candidate, minimized } : candidate
)
);
}}
onOpenChange={(open) => {
if (!open) closeSession(session.id);
}}
onReturnToThread={
origin
? () => {
updateSessions((current) =>
current.map((candidate) =>
candidate.id === session.id
? {
...candidate,
detached: false,
minimized: false
}
: candidate
)
);
navigateRef.current({
kind: "mail",
folder: origin.folder,
messageId: origin.messageId
});
}
: undefined
}
onSent={() => {
onSentRef.current();
session.onSent?.();
}}
/>
</React.Suspense>
</LoadBoundary>
);
})}
</ComposerContext.Provider>
Expand Down
Loading
Loading