Skip to content
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

[full-ci] Create space from resource #8730

Merged
merged 25 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions changelog/unreleased/enhancement-create-space-from-selection
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Enhancement: Create Space from selection

We've added a new action 'Create Space from selection' to the users
personal home so they can create a Space with the copied content of their
selected files and folders.

https://github.com/owncloud/web/pull/8730
https://github.com/owncloud/web/issues/8735
42 changes: 25 additions & 17 deletions packages/design-system/src/components/OcModal/OcModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,31 @@
<div v-if="$slots.content" key="modal-slot-content" class="oc-modal-body-message">
<slot name="content" />
</div>
<oc-text-input
v-else-if="hasInput"
key="modal-input"
ref="ocModalInput"
v-model="userInputValue"
class="oc-modal-body-input"
:error-message="inputError"
:label="inputLabel"
:type="inputType"
:description-message="inputDescription"
:disabled="inputDisabled"
:fix-message-line="true"
:selection-range="inputSelectionRange"
@update:model-value="inputOnInput"
@keydown.enter.prevent="confirm"
/>
<p v-else key="modal-message" class="oc-modal-body-message oc-m-rm" v-text="message" />
<template v-else>
<p
v-if="message"
key="modal-message"
class="oc-modal-body-message oc-mt-rm"
:class="{ 'oc-mb-rm': !hasInput }"
v-text="message"
/>
<oc-text-input
v-if="hasInput"
key="modal-input"
ref="ocModalInput"
v-model="userInputValue"
class="oc-modal-body-input"
:error-message="inputError"
:label="inputLabel"
:type="inputType"
:description-message="inputDescription"
:disabled="inputDisabled"
:fix-message-line="true"
:selection-range="inputSelectionRange"
@update:model-value="inputOnInput"
@keydown.enter.prevent="confirm"
/>
</template>
<div v-if="checkboxLabel" class="oc-modal-body-actions oc-flex oc-flex-left">
<oc-checkbox
v-model="checkboxValue"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exports[`OcModal displays input 1`] = `
<h2 id="oc-modal-title">Create new folder</h2>
</div>
<div class="oc-modal-body">
<!--v-if-->
<oc-text-input-stub class="oc-modal-body-input" clearbuttonaccessiblelabel="" clearbuttonenabled="false" disabled="false" fixmessageline="true" id="oc-textinput-1" label="Folder name" modelvalue="New folder" type="text"></oc-text-input-stub>
<!--v-if-->
<!--v-if-->
Expand All @@ -32,7 +33,8 @@ exports[`OcModal hides icon if not specified 1`] = `
<h2 id="oc-modal-title">Example title</h2>
</div>
<div class="oc-modal-body">
<p class="oc-modal-body-message oc-m-rm">Example message</p>
<p class="oc-modal-body-message oc-mt-rm oc-mb-rm">Example message</p>
<!--v-if-->
<!--v-if-->
<!--v-if-->
</div>
Expand All @@ -55,7 +57,8 @@ exports[`OcModal matches snapshot 1`] = `
<h2 id="oc-modal-title">Example title</h2>
</div>
<div class="oc-modal-body">
<p class="oc-modal-body-message oc-m-rm">Example message</p>
<p class="oc-modal-body-message oc-mt-rm oc-mb-rm">Example message</p>
<!--v-if-->
<!--v-if-->
<!--v-if-->
</div>
Expand Down
69 changes: 9 additions & 60 deletions packages/web-app-files/src/components/AppBar/CreateSpace.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@

<script lang="ts">
import { mapActions, mapMutations } from 'vuex'

import { defineComponent } from 'vue'
import { buildSpace } from 'web-client/src/helpers'
import { configurationManager } from 'web-pkg/src/configuration'
import { WebDAV } from 'web-client/src/webdav'
import { useClientService } from 'web-pkg/src/composables'
import { Drive } from 'web-client/src/generated'
import { useCreateSpace } from 'web-app-files/src/composables'
import { useSpaceHelpers } from 'web-pkg/src/composables/spaces'

export default defineComponent({
setup() {
const clientService = useClientService()
return { clientService }
const { createSpace } = useCreateSpace()
const { checkSpaceNameModalInput } = useSpaceHelpers()
return { clientService, createSpace, checkSpaceNameModalInput }
},
methods: {
...mapActions(['showMessage', 'createModal', 'hideModal', 'setModalInputErrorMessage']),
Expand All @@ -44,68 +43,18 @@ export default defineComponent({
inputValue: this.$gettext('New space'),
onCancel: this.hideModal,
onConfirm: this.addNewSpace,
onInput: this.checkSpaceName
onInput: this.checkSpaceNameModalInput
}

this.createModal(modal)
},

checkSpaceName(name) {
if (name.trim() === '') {
return this.setModalInputErrorMessage(this.$gettext('Space name cannot be empty'))
}
if (name.length > 255) {
return this.setModalInputErrorMessage(
this.$gettext('Space name cannot exceed 255 characters')
)
}
if (/[/\\.:?*"><|]/.test(name)) {
return this.setModalInputErrorMessage(
this.$gettext('Space name cannot contain the following characters: / \\ . : ? * " > < |')
)
}
return this.setModalInputErrorMessage(null)
},

async addNewSpace(name) {
try {
const client = this.clientService.graphAuthenticated
const { data: space } = await client.drives.createDrive({ name }, {})
const createdSpace = await this.createSpace(name)
this.hideModal()
const resource = buildSpace({ ...space, serverUrl: configurationManager.serverUrl })
this.UPSERT_RESOURCE(resource)
this.UPSERT_SPACE(resource)

await (this.$clientService.webdav as WebDAV).createFolder(resource, { path: '.space' })
const markdown = await (this.$clientService.webdav as WebDAV).putFileContents(resource, {
path: '.space/readme.md',
content: this.$gettext('Here you can add a description for this Space.')
})

const { data: updatedSpace } = await client.drives.updateDrive(
space.id,
{
special: [
{
specialFolder: {
name: 'readme'
},
id: markdown.id as string
}
]
} as Drive,
{}
)
this.UPDATE_RESOURCE_FIELD({
id: space.id,
field: 'spaceReadmeData',
value: updatedSpace.special.find((special) => special.specialFolder.name === 'readme')
})
this.UPDATE_RESOURCE_FIELD({
id: space.id,
field: 'spaceQuota',
value: updatedSpace.quota
})
this.UPSERT_RESOURCE(createdSpace)
this.UPSERT_SPACE(createdSpace)
} catch (error) {
console.error(error)
this.showMessage({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
useFileActionsSetImage,
useFileActionsShowEditTags,
useFileActionsNavigate,
useFileActionsFavorite
useFileActionsFavorite,
useFileActionsCreateSpaceFromResource
} from '../../composables/actions/files'
import { FileActionOptions } from 'web-pkg/src/composables/actions'

Expand Down Expand Up @@ -66,6 +67,9 @@ export default defineComponent({
const { actions: setSpaceReadmeActions } = useFileActionsSetReadme({ store })
const { actions: showDetailsActions } = useFileActionsShowDetails({ store })
const { actions: showEditTagsActions } = useFileActionsShowEditTags({ store })
const { actions: createSpaceFromResourceActions } = useFileActionsCreateSpaceFromResource({
store
})
const { actions: showSharesActions } = useFileActionsShowShares({ store })

// type cast to make vue-tsc aware of the type
Expand All @@ -79,7 +83,8 @@ export default defineComponent({
...unref(copyActions),
...unref(emptyTrashBinActions),
...unref(deleteActions),
...unref(restoreActions)
...unref(restoreActions),
...unref(createSpaceFromResourceActions)
].filter((item) => item.isEnabled(unref(actionOptions)))
)

Expand Down Expand Up @@ -109,6 +114,7 @@ export default defineComponent({
...unref(copyActions),
...unref(pasteActions),
...unref(renameActions),
...unref(createSpaceFromResourceActions),
...unref(showEditTagsActions),
...unref(restoreActions),
...unref(acceptShareActions),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './useFileActionsShowActions'
export * from './useFileActionsShowDetails'
export * from './useFileActionsShowEditTags'
export * from './useFileActionsShowShares'
export * from './useFileActionsCreateSpaceFromResource'
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import {
useFileActionsNavigate,
useFileActionsRename,
useFileActionsRestore,
useFileActionsShowEditTags
useFileActionsShowEditTags,
useFileActionsCreateSpaceFromResource
} from './index'

export const EDITOR_MODE_EDIT = 'edit'
Expand Down Expand Up @@ -55,6 +56,7 @@ export const useFileActions = ({ store }: { store?: Store<any> } = {}) => {
const { actions: renameActions } = useFileActionsRename({ store })
const { actions: restoreActions } = useFileActionsRestore({ store })
const { actions: showEditTagsActions } = useFileActionsShowEditTags({ store })
const { actions: createSpaceFromResource } = useFileActionsCreateSpaceFromResource({ store })

const systemActions = computed((): Action[] => [
...unref(downloadArchiveActions),
Expand All @@ -63,6 +65,7 @@ export const useFileActions = ({ store }: { store?: Store<any> } = {}) => {
...unref(moveActions),
...unref(copyActions),
...unref(renameActions),
...unref(createSpaceFromResource),
...unref(showEditTagsActions),
...unref(restoreActions),
...unref(acceptShareActions),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { Store } from 'vuex'
import { computed, unref } from 'vue'
import { useGettext } from 'vue3-gettext'
import { FileAction, FileActionOptions } from 'web-pkg/src/composables/actions'
import { useAbility, useClientService, useLoadingService, useRouter } from 'web-pkg/src/composables'
import { isPersonalSpaceResource } from 'web-client/src/helpers'
import { isLocationSpacesActive } from 'web-app-files/src/router'
import { useCreateSpace } from 'web-app-files/src/composables/spaces'
import { useSpaceHelpers } from 'web-pkg/src/composables/spaces'
import PQueue from 'p-queue'

export const useFileActionsCreateSpaceFromResource = ({ store }: { store?: Store<any> } = {}) => {
const { can } = useAbility()
const { $gettext, $ngettext } = useGettext()
const loadingService = useLoadingService()
const { createSpace } = useCreateSpace()
const { checkSpaceNameModalInput } = useSpaceHelpers()
const { webdav } = useClientService()
const router = useRouter()
const hasCreatePermission = computed(() => can('create-all', 'Space'))

const confirmAction = async ({ spaceName, resources, space }) => {
store.dispatch('hideModal')
const queue = new PQueue({ concurrency: 4 })
const copyOps = []

try {
const createdSpace = await createSpace(spaceName)
store.commit('runtime/spaces/UPSERT_SPACE', createdSpace)

if (resources.length === 1 && resources[0].isFolder) {
//If a single folder is selected we copy it's content to the Space's root folder
resources = (await webdav.listFiles(space, { path: resources[0].path })).children
}

for (const resource of resources) {
copyOps.push(
queue.add(() => webdav.copyFiles(space, resource, createdSpace, { path: resource.name }))
)
}

await Promise.all(copyOps)
store.dispatch('Files/resetFileSelection')
store.dispatch('showMessage', {
title: $gettext('Space was created successfully')
})
} catch (error) {
console.error(error)
store.dispatch('showMessage', {
title: $gettext('Creating space failed…'),
status: 'danger'
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, what if the space was created partially?
Like 3 out of 5 files were copied: do we want to keep the broken space or would it potentially make sense to delete it?
I would really like to avoid that users somehow miss the error message, see the space was created and then delete the source files because they think their data is in the space now.

@tbsbdr what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created #8775 to follow up on this (very valid!) point. However, IMO that's not a merge blocker for now as we're heading towards the finish line for this sprint.

}
}
const handler = ({ resources, space }: FileActionOptions) => {
const modal = {
variation: 'passive',
title: $ngettext(
'Create Space from "%{resourceName}"',
'Create Space from selection',
resources.length,
{
resourceName: resources[0].name
}
),
message: $ngettext(
'Create Space with the content of "%{resourceName}". The content will be copied.',
'Create Space with the selected files. The content will be copied.',
resources.length,
{
resourceName: resources[0].name
}
),
cancelText: $gettext('Cancel'),
confirmText: $gettext('Create'),
hasInput: true,
inputLabel: $gettext('Space name'),
onInput: checkSpaceNameModalInput,
onCancel: () => store.dispatch('hideModal'),
onConfirm: (spaceName) =>
loadingService.addTask(() => confirmAction({ spaceName, space, resources }))
}

store.dispatch('createModal', modal)
}

const actions = computed((): FileAction[] => {
return [
{
name: 'create-space-from-resource',
icon: 'layout-grid',
handler,
label: () => {
return $gettext('Create Space from selection')
},
isEnabled: ({ resources, space }) => {
if (!resources.length) {
return false
}

if (!unref(hasCreatePermission)) {
return false
}

if (
!isLocationSpacesActive(router, 'files-spaces-generic') ||
!isPersonalSpaceResource(space)
) {
return false
}

return true
},
componentType: 'button',
class: 'oc-files-actions-create-space-from-resource-trigger'
}
]
})

return {
actions
}
}
1 change: 1 addition & 0 deletions packages/web-app-files/src/composables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export * from './resourcesViewDefaults'
export * from './router'
export * from './selection'
export * from './shares'
export * from './spaces'
export * from './sort'
export * from './viewMode'
1 change: 1 addition & 0 deletions packages/web-app-files/src/composables/spaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useCreateSpace'
Loading