|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { type ReactNode, useState } from 'react' |
| 4 | +import { Chip, DropdownMenuItem, toast, useCopyToClipboard } from '@sim/emcn' |
| 5 | +import { Check, Link, Plus } from '@sim/emcn/icons' |
| 6 | +import { simLinkUrl } from '@/lib/navigation/paths' |
| 7 | +import { suggestSimLinkName } from '@/lib/sim-links/validation' |
| 8 | +import { SimLinkModal } from '@/app/o/[organizationId]/links/sim-link-modal' |
| 9 | +import { useOptionalOrganizationContext } from '@/app/o/[organizationId]/providers/organization-provider' |
| 10 | +import { useOptionalFeatureFlag } from '@/app/workspace/[workspaceId]/providers/feature-flags-provider' |
| 11 | +import { useOptionalWorkspaceHostContext } from '@/app/workspace/[workspaceId]/providers/workspace-host-provider' |
| 12 | +import { useSimLinksForUrl } from '@/hooks/queries/sim-links' |
| 13 | + |
| 14 | +/** |
| 15 | + * The organization a Sim link made on this surface belongs to: the organization being viewed, or |
| 16 | + * the one that owns the current workspace. Null where Sim links are off, so the action is hidden. |
| 17 | + */ |
| 18 | +function useSimLinkOrganizationId(): string | null { |
| 19 | + const enabled = useOptionalFeatureFlag('sim-links') |
| 20 | + const organization = useOptionalOrganizationContext() |
| 21 | + const host = useOptionalWorkspaceHostContext() |
| 22 | + if (!enabled) return null |
| 23 | + return organization?.organization.id ?? host?.hostOrganizationId ?? null |
| 24 | +} |
| 25 | + |
| 26 | +interface SimLinkCreator { |
| 27 | + /** Null where Sim links are off; render no shortcut then. */ |
| 28 | + organizationId: string | null |
| 29 | + openCreate: () => void |
| 30 | + /** The new-link form while it is open; render it outside any menu, which unmounts on close. */ |
| 31 | + modal: ReactNode |
| 32 | +} |
| 33 | + |
| 34 | +/** Opens the new-link form for `url`, prefilled with a name suggested from `title`. */ |
| 35 | +export function useSimLinkCreator(url: string, title: string): SimLinkCreator { |
| 36 | + const organizationId = useSimLinkOrganizationId() |
| 37 | + const [open, setOpen] = useState(false) |
| 38 | + return { |
| 39 | + organizationId, |
| 40 | + openCreate: () => setOpen(true), |
| 41 | + modal: |
| 42 | + open && organizationId ? ( |
| 43 | + <SimLinkModal |
| 44 | + organizationId={organizationId} |
| 45 | + initialUrl={url} |
| 46 | + initialName={suggestSimLinkName(title)} |
| 47 | + onClose={() => setOpen(false)} |
| 48 | + /> |
| 49 | + ) : null, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/** The first link that already opens `url`, and a copy of its address. */ |
| 54 | +function useExistingSimLink(organizationId: string, url: string) { |
| 55 | + const { data, isPending } = useSimLinksForUrl(organizationId, url) |
| 56 | + const { copied, copy } = useCopyToClipboard({ resetMs: 1500 }) |
| 57 | + const existing = data?.at(0) |
| 58 | + const copyExisting = async () => { |
| 59 | + if (!existing) return false |
| 60 | + const done = await copy(simLinkUrl(window.location.origin, existing.name)) |
| 61 | + if (!done) toast.error('Unable to copy Sim link') |
| 62 | + return done |
| 63 | + } |
| 64 | + return { existing, isPending, copied, copyExisting } |
| 65 | +} |
| 66 | + |
| 67 | +interface SimLinkShortcutProps { |
| 68 | + organizationId: string |
| 69 | + url: string |
| 70 | + onCreate: () => void |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * The Sim link entry of an actions menu: copies the link that already opens this address, so a |
| 75 | + * menu never offers a duplicate, or offers to create one. It looks up when the menu opens. |
| 76 | + */ |
| 77 | +export function SimLinkMenuItem({ organizationId, url, onCreate }: SimLinkShortcutProps) { |
| 78 | + const { existing, isPending, copyExisting } = useExistingSimLink(organizationId, url) |
| 79 | + if (existing) { |
| 80 | + return ( |
| 81 | + <DropdownMenuItem |
| 82 | + onSelect={async () => { |
| 83 | + if (await copyExisting()) toast.success(`Copied sim/${existing.name}`) |
| 84 | + }} |
| 85 | + > |
| 86 | + <Link className='size-[14px]' /> |
| 87 | + Copy sim/{existing.name} |
| 88 | + </DropdownMenuItem> |
| 89 | + ) |
| 90 | + } |
| 91 | + return ( |
| 92 | + <DropdownMenuItem disabled={isPending} onSelect={onCreate}> |
| 93 | + <Plus className='size-[14px]' /> |
| 94 | + Create Sim link |
| 95 | + </DropdownMenuItem> |
| 96 | + ) |
| 97 | +} |
| 98 | + |
| 99 | +function ShortcutChip({ organizationId, url, onCreate }: SimLinkShortcutProps) { |
| 100 | + const { existing, isPending, copied, copyExisting } = useExistingSimLink(organizationId, url) |
| 101 | + if (existing) { |
| 102 | + return ( |
| 103 | + <Chip leftIcon={copied ? Check : Link} onClick={() => void copyExisting()}> |
| 104 | + {copied ? 'Copied!' : `Copy sim/${existing.name}`} |
| 105 | + </Chip> |
| 106 | + ) |
| 107 | + } |
| 108 | + return ( |
| 109 | + <Chip leftIcon={Plus} disabled={isPending} onClick={onCreate}> |
| 110 | + Create Sim link |
| 111 | + </Chip> |
| 112 | + ) |
| 113 | +} |
| 114 | + |
| 115 | +interface SimLinkChipProps { |
| 116 | + url: string |
| 117 | + /** The title a new link's name is suggested from. */ |
| 118 | + title: string |
| 119 | +} |
| 120 | + |
| 121 | +/** The same choice as a footer chip, for a dialog that already shows the address it shortens. */ |
| 122 | +export function SimLinkChip({ url, title }: SimLinkChipProps) { |
| 123 | + const { organizationId, openCreate, modal } = useSimLinkCreator(url, title) |
| 124 | + if (!organizationId) return null |
| 125 | + return ( |
| 126 | + <> |
| 127 | + <ShortcutChip organizationId={organizationId} url={url} onCreate={openCreate} /> |
| 128 | + {modal} |
| 129 | + </> |
| 130 | + ) |
| 131 | +} |
0 commit comments