All files / src/utils systemListViewModels.ts

100% Statements 9/9
88.88% Branches 16/18
100% Functions 6/6
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102                              8x     8x                 17x                                             49x     49x 3x     49x                                                                   39x 39x                  
import type { ApiKey } from '@/stores/apiKeys'
import type { Role } from '@/stores/roles'
import type { FlowMember } from '@/stores/flows'
import type { SystemChipValue } from '@/types/systemField'
import { HOME_SLUG, type SpacePageSummary } from './spacePages'
 
export interface ApiKeyViewModel extends ApiKey {
  idShort: string
  roleNames: string[]
}
 
export function toApiKeyViewModel(
  apiKey: ApiKey,
  getRoleName: (roleId: string) => string
): ApiKeyViewModel {
  return {
    ...apiKey,
    idShort: apiKey.id ? `${apiKey.id.substring(0, 12)}...` : '—',
    roleNames: (apiKey.roleIds || []).map((roleId) => getRoleName(roleId))
  }
}
 
export interface RoleViewModel extends Role {
  createdByDisplay: string
}
 
export function toRoleViewModel(role: Role): RoleViewModel {
  return {
    ...role,
    createdByDisplay: role.createdByName || role.createdBy
  }
}
 
export interface MemberViewModel extends FlowMember {
  name: string
  nicknameDisplay: string
  avatarUrl: string
  roleChips: SystemChipValue[]
}
 
export interface MemberViewModelDeps {
  getRoleName: (roleId: string) => string
  getMemberAvatarUrl: (member: FlowMember) => string
  t: (key: string) => string
}
 
export function toMemberViewModel(
  member: FlowMember,
  { getRoleName, getMemberAvatarUrl, t }: MemberViewModelDeps
): MemberViewModel {
  const roleChips: SystemChipValue[] = member.isOwner
    ? [{ label: t('member.owner'), color: 'warning' }]
    : []
  roleChips.push(
    ...member.roleIds.map((roleId) => ({ label: getRoleName(roleId), color: 'primary' as const }))
  )
 
  return {
    ...member,
    name: member.name || member.userId,
    nicknameDisplay: member.nickname ? `@${member.nickname}` : '',
    avatarUrl: getMemberAvatarUrl(member),
    roleChips
  }
}
 
/** A space page's publication state, as one badge */
export type SpacePageState = 'draft' | 'published' | 'changes'
 
export interface SpacePageViewModel extends SpacePageSummary {
  /** The slug: what VirtualCardGrid keys the rows with */
  id: string
  titleDisplay: string
  /** Where visitors find the page, e.g. `/space/club/prices` */
  address: string
  isHome: boolean
  state: SpacePageState
}
 
/**
 * `titleDisplay` is the title in the language `localizeTitle` picks; a
 * published page with a newer draft is `changes`, so the list tells at a
 * glance which pages need publishing again.
 */
export function toSpacePageViewModel(
  page: SpacePageSummary,
  {
    flowSlug,
    localizeTitle
  }: { flowSlug: string; localizeTitle: (page: SpacePageSummary) => string }
): SpacePageViewModel {
  const isHome = page.slug === HOME_SLUG
  return {
    ...page,
    id: page.slug,
    titleDisplay: localizeTitle(page),
    address: `/space/${flowSlug}${isHome ? '' : `/${page.slug}`}`,
    isHome,
    state: !page.published ? 'draft' : page.hasUnpublishedChanges ? 'changes' : 'published'
  }
}