New Rules stay off lists until the person who holds the pen turns on a share link, or the team features one. Uploaded files are readable only by those readers, stripped of location metadata, and deleted with the Rule, draft, or account.
57 lines
1.7 KiB
TypeScript
57 lines
1.7 KiB
TypeScript
import { RULE_VISIBILITY, type RuleVisibility } from "./ruleVisibility";
|
|
|
|
const SHARE_SLUG_RE = /^[a-z0-9]{4}-[a-z0-9]{4}$/;
|
|
|
|
export function isShareSlug(value: string): boolean {
|
|
return SHARE_SLUG_RE.test(value);
|
|
}
|
|
|
|
export function publishedRulePath(ruleId: string): string {
|
|
return `/rules/${encodeURIComponent(ruleId)}`;
|
|
}
|
|
|
|
export function shareRulePath(shareSlug: string): string {
|
|
return `/r/${encodeURIComponent(shareSlug)}`;
|
|
}
|
|
|
|
export function buildPublicRuleUrl(origin: string, ruleId: string): string {
|
|
const base = origin.replace(/\/$/, "");
|
|
return `${base}${publishedRulePath(ruleId)}`;
|
|
}
|
|
|
|
export function buildShareRuleUrl(origin: string, shareSlug: string): string {
|
|
const base = origin.replace(/\/$/, "");
|
|
return `${base}${shareRulePath(shareSlug)}`;
|
|
}
|
|
|
|
/** Host + path as drawn in the Share control, e.g. `communityrule.info/r/k7f2-9qxm`. */
|
|
export function formatShareUrlDisplay(origin: string, shareSlug: string): string {
|
|
try {
|
|
return `${new URL(origin).host}/r/${shareSlug}`;
|
|
} catch {
|
|
const host = origin.replace(/^https?:\/\//, "").replace(/\/$/, "");
|
|
return `${host}/r/${shareSlug}`;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* URL the Share control copies and shows.
|
|
* Shared-by-link (and featured rows that still have a slug) use `/r/{slug}`.
|
|
* Featured without a slug uses `/rules/{id}`. Private uses `/rules/{id}`.
|
|
*/
|
|
export function shareCopyUrl(opts: {
|
|
origin: string;
|
|
ruleId: string;
|
|
shareSlug: string | null;
|
|
visibility: RuleVisibility;
|
|
}): string {
|
|
if (
|
|
opts.shareSlug &&
|
|
(opts.visibility === RULE_VISIBILITY.shared_by_link ||
|
|
opts.visibility === RULE_VISIBILITY.public_and_featured)
|
|
) {
|
|
return buildShareRuleUrl(opts.origin, opts.shareSlug);
|
|
}
|
|
return buildPublicRuleUrl(opts.origin, opts.ruleId);
|
|
}
|