Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(command): add global commands to upload file/folders to soup (#1791)

Seamus91f351639306
diff --git a/js/app/packages/app/component/GlobalHotkeys.tsx b/js/app/packages/app/component/GlobalHotkeys.tsx
index 6575602cc8..31ab83001e 100644
--- a/js/app/packages/app/component/GlobalHotkeys.tsx
+++ b/js/app/packages/app/component/GlobalHotkeys.tsx
@@ -19,11 +19,20 @@ import { globalSplitManager } from '../signal/splitLayout';
import { CommandState } from './command';
import { CREATABLE_BLOCKS, setCreateMenuOpen } from './Launcher';
import { useSplitLayout } from './split-layout/layout';
+import {
+ openFilePicker,
+ openFolderPicker,
+ handleFolderSelect,
+} from '@core/util/upload';
+import { useHandleFileUpload } from '@app/util/handleFileUpload';
+import Upload from '@icon/regular/upload.svg';
export default function GlobalShortcuts() {
const canFit = () => globalSplitManager()?.canAppendSplit() ?? true;
const { toggleSettings } = useSettingsState();
+ const handleFileUpload = useHandleFileUpload();
+
const handleCommandMenu = () => {
CommandState.toggle();
};
@@ -232,5 +241,31 @@ export default function GlobalShortcuts() {
runWithInputFocused: true,
});
+ registerHotkey({
+ scopeId: 'global',
+ description: 'Upload files',
+ icon: () => <Upload class="size-4" />,
+ keyDownHandler: () => {
+ openFilePicker({ multiple: true }, async (files) => {
+ await handleFileUpload(files, false);
+ });
+ return true;
+ },
+ });
+
+ registerHotkey({
+ scopeId: 'global',
+ description: 'Upload folders',
+ icon: () => <Upload class="size-4" />,
+ keyDownHandler: () => {
+ openFolderPicker({ multiple: true }, async (files) => {
+ await handleFolderSelect(files, async (entries) => {
+ await handleFileUpload(entries, false);
+ });
+ });
+ return true;
+ },
+ });
+
return null;
}
diff --git a/js/app/packages/app/util/handleFileUpload.ts b/js/app/packages/app/util/handleFileUpload.ts
index f9f882eadb..74b59e4c85 100644
--- a/js/app/packages/app/util/handleFileUpload.ts
+++ b/js/app/packages/app/util/handleFileUpload.ts
@@ -2,15 +2,16 @@ import { fileTypeToBlockName } from '@core/constant/allBlocks';
import { type UploadInput, uploadFiles } from '@core/util/upload';
import { useSplitLayout } from '../component/split-layout/layout';
import { refetchSoupEntity } from '@queries/soup/cache';
+import { toast } from '@core/component/Toast/Toast';
export function useHandleFileUpload({
projectId,
}: {
projectId?: string;
} = {}) {
- const { replaceOrInsertSplit } = useSplitLayout();
+ const { replaceOrInsertSplit, openWithSplit } = useSplitLayout();
- return async (files: UploadInput[]) => {
+ return async (files: UploadInput[], withOpen = true) => {
const results = await uploadFiles(files, 'dss', {
projectId,
});
@@ -26,33 +27,49 @@ export function useHandleFileUpload({
.filter((result) => result.pending)
.filter((result) => result.type === 'folder');
- // refetch soup for folders once all pending uploads are done
- Promise.allSettled(pendingUploads.map((upload) => upload.projectId)).then(
- (results) => {
- for (const result of results) {
- if (result.status === 'fulfilled' && result.value) {
- refetchSoupEntity(result.value, 'project');
- }
- }
- }
- );
+ // Refetch soup for folders once pending uploads are done, and show a success
+ // toast with an action to open the created folder/project.
+ for (const upload of pendingUploads) {
+ upload.projectId.then((createdProjectId) => {
+ if (!createdProjectId) return;
- // if there is a single file uploaded then open it
- if (successfulUploads.length !== 1 || failedUploads.length > 0) {
- return;
+ refetchSoupEntity(createdProjectId, 'project');
+
+ toast.success(`Uploaded ${upload.name}`, undefined, {
+ text: 'Open folder',
+ onClick: () => {
+ openWithSplit(
+ { type: 'project', id: createdProjectId },
+ { referredFrom: 'file-upload', activate: true }
+ );
+ },
+ });
+ });
}
- const upload = successfulUploads[0];
- // refetch the uploaded document into soup
- if (upload.type === 'document') {
- refetchSoupEntity(upload.documentId, 'document');
- replaceOrInsertSplit(
- {
- type: fileTypeToBlockName(upload.fileType),
- id: upload.documentId,
- },
- 'file-upload'
- );
+ // refetch soup for uploaded docs
+ for (const upload of successfulUploads) {
+ if (upload.type === 'document') {
+ refetchSoupEntity(upload.documentId, 'document');
+ }
+ }
+
+ // optionally nav to singular uploaded doc
+ if (
+ withOpen &&
+ successfulUploads.length === 1 &&
+ failedUploads.length === 0
+ ) {
+ const upload = successfulUploads[0];
+ if (upload.type === 'document') {
+ replaceOrInsertSplit(
+ {
+ type: fileTypeToBlockName(upload.fileType),
+ id: upload.documentId,
+ },
+ 'file-upload'
+ );
+ }
}
};
}
diff --git a/js/app/packages/core/util/upload.ts b/js/app/packages/core/util/upload.ts
index a3360228be..0f3d13e1c7 100644
--- a/js/app/packages/core/util/upload.ts
+++ b/js/app/packages/core/util/upload.ts
@@ -33,6 +33,121 @@ const MAX_FILE_BYTE_SIZE = 2 * 1000 * 1000 * 1000; // 2GB
type UploadDestination = 'dss' | 'static';
+type UploadPickerCleanup = () => void;
+
+type UploadPickerOptions = {
+ multiple?: boolean;
+ acceptedMimeTypes?: string[];
+ acceptedFileExtensions?: string[];
+ directory?: boolean;
+ filterDotfiles?: boolean;
+};
+
+function buildAcceptString(options?: UploadPickerOptions): string {
+ const mimeTypes = options?.acceptedMimeTypes?.join(',');
+ const fileExtensions = options?.acceptedFileExtensions?.join(',.');
+ let acceptString = '';
+ if (mimeTypes) {
+ acceptString += `${mimeTypes},`;
+ }
+ if (fileExtensions) {
+ acceptString += `.${fileExtensions}`;
+ }
+ return acceptString;
+}
+
+function filterFilesFromInput(
+ fileList: FileList | null,
+ options?: UploadPickerOptions
+): File[] {
+ const filterDotfiles = options?.filterDotfiles ?? true;
+ const files = Array.from(fileList || []);
+ return filterDotfiles ? files.filter((f) => !f.name.startsWith('.')) : files;
+}
+
+function createHiddenFileInput(
+ options?: UploadPickerOptions
+): HTMLInputElement {
+ const input = document.createElement('input');
+ input.type = 'file';
+ input.style.display = 'none';
+ input.multiple = options?.multiple ?? false;
+ input.accept = buildAcceptString(options);
+
+ // Enable folder picking in browsers that support it (Safari/Chrome/WebKit)
+ if (options?.directory) {
+ // property exists in TS lib for WebKit dir inputs on some setups
+ (
+ input as HTMLInputElement & { webkitdirectory?: boolean }
+ ).webkitdirectory = true;
+ // ensure attribute is present for environments that rely on it
+ input.setAttribute('webkitdirectory', '');
+ input.setAttribute('directory', '');
+ }
+
+ return input;
+}
+
+/**
+ * Imperative picker for files/folders, using a hidden <input type="file" />.
+ * Returns a cleanup function; you generally don't need it because the util
+ * removes listeners and DOM nodes once a selection is made.
+ */
+export function openUploadPicker(
+ options: UploadPickerOptions,
+ onSelect: (files: File[]) => void | Promise<void>
+): UploadPickerCleanup {
+ const input = createHiddenFileInput(options);
+
+ const handleChange = () => {
+ const files = filterFilesFromInput(input.files, options);
+ // Always cleanup before invoking callback to avoid re-entrancy leaks
+ cleanup();
+ // Only call onSelect if there are files selected
+ if (files.length > 0) {
+ void onSelect(files);
+ }
+ };
+
+ const cleanup = () => {
+ input.removeEventListener('change', handleChange);
+ input.remove();
+ };
+
+ input.addEventListener('change', handleChange);
+ document.body.appendChild(input);
+ input.click();
+
+ return cleanup;
+}
+
+export function openFilePicker(
+ options: Omit<UploadPickerOptions, 'directory'> = {},
+ onSelect: (files: File[]) => void | Promise<void>
+): UploadPickerCleanup {
+ return openUploadPicker(
+ {
+ ...options,
+ directory: false,
+ },
+ onSelect
+ );
+}
+
+export function openFolderPicker(
+ options: Omit<UploadPickerOptions, 'directory'> = {},
+ onSelect: (files: File[]) => void | Promise<void>
+): UploadPickerCleanup {
+ return openUploadPicker(
+ {
+ ...options,
+ directory: true,
+ multiple: options.multiple ?? true,
+ },
+ onSelect
+ );
+}
+
type StaticUploadSuccessResult = {
name: string;
id: string;
@@ -89,7 +204,7 @@ const getDestination = (file: File, ruleset: DestinationRuleset) => {
return ruleset instanceof Function ? ruleset(file) : ruleset;
};
-const DEFAULT_DESTINATION_RULESET: DestinationRuleset = 'dss';
+export const DEFAULT_DESTINATION_RULESET: DestinationRuleset = 'dss';
// Shared ruleset for chat input -> images/videos are static for inline display, everything else to DSS
export const chatRuleset: DestinationRuleset = (file: File) => {