import * as React from 'react'; import type { SlateElementProps, TComboboxInputElement, TMentionElement, } from 'platejs'; import type { PlateElementProps } from 'platejs/react'; import { BaseMentionPlugin } from '@platejs/mention'; import { MentionInputPlugin, MentionPlugin } from '@platejs/mention/react'; import Api from '@plone/volto/helpers/Api/Api'; import { getBaseUrl } from '@plone/volto/helpers/Url/Url'; import { SlateElement } from 'platejs'; import { PlateElement, useFocused, useReadOnly, useSelected, } from 'platejs/react'; import { useSelector } from 'react-redux'; import { InlineCombobox, InlineComboboxContent, InlineComboboxEmpty, InlineComboboxGroup, InlineComboboxGroupLabel, InlineComboboxInput, InlineComboboxItem, } from '@plone/plate/components/ui/inline-combobox'; import { cn } from '@plone/plate/lib/utils'; import PersonPill from '../../components/PersonPill/PersonPill'; type Mentionable = { id: string; fullname: string; portrait?: string | null; }; const portraits = new Map(); const RECENT_MENTIONS_KEY = '@plone/plate:recent-mentions'; const RECENT_MENTIONS_LIMIT = 5; function getCurrentUserId(token?: string | null) { if (!token) return null; try { const [, payload] = token.split('.'); if (!payload) return null; const normalizedPayload = payload.replace(/-/g, '+').replace(/_/g, '/'); const paddedPayload = normalizedPayload + '='.repeat((4 - (normalizedPayload.length % 4)) % 4); return (JSON.parse(atob(paddedPayload)) as { sub?: string }).sub ?? null; } catch { return null; } } function getRecentMentionsKey(userId: string | null) { if (typeof window === 'undefined' || !userId) return null; return `${RECENT_MENTIONS_KEY}:${window.location.origin}:${userId}`; } function readRecentMentionIds(key: string | null) { if (!key || typeof window === 'undefined') return []; try { const stored = JSON.parse(localStorage.getItem(key) ?? '[]'); if (!Array.isArray(stored)) return []; return [ ...new Set(stored.filter((id): id is string => typeof id === 'string')), ].slice(0, RECENT_MENTIONS_LIMIT); } catch { return []; } } function saveRecentMentionId(key: string | null, userId: string) { if (!key || typeof window === 'undefined') return; try { const ids = readRecentMentionIds(key); localStorage.setItem( key, JSON.stringify( [userId, ...ids.filter((id) => id !== userId)].slice( 0, RECENT_MENTIONS_LIMIT, ), ), ); } catch { // Private browsing or quota limits should not prevent mentioning users. } } const makeMentionId = () => typeof crypto !== 'undefined' && crypto.randomUUID ? crypto.randomUUID() : `${Date.now()}-${Math.random().toString(36).slice(2)}`; function MentionPill({ children, portrait, prefix, resolvePortrait = true, userId, value, }: React.PropsWithChildren<{ portrait?: string | null; prefix?: string; resolvePortrait?: boolean; userId?: string; value: string; }>) { const [currentPortrait, setCurrentPortrait] = React.useState(portrait); React.useEffect(() => { if (!resolvePortrait || !userId) return; const cachedPortrait = portraits.get(userId); if (cachedPortrait !== undefined) { setCurrentPortrait(cachedPortrait); return; } const contextPath = getBaseUrl(window.location.pathname) || '/'; const api = new Api() as any; api .get(`${contextPath}/@mentions`, { params: { id: userId } }) .then((response) => { const resolvedPortrait = response.items?.[0]?.portrait ?? null; portraits.set(userId, resolvedPortrait); setCurrentPortrait(resolvedPortrait); }) // Keep the persisted value if the current user cannot resolve it. .catch(() => setCurrentPortrait(portrait)); }, [portrait, resolvePortrait, userId]); return ( <> {children} ); } export function VoltoMentionElement( props: PlateElementProps & { prefix?: string }, ) { const { element } = props; const selected = useSelected(); const focused = useFocused(); const readOnly = useReadOnly(); return ( {props.children} ); } export function VoltoMentionElementStatic( props: SlateElementProps & { prefix?: string }, ) { const { element } = props; return ( {props.children} ); } export function VoltoMentionInputElement( props: PlateElementProps, ) { const { editor, element } = props; const token = useSelector((state: any) => state.userSession?.token); const currentUserId = React.useMemo(() => getCurrentUserId(token), [token]); const recentMentionsKey = React.useMemo( () => getRecentMentionsKey(currentUserId), [currentUserId], ); const [search, setSearch] = React.useState(''); const [items, setItems] = React.useState([]); React.useEffect(() => { const normalizedSearch = search.trim(); if (!normalizedSearch) { const recentIds = readRecentMentionIds(recentMentionsKey); if (!recentIds.length) { setItems([]); return undefined; } const contextPath = getBaseUrl(window.location.pathname) || '/'; const api = new Api() as any; Promise.all( recentIds.map((id) => api .get(`${contextPath}/@mentions`, { params: { id } }) .then((response) => response.items?.[0] ?? null) .catch(() => null), ), ).then((recentItems) => setItems(recentItems.filter((item): item is Mentionable => !!item)), ); return undefined; } const timer = window.setTimeout(() => { const contextPath = getBaseUrl(window.location.pathname) || '/'; const api = new Api() as any; api .get(`${contextPath}/@mentions`, { params: { search: normalizedSearch, limit: 25 }, }) .then((response) => setItems(response.items ?? [])) .catch(() => setItems([])); }, 200); return () => window.clearTimeout(timer); }, [recentMentionsKey, search]); const selectItem = React.useCallback( (item: Mentionable) => { const mentionId = makeMentionId(); saveRecentMentionId(recentMentionsKey, item.id); (editor.tf as any).insert.mention({ key: item.id, search, value: item.fullname, } as any); const mentionPath = editor.selection?.anchor.path.slice(0, -1); if (mentionPath) { editor.tf.setNodes( { mentionId, portrait: item.portrait ?? undefined } as any, { at: mentionPath }, ); } editor.tf.move({ unit: 'offset' }); }, [editor, recentMentionsKey, search], ); return ( {search.trim() ? 'No matching users' : 'Type to search people'} {search.trim() ? 'Mention people' : 'Recently mentioned'} {items.map((item) => ( selectItem(item)} value={item.fullname} > ))} {props.children} ); } export const VoltoMentionKit = [ MentionPlugin.configure({ options: { triggerPreviousCharPattern: /^$|^[\s"']$/ }, }).withComponent(VoltoMentionElement), MentionInputPlugin.withComponent(VoltoMentionInputElement), ]; export const VoltoMentionBaseKit = [ BaseMentionPlugin.withComponent(VoltoMentionElementStatic), ];