🤯 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 { validateVideoFileSize } from '@lobechat/utils/client';
import { Icon, toast, Tooltip, Upload } from '@lobehub/ui';
import * as stylex from '@stylexjs/stylex';
import { FileUp, FolderUp, ImageUp, type LucideIcon, Paperclip } from 'lucide-react';
import { memo, Suspense, useState } from 'react';
import { useTranslation } from 'react-i18next';
import TipGuide from '@/components/TipGuide';
import { useMediaUploadAbility } from '@/hooks/useMediaUploadAbility';
import { usePermission } from '@/hooks/usePermission';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';
import { useUserStore } from '@/store/user';
import { preferenceSelectors } from '@/store/user/selectors';
import { useAgentId } from '../../hooks/useAgentId';
import { useChatInputUpload } from '../../hooks/useChatInputUpload';
import { useEffectiveModel } from '../../hooks/useEffectiveModel';
import { useLargeFileLocalPath } from '../../hooks/useLargeFileLocalPath';
import { useChatInputStore } from '../../store';
import { type ActionDropdownMenuItems } from '../components/ActionDropdown';
import { ChatInputAction } from '../components/ChatInputAction';
import { MENU_ICON_SIZE, useKnowledgeMenuItems } from './useKnowledgeMenuItems';
const styles = stylex.create({
hotArea: {
'::before': {
content: '""',
position: 'absolute',
inset: 0,
backgroundColor: 'transparent',
},
},
});
interface FileUploadProps {
/** The trigger's icon; a paperclip unless the host's composer reads better with another. */
icon?: LucideIcon;
includeKnowledge?: boolean;
}
const FileUpload = memo<FileUploadProps>(({ icon = Paperclip, includeKnowledge = true }) => {
const { t } = useTranslation('chat');
const enableKnowledgeBase = useServerConfigStore(
(s) => featureFlagsSelectors(s).enableKnowledgeBase,
);
const upload = useChatInputUpload();
const editor = useChatInputStore((s) => s.editor);
const agentId = useAgentId();
const { model, provider } = useEffectiveModel(agentId);
const routeLargeFilesToLocalPaths = useLargeFileLocalPath(agentId, editor);
const { canUploadImage, canUploadVideo, canUploadAudio } = useMediaUploadAbility(
model,
provider,
agentId,
);
const [showTip, updateGuideState] = useUserStore((s) => [
preferenceSelectors.showUploadFileInKnowledgeBaseTip(s),
s.updateGuideState,
]);
const [dropdownOpen, setDropdownOpen] = useState(false);
const [updating, setUpdating] = useState(false);
const knowledgeItems = useKnowledgeMenuItems({
enabled: includeKnowledge,
onUpdatingChange: setUpdating,
});
// Viewer doesn't have `file:upload` permission — backend would 403.
// Render the disabled paperclip with a tooltip so the entry stays visible
// (per disabled-not-hidden UX rule), but block the dropdown which would
// otherwise let users trigger the upload anyway.
const { allowed: canUpload, reason } = usePermission('create_content');
if (includeKnowledge && !enableKnowledgeBase) return null;
if (!canUpload) {
return (
<Tooltip title={reason}>
<ChatInputAction
disabled
icon={icon}
showTooltip={false}
title={t('upload.action.tooltip')}
/>
</Tooltip>
);
}
const uploadItems: ActionDropdownMenuItems = [
{
closeOnClick: false,
disabled: !canUploadImage,
icon: <Icon icon={ImageUp} size={MENU_ICON_SIZE} />,
key: 'upload-image',
label: canUploadImage ? (
<Upload
multiple
accept={'image/*'}
onFiles={async (files) => {
setDropdownOpen(false);
editor?.focus();
await upload(files, agentId);
}}
>
<div {...stylex.props(styles.hotArea)}>{t('upload.action.imageUpload')}</div>
</Upload>
) : (
<Tooltip placement={'right'} title={t('upload.action.imageDisabled')}>
<div {...stylex.props(styles.hotArea)}>{t('upload.action.imageUpload')}</div>
</Tooltip>
),
},
{
closeOnClick: false,
icon: <Icon icon={FileUp} size={MENU_ICON_SIZE} />,
key: 'upload-file',
label: (
<Upload
multiple
beforeUpload={(file) => {
if (
(file.type.startsWith('image') && !canUploadImage) ||
(file.type.startsWith('video') && !canUploadVideo) ||
(file.type.startsWith('audio') && !canUploadAudio)
)
return false;
const validation = validateVideoFileSize(file);
if (!validation.isValid) {
toast.error(
t('upload.validation.videoSizeExceeded', {
actualSize: validation.actualSize,
maxSize: validation.maxSize,
}),
);
return false;
}
return true;
}}
onFiles={async (files) => {
setDropdownOpen(false);
editor?.focus();
const filesToUpload = routeLargeFilesToLocalPaths(files);
if (filesToUpload.length > 0) await upload(filesToUpload, agentId);
}}
>
<div {...stylex.props(styles.hotArea)}>{t('upload.action.fileUpload')}</div>
</Upload>
),
},
{
closeOnClick: false,
icon: <Icon icon={FolderUp} size={MENU_ICON_SIZE} />,
key: 'upload-folder',
label: (
<Upload
directory
multiple={true}
beforeUpload={(file) => {
if (
(file.type.startsWith('image') && !canUploadImage) ||
(file.type.startsWith('video') && !canUploadVideo) ||
(file.type.startsWith('audio') && !canUploadAudio)
)
return false;
const validation = validateVideoFileSize(file);
if (!validation.isValid) {
toast.error(
t('upload.validation.videoSizeExceeded', {
actualSize: validation.actualSize,
maxSize: validation.maxSize,
}),
);
return false;
}
return true;
}}
onFiles={async (files) => {
setDropdownOpen(false);
editor?.focus();
const filesToUpload = routeLargeFilesToLocalPaths(files);
if (filesToUpload.length > 0) await upload(filesToUpload, agentId);
}}
>
<div {...stylex.props(styles.hotArea)}>{t('upload.action.folderUpload')}</div>
</Upload>
),
},
];
const items: ActionDropdownMenuItems = [...uploadItems, ...knowledgeItems];
const content = (
<ChatInputAction
icon={icon}
loading={updating}
open={dropdownOpen}
showTooltip={false}
title={t('upload.action.tooltip')}
trigger={'both'}
dropdown={{
maxHeight: 500,
maxWidth: 480,
menu: { items },
minWidth: 240,
}}
onOpenChange={setDropdownOpen}
/>
);
return (
<Suspense
fallback={<ChatInputAction disabled icon={icon} title={t('upload.action.tooltip')} />}
>
{includeKnowledge && showTip ? (
<TipGuide
open={showTip}
placement={'top'}
title={t('knowledgeBase.uploadGuide')}
onOpenChange={() => {
updateGuideState({ uploadFileInKnowledgeBase: false });
}}
>
{content}
</TipGuide>
) : (
content
)}
</Suspense>
);
});
export default FileUpload;