Newsletter: Pick a redirect on every link field

Link fields with redirects: true get a list of Data > Links/redirects that fills in the short link's full address; the newsletter turns it on for its blocks' links and their items' links.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-10-05 00:39:52 -05:00
parent 6289e7203f
commit e060153798
3 changed files with 41 additions and 3 deletions

View file

@ -103,7 +103,18 @@ const sourceOf = (part: NewsletterPart): PickSource => (SOURCES.includes(part.so
/* Every part is a row: one editor for blocks and the email's own
parts, so any of them can be moved, copied or removed. */
const blockFields = blocksChild.fields.filter((field) => !['slot', 'team_id', 'type', 'captions'].includes(field.path))
/* Every link a block or its items carry can be a short link
(Data → Links/redirects), so its clicks are counted. */
const LINK_PATHS = ['href', 'url']
const withRedirects = (field: any) => (LINK_PATHS.includes(field.path) ? { ...field, redirects: true } : field)
const withRedirectsChild = (child: any) => ({
...child,
fields: child.fields.map(withRedirects),
children: child.children?.map(withRedirectsChild),
})
const blockFields = blocksChild.fields
.filter((field) => !['slot', 'team_id', 'type', 'captions'].includes(field.path))
.map(withRedirects)
const PARTS_SPEC = {
key: 'parts',
label: 'Parts',
@ -188,7 +199,7 @@ const PARTS_SPEC = {
{ path: 'space', label: 'Space above', widget: 'number', when: { path: 'type', value: 'divider' }, help: 'In pixels, 0 to 64. Default: 24' },
],
children: [
...(blocksChild.children ?? []),
...(blocksChild.children ?? []).map(withRedirectsChild),
{
key: 'socials',
label: 'Links',
@ -254,6 +265,7 @@ export default function AdminNewsletter() {
const [posts, setPosts] = useState<PostRow[] | null>(null)
const [postDetails, setPostDetails] = useState<Record<string, BlogPostRecord>>({})
const [copied, setCopied] = useState<'html' | 'text' | null>(null)
const [redirects, setRedirects] = useState<{ id: string; label: string }[]>([])
// The draft being edited: null is a new one, not saved yet.
const [drafts, setDrafts] = useState<DraftRow[]>([])
@ -269,6 +281,9 @@ export default function AdminNewsletter() {
get('/admin/blog_posts?is_published=1&is_listed=1&limit=200', { ttl: 0 })
.then((data: { rows: PostRow[] }) => setPosts(data.rows))
.catch(() => setPosts([]))
get('/admin/options?only=redirects', { ttl: 0 })
.then((data: { options: { redirects: { id: string; label: string }[] } }) => setRedirects(data.options.redirects))
.catch(() => setRedirects([]))
}, [])
const loadDrafts = () =>
@ -411,8 +426,9 @@ export default function AdminNewsletter() {
countdownEvents: events
.filter((event) => !event.is_series && event.starts_on)
.map((event) => ({ id: event.id, label: [event.title, event.date_label].filter(Boolean).join(', ') })),
redirects,
}),
[events, posts, cards],
[events, posts, cards, redirects],
)
const newsletter = useMemo(() => {