Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion frontend/app/api/queries/useGetSearchQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ export { EMPTY_SEARCH_RESULT };
export const useGetSearchQuery = (
query: string,
queryData?: ParsedQueryData | null,
options?: Omit<UseQueryOptions, "queryKey" | "queryFn">,
options?: Omit<
UseQueryOptions<SearchResult, Error, SearchResult, any[]>,
"queryKey" | "queryFn"
>,
) => {
const queryClient = useQueryClient();
const getFileIdentity = (chunk: ChunkResult): string => {
Expand Down
14 changes: 14 additions & 0 deletions frontend/components/cloud-picker/file-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,24 @@ const getMimeTypeLabel = (mimeType: string) => {
"application/vnd.google-apps.folder": "Folder",
"application/pdf": "PDF",
"text/plain": "Text",
"text/csv": "CSV",
"text/html": "HTML",
"application/xml": "XML",
"application/json": "JSON",
"application/msword": "Word Doc",
"application/vnd.ms-excel": "Excel",
"application/vnd.ms-powerpoint": "PowerPoint",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document":
"Word Doc",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
"Excel",
"application/vnd.openxmlformats-officedocument.presentationml.presentation":
"PowerPoint",
"application/octet-stream": "File",
"image/jpeg": "JPEG",
"image/png": "PNG",
"image/gif": "GIF",
"image/svg+xml": "SVG",
};

return typeMap[mimeType] || mimeType?.split("/").pop() || "Document";
Expand Down
145 changes: 104 additions & 41 deletions frontend/components/cloud-picker/provider-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,20 @@ export class OneDriveHandler {
private clientId: string;
private provider: CloudProvider;
private baseUrl?: string;
private onPickerStateChange?: (isOpen: boolean) => void;

constructor(
accessToken: string,
clientId: string,
provider: CloudProvider = "onedrive",
baseUrl?: string,
onPickerStateChange?: (isOpen: boolean) => void,
) {
this.accessToken = accessToken;
this.clientId = clientId;
this.provider = provider;
this.baseUrl = baseUrl;
this.onPickerStateChange = onPickerStateChange;
}

async loadPickerApi(): Promise<boolean> {
Expand Down Expand Up @@ -206,57 +209,111 @@ export class OneDriveHandler {
console.log("OneDrive picker success callback:", response);
if (!response || !response.value) {
console.warn("OneDrive picker returned no value");
this.onPickerStateChange?.(false);
return;
}

const newFiles: CloudFile[] =
response.value?.map((item: any) => {
// Extract mimeType from file object or infer from name
let mimeType = item.file?.mimeType;
if (!mimeType && item.name) {
// Infer from extension if mimeType not provided
const ext = item.name.split(".").pop()?.toLowerCase();
const mimeTypes: { [key: string]: string } = {
pdf: "application/pdf",
doc: "application/msword",
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
xls: "application/vnd.ms-excel",
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
ppt: "application/vnd.ms-powerpoint",
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
txt: "text/plain",
csv: "text/csv",
json: "application/json",
xml: "application/xml",
html: "text/html",
jpg: "image/jpeg",
jpeg: "image/jpeg",
png: "image/png",
gif: "image/gif",
svg: "image/svg+xml",
};
mimeType = mimeTypes[ext || ""] || "application/octet-stream";
}
// v7.2 action:"query" returns stubs with only id/endpoint/parentReference.
// Enrich each item by fetching full metadata from the Graph API.
const enrichItems = async () => {
const enriched = await Promise.all(
response.value.map(async (item: any) => {
const driveId =
item.parentReference?.driveId || item.id?.split("!")[0];
const itemId = item.id;

return {
id: item.id,
name: item.name || `${this.getProviderName()} File`,
mimeType: mimeType || "application/octet-stream",
webUrl: item.webUrl || "",
downloadUrl: item["@microsoft.graph.downloadUrl"] || "",
size: item.size,
modifiedTime: item.lastModifiedDateTime,
isFolder: !!item.folder,
};
}) || [];
if (driveId && itemId) {
try {
const url = `https://graph.microsoft.com/v1.0/drives/${driveId}/items/${itemId}`;
const res = await fetch(url, {
headers: { Authorization: `Bearer ${this.accessToken}` },
});
if (res.ok) {
const meta = await res.json();
console.log(
"OneDrive enriched metadata:",
meta.name,
meta.file?.mimeType,
);

let mimeType = meta.file?.mimeType;
if (!mimeType && meta.name) {
const ext = meta.name.split(".").pop()?.toLowerCase();
const mimeTypes: { [key: string]: string } = {
pdf: "application/pdf",
doc: "application/msword",
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
xls: "application/vnd.ms-excel",
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
ppt: "application/vnd.ms-powerpoint",
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
txt: "text/plain",
csv: "text/csv",
json: "application/json",
xml: "application/xml",
html: "text/html",
jpg: "image/jpeg",
jpeg: "image/jpeg",
png: "image/png",
gif: "image/gif",
svg: "image/svg+xml",
};
mimeType =
mimeTypes[ext || ""] || "application/octet-stream";
}

onFileSelected(newFiles);
return {
id: meta.id,
name: meta.name || `${this.getProviderName()} File`,
mimeType: mimeType || "application/octet-stream",
webUrl: meta.webUrl || "",
downloadUrl: meta["@microsoft.graph.downloadUrl"] || "",
size: meta.size,
modifiedTime: meta.lastModifiedDateTime,
isFolder: !!meta.folder,
} as CloudFile;
} else {
console.warn(
"Graph API metadata fetch failed:",
res.status,
await res.text(),
);
}
} catch (e) {
console.warn("Graph API metadata fetch error:", e);
}
}

// Fallback: use stub data if Graph call fails
return {
id: item.id,
name: item.name || `${this.getProviderName()} File`,
mimeType: "application/octet-stream",
webUrl: item.webUrl || "",
downloadUrl: item["@microsoft.graph.downloadUrl"] || "",
size: item.size,
modifiedTime: item.lastModifiedDateTime,
isFolder: !!item.folder,
} as CloudFile;
}),
);

onFileSelected(enriched);
this.onPickerStateChange?.(false);
};

enrichItems().catch((e) => {
console.error("Failed to enrich OneDrive items:", e);
this.onPickerStateChange?.(false);
});
},
cancel: () => {
console.log("Picker cancelled");
this.onPickerStateChange?.(false);
},
error: (error: any) => {
console.error("Picker error callback:", error);
this.onPickerStateChange?.(false);
},
});
}
Expand Down Expand Up @@ -310,7 +367,13 @@ export const createProviderHandler = (
if (!clientId) {
throw new Error("Client ID required for OneDrive");
}
return new OneDriveHandler(accessToken, clientId, provider, baseUrl);
return new OneDriveHandler(
accessToken,
clientId,
provider,
baseUrl,
onPickerStateChange,
);
default:
throw new Error(`Unsupported provider: ${provider}`);
}
Expand Down
Loading
Loading