-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathActions.vue
176 lines (162 loc) · 4.52 KB
/
Actions.vue
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!-- eslint-disable vuejs-accessibility/mouse-events-have-key-events -->
<template>
<div>
<LayoutMenu
v-model:open="showActionsMenu"
:menu-id="menuId"
:items="actionsItems"
@click.stop.prevent
@chosen="onActionChosen"
>
<FormButton size="sm" text @click="onButtonClick">
<EllipsisVerticalIcon class="w-4 h-4" />
</FormButton>
</LayoutMenu>
<ProjectPageModelsCardEditDialog
v-model:open="isRenameDialogOpen"
:model="model"
:project-id="project.id"
@updated="$emit('model-updated')"
/>
<ProjectPageModelsCardDeleteDialog
v-model:open="isDeleteDialogOpen"
:model="model"
:project-id="project.id"
@deleted="$emit('model-updated')"
/>
<ProjectModelPageDialogEmbed
v-model:open="embedDialogOpen"
:project="project"
:model-id="model.id"
/>
</div>
</template>
<script setup lang="ts">
import type { Nullable } from '@speckle/shared'
import type {
ProjectPageModelsActionsFragment,
ProjectPageModelsActions_ProjectFragment
} from '~~/lib/common/generated/gql/graphql'
import type { LayoutMenuItem } from '~~/lib/layout/helpers/components'
import { useCopyModelLink } from '~~/lib/projects/composables/modelManagement'
import { EllipsisVerticalIcon } from '@heroicons/vue/24/solid'
import {
TrashIcon,
PencilIcon,
LinkIcon,
FingerPrintIcon,
ArrowUpTrayIcon,
CodeBracketIcon
} from '@heroicons/vue/24/outline'
import { graphql } from '~~/lib/common/generated/gql'
import { useMixpanel } from '~~/lib/core/composables/mp'
graphql(`
fragment ProjectPageModelsActions on Model {
id
name
}
`)
graphql(`
fragment ProjectPageModelsActions_Project on Project {
id
...ProjectsModelPageEmbed_Project
}
`)
enum ActionTypes {
Rename = 'rename',
Delete = 'delete',
Share = 'share',
UploadVersion = 'upload-version',
CopyId = 'copy-id',
Embed = 'embed'
}
const emit = defineEmits<{
(e: 'update:open', v: boolean): void
(e: 'model-updated'): void
(e: 'upload-version'): void
(e: 'embed'): void
}>()
const props = defineProps<{
open?: boolean
model: ProjectPageModelsActionsFragment
project: ProjectPageModelsActions_ProjectFragment
canEdit?: boolean
}>()
const copyModelLink = useCopyModelLink()
const { copy } = useClipboard()
const menuId = useId()
const showActionsMenu = ref(false)
const openDialog = ref(null as Nullable<ActionTypes>)
const embedDialogOpen = ref(false)
const isMain = computed(() => props.model.name === 'main')
const actionsItems = computed<LayoutMenuItem[][]>(() => [
[
{
title: 'Edit',
id: ActionTypes.Rename,
disabled: !props.canEdit,
icon: PencilIcon
},
{
title: 'Upload new version',
id: ActionTypes.UploadVersion,
disabled: !props.canEdit,
icon: ArrowUpTrayIcon
}
],
[
{ title: 'Copy Link', id: ActionTypes.Share, icon: LinkIcon },
{ title: 'Copy ID', id: ActionTypes.CopyId, icon: FingerPrintIcon },
{ title: 'Embed Model', id: ActionTypes.Embed, icon: CodeBracketIcon }
],
[
{
title: 'Delete',
id: ActionTypes.Delete,
disabled: isMain.value || !props.canEdit,
icon: TrashIcon,
color: 'danger'
}
]
])
const isRenameDialogOpen = computed({
get: () => openDialog.value === ActionTypes.Rename,
set: (isOpen) => (openDialog.value = isOpen ? ActionTypes.Rename : null)
})
const isDeleteDialogOpen = computed({
get: () => openDialog.value === ActionTypes.Delete,
set: (isOpen) => (openDialog.value = isOpen ? ActionTypes.Delete : null)
})
const mp = useMixpanel()
const onActionChosen = (params: { item: LayoutMenuItem; event: MouseEvent }) => {
const { item } = params
switch (item.id) {
case ActionTypes.Rename:
case ActionTypes.Delete:
openDialog.value = item.id
break
case ActionTypes.Share:
mp.track('Branch Action', { type: 'action', name: 'share' })
copyModelLink(props.project.id, props.model.id)
break
case ActionTypes.UploadVersion:
emit('upload-version')
break
case ActionTypes.CopyId:
copy(props.model.id, { successMessage: 'Copied model ID to clipboard' })
break
case ActionTypes.Embed:
embedDialogOpen.value = true
break
}
}
const onButtonClick = () => {
showActionsMenu.value = !showActionsMenu.value
}
// doing it this way with 2 watchers so that using the 'open' prop is optional
watch(showActionsMenu, (newVal) => emit('update:open', newVal))
watch(
() => props.open || false,
(newVal) => (showActionsMenu.value = newVal)
)
</script>