Files
korean_comments_action/src/main.ts
T

70 lines
1.9 KiB
TypeScript
Raw Normal View History

import type { IFileAction } from '@nextcloud/files'
import { getSidebar, Permission, registerFileAction } from '@nextcloud/files'
import { loadState } from '@nextcloud/initial-state'
const COMMENT_ICON = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M18 8H6V6h12v2m0 3H6V9h12v2m0 3H6v-2h12v2m4-10v18l-4-4H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2Z"/></svg>'
type ActionContext = Parameters<NonNullable<IFileAction['enabled']>>[0]
function commentsTab(context: ActionContext): 'activity' | 'comments' | undefined {
const [node] = context.nodes
if (!node) {
return undefined
}
const tabs = getSidebar().getTabs({
node,
folder: context.folder,
view: context.view,
})
const commentsUseActivity = loadState<boolean>('comments', 'activityEnabled', false)
if (commentsUseActivity && tabs.some(({ id }) => id === 'activity')) {
return 'activity'
}
if (tabs.some(({ id }) => id === 'comments')) {
return 'comments'
}
return undefined
}
export const action: IFileAction = {
id: 'korean-comments-action',
displayName: () => '댓글',
title: () => '댓글',
iconSvgInline: () => COMMENT_ICON,
enabled(context) {
const [node] = context.nodes
return context.nodes.length === 1
&& node !== undefined
&& node.root.startsWith('/files/')
&& node.permissions !== Permission.NONE
&& getSidebar().available
&& commentsTab(context) !== undefined
},
async exec(context) {
const [node] = context.nodes
const tabId = commentsTab(context)
if (!node || !tabId) {
return false
}
try {
const sidebar = getSidebar()
if (sidebar.isOpen && sidebar.node?.source === node.source) {
sidebar.setActiveTab(tabId)
} else {
sidebar.open(node, tabId)
}
return null
} catch (error) {
console.error('Could not open the comments sidebar', error)
return false
}
},
inline: () => false,
order: -45,
}
registerFileAction(action)