Repository navigation
feat(sim-links): create or copy a Sim link from search results and file shares #9648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
apps/sim/app/o/[organizationId]/links/sim-link-organization-provider.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| 'use client' | ||
|
|
||
| import { createContext, type ReactNode, useContext } from 'react' | ||
|
|
||
| const SimLinkOrganizationContext = createContext<string | null>(null) | ||
|
|
||
| interface SimLinkOrganizationProviderProps { | ||
| /** Where `sim/<name>` resolves for this session, or null when it resolves nowhere. */ | ||
| organizationId: string | null | ||
| children: ReactNode | ||
| } | ||
|
|
||
| /** Scopes Sim link shortcuts to the organization whose links `sim/` opens for the viewer. */ | ||
| export function SimLinkOrganizationProvider({ | ||
| organizationId, | ||
| children, | ||
| }: SimLinkOrganizationProviderProps) { | ||
| return ( | ||
| <SimLinkOrganizationContext.Provider value={organizationId}> | ||
| {children} | ||
| </SimLinkOrganizationContext.Provider> | ||
| ) | ||
| } | ||
|
|
||
| /** The organization a new or copied Sim link belongs to; null hides every Sim link shortcut. */ | ||
| export function useSimLinkOrganizationId(): string | null { | ||
| return useContext(SimLinkOrganizationContext) | ||
| } |
123 changes: 123 additions & 0 deletions
123
apps/sim/app/o/[organizationId]/links/sim-link-shortcut.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| 'use client' | ||
|
|
||
| import { type ReactNode, useState } from 'react' | ||
| import { Chip, DropdownMenuItem, toast, useCopyToClipboard } from '@sim/emcn' | ||
| import { Check, Link, Plus } from '@sim/emcn/icons' | ||
| import { simLinkUrl } from '@/lib/navigation/paths' | ||
| import { suggestSimLinkName } from '@/lib/sim-links/validation' | ||
| import { SimLinkModal } from '@/app/o/[organizationId]/links/sim-link-modal' | ||
| import { useSimLinkOrganizationId } from '@/app/o/[organizationId]/links/sim-link-organization-provider' | ||
| import { useSimLinksForUrl } from '@/hooks/queries/sim-links' | ||
|
|
||
| interface SimLinkCreator { | ||
| /** Null where Sim links are off; render no shortcut then. */ | ||
| organizationId: string | null | ||
| openCreate: () => void | ||
| /** The new-link form while it is open; render it outside any menu, which unmounts on close. */ | ||
| modal: ReactNode | ||
| } | ||
|
|
||
| /** Opens the new-link form for `url`, prefilled with a name suggested from `title`. */ | ||
| export function useSimLinkCreator(url: string, title: string): SimLinkCreator { | ||
| const organizationId = useSimLinkOrganizationId() | ||
| const [open, setOpen] = useState(false) | ||
| return { | ||
| organizationId, | ||
| openCreate: () => setOpen(true), | ||
| modal: | ||
| open && organizationId ? ( | ||
| <SimLinkModal | ||
| organizationId={organizationId} | ||
| initialUrl={url} | ||
| initialName={suggestSimLinkName(title)} | ||
| onClose={() => setOpen(false)} | ||
| /> | ||
| ) : null, | ||
| } | ||
| } | ||
|
|
||
| /** The first link that already opens `url`, and a copy of its address. */ | ||
| function useExistingSimLink(organizationId: string, url: string) { | ||
| const { data, isPending, isError } = useSimLinksForUrl(organizationId, url) | ||
| const { copied, copy } = useCopyToClipboard({ resetMs: 1500 }) | ||
| const existing = data?.at(0) | ||
| const copyExisting = async () => { | ||
| if (!existing) return false | ||
| const done = await copy(simLinkUrl(window.location.origin, existing.name)) | ||
| if (!done) toast.error('Unable to copy Sim link') | ||
| return done | ||
| } | ||
| return { existing, isPending, isError, copied, copyExisting } | ||
| } | ||
|
|
||
| interface SimLinkShortcutProps { | ||
| organizationId: string | ||
| url: string | ||
| onCreate: () => void | ||
| } | ||
|
|
||
| /** | ||
| * The Sim link entry of an actions menu: copies the link that already opens this address, so a | ||
| * menu never offers a duplicate, or offers to create one. It looks up when the menu opens, and | ||
| * offers nothing when the lookup fails rather than risk a duplicate. | ||
| */ | ||
| export function SimLinkMenuItem({ organizationId, url, onCreate }: SimLinkShortcutProps) { | ||
| const { existing, isPending, isError, copyExisting } = useExistingSimLink(organizationId, url) | ||
| if (isError) return null | ||
| if (existing) { | ||
| return ( | ||
| <DropdownMenuItem | ||
| onSelect={async () => { | ||
| if (await copyExisting()) toast.success(`Copied sim/${existing.name}`) | ||
| }} | ||
| > | ||
| <Link className='size-[14px]' /> | ||
| Copy sim/{existing.name} | ||
| </DropdownMenuItem> | ||
| ) | ||
| } | ||
| return ( | ||
| <DropdownMenuItem disabled={isPending} onSelect={onCreate}> | ||
| <Plus className='size-[14px]' /> | ||
| Create Sim link | ||
| </DropdownMenuItem> | ||
| ) | ||
| } | ||
|
|
||
| function ShortcutChip({ organizationId, url, onCreate }: SimLinkShortcutProps) { | ||
| const { existing, isPending, isError, copied, copyExisting } = useExistingSimLink( | ||
| organizationId, | ||
| url | ||
| ) | ||
| if (isError) return null | ||
| if (existing) { | ||
| return ( | ||
| <Chip leftIcon={copied ? Check : Link} onClick={() => void copyExisting()}> | ||
| {copied ? 'Copied!' : `Copy sim/${existing.name}`} | ||
| </Chip> | ||
| ) | ||
| } | ||
| return ( | ||
| <Chip leftIcon={Plus} disabled={isPending} onClick={onCreate}> | ||
| Create Sim link | ||
| </Chip> | ||
| ) | ||
| } | ||
|
|
||
| interface SimLinkChipProps { | ||
| url: string | ||
| /** The title a new link's name is suggested from. */ | ||
| title: string | ||
| } | ||
|
|
||
| /** The same choice as a footer chip, for a dialog that already shows the address it shortens. */ | ||
| export function SimLinkChip({ url, title }: SimLinkChipProps) { | ||
| const { organizationId, openCreate, modal } = useSimLinkCreator(url, title) | ||
| if (!organizationId) return null | ||
| return ( | ||
| <> | ||
| <ShortcutChip organizationId={organizationId} url={url} onCreate={openCreate} /> | ||
| {modal} | ||
| </> | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.