Share asks the person who holds the pen to confirm their community agreed before a link turns on, and deleting an account lists each owned Rule with PDF and RULE.md export. Case-study Share copies the page link instead of opening the privacy modal, and the sign-in banner stays below the header so Share remains clickable.
340 lines
9.8 KiB
TypeScript
340 lines
9.8 KiB
TypeScript
"use client";
|
|
|
|
/**
|
|
* Figma: Overlay / Modal / Share (24972:2094)
|
|
* https://www.figma.com/design/agv0VBLiBlcnSAaiAORgPR/Community-Rule-System?node-id=24972-2094
|
|
*/
|
|
import { memo, useCallback, useEffect, useId, useRef, useState } from "react";
|
|
import { useMessages } from "../../../contexts/MessagesContext";
|
|
import {
|
|
addRuleStakeholder,
|
|
fetchRuleShare,
|
|
patchRuleShare,
|
|
} from "../../../../lib/create/api";
|
|
import { MAX_STAKEHOLDER_EMAILS } from "../../../../lib/create/stakeholderLimits";
|
|
import {
|
|
formatInviteCountWord,
|
|
parseInvitePaste,
|
|
} from "../../../../lib/create/parseInviteEmails";
|
|
import {
|
|
emailInitials,
|
|
visibleShareReaders,
|
|
type RuleShareSettings,
|
|
} from "../../../../lib/ruleShareTypes";
|
|
import { RULE_VISIBILITY } from "../../../../lib/ruleVisibility";
|
|
import { useCreateModalA11y } from "../Create/useCreateModalA11y";
|
|
import { ShareView } from "./Share.view";
|
|
import type { SharePhase, ShareProps } from "./Share.types";
|
|
|
|
function selectValueFor(
|
|
visibility: RuleShareSettings["visibility"],
|
|
phase: SharePhase,
|
|
): "private" | "shared_by_link" {
|
|
if (phase === "confirm" || phase === "error") return "shared_by_link";
|
|
if (visibility === RULE_VISIBILITY.private) return "private";
|
|
return "shared_by_link";
|
|
}
|
|
|
|
function titleCountWord(n: number): string {
|
|
const word = formatInviteCountWord(n);
|
|
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
}
|
|
|
|
const ShareContainer = memo<ShareProps>((props) => {
|
|
const {
|
|
isOpen,
|
|
onClose,
|
|
ruleId = null,
|
|
previewSettings,
|
|
onRequestLogin,
|
|
className = "",
|
|
backdropVariant = "blurredYellow",
|
|
} = props;
|
|
const dialogRef = useRef<HTMLDivElement>(null);
|
|
const overlayRef = useRef<HTMLDivElement>(null);
|
|
const titleId = useId();
|
|
const copy = useMessages().modals.share;
|
|
|
|
const [settings, setSettings] = useState<RuleShareSettings | null>(
|
|
previewSettings ?? null,
|
|
);
|
|
const [phase, setPhase] = useState<SharePhase>("idle");
|
|
const [communityAgreed, setCommunityAgreed] = useState(false);
|
|
const [inviteValue, setInviteValue] = useState("");
|
|
const [inviteBusy, setInviteBusy] = useState(false);
|
|
const [inviteError, setInviteError] = useState<string | null>(null);
|
|
const [turnOnBusy, setTurnOnBusy] = useState(false);
|
|
const [linkCopied, setLinkCopied] = useState(false);
|
|
|
|
useCreateModalA11y(isOpen, onClose, dialogRef);
|
|
|
|
useEffect(() => {
|
|
if (!isOpen) {
|
|
setPhase("idle");
|
|
setCommunityAgreed(false);
|
|
setInviteValue("");
|
|
setInviteError(null);
|
|
setLinkCopied(false);
|
|
setTurnOnBusy(false);
|
|
if (!previewSettings) setSettings(null);
|
|
return;
|
|
}
|
|
if (previewSettings) {
|
|
setSettings(previewSettings);
|
|
return;
|
|
}
|
|
if (!ruleId) return;
|
|
let cancelled = false;
|
|
void (async () => {
|
|
const res = await fetchRuleShare(ruleId);
|
|
if (cancelled) return;
|
|
if (res.ok === false) {
|
|
if (res.status === 401) {
|
|
onRequestLogin?.();
|
|
onClose();
|
|
}
|
|
return;
|
|
}
|
|
setSettings(res.share);
|
|
})();
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [isOpen, onClose, onRequestLogin, previewSettings, ruleId]);
|
|
|
|
const applySettings = useCallback((next: RuleShareSettings) => {
|
|
setSettings(next);
|
|
setPhase("idle");
|
|
setCommunityAgreed(false);
|
|
}, []);
|
|
|
|
const handleSelectValue = useCallback(
|
|
(value: string) => {
|
|
if (!settings) return;
|
|
if (value === "private") {
|
|
if (
|
|
settings.visibility === RULE_VISIBILITY.private &&
|
|
(phase === "confirm" || phase === "error")
|
|
) {
|
|
setPhase("idle");
|
|
setCommunityAgreed(false);
|
|
return;
|
|
}
|
|
if (settings.visibility === RULE_VISIBILITY.private) return;
|
|
if (!ruleId) {
|
|
applySettings({
|
|
...settings,
|
|
visibility: RULE_VISIBILITY.private,
|
|
shareSlug: null,
|
|
shareUrlDisplay: null,
|
|
});
|
|
return;
|
|
}
|
|
void (async () => {
|
|
const res = await patchRuleShare(ruleId, "private");
|
|
if (res.ok) applySettings(res.share);
|
|
})();
|
|
return;
|
|
}
|
|
if (value === "shared_by_link") {
|
|
if (
|
|
settings.visibility === RULE_VISIBILITY.shared_by_link ||
|
|
settings.visibility === RULE_VISIBILITY.public_and_featured
|
|
) {
|
|
setPhase("idle");
|
|
return;
|
|
}
|
|
setPhase("confirm");
|
|
setCommunityAgreed(false);
|
|
}
|
|
},
|
|
[applySettings, phase, ruleId, settings],
|
|
);
|
|
|
|
const handleTurnOnLink = useCallback(async () => {
|
|
if (!settings || !communityAgreed || turnOnBusy) return;
|
|
if (!ruleId) {
|
|
applySettings({
|
|
...settings,
|
|
visibility: RULE_VISIBILITY.shared_by_link,
|
|
shareSlug: settings.shareSlug ?? "k7f2-9qxm",
|
|
shareUrlDisplay:
|
|
settings.shareUrlDisplay ?? "communityrule.info/r/k7f2-9qxm",
|
|
});
|
|
return;
|
|
}
|
|
setTurnOnBusy(true);
|
|
const res = await patchRuleShare(ruleId, "shared_by_link", true);
|
|
setTurnOnBusy(false);
|
|
if (res.ok === false) {
|
|
setPhase("error");
|
|
return;
|
|
}
|
|
applySettings(res.share);
|
|
}, [applySettings, communityAgreed, ruleId, settings, turnOnBusy]);
|
|
|
|
const handleTurnOffLink = useCallback(async () => {
|
|
if (!settings) return;
|
|
if (!ruleId) {
|
|
applySettings({
|
|
...settings,
|
|
visibility: RULE_VISIBILITY.private,
|
|
shareSlug: null,
|
|
shareUrlDisplay: null,
|
|
});
|
|
return;
|
|
}
|
|
const res = await patchRuleShare(ruleId, "private");
|
|
if (res.ok) applySettings(res.share);
|
|
}, [applySettings, ruleId, settings]);
|
|
|
|
const handleInvite = useCallback(async () => {
|
|
if (!settings || inviteBusy) return;
|
|
const existing = [
|
|
settings.ownerEmail,
|
|
...settings.readers.map((r) => r.email),
|
|
];
|
|
const parsed = parseInvitePaste(
|
|
inviteValue,
|
|
existing,
|
|
MAX_STAKEHOLDER_EMAILS,
|
|
);
|
|
const email = parsed.added[0];
|
|
if (!email) {
|
|
setInviteError(copy.inviteInvalid);
|
|
return;
|
|
}
|
|
setInviteError(null);
|
|
if (!ruleId) {
|
|
setSettings({
|
|
...settings,
|
|
readers: [
|
|
...settings.readers,
|
|
{ email, initials: emailInitials(email), role: "reader" },
|
|
],
|
|
});
|
|
setInviteValue("");
|
|
return;
|
|
}
|
|
setInviteBusy(true);
|
|
const res = await addRuleStakeholder(ruleId, email);
|
|
if (res.ok === false) {
|
|
setInviteBusy(false);
|
|
setInviteError(res.error);
|
|
return;
|
|
}
|
|
const next = await fetchRuleShare(ruleId);
|
|
setInviteBusy(false);
|
|
if (next.ok) {
|
|
setSettings(next.share);
|
|
setInviteValue("");
|
|
}
|
|
}, [copy.inviteInvalid, inviteBusy, inviteValue, ruleId, settings]);
|
|
|
|
const handleCopyLink = useCallback(async () => {
|
|
if (!settings) return;
|
|
try {
|
|
await navigator.clipboard.writeText(settings.shareUrl);
|
|
setLinkCopied(true);
|
|
} catch {
|
|
setLinkCopied(false);
|
|
}
|
|
}, [settings]);
|
|
|
|
if (!settings) {
|
|
return (
|
|
<ShareView
|
|
isOpen={isOpen}
|
|
onClose={onClose}
|
|
className={className}
|
|
backdropVariant={backdropVariant}
|
|
dialogRef={dialogRef}
|
|
overlayRef={overlayRef}
|
|
titleId={titleId}
|
|
copy={copy}
|
|
communityName=""
|
|
inviteValue=""
|
|
onInviteValueChange={setInviteValue}
|
|
onInvite={() => undefined}
|
|
inviteBusy={false}
|
|
inviteError={null}
|
|
ownerEmail=""
|
|
ownerInitials="?"
|
|
shownReaders={[]}
|
|
overflowLabel={null}
|
|
selectValue="private"
|
|
onSelectValue={() => undefined}
|
|
showLinkField={false}
|
|
shareUrlDisplay=""
|
|
onTurnOffLink={() => undefined}
|
|
showConfirm={false}
|
|
communityAgreed={false}
|
|
onCommunityAgreedChange={setCommunityAgreed}
|
|
onTurnOnLink={() => undefined}
|
|
turnOnDisabled
|
|
turnOnBusy={false}
|
|
showError={false}
|
|
copyEnabled={false}
|
|
linkCopied={false}
|
|
onCopyLink={() => undefined}
|
|
/>
|
|
);
|
|
}
|
|
|
|
const selectValue = selectValueFor(settings.visibility, phase);
|
|
const { shown, overflowCount } = visibleShareReaders(settings.readers);
|
|
const overflowLabel =
|
|
overflowCount > 0
|
|
? copy.overflowReaders.replace("{{count}}", titleCountWord(overflowCount))
|
|
: null;
|
|
const showLinkField =
|
|
settings.visibility === RULE_VISIBILITY.shared_by_link && phase === "idle";
|
|
const showConfirm = phase === "confirm" || phase === "error";
|
|
const copyEnabled = phase !== "confirm" && phase !== "error";
|
|
|
|
return (
|
|
<ShareView
|
|
isOpen={isOpen}
|
|
onClose={onClose}
|
|
className={className}
|
|
backdropVariant={backdropVariant}
|
|
dialogRef={dialogRef}
|
|
overlayRef={overlayRef}
|
|
titleId={titleId}
|
|
copy={copy}
|
|
communityName={settings.title}
|
|
inviteValue={inviteValue}
|
|
onInviteValueChange={(value) => {
|
|
setInviteValue(value);
|
|
if (inviteError) setInviteError(null);
|
|
}}
|
|
onInvite={() => void handleInvite()}
|
|
inviteBusy={inviteBusy}
|
|
inviteError={inviteError}
|
|
ownerEmail={settings.ownerEmail}
|
|
ownerInitials={settings.ownerInitials}
|
|
shownReaders={shown}
|
|
overflowLabel={overflowLabel}
|
|
selectValue={selectValue}
|
|
onSelectValue={handleSelectValue}
|
|
showLinkField={showLinkField}
|
|
shareUrlDisplay={settings.shareUrlDisplay ?? ""}
|
|
onTurnOffLink={() => void handleTurnOffLink()}
|
|
showConfirm={showConfirm}
|
|
communityAgreed={communityAgreed}
|
|
onCommunityAgreedChange={setCommunityAgreed}
|
|
onTurnOnLink={() => void handleTurnOnLink()}
|
|
turnOnDisabled={!communityAgreed}
|
|
turnOnBusy={turnOnBusy}
|
|
showError={phase === "error"}
|
|
copyEnabled={copyEnabled}
|
|
linkCopied={linkCopied}
|
|
onCopyLink={() => void handleCopyLink()}
|
|
/>
|
|
);
|
|
});
|
|
|
|
ShareContainer.displayName = "Share";
|
|
|
|
export default ShareContainer;
|