Skip to content

Commit 9bf6aff

Browse files
authored
Copy link in share dialog (#5069)
* fix server hot reload * zustand for env * link when collaborative share * revert line change * link component
1 parent 2dd1dbc commit 9bf6aff

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

utopia-remix/app/components/projectActionContextMenu.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { ContextMenu, Separator, Dialog, Flex, Text } from '@radix-ui/themes'
1818
import { DotFilledIcon } from '@radix-ui/react-icons'
1919
import { SharingDialog } from './sharingDialog'
2020
import { when } from '~/util/react-conditionals'
21+
import { useCopyProjectLinkToClipboard } from '../util/copyProjectLink'
2122

2223
type ContextMenuEntry =
2324
| {
@@ -88,6 +89,7 @@ export const ProjectActionsMenu = React.memo(
8889
)
8990

9091
const projectEditorLink = useProjectEditorLink()
92+
const copyProjectLink = useCopyProjectLinkToClipboard()
9193

9294
const menuEntries = React.useMemo((): ContextMenuEntry[] => {
9395
switch (selectedCategory) {
@@ -102,10 +104,7 @@ export const ProjectActionsMenu = React.memo(
102104
'separator',
103105
{
104106
text: 'Copy Link',
105-
onClick: (selectedProject) => {
106-
window.navigator.clipboard.writeText(projectEditorLink(selectedProject.proj_id))
107-
// TODO notification toast
108-
},
107+
onClick: (selectedProject) => copyProjectLink(selectedProject.proj_id),
109108
},
110109
{
111110
text: 'Fork',
@@ -157,6 +156,7 @@ export const ProjectActionsMenu = React.memo(
157156
destroyProject,
158157
renameProject,
159158
restoreProject,
159+
copyProjectLink,
160160
])
161161

162162
const preventDefault = React.useCallback((event: Event) => {

utopia-remix/app/components/sharingDialog.tsx

+44
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import { useFetcherWithOperation } from '../hooks/useFetcherWithOperation'
2020
import React from 'react'
2121
import { when } from '../util/react-conditionals'
2222
import moment from 'moment'
23+
import { useProjectEditorLink } from '../util/links'
24+
import { button } from '../styles/button.css'
25+
import { useCopyProjectLinkToClipboard } from '../util/copyProjectLink'
2326

2427
export function SharingDialog({
2528
project,
@@ -86,6 +89,10 @@ export function SharingDialog({
8689
changeProjectAccessLevel={changeProjectAccessLevel}
8790
/>
8891
</Flex>
92+
{when(
93+
accessLevel === AccessLevel.COLLABORATIVE || accessLevel === AccessLevel.PUBLIC,
94+
<ProjectLink projectId={project.proj_id} />,
95+
)}
8996
{when(
9097
accessRequests.length > 0,
9198
<>
@@ -200,3 +207,40 @@ function VisibilityDropdown({
200207
</DropdownMenu.Root>
201208
)
202209
}
210+
211+
const ProjectLink = React.memo(({ projectId }: { projectId: string }) => {
212+
const projectLinkRef = React.useRef<HTMLInputElement | null>(null)
213+
214+
const projectLink = useProjectEditorLink()
215+
const copyProjectLink = useCopyProjectLinkToClipboard()
216+
217+
const onClickCopyProjectLink = React.useCallback(() => {
218+
copyProjectLink(projectId)
219+
projectLinkRef.current?.select()
220+
}, [projectId, copyProjectLink])
221+
222+
return (
223+
<Flex style={{ gap: 10, alignItems: 'stretch' }}>
224+
<input
225+
ref={projectLinkRef}
226+
type='text'
227+
value={projectLink(projectId)}
228+
readOnly={true}
229+
style={{
230+
flex: 1,
231+
fontSize: 13,
232+
padding: '0px 4px',
233+
cursor: 'default',
234+
}}
235+
/>
236+
<button
237+
className={button({ color: 'subtle' })}
238+
style={{ fontSize: 13 }}
239+
onClick={onClickCopyProjectLink}
240+
>
241+
Copy
242+
</button>
243+
</Flex>
244+
)
245+
})
246+
ProjectLink.displayName = 'ProjectLink'
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react'
2+
import { useProjectEditorLink } from './links'
3+
4+
export function useCopyProjectLinkToClipboard() {
5+
const projectEditorLink = useProjectEditorLink()
6+
return React.useCallback(
7+
(projectId: string) => {
8+
window.navigator.clipboard.writeText(projectEditorLink(projectId))
9+
// TODO notification toast
10+
},
11+
[projectEditorLink],
12+
)
13+
}

0 commit comments

Comments
 (0)