Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

refactor(sharing): streamline share dialogs and create links on open

tianma-if80c83023cf39
diff --git a/apps/api/src/share-routes.test.mjs b/apps/api/src/share-routes.test.mjs
index 764e56cd..14f6da9c 100644
--- a/apps/api/src/share-routes.test.mjs
+++ b/apps/api/src/share-routes.test.mjs
@@ -559,3 +559,29 @@ describe("memo share password recovery", () => {
sqlite.close();
});
});
+
+describe("one-step memo sharing", () => {
+ test("concurrent creation and retries reuse one link; reopening preserves its password", async () => {
+ const { sqlite, environment } = createDatabaseEnvironment();
+ const app = createShareApp(environment);
+ const path = "/api/v1/memos/memo_private/share";
+ const responses = await Promise.all([
+ app.request(path, { method: "POST" }, environment),
+ app.request(path, { method: "POST" }, environment),
+ ]);
+ const shares = await Promise.all(responses.map(async (response) => (await response.json()).share));
+ expect(shares[0].token).toBe(shares[1].token);
+ expect(sqlite.query("SELECT COUNT(*) AS count FROM memo_shares WHERE memo_id = 'memo_private'").get().count).toBe(1);
+ const protectedShare = (await (await app.request(path, {
+ method: "PATCH", headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ passwordProtected: true }),
+ }, environment)).json()).share;
+ const reopened = (await (await app.request(path, { method: "POST" }, environment)).json()).share;
+ expect(reopened.token).toBe(protectedShare.token);
+ expect(reopened.password).toBe(protectedShare.password);
+ expect(reopened.passwordProtected).toBe(true);
+ await app.request(path, { method: "DELETE" }, environment);
+ expect((await (await app.request(path, {}, environment)).json()).share).toBeNull();
+ sqlite.close();
+ });
+});
diff --git a/apps/web/src/components/dialogs/ShareMemoDialog.tsx b/apps/web/src/components/dialogs/ShareMemoDialog.tsx
index c3f5db21..d0c8f2df 100644
--- a/apps/web/src/components/dialogs/ShareMemoDialog.tsx
+++ b/apps/web/src/components/dialogs/ShareMemoDialog.tsx
@@ -1,12 +1,13 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
-import { Check, Copy, ExternalLink, Link2, LoaderCircle, Share2, Trash2 } from "lucide-react";
+import { Check, Copy, ExternalLink, LoaderCircle, Share2, Trash2 } from "lucide-react";
import { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { Link, useLocation } from "react-router";
import { Button } from "@/components/ui/button";
-import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog";
+import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { SharePasswordControls } from "./SharePasswordControls";
+import { SHARE_DIALOG_WIDTH_CLASS_NAME } from "./share-dialog-layout";
import { api, getConfiguredDesktopApiBaseUrl } from "@/lib/api";
import { copyTextToClipboard } from "@/lib/clipboard";
@@ -32,6 +33,7 @@ export const ShareMemoDialog = ({
const [copyState, setCopyState] = useState<"idle" | "copied" | "error">("idle");
const [copyTarget, setCopyTarget] = useState<"link" | "password" | "both">("link");
const copyResetTimerRef = useRef<number | null>(null);
+ const autoCreateAttemptRef = useRef<string | null>(null);
const queryKey = memoShareQueryKey(memoId);
const shareQuery = useQuery({
queryKey,
@@ -60,6 +62,7 @@ export const ShareMemoDialog = ({
onSuccess: () => {
queryClient.setQueryData(queryKey, { share: null });
void queryClient.invalidateQueries({ queryKey: ["memo-shares"] });
+ onOpenChange(false);
},
});
useEffect(() => {
@@ -68,7 +71,7 @@ export const ShareMemoDialog = ({
revokeMutation.reset();
setCopyState("idle");
setCopyTarget("link");
- }, [memoId]);
+ }, [memoId, open]);
useEffect(() => () => {
if (copyResetTimerRef.current !== null) window.clearTimeout(copyResetTimerRef.current);
}, []);
@@ -80,6 +83,20 @@ export const ShareMemoDialog = ({
const isWorking = shareQuery.isLoading || createMutation.isPending || passwordMutation.isPending || revokeMutation.isPending;
const error = shareQuery.error || createMutation.error || passwordMutation.error || revokeMutation.error;
+ useEffect(() => {
+ if (!open) {
+ autoCreateAttemptRef.current = null;
+ return;
+ }
+ // Wait for a successful read: a failed lookup must not enable public access.
+ // Attempt once per opening, including under StrictMode and after refetches.
+ if (shareQuery.isSuccess && !shareQuery.isFetching && !share &&
+ !revokeMutation.isSuccess && autoCreateAttemptRef.current !== memoId) {
+ autoCreateAttemptRef.current = memoId;
+ createMutation.mutate();
+ }
+ }, [open, memoId, shareQuery.isSuccess, shareQuery.isFetching, share, revokeMutation.isSuccess, createMutation.mutate]);
+
const markCopied = (target: "link" | "password" | "both", copied: boolean) => {
setCopyTarget(target);
setCopyState(copied ? "copied" : "error");
@@ -103,20 +120,17 @@ export const ShareMemoDialog = ({
return (
<Dialog open={open} onOpenChange={onOpenChange}>
- <DialogContent className="w-[calc(100%-2rem)] max-w-[640px] space-y-4">
+ <DialogContent aria-describedby={undefined} className={`${SHARE_DIALOG_WIDTH_CLASS_NAME} space-y-4`}>
<DialogHeader className="space-y-1.5 text-left">
<DialogTitle className="flex items-center gap-2 text-base font-semibold">
<Share2 className="h-5 w-5 text-slate-700" />
{t("sharing.title")}
</DialogTitle>
- <DialogDescription className="text-xs leading-5 text-slate-500">
- {t(share?.passwordProtected ? "sharing.descriptionProtected" : "sharing.description")}
- </DialogDescription>
</DialogHeader>
- {shareQuery.isLoading ? (
+ {shareQuery.isLoading || (!share && !error) ? (
<div className="flex min-h-20 items-center justify-center text-slate-500" role="status">
- <LoaderCircle className="h-5 w-5 animate-spin" />
+ <LoaderCircle className="h-5 w-5 animate-spin" aria-label={t("sharing.publicLoading")} />
</div>
) : share ? (
<div className="space-y-3.5">
@@ -170,13 +184,18 @@ export const ShareMemoDialog = ({
</div>
</div>
) : (
- <div className="space-y-3">
- <p className="text-xs leading-6 text-slate-600">{t("sharing.inactiveHint")}</p>
- <Button className="w-full" variant="solid" disabled={isWorking} onClick={() => createMutation.mutate()}>
- {createMutation.isPending ? <LoaderCircle className="h-4 w-4 animate-spin" /> : <Link2 className="h-4 w-4" />}
- {t("sharing.create")}
- </Button>
- </div>
+ <Button
+ className="w-full"
+ variant="solid"
+ disabled={isWorking}
+ onClick={() => {
+ if (shareQuery.isError) void shareQuery.refetch();
+ else createMutation.mutate();
+ }}
+ >
+ {createMutation.isPending ? <LoaderCircle className="h-4 w-4 animate-spin" /> : null}
+ {t("sharing.retry")}
+ </Button>
)}
{error ? <p className="text-xs leading-5 text-rose-600" role="alert">{t("sharing.error")}</p> : null}
diff --git a/apps/web/src/components/dialogs/ShareNotebookDialog.tsx b/apps/web/src/components/dialogs/ShareNotebookDialog.tsx
index 5daffee0..6386d967 100644
--- a/apps/web/src/components/dialogs/ShareNotebookDialog.tsx
+++ b/apps/web/src/components/dialogs/ShareNotebookDialog.tsx
@@ -12,6 +12,7 @@ import {
DialogTitle,
} from "@/components/ui/dialog";
import { SharePasswordControls } from "./SharePasswordControls";
+import { SHARE_DIALOG_WIDTH_CLASS_NAME } from "./share-dialog-layout";
import { Input } from "@/components/ui/input";
import { api, getConfiguredDesktopApiBaseUrl } from "@/lib/api";
import { copyTextToClipboard } from "@/lib/clipboard";
@@ -117,7 +118,7 @@ export const ShareNotebookDialog = ({
return (
<Dialog open={open} onOpenChange={onOpenChange}>
- <DialogContent className="w-[calc(100%-2rem)] max-w-[640px] space-y-4">
+ <DialogContent className={`${SHARE_DIALOG_WIDTH_CLASS_NAME} space-y-4`}>
<DialogHeader className="space-y-1.5 text-left">
<DialogTitle className="flex items-center gap-2 text-base font-semibold">
<Share2 className="h-5 w-5 text-slate-700" />
diff --git a/apps/web/src/components/dialogs/SharePasswordControls.tsx b/apps/web/src/components/dialogs/SharePasswordControls.tsx
index 448c412f..2684742a 100644
--- a/apps/web/src/components/dialogs/SharePasswordControls.tsx
+++ b/apps/web/src/components/dialogs/SharePasswordControls.tsx
@@ -14,7 +14,6 @@ export const SharePasswordControls = ({
shareUrl,
onChange,
hideCopyBoth = false,
- scopeHint,
}: {
passwordProtected: boolean;
password: string;
@@ -22,7 +21,6 @@ export const SharePasswordControls = ({
shareUrl: string;
onChange: (enabled: boolean) => void;
hideCopyBoth?: boolean;
- scopeHint?: string;
}) => {
const { t } = useTranslation();
const [copyState, setCopyState] = useState<"idle" | "copied" | "error">("idle");
@@ -92,9 +90,11 @@ export const SharePasswordControls = ({
<TooltipContent>{t("sharing.regeneratePassword")}</TooltipContent>
</Tooltip>
</div>
- <p className="text-xs leading-5 text-slate-500">
- {password ? (scopeHint || t("sharing.notebookPasswordScopeHint")) : t("sharing.passwordUnavailableHint")}
- </p>
+ {!password ? (
+ <p className="text-xs leading-5 text-slate-500">
+ {t("sharing.passwordUnavailableHint")}
+ </p>
+ ) : null}
{!hideCopyBoth && password ? (
<Button
variant="ghost"
diff --git a/apps/web/src/components/dialogs/share-dialog-layout.ts b/apps/web/src/components/dialogs/share-dialog-layout.ts
new file mode 100644
index 00000000..94cdeb76
--- /dev/null
+++ b/apps/web/src/components/dialogs/share-dialog-layout.ts
@@ -0,0 +1 @@
+export const SHARE_DIALOG_WIDTH_CLASS_NAME = "w-[calc(100%-2rem)] max-w-[576px]";