🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { type DropdownItem } from '@lobehub/ui';
import { confirmModal, css, cx, Icon, toast, Upload } from '@lobehub/ui';
import { Archive, Hash, Import, LucideCheck, Trash } from 'lucide-react';
import { useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { useActiveWorkspaceId } from '@/business/client/hooks/useActiveWorkspaceId';
import { useIsWorkspaceOwner } from '@/business/client/hooks/useIsWorkspaceOwner';
import { archiveMergedPullRequestTopics } from '@/features/AgentSidebar/Topic/archiveMergedPullRequestTopics';
import { openWorkspaceDeleteAllModal } from '@/features/WorkspaceDeleteAllModal';
import { usePermission } from '@/hooks/usePermission';
import { useChatStore } from '@/store/chat';
import { topicSelectors } from '@/store/chat/selectors';
import { useGlobalStore } from '@/store/global';
import { systemStatusSelectors } from '@/store/global/selectors';
import { useUserStore } from '@/store/user';
import { userProfileSelectors } from '@/store/user/selectors';
const hotArea = css`
&::before {
content: '';
position: absolute;
inset: 0;
background-color: transparent;
}
`;
interface UseTopicActionsDropdownMenuOptions {
onUploadClose?: () => void;
}
type TopicMaintenanceScope = 'own' | 'workspace';
export const useTopicActionsDropdownMenu = (
options: UseTopicActionsDropdownMenuOptions = {},
): DropdownItem[] => {
const { t } = useTranslation(['topic', 'common']);
const { onUploadClose } = options;
const activeWorkspaceId = useActiveWorkspaceId();
const isWorkspaceOwner = useIsWorkspaceOwner();
const currentUserId = useUserStore(userProfileSelectors.userId);
const { allowed: canCreateTopic } = usePermission('create_content');
const { allowed: canEditTopic } = usePermission('edit_own_content');
const topics = useChatStore(topicSelectors.currentTopics);
const [activeGroupId, removeUnstarredTopic, removeGroupTopics, importTopic] = useChatStore(
(s) => [s.activeGroupId, s.removeUnstarredTopic, s.removeGroupTopics, s.importTopic],
);
const removeAllTopic = useCallback(
async (scope: 'own' | 'workspace' = 'own') => {
if (!activeGroupId) return;
await removeGroupTopics(activeGroupId, scope);
},
[activeGroupId, removeGroupTopics],
);
const handleArchiveMergedPullRequests = useCallback(
(scope: TopicMaintenanceScope = 'own') =>
archiveMergedPullRequestTopics({
ownerId: activeWorkspaceId && scope === 'own' ? (currentUserId ?? null) : undefined,
t,
topics: topics ?? [],
}),
[activeWorkspaceId, currentUserId, t, topics],
);
const handleImport = useCallback(
async (file: File) => {
onUploadClose?.();
try {
const text = await file.text();
// Validate JSON format
JSON.parse(text);
await importTopic(text);
} catch {
toast.error({
description: t('importInvalidFormat'),
title: t('importError'),
});
}
},
[importTopic, onUploadClose, t],
);
const [topicPageSize, updateSystemStatus] = useGlobalStore((s) => [
systemStatusSelectors.topicPageSize(s),
s.updateSystemStatus,
]);
return useMemo(() => {
const pageSizeOptions = [20, 40, 60, 100];
const pageSizeItems = pageSizeOptions.map((size) => ({
icon: topicPageSize === size ? <Icon icon={LucideCheck} /> : <div />,
key: `pageSize-${size}`,
label: t('pageSizeItem', { count: size, ns: 'common' }),
onClick: () => {
updateSystemStatus({ topicPageSize: size });
},
}));
return [
{
children: pageSizeItems,
extra: topicPageSize,
icon: <Icon icon={Hash} />,
key: 'displayItems',
label: t('displayItems'),
},
{
type: 'divider' as const,
},
{
disabled: !canCreateTopic,
icon: <Icon icon={Import} />,
key: 'import',
label: (
<Upload
accept=".json"
disabled={!canCreateTopic}
onFiles={([file]) => handleImport(file)}
>
<div className={cx(hotArea)}>{t('actions.import')}</div>
</Upload>
),
...(onUploadClose ? { closeOnClick: false } : null),
},
{
type: 'divider' as const,
},
{
disabled: !canEditTopic,
icon: <Icon icon={Archive} />,
key: 'archiveMergedPullRequests',
label: t(
activeWorkspaceId
? 'actions.archiveMergedPullRequestsOwn'
: 'actions.archiveMergedPullRequests',
),
onClick: () => handleArchiveMergedPullRequests('own'),
},
...(activeWorkspaceId && isWorkspaceOwner
? [
{ type: 'divider' as const },
{
disabled: !canEditTopic,
icon: <Icon icon={Archive} />,
key: 'archiveMergedPullRequestsWorkspace',
label: t('actions.archiveMergedPullRequestsWorkspace'),
onClick: () => {
confirmModal({
cancelText: t('cancel', { ns: 'common' }),
okText: t('ok', { ns: 'common' }),
onOk: () => handleArchiveMergedPullRequests('workspace'),
content: t('actions.confirmArchiveMergedPullRequestsWorkspace'),
title: t('actions.archiveMergedPullRequestsWorkspace'),
});
},
},
{
danger: true,
disabled: !canEditTopic,
icon: <Icon icon={Trash} />,
key: 'deleteUnstarredWorkspace',
label: t('actions.removeUnpinnedWorkspace'),
onClick: () => {
openWorkspaceDeleteAllModal({
acknowledgeText: t('actions.confirmRemoveUnpinnedWorkspaceAcknowledge'),
cancelText: t('cancel', { ns: 'common' }),
confirmText: t('actions.removeUnpinnedWorkspace'),
description: t('actions.confirmRemoveUnpinnedWorkspace'),
onConfirm: () => removeUnstarredTopic({ onlyOwn: false }),
title: t('actions.removeUnpinnedWorkspace'),
});
},
},
{
danger: true,
disabled: !canEditTopic,
icon: <Icon icon={Trash} />,
key: 'deleteAllWorkspace',
label: t('actions.removeAllWorkspace'),
onClick: () => {
openWorkspaceDeleteAllModal({
acknowledgeText: t('actions.confirmRemoveAllWorkspaceAcknowledge'),
cancelText: t('cancel', { ns: 'common' }),
confirmText: t('actions.removeAllWorkspace'),
description: t('actions.confirmRemoveAllWorkspace'),
onConfirm: () => removeAllTopic('workspace'),
title: t('actions.removeAllWorkspace'),
});
},
},
]
: []),
].filter(Boolean) as DropdownItem[];
}, [
topicPageSize,
updateSystemStatus,
handleImport,
canCreateTopic,
canEditTopic,
onUploadClose,
handleArchiveMergedPullRequests,
removeUnstarredTopic,
removeAllTopic,
activeWorkspaceId,
isWorkspaceOwner,
t,
]);
};