-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* context menu * remove unused * use radix * update imports * use button style * remove old commented out * fix colors
- Loading branch information
Showing
9 changed files
with
852 additions
and
55 deletions.
There are no files selected for viewing
163 changes: 163 additions & 0 deletions
163
utopia-remix/app/components/projectActionContextMenu.tsx
This file contains 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,163 @@ | ||
import * as DropdownMenu from '@radix-ui/react-dropdown-menu' | ||
|
||
import { useFetcher } from '@remix-run/react' | ||
import React from 'react' | ||
import { Category } from '../routes/projects' | ||
import { ProjectWithoutContent } from '../types' | ||
import { assertNever } from '../util/assertNever' | ||
import { projectEditorLink } from '../util/links' | ||
import { contextMenuItem } from '../styles/contextMenuItem.css' | ||
import { colors } from '../styles/sprinkles.css' | ||
|
||
type ContextMenuEntry = | ||
| { | ||
text: string | ||
onClick: (project: ProjectWithoutContent) => void | ||
} | ||
| 'separator' | ||
|
||
export const ProjectContextMenu = React.memo( | ||
({ | ||
selectedCategory, | ||
project, | ||
}: { | ||
selectedCategory: Category | ||
project: ProjectWithoutContent | ||
}) => { | ||
const fetcher = useFetcher() | ||
|
||
const deleteProject = React.useCallback( | ||
(projectId: string) => { | ||
fetcher.submit({}, { method: 'POST', action: `/internal/projects/${projectId}/delete` }) | ||
}, | ||
[fetcher], | ||
) | ||
|
||
const destroyProject = React.useCallback( | ||
(projectId: string) => { | ||
const ok = window.confirm('Are you sure? The project contents will be deleted permanently.') | ||
if (ok) { | ||
fetcher.submit({}, { method: 'POST', action: `/internal/projects/${projectId}/destroy` }) | ||
} | ||
}, | ||
[fetcher], | ||
) | ||
|
||
const restoreProject = React.useCallback( | ||
(projectId: string) => { | ||
fetcher.submit({}, { method: 'POST', action: `/internal/projects/${projectId}/restore` }) | ||
}, | ||
[fetcher], | ||
) | ||
|
||
const renameProject = React.useCallback( | ||
(projectId: string, newTitle: string) => { | ||
fetcher.submit( | ||
{ title: newTitle }, | ||
{ method: 'POST', action: `/internal/projects/${projectId}/rename` }, | ||
) | ||
}, | ||
[fetcher], | ||
) | ||
|
||
const menuEntries = React.useMemo((): ContextMenuEntry[] => { | ||
switch (selectedCategory) { | ||
case 'allProjects': | ||
return [ | ||
{ | ||
text: 'Open', | ||
onClick: (project) => { | ||
window.open(projectEditorLink(project.proj_id), '_blank') | ||
}, | ||
}, | ||
'separator', | ||
{ | ||
text: 'Copy Link', | ||
onClick: (project) => { | ||
navigator.clipboard.writeText(projectEditorLink(project.proj_id)) | ||
// TODO notification toast | ||
}, | ||
}, | ||
'separator', | ||
{ | ||
text: 'Rename', | ||
onClick: (project) => { | ||
const newTitle = window.prompt('New title:', project.title) | ||
if (newTitle != null) { | ||
renameProject(project.proj_id, newTitle) | ||
} | ||
}, | ||
}, | ||
{ | ||
text: 'Delete', | ||
onClick: (project) => { | ||
deleteProject(project.proj_id) | ||
}, | ||
}, | ||
] | ||
case 'trash': | ||
return [ | ||
{ | ||
text: 'Restore', | ||
onClick: (project) => { | ||
restoreProject(project.proj_id) | ||
}, | ||
}, | ||
'separator', | ||
{ | ||
text: 'Delete permanently', | ||
onClick: (project) => { | ||
destroyProject(project.proj_id) | ||
}, | ||
}, | ||
] | ||
default: | ||
assertNever(selectedCategory) | ||
} | ||
}, [selectedCategory]) | ||
|
||
return ( | ||
<> | ||
<DropdownMenu.Portal> | ||
<DropdownMenu.Content | ||
style={{ | ||
background: 'white', | ||
padding: 4, | ||
boxShadow: '2px 3px 4px #dddddd', | ||
border: '1px solid #ccc', | ||
borderRadius: 4, | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: 4, | ||
minWidth: 100, | ||
}} | ||
sideOffset={5} | ||
> | ||
{menuEntries.map((entry, index) => { | ||
if (entry === 'separator') { | ||
return ( | ||
<DropdownMenu.Separator | ||
key={`separator-${index}`} | ||
style={{ backgroundColor: colors.separator, height: 1 }} | ||
/> | ||
) | ||
} | ||
return ( | ||
<DropdownMenu.Item | ||
key={`entry-${index}`} | ||
onClick={() => entry.onClick(project)} | ||
className={contextMenuItem()} | ||
> | ||
{entry.text} | ||
</DropdownMenu.Item> | ||
) | ||
})} | ||
</DropdownMenu.Content> | ||
</DropdownMenu.Portal> | ||
|
||
<fetcher.Form /> | ||
</> | ||
) | ||
}, | ||
) | ||
ProjectContextMenu.displayName = 'ProjectContextMenu' |
This file contains 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,4 @@ | ||
body { | ||
border: 1px solid transparent; /* Fix jumpy content when opening dropdowns */ | ||
} | ||
|
This file contains 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 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 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,21 @@ | ||
import { recipe } from '@vanilla-extract/recipes' | ||
import { colors, sprinkles } from './sprinkles.css' | ||
|
||
export const contextMenuItem = recipe({ | ||
base: [ | ||
sprinkles({ | ||
borderRadius: 'small', | ||
color: 'lightModeBlack', | ||
}), | ||
{ | ||
outline: 'none', | ||
padding: '6px 8px', | ||
cursor: 'pointer', | ||
border: 'none !important', | ||
':hover': { | ||
backgroundColor: colors.primary, | ||
color: 'white', | ||
}, | ||
}, | ||
], | ||
}) |
This file contains 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 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,9 @@ | ||
import urlJoin from 'url-join' | ||
|
||
export function projectEditorLink(projectId: string | null): string { | ||
const editorURL = window.ENV.EDITOR_URL | ||
if (editorURL == null) { | ||
throw new Error('missing editor url') | ||
} | ||
return urlJoin(editorURL, 'project', projectId ?? '') | ||
} |
This file contains 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.