Skip to content

Commit

Permalink
refactor: move quick actions into injection context
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed Dec 1, 2023
1 parent 340ea48 commit 210db22
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 132 deletions.
45 changes: 16 additions & 29 deletions packages/web-app-files/src/components/FilesList/QuickActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
<div v-if="!isEmbedModeEnabled" class="oc-flex">
<oc-button
v-for="action in filteredActions"
:key="action.label($gettext)"
v-oc-tooltip="action.label($gettext)"
:aria-label="action.label($gettext)"
:key="action.label()"
v-oc-tooltip="action.label()"
:aria-label="action.label()"
appearance="raw"
class="oc-mr-xs quick-action-button oc-p-xs"
:class="`files-quick-action-${action.id}`"
@click="
action.handler({ ability, clientService, passwordPolicyService, item, language, store })
"
@click="action.handler({ space, resources: [item] })"
>
<oc-icon :name="action.icon" fill-type="line" />
</oc-button>
Expand All @@ -19,47 +17,36 @@

<script lang="ts">
import pickBy from 'lodash-es/pickBy'
import { computed, defineComponent } from 'vue'
import {
useAbility,
useClientService,
useEmbedMode,
usePasswordPolicyService,
useStore
} from '@ownclouders/web-pkg'
import { useGettext } from 'vue3-gettext'
import { computed, defineComponent, PropType } from 'vue'
import { FileAction, useEmbedMode } from '@ownclouders/web-pkg'
import { Resource, SpaceResource } from '@ownclouders/web-client'
export default defineComponent({
name: 'QuickActions',
props: {
actions: {
type: Object,
type: Object as PropType<FileAction[]>,
required: true
},
item: {
type: Object,
type: Object as PropType<Resource>,
required: true
},
space: {
type: Object as PropType<SpaceResource>,
default: undefined
}
},
setup(props) {
const store = useStore()
const ability = useAbility()
const clientService = useClientService()
const passwordPolicyService = usePasswordPolicyService()
const language = useGettext()
const { isEnabled: isEmbedModeEnabled } = useEmbedMode()
const filteredActions = computed(() =>
pickBy(props.actions, (action) => action.displayed(props.item, store, ability) === true)
pickBy(props.actions, (action) =>
action.isEnabled({ space: props.space, resources: [props.item] })
)
)
return {
ability,
clientService,
passwordPolicyService,
store,
language,
filteredActions,
isEmbedModeEnabled
}
Expand Down
1 change: 1 addition & 0 deletions packages/web-app-files/src/composables/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './spaces'
export * from './useQuickActions'
54 changes: 54 additions & 0 deletions packages/web-app-files/src/composables/actions/useQuickActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Ref, computed } from 'vue'
import { useGettext } from 'vue3-gettext'
import {
eventBus,
SideBarEventTopics,
copyQuicklink,
FileAction,
useStore,
useAbility,
usePasswordPolicyService,
useClientService,
useCanShare
} from '@ownclouders/web-pkg'

export const useQuickActions = (): Ref<Record<string, FileAction>> => {
const store = useStore()
const ability = useAbility()
const passwordPolicyService = usePasswordPolicyService()
const { canShare } = useCanShare()
const clientService = useClientService()
const language = useGettext()
const { $gettext } = language

return computed(() => ({
collaborators: {
name: 'quick-action-collaborators',
id: 'collaborators',
componentType: 'button',
label: () => $gettext('Add people'),
icon: 'user-add',
handler: () => eventBus.publish(SideBarEventTopics.openWithPanel, 'sharing#peopleShares'),
isEnabled: ({ resources }) => canShare(resources[0])
},
quicklink: {
name: 'quick-action-quicklink',
id: 'quicklink',
componentType: 'button',
label: () => $gettext('Copy link'),
icon: 'link',
handler: async ({ resources }) => {
console.log(22, passwordPolicyService)
await copyQuicklink({
ability,
passwordPolicyService,
clientService,
language,
resource: resources[0],
store
})
},
isEnabled: ({ resources }) => canShare(resources[0])
}
}))
}
8 changes: 6 additions & 2 deletions packages/web-app-files/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import SpaceDriveResolver from './views/spaces/DriveResolver.vue'
import SpaceProjects from './views/spaces/Projects.vue'
import TrashOverview from './views/trash/Overview.vue'
import translations from '../l10n/translations.json'
import { defineWebApplication, quickActions } from '@ownclouders/web-pkg'
import { defineWebApplication } from '@ownclouders/web-pkg'
import store from './store'
import { extensions } from './extensions'
import fileSideBars from './fileSideBars'
Expand All @@ -22,6 +22,8 @@ import {
isPersonalSpaceResource,
isShareSpaceResource
} from '@ownclouders/web-client/src/helpers'
import { unref } from 'vue'
import { useQuickActions } from './composables'

// just a dummy function to trick gettext tools
function $gettext(msg) {
Expand Down Expand Up @@ -129,6 +131,8 @@ export const navItems = (context): AppNavigationItem[] => {

export default defineWebApplication({
setup(args) {
const quickActions = useQuickActions()

return {
appInfo,
store,
Expand All @@ -151,7 +155,7 @@ export default defineWebApplication({
}
}),
navItems,
quickActions,
quickActions: unref(quickActions),
translations,
extensions: extensions(args)
}
Expand Down
1 change: 1 addition & 0 deletions packages/web-app-files/src/views/spaces/GenericSpace.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
<quick-actions
:class="resource.preview"
class="oc-visible@s"
:space="space"
:item="resource"
:actions="app.quickActions"
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import quickActions, { canShare } from '../../../quickActions'
import { copyQuicklink } from '../../../helpers/share'
import { ShareStatus } from '@ownclouders/web-client/src/helpers/share'

Expand All @@ -12,6 +11,7 @@ import { useGettext } from 'vue3-gettext'
import { Store } from 'vuex'
import { FileAction, FileActionOptions } from '../types'
import { usePasswordPolicyService } from '../../passwordPolicyService'
import { useCanShare } from '../../shares'

export const useFileActionsCreateQuickLink = ({
store
Expand All @@ -25,6 +25,7 @@ export const useFileActionsCreateQuickLink = ({
const ability = useAbility()
const clientService = useClientService()
const passwordPolicyService = usePasswordPolicyService()
const { canShare } = useCanShare()

const handler = async ({ space, resources }: FileActionOptions) => {
const [resource] = resources
Expand All @@ -43,8 +44,7 @@ export const useFileActionsCreateQuickLink = ({
const actions = computed((): FileAction[] => [
{
name: 'create-quicklink',
icon: quickActions.quicklink.icon,
iconFillType: quickActions.quicklink.iconFillType,
icon: 'link',
label: () => $gettext('Copy link'),
handler,
isEnabled: ({ resources }) => {
Expand All @@ -56,7 +56,7 @@ export const useFileActionsCreateQuickLink = ({
return false
}
}
return canShare(resources[0], store, ability)
return canShare(resources[0])
},
componentType: 'button',
class: 'oc-files-actions-create-quicklink-trigger'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import quickActions, { canShare } from '../../../quickActions'
import { isLocationSharesActive, isLocationTrashActive } from '../../../router'
import { ShareStatus } from '@ownclouders/web-client/src/helpers/share'
import { eventBus } from '../../../services'
Expand All @@ -10,14 +9,14 @@ import { useRouter } from '../../router'
import { useStore } from '../../store'
import { Store } from 'vuex'
import { FileAction, FileActionOptions } from '../types'
import { useAbility } from '../../ability'
import { useCanShare } from '../../shares'

export const useFileActionsShowShares = ({ store }: { store?: Store<any> } = {}) => {
store = store || useStore()
const router = useRouter()
const ability = useAbility()
const { $gettext } = useGettext()
const isFilesAppActive = useIsFilesAppActive()
const { canShare } = useCanShare()

const handler = ({ resources }: FileActionOptions) => {
store.commit('Files/SET_FILE_SELECTION', resources)
Expand All @@ -27,8 +26,7 @@ export const useFileActionsShowShares = ({ store }: { store?: Store<any> } = {})
const actions = computed((): FileAction[] => [
{
name: 'show-shares',
icon: quickActions.collaborators.icon,
iconFillType: quickActions.collaborators.iconFillType,
icon: 'user-add',
label: () => $gettext('Share'),
handler,
isEnabled: ({ resources }) => {
Expand All @@ -48,7 +46,7 @@ export const useFileActionsShowShares = ({ store }: { store?: Store<any> } = {})
return false
}
}
return canShare(resources[0], store, ability)
return canShare(resources[0])
},
componentType: 'button',
class: 'oc-files-actions-show-shares-trigger'
Expand Down
1 change: 1 addition & 0 deletions packages/web-pkg/src/composables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export * from './scrollTo'
export * from './search'
export * from './selection'
export * from './service'
export * from './shares'
export * from './sideBar'
export * from './sort'
export * from './spaces'
Expand Down
1 change: 1 addition & 0 deletions packages/web-pkg/src/composables/shares/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useCanShare'
21 changes: 21 additions & 0 deletions packages/web-pkg/src/composables/shares/useCanShare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Resource } from '@ownclouders/web-client'
import { useStore } from '../store'
import { useAbility } from '../ability'

export const useCanShare = () => {
const store = useStore()
const ability = useAbility()

const canShare = (item: Resource) => {
const { capabilities } = store.state.user
if (!capabilities.files_sharing || !capabilities.files_sharing.api_enabled) {
return false
}
if (item.isReceivedShare() && !capabilities.files_sharing.resharing) {
return false
}
return item.canShare({ ability })
}

return { canShare }
}
25 changes: 24 additions & 1 deletion packages/web-pkg/src/helpers/share/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { Ability } from '@ownclouders/web-client/src/helpers/resource/types'
import { Resource } from '@ownclouders/web-client'
import { Language } from 'vue3-gettext'
import { unref } from 'vue'
import { showQuickLinkPasswordModal } from '../../quickActions'
import { getLocaleFromLanguage } from '../locale'
import { PublicExpirationCapability } from '@ownclouders/web-client/src/ocs/capabilities'

Expand All @@ -30,6 +29,30 @@ export interface CopyQuickLink extends CreateQuicklink {
passwordPolicyService: PasswordPolicyService
}

export function showQuickLinkPasswordModal({ $gettext, store, passwordPolicyService }, onConfirm) {
const modal = {
variation: 'passive',
title: $gettext('Set password'),
cancelText: $gettext('Cancel'),
confirmText: $gettext('Set'),
hasInput: true,
inputDescription: $gettext('Passwords for links are required.'),
inputPasswordPolicy: passwordPolicyService.getPolicy(),
inputGeneratePasswordMethod: () => passwordPolicyService.generatePassword(),
inputLabel: $gettext('Password'),
inputType: 'password',
onInput: () => store.dispatch('setModalInputErrorMessage', ''),
onPasswordChallengeCompleted: () => store.dispatch('setModalConfirmButtonDisabled', false),
onPasswordChallengeFailed: () => store.dispatch('setModalConfirmButtonDisabled', true),
onCancel: () => store.dispatch('hideModal'),
onConfirm: async (password) => {
onConfirm(password)
}
}

return store.dispatch('createModal', modal)
}

// doCopy creates the requested link and copies the url to the clipboard,
// the copy action uses the clipboard // clipboardItem api to work around the webkit limitations.
//
Expand Down
2 changes: 0 additions & 2 deletions packages/web-pkg/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ export * from './errors'
export * from './helpers'
export * from './http'
export * from './observer'
export { default as quickActions } from './quickActions'
export * from './quickActions'
export * from './router'
export * from './services'
export * from './types'
Expand Down
Loading

0 comments on commit 210db22

Please sign in to comment.