import { useAuth } from 'react-oidc-context' /** * Resolve UUID sub claim (Keycloak {@code sub} → makerId / userId) к * human-friendly display string. * *
Cases: *
TODO Phase 1d: для не-current-user backend endpoint * {@code /admin/users/{sub}/display} → resolve в username. Пока best-effort: * current user resolved через JWT profile claim, остальные — UUID prefix * (achievable via JWT alone). * *
Используется везде где UI показывает actor: AUTHOR в таблицах * /reviews (record + schema), USER в /audit log, AUTHOR в schema draft * drawer. */ export function useUserDisplay(uuid: string | null | undefined): { display: string isMe: boolean fullId: string } { const auth = useAuth() const fullId = uuid ?? '' if (!fullId) { return { display: 'anonymous', isMe: false, fullId: '' } } const currentSub = auth.user?.profile?.sub const isMe = Boolean(currentSub) && currentSub === fullId if (isMe) { const me = auth.user?.profile?.preferred_username || auth.user?.profile?.name || auth.user?.profile?.email return { display: me ? `Я • ${me}` : 'Я', isMe, fullId } } return { display: fullId.substring(0, 8), isMe, fullId } } /** * UserCell — convenience component для тех мест где нужен readonly displaу * с tooltip. Используется в таблицах. */ export function UserCell({ uuid }: { uuid: string | null | undefined }) { const { display, isMe, fullId } = useUserDisplay(uuid) if (!fullId) return anonymous return ( {display} ) }