A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(app): add account switcher tiles with instance badges
Hampus41f99f80cf7d
diff --git a/fluxer_app/src/features/auth/components/accounts/account_switcher/AccountInstanceBadge.tsx b/fluxer_app/src/features/auth/components/accounts/account_switcher/AccountInstanceBadge.tsxnew file mode 100644index 000000000..05a008f17--- /dev/null+++ b/fluxer_app/src/features/auth/components/accounts/account_switcher/AccountInstanceBadge.tsx@@ -0,0 +1,65 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import styles from '@app/features/auth/components/accounts/account_switcher/AccountSwitcherTile.module.css';+import type {AccountInstanceBadgeInfo} from '@app/features/auth/components/accounts/account_switcher/useAccountInstanceBadge';+import {InstanceBrandMark} from '@app/features/auth/components/InstanceBrandMark';+import {instanceDomainHost} from '@app/features/auth/flow/instance_selector/InstanceDirectoryStorage';+import {OFFICIAL_INSTANCE_DESCRIPTOR} from '@app/features/i18n/utils/CommonMessageDescriptors';+import {VerifiedConnectionIcon} from '@app/features/ui/components/icons/VerifiedConnectionIcon';+import {Tooltip} from '@app/features/ui/tooltip/Tooltip';+import {useLingui} from '@lingui/react/macro';+import type React from 'react';++function instanceBadgeName(badge: AccountInstanceBadgeInfo): string | null {+ return badge.label !== instanceDomainHost(badge.title) ? badge.label : null;+}++interface AccountInstanceBadgeProps {+ readonly badge: AccountInstanceBadgeInfo;+}++export function AccountInstanceBadge({badge}: AccountInstanceBadgeProps): React.ReactElement {+ const {i18n} = useLingui();+ const officialLabel = i18n._(OFFICIAL_INSTANCE_DESCRIPTOR);+ const instanceName = instanceBadgeName(badge);+ const renderTooltip = () => (+ <span className={styles.instanceTooltip} data-flx="auth.accounts.account-switcher.instance-badge.tooltip">+ <span className={styles.instanceTooltipLabel} data-flx="auth.accounts.account-switcher.instance-badge.label">+ {instanceName == null ? badge.title : `${instanceName} (${badge.title})`}+ </span>+ {badge.isOfficial ? (+ <span+ className={styles.instanceTooltipOfficial}+ role="img"+ aria-label={officialLabel}+ data-flx="auth.accounts.account-switcher.instance-badge.official"+ >+ <VerifiedConnectionIcon size={14} data-flx="auth.accounts.account-switcher.instance-badge.verified-icon" />+ </span>+ ) : null}+ </span>+ );+ return (+ <Tooltip+ text={renderTooltip}+ position="top"+ maxWidth="none"+ data-flx="auth.accounts.account-switcher.instance-badge.tooltip-trigger"+ >+ <span+ className={styles.instanceBadge}+ aria-hidden="true"+ data-badge-cutout=""+ data-flx="auth.accounts.account-switcher.instance-badge"+ >+ <InstanceBrandMark+ isOfficial={badge.isOfficial}+ iconUrl={badge.iconUrl}+ size={15}+ globeWeight="bold"+ data-flx="auth.accounts.account-switcher.instance-badge.brand-mark"+ />+ </span>+ </Tooltip>+ );+}diff --git a/fluxer_app/src/features/auth/components/accounts/account_switcher/AccountSwitcherTile.tsx b/fluxer_app/src/features/auth/components/accounts/account_switcher/AccountSwitcherTile.tsxnew file mode 100644index 000000000..4bea1c582--- /dev/null+++ b/fluxer_app/src/features/auth/components/accounts/account_switcher/AccountSwitcherTile.tsx@@ -0,0 +1,370 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {AccountInstanceBadge} from '@app/features/auth/components/accounts/account_switcher/AccountInstanceBadge';+import styles from '@app/features/auth/components/accounts/account_switcher/AccountSwitcherTile.module.css';+import type {AccountInstanceBadgeInfo} from '@app/features/auth/components/accounts/account_switcher/useAccountInstanceBadge';+import {+ type AvatarCutoutMask,+ useAvatarBadgeCutouts,+} from '@app/features/auth/components/accounts/account_switcher/useAvatarBadgeCutouts';+import {MentionBadge} from '@app/features/ui/components/MentionBadge';+import FocusRing from '@app/features/ui/focus_ring/FocusRing';+import {CheckIcon, PencilSimpleIcon, PlusIcon, WarningCircleIcon} from '@phosphor-icons/react';+import clsx from 'clsx';+import {motion} from 'framer-motion';+import type React from 'react';+import {useId, useRef} from 'react';++export const ADD_ACCOUNT_TILE_KEY = 'add-account';++export const AccountTileLineTone = Object.freeze({+ MUTED: 'muted',+ WARNING: 'warning',+ DANGER: 'danger',+} as const);++export type AccountTileLineTone = (typeof AccountTileLineTone)[keyof typeof AccountTileLineTone];++export type AccountMenuTrigger = React.MouseEvent<HTMLButtonElement> | HTMLButtonElement;++export interface AccountSwitcherTileModel {+ readonly accountKey: string;+ readonly displayName: string;+ readonly secondLine: string;+ readonly secondLineTone: AccountTileLineTone;+ readonly avatarUrl: string;+ readonly instanceBadge: AccountInstanceBadgeInfo | null;+ readonly isCurrent: boolean;+ readonly isExpired: boolean;+ readonly mentionCount: number;+ readonly hasUnread: boolean;+ readonly ariaLabel: string;+}++interface TileInteraction {+ readonly isActive: boolean;+ readonly isManaging: boolean;+ readonly isPending: boolean;+ readonly isDimmed: boolean;+ readonly isMenuOpen: boolean;+ readonly disabled: boolean;+ readonly onFocusTile: (key: string) => void;+}++interface AccountTileDrag {+ readonly isDragging: boolean;+ readonly isReordering: boolean;+ readonly animateLayout: boolean;+ readonly dragRef: (node: HTMLElement | null) => void;+ readonly dropRef: (node: HTMLElement | null) => void;+}++const TILE_LAYOUT_TRANSITION = {duration: 0.18, ease: 'easeOut'} as const;++interface AccountSwitcherTileViewProps extends TileInteraction {+ readonly tile: AccountSwitcherTileModel;+ readonly drag: AccountTileDrag;+ readonly onActivate: (accountKey: string, event: React.MouseEvent<HTMLButtonElement>) => void;+ readonly onOpenMenu: (accountKey: string, trigger: AccountMenuTrigger) => void;+}++function isContextMenuKey(event: React.KeyboardEvent): boolean {+ return event.key === 'ContextMenu' || (event.shiftKey && event.key === 'F10');+}++function renderActivityBadge(tile: AccountSwitcherTileModel): React.ReactNode {+ if (tile.isExpired) {+ return (+ <span+ className={clsx(styles.cornerBadge, styles.expiredBadge)}+ data-badge-cutout=""+ aria-hidden="true"+ data-flx="auth.accounts.account-switcher.tile.expired-badge"+ >+ <WarningCircleIcon weight="fill" data-flx="auth.accounts.account-switcher.tile.expired-icon" />+ </span>+ );+ }+ if (tile.mentionCount > 0) {+ return (+ <span+ className={clsx(styles.cornerBadge, styles.mentionBadge)}+ data-badge-cutout=""+ aria-hidden="true"+ data-flx="auth.accounts.account-switcher.tile.mention-badge"+ >+ <MentionBadge+ mentionCount={tile.mentionCount}+ size="medium"+ data-flx="auth.accounts.account-switcher.tile.mention-count"+ />+ </span>+ );+ }+ if (tile.hasUnread) {+ return (+ <span+ className={clsx(styles.cornerBadge, styles.unreadDot)}+ data-badge-cutout=""+ aria-hidden="true"+ data-flx="auth.accounts.account-switcher.tile.unread-dot"+ />+ );+ }+ return null;+}++function tileBadgeKey(tile: AccountSwitcherTileModel): string {+ return [+ tile.isCurrent ? 'current' : '',+ tile.instanceBadge == null ? '' : 'instance',+ tile.isExpired ? 'expired' : '',+ tile.mentionCount > 0 ? `mentions-${tile.mentionCount}` : '',+ tile.hasUnread ? 'unread' : '',+ ]+ .filter((part) => part.length > 0)+ .join(' ');+}++function AvatarCutoutMaskDefs({id, mask}: {readonly id: string; readonly mask: AvatarCutoutMask}): React.ReactElement {+ return (+ <svg className={styles.cutoutDefs} aria-hidden="true" data-flx="auth.accounts.account-switcher.tile.cutout-defs">+ <defs data-flx="auth.accounts.account-switcher.tile.cutout-defs.defs">+ <mask+ id={id}+ maskUnits="userSpaceOnUse"+ maskContentUnits="userSpaceOnUse"+ x={0}+ y={0}+ width={mask.width}+ height={mask.height}+ data-flx="auth.accounts.account-switcher.tile.cutout-mask"+ >+ <rect+ fill="white"+ x={0}+ y={0}+ width={mask.width}+ height={mask.height}+ data-flx="auth.accounts.account-switcher.tile.cutout-mask.base"+ />+ {mask.cutouts.map((cutout) => (+ <rect+ key={`${cutout.x}:${cutout.y}`}+ fill="black"+ x={cutout.x}+ y={cutout.y}+ width={cutout.width}+ height={cutout.height}+ rx={cutout.radius}+ ry={cutout.radius}+ data-flx="auth.accounts.account-switcher.tile.cutout-mask.cutout"+ />+ ))}+ </mask>+ </defs>+ </svg>+ );+}++function BusyRing(): React.ReactElement {+ return (+ <svg+ className={styles.busyRing}+ viewBox="0 0 100 100"+ aria-hidden="true"+ data-flx="auth.accounts.account-switcher.tile.busy-ring"+ >+ <rect+ x="1"+ y="1"+ width="98"+ height="98"+ rx="23"+ ry="23"+ pathLength={100}+ data-flx="auth.accounts.account-switcher.account-switcher-tile.busy-ring.rect"+ />+ </svg>+ );+}++export function AccountSwitcherTileView({+ tile,+ drag,+ isActive,+ isManaging,+ isPending,+ isDimmed,+ isMenuOpen,+ disabled,+ onFocusTile,+ onActivate,+ onOpenMenu,+}: AccountSwitcherTileViewProps): React.ReactElement {+ const frameRef = useRef<HTMLSpanElement | null>(null);+ const maskId = useId();+ const cutoutMask = useAvatarBadgeCutouts(frameRef, tileBadgeKey(tile));+ return (+ <motion.li+ ref={drag.dropRef}+ layout={drag.animateLayout ? 'position' : false}+ transition={TILE_LAYOUT_TRANSITION}+ className={clsx(styles.slot, isDimmed && styles.slotDimmed, drag.isDragging && styles.slotDragging)}+ data-account-slot=""+ data-flx="auth.accounts.account-switcher.tile.slot"+ >+ <FocusRing offset={-8} data-flx="auth.accounts.account-switcher.tile.focus-ring">+ <button+ ref={drag.dragRef}+ type="button"+ className={clsx(+ styles.tile,+ tile.isExpired && styles.tileExpired,+ (isManaging || drag.isReordering) && styles.tileStill,+ )}+ tabIndex={isActive ? 0 : -1}+ disabled={disabled}+ aria-label={tile.ariaLabel}+ aria-current={tile.isCurrent ? 'true' : undefined}+ aria-haspopup={isManaging ? 'menu' : undefined}+ aria-expanded={isManaging ? isMenuOpen : undefined}+ aria-busy={isPending || undefined}+ aria-keyshortcuts="Shift+F10"+ data-switcher-tile={tile.accountKey}+ onFocus={() => onFocusTile(tile.accountKey)}+ onClick={(event) => onActivate(tile.accountKey, event)}+ onContextMenu={(event) => onOpenMenu(tile.accountKey, event)}+ onKeyDown={(event) => {+ if (!isContextMenuKey(event)) return;+ event.preventDefault();+ event.stopPropagation();+ onOpenMenu(tile.accountKey, event.currentTarget);+ }}+ data-flx="auth.accounts.account-switcher.tile.button"+ >+ <span+ ref={frameRef}+ className={styles.avatarFrame}+ data-tile-avatar=""+ data-flx="auth.accounts.account-switcher.tile.avatar-frame"+ >+ {cutoutMask == null ? null : (+ <AvatarCutoutMaskDefs+ id={maskId}+ mask={cutoutMask}+ data-flx="auth.accounts.account-switcher.tile.cutout-mask-defs"+ />+ )}+ <span+ className={styles.avatarClip}+ style={cutoutMask == null ? undefined : {maskImage: `url(#${maskId})`}}+ data-flx="auth.accounts.account-switcher.tile.avatar-clip"+ >+ <img+ className={styles.avatar}+ src={tile.avatarUrl}+ alt=""+ draggable={false}+ decoding="async"+ loading="lazy"+ data-flx="auth.accounts.account-switcher.tile.avatar"+ />+ {isManaging ? (+ <span+ className={styles.manageOverlay}+ aria-hidden="true"+ data-flx="auth.accounts.account-switcher.tile.manage-overlay"+ >+ <PencilSimpleIcon weight="bold" data-flx="auth.accounts.account-switcher.tile.manage-icon" />+ </span>+ ) : null}+ </span>+ {isPending ? <BusyRing data-flx="auth.accounts.account-switcher.tile.busy" /> : null}+ {tile.isCurrent ? (+ <span+ className={clsx(styles.cornerBadge, styles.currentBadge)}+ data-badge-cutout=""+ aria-hidden="true"+ data-flx="auth.accounts.account-switcher.tile.current-badge"+ >+ <CheckIcon weight="bold" data-flx="auth.accounts.account-switcher.tile.current-icon" />+ </span>+ ) : null}+ {tile.instanceBadge == null ? null : (+ <AccountInstanceBadge+ badge={tile.instanceBadge}+ data-flx="auth.accounts.account-switcher.tile.instance-badge"+ />+ )}+ {renderActivityBadge(tile)}+ </span>+ <span+ className={clsx('user-text', styles.name)}+ dir="auto"+ aria-hidden="true"+ data-flx="auth.accounts.account-switcher.tile.name"+ >+ {tile.displayName}+ </span>+ <span+ className={clsx(+ styles.secondLine,+ tile.secondLineTone === AccountTileLineTone.WARNING && styles.secondLineWarning,+ tile.secondLineTone === AccountTileLineTone.DANGER && styles.secondLineDanger,+ )}+ dir="auto"+ aria-hidden="true"+ data-flx="auth.accounts.account-switcher.tile.second-line"+ >+ {tile.secondLine}+ </span>+ </button>+ </FocusRing>+ </motion.li>+ );+}++interface AccountSwitcherAddTileProps extends Omit<TileInteraction, 'isManaging' | 'isPending' | 'isMenuOpen'> {+ readonly label: string;+ readonly onAdd: () => void;+}++export function AccountSwitcherAddTile({+ label,+ isActive,+ isDimmed,+ disabled,+ onFocusTile,+ onAdd,+}: AccountSwitcherAddTileProps): React.ReactElement {+ return (+ <li+ className={clsx(styles.slot, isDimmed && styles.slotDimmed)}+ data-flx="auth.accounts.account-switcher.add-tile.slot"+ >+ <FocusRing offset={-8} data-flx="auth.accounts.account-switcher.add-tile.focus-ring">+ <button+ type="button"+ className={clsx(styles.tile, styles.tileAdd)}+ tabIndex={isActive ? 0 : -1}+ disabled={disabled}+ data-switcher-tile={ADD_ACCOUNT_TILE_KEY}+ onFocus={() => onFocusTile(ADD_ACCOUNT_TILE_KEY)}+ onClick={onAdd}+ data-flx="auth.accounts.account-switcher.add-tile.button"+ >+ <span+ className={clsx(styles.avatarFrame, styles.addFrame)}+ data-flx="auth.accounts.account-switcher.add-tile.frame"+ >+ <PlusIcon weight="bold" aria-hidden="true" data-flx="auth.accounts.account-switcher.add-tile.plus-icon" />+ </span>+ <span className={styles.name} data-flx="auth.accounts.account-switcher.add-tile.label">+ {label}+ </span>+ </button>+ </FocusRing>+ </li>+ );+}diff --git a/fluxer_app/src/features/auth/components/accounts/account_switcher_modal/useAccountInstanceBadge.ts b/fluxer_app/src/features/auth/components/accounts/account_switcher/useAccountInstanceBadge.tssimilarity index 100%rename from fluxer_app/src/features/auth/components/accounts/account_switcher_modal/useAccountInstanceBadge.tsrename to fluxer_app/src/features/auth/components/accounts/account_switcher/useAccountInstanceBadge.tsdiff --git a/fluxer_app/src/features/auth/components/accounts/account_switcher/useAvatarBadgeCutouts.ts b/fluxer_app/src/features/auth/components/accounts/account_switcher/useAvatarBadgeCutouts.tsnew file mode 100644index 000000000..4b49ca4a9--- /dev/null+++ b/fluxer_app/src/features/auth/components/accounts/account_switcher/useAvatarBadgeCutouts.ts@@ -0,0 +1,108 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {observeResize} from '@app/features/platform/utils/SharedResizeObserver';+import {type RefObject, useLayoutEffect, useState} from 'react';++const BADGE_CUTOUT_SELECTOR = '[data-badge-cutout]';++const BADGE_CUTOUT_GAP_REM = 0.1875;++interface BadgeCutout {+ readonly x: number;+ readonly y: number;+ readonly width: number;+ readonly height: number;+ readonly radius: number;+}++export interface AvatarCutoutMask {+ readonly width: number;+ readonly height: number;+ readonly cutouts: ReadonlyArray<BadgeCutout>;+}++function cornerRadius(element: HTMLElement, width: number, height: number): number {+ const value = getComputedStyle(element).borderTopLeftRadius.split(' ')[0] ?? '0';+ const parsed = Number.parseFloat(value);+ const shortest = Math.min(width, height);+ const radius = value.endsWith('%') ? (parsed / 100) * shortest : parsed;+ return Math.min(Number.isFinite(radius) ? radius : 0, shortest / 2);+}++function measureMask(frame: HTMLElement): AvatarCutoutMask | null {+ const elements = frame.querySelectorAll<HTMLElement>(BADGE_CUTOUT_SELECTOR);+ if (elements.length === 0) {+ return null;+ }+ const frameStyle = getComputedStyle(frame);+ const width = Number.parseFloat(frameStyle.width);+ const height = Number.parseFloat(frameStyle.height);+ const frameRect = frame.getBoundingClientRect();+ if (!(width > 0 && height > 0 && frameRect.width > 0)) {+ return null;+ }+ const scale = frameRect.width / width;+ const gap = Number.parseFloat(getComputedStyle(frame.ownerDocument.documentElement).fontSize) * BADGE_CUTOUT_GAP_REM;+ const cutouts = Array.from(elements, (element) => {+ const rect = element.getBoundingClientRect();+ const badgeWidth = rect.width / scale;+ const badgeHeight = rect.height / scale;+ return {+ x: (rect.left - frameRect.left) / scale - gap,+ y: (rect.top - frameRect.top) / scale - gap,+ width: badgeWidth + gap * 2,+ height: badgeHeight + gap * 2,+ radius: cornerRadius(element, badgeWidth, badgeHeight) + gap,+ };+ });+ return {width, height, cutouts};+}++function sameMask(a: AvatarCutoutMask | null, b: AvatarCutoutMask | null): boolean {+ if (a == null || b == null) {+ return a === b;+ }+ return (+ a.width === b.width &&+ a.height === b.height &&+ a.cutouts.length === b.cutouts.length &&+ a.cutouts.every((cutout, index) => {+ const other = b.cutouts[index];+ return (+ other != null &&+ cutout.x === other.x &&+ cutout.y === other.y &&+ cutout.width === other.width &&+ cutout.height === other.height &&+ cutout.radius === other.radius+ );+ })+ );+}++export function useAvatarBadgeCutouts(+ frameRef: RefObject<HTMLElement | null>,+ badgeKey: string,+): AvatarCutoutMask | null {+ const [mask, setMask] = useState<AvatarCutoutMask | null>(null);+ useLayoutEffect(() => {+ const frame = frameRef.current;+ if (frame == null || badgeKey.length === 0) {+ setMask(null);+ return;+ }+ const update = () => {+ const next = measureMask(frame);+ setMask((previous) => (sameMask(previous, next) ? previous : next));+ };+ update();+ const targets = [frame, ...frame.querySelectorAll<HTMLElement>(BADGE_CUTOUT_SELECTOR)];+ const releases = targets.map((target) => observeResize(target, update));+ return () => {+ for (const release of releases) {+ release();+ }+ };+ }, [frameRef, badgeKey]);+ return mask;+}diff --git a/fluxer_app/src/features/auth/components/accounts/account_switcher_modal/AccountInstanceBadge.tsx b/fluxer_app/src/features/auth/components/accounts/account_switcher_modal/AccountInstanceBadge.tsxdeleted file mode 100644index 0a11e5297..000000000--- a/fluxer_app/src/features/auth/components/accounts/account_switcher_modal/AccountInstanceBadge.tsx+++ /dev/null@@ -1,85 +0,0 @@-// SPDX-License-Identifier: AGPL-3.0-or-later--import styles from '@app/features/auth/components/accounts/account_switcher_modal/AccountProfilePicker.module.css';-import type {AccountInstanceBadgeInfo} from '@app/features/auth/components/accounts/account_switcher_modal/useAccountInstanceBadge';-import {InstanceBrandMark} from '@app/features/auth/components/InstanceBrandMark';-import {instanceDomainHost} from '@app/features/auth/flow/instance_selector/InstanceDirectoryStorage';-import {VerifiedConnectionIcon} from '@app/features/ui/components/icons/VerifiedConnectionIcon';-import {Tooltip} from '@app/features/ui/tooltip/Tooltip';-import type React from 'react';--interface AccountInstanceBadgeProps {- readonly badge: AccountInstanceBadgeInfo | null;- readonly fallbackLabel: string;- readonly officialLabel: string;-}--export function AccountInstanceBadge({- badge,- fallbackLabel,- officialLabel,-}: AccountInstanceBadgeProps): React.ReactElement {- let domainLabel = fallbackLabel;- let isOfficial = false;- let iconUrl: string | null = null;- let instanceName: string | null = null;- if (badge != null) {- domainLabel = badge.title;- isOfficial = badge.isOfficial;- iconUrl = badge.iconUrl;- instanceName = badge.label !== instanceDomainHost(badge.title) ? badge.label : null;- }- const ariaLabels = instanceName == null ? [domainLabel] : [instanceName, domainLabel];- if (isOfficial) {- ariaLabels.push(officialLabel);- }- const renderTooltip = () => (- <span- className={styles.instanceTooltip}- data-flx="auth.accounts.account-switcher-modal.account-instance-badge.render-tooltip.instance-tooltip"- >- <span- className={styles.instanceTooltipLabel}- data-flx="auth.accounts.account-switcher-modal.account-instance-badge.render-tooltip.instance-tooltip-label"- >- {instanceName == null ? domainLabel : `${instanceName} (${domainLabel})`}- </span>- {isOfficial && (- <span- className={styles.instanceTooltipOfficial}- role="img"- aria-label={officialLabel}- data-flx="auth.accounts.account-switcher-modal.account-instance-badge.render-tooltip.instance-tooltip-official"- >- <VerifiedConnectionIcon- size={14}- data-flx="auth.accounts.account-switcher-modal.account-instance-badge.render-tooltip.verified-connection-icon"- />- </span>- )}- </span>- );- return (- <Tooltip- text={renderTooltip}- position="top"- maxWidth="none"- data-flx="auth.accounts.account-switcher-modal.account-instance-badge.tooltip"- >- <span- className={styles.instanceBadge}- role="img"- aria-label={ariaLabels.join(', ')}- data-flx="auth.accounts.account-switcher-modal.account-instance-badge.instance-badge"- >- <InstanceBrandMark- isOfficial={isOfficial}- iconUrl={iconUrl}- size={15}- globeWeight="bold"- data-flx="auth.accounts.account-switcher-modal.account-instance-badge.instance-brand-mark"- />- </span>- </Tooltip>- );-}