|
| 1 | +// Prototype - the Ambient header (variant 7 of round 2) shared by this round's riffs, |
| 2 | +// plus the content blocks modelled on the screenshot. |
| 3 | +import { parseEmojiIcon } from '@vitnode/api/lib/emoji-icon' |
| 4 | +import { RoleFormat } from '@vitnode/core/components/role-format' |
| 5 | +import { resolveRoleName } from '@vitnode/core/components/role-name' |
| 6 | +import { EmojiIcon } from '@vitnode/core/components/ui/emoji-icon' |
| 7 | +import { FEEDBACK_TONE_TEXT } from '@vitnode/core/components/ui/feedback-tone' |
| 8 | +import { Skeleton } from '@vitnode/core/components/ui/skeleton' |
| 9 | +import { UserCoverImage } from '@vitnode/core/components/user-cover-image' |
| 10 | +import { userCoverStyle } from '@vitnode/core/views/profile/images/cover-style' |
| 11 | +import { cn } from 'cn' |
| 12 | +import { useLocale } from 'use-intl' |
| 13 | + |
| 14 | +import type { Presence, Profile, Role } from './shared' |
| 15 | + |
| 16 | +import { NameLink, PendingLabel, PresenceAvatar } from './shared' |
| 17 | + |
| 18 | +const FADE = |
| 19 | + 'linear-gradient(to bottom, black 0%, rgb(0 0 0 / 0.85) 30%, rgb(0 0 0 / 0.45) 60%, rgb(0 0 0 / 0.12) 85%, transparent 100%)' |
| 20 | + |
| 21 | +/** Short elapsed time, like the screenshot's "9 yr" / "14 hr". */ |
| 22 | +export const useElapsed = () => { |
| 23 | + const locale = useLocale() |
| 24 | + |
| 25 | + return (date: Date | string) => { |
| 26 | + const seconds = Math.max(0, (Date.now() - new Date(date).getTime()) / 1000) |
| 27 | + const steps = [ |
| 28 | + ['year', 31_536_000], |
| 29 | + ['month', 2_592_000], |
| 30 | + ['day', 86_400], |
| 31 | + ['hour', 3600], |
| 32 | + ['minute', 60], |
| 33 | + ] as const |
| 34 | + const [unit, size] = steps.find(([, s]) => seconds >= s) ?? ['minute', 60] |
| 35 | + |
| 36 | + return new Intl.NumberFormat(locale, { |
| 37 | + style: 'unit', |
| 38 | + unit, |
| 39 | + unitDisplay: 'short', |
| 40 | + }).format(Math.max(1, Math.floor(seconds / size))) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export const LastVisited = ({ presence }: { presence: Presence }) => { |
| 45 | + const elapsed = useElapsed() |
| 46 | + if (!presence) return <>-</> |
| 47 | + if (presence.status === 'hidden') return <>Hidden</> |
| 48 | + if (presence.status === 'online') { |
| 49 | + return <span className={FEEDBACK_TONE_TEXT.success}>Online now</span> |
| 50 | + } |
| 51 | + |
| 52 | + return <>{presence.lastSeenAt ? elapsed(presence.lastSeenAt) : '-'}</> |
| 53 | +} |
| 54 | + |
| 55 | +export const AmbientHeader = ({ |
| 56 | + presence, |
| 57 | + user, |
| 58 | +}: { |
| 59 | + presence: Presence |
| 60 | + user: Profile |
| 61 | +}) => ( |
| 62 | + <div className="relative isolate flex flex-col gap-3 px-4 pt-14 pb-4"> |
| 63 | + <div |
| 64 | + aria-hidden="true" |
| 65 | + className="pointer-events-none absolute inset-x-0 top-0 -z-10 h-32 opacity-70" |
| 66 | + style={{ |
| 67 | + ...userCoverStyle(user.avatarColor), |
| 68 | + maskImage: FADE, |
| 69 | + WebkitMaskImage: FADE, |
| 70 | + }} |
| 71 | + > |
| 72 | + <UserCoverImage url={user.coverUrl} /> |
| 73 | + </div> |
| 74 | + <PresenceAvatar |
| 75 | + className="ring-popover/60 ring-4" |
| 76 | + presence={presence} |
| 77 | + size={56} |
| 78 | + user={user} |
| 79 | + /> |
| 80 | + <div className="flex min-w-0 flex-col gap-0.5"> |
| 81 | + <NameLink |
| 82 | + className="text-foreground truncate text-base font-semibold" |
| 83 | + name={user.name} |
| 84 | + nameCode={user.nameCode} |
| 85 | + /> |
| 86 | + <p className="flex min-w-0 items-center gap-1.5 text-sm"> |
| 87 | + <RoleFormat className="max-w-3/5 shrink-0" role={user.role} /> |
| 88 | + <span aria-hidden="true" className="text-muted-foreground"> |
| 89 | + · |
| 90 | + </span> |
| 91 | + <span className="text-muted-foreground truncate">@{user.nameCode}</span> |
| 92 | + </p> |
| 93 | + </div> |
| 94 | + </div> |
| 95 | +) |
| 96 | + |
| 97 | +export const AmbientHeaderSkeleton = () => ( |
| 98 | + <div className="flex flex-col gap-3 px-4 pt-14 pb-4"> |
| 99 | + <PendingLabel /> |
| 100 | + <Skeleton className="size-14 rounded-full" /> |
| 101 | + <Skeleton className="h-5 w-3/5" /> |
| 102 | + <Skeleton className="h-4 w-2/5" /> |
| 103 | + </div> |
| 104 | +) |
| 105 | + |
| 106 | +const tint = (color: null | string, amount = 15) => |
| 107 | + color |
| 108 | + ? { |
| 109 | + backgroundColor: `color-mix(in oklab, ${color} ${amount}%, transparent)`, |
| 110 | + color, |
| 111 | + } |
| 112 | + : undefined |
| 113 | + |
| 114 | +const RoleIcon = ({ className, role }: { className?: string; role: Role }) => { |
| 115 | + const locale = useLocale() |
| 116 | + const prefix = parseEmojiIcon(role.prefix) |
| 117 | + |
| 118 | + return ( |
| 119 | + <span |
| 120 | + aria-hidden="true" |
| 121 | + className={cn( |
| 122 | + 'bg-muted text-muted-foreground flex shrink-0 items-center justify-center', |
| 123 | + className, |
| 124 | + )} |
| 125 | + style={tint(role.color)} |
| 126 | + > |
| 127 | + {prefix ? ( |
| 128 | + <EmojiIcon value={prefix} /> |
| 129 | + ) : ( |
| 130 | + <span className="text-sm font-semibold"> |
| 131 | + {resolveRoleName(role, locale).slice(0, 1).toLocaleUpperCase()} |
| 132 | + </span> |
| 133 | + )} |
| 134 | + </span> |
| 135 | + ) |
| 136 | +} |
| 137 | + |
| 138 | +/** The screenshot's rank row: the group with its icon, other roles as badges. */ |
| 139 | +export const GroupRow = ({ user }: { user: Profile }) => { |
| 140 | + const locale = useLocale() |
| 141 | + |
| 142 | + return ( |
| 143 | + <div className="flex min-w-0 items-center justify-between gap-3"> |
| 144 | + <div className="flex min-w-0 items-center gap-3"> |
| 145 | + <RoleIcon className="size-9 rounded-lg" role={user.role} /> |
| 146 | + <span |
| 147 | + className="truncate font-medium" |
| 148 | + style={user.role.color ? { color: user.role.color } : undefined} |
| 149 | + > |
| 150 | + {resolveRoleName(user.role, locale)} |
| 151 | + </span> |
| 152 | + </div> |
| 153 | + {user.secondaryRoles.length > 0 ? ( |
| 154 | + <ul |
| 155 | + aria-label="Other roles" |
| 156 | + className="flex shrink-0 items-center gap-1" |
| 157 | + > |
| 158 | + {user.secondaryRoles.map((role) => { |
| 159 | + const name = resolveRoleName(role, locale) |
| 160 | + |
| 161 | + return ( |
| 162 | + <li key={role.id} title={name}> |
| 163 | + <RoleIcon className="size-7 rounded-full" role={role} /> |
| 164 | + <span className="sr-only">{name}</span> |
| 165 | + </li> |
| 166 | + ) |
| 167 | + })} |
| 168 | + </ul> |
| 169 | + ) : null} |
| 170 | + </div> |
| 171 | + ) |
| 172 | +} |
0 commit comments