From 6209c3313965f0d571a07a1dca64822b0da7b660 Mon Sep 17 00:00:00 2001 From: oshahid Date: Tue, 17 Nov 2026 20:03:55 +0530 Subject: [PATCH 1/5] feat: add M365 folder integration with schemas and API functions --- .../features/connectors/api/m365-folders.ts | 155 ++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 packages/collaboration/src/features/connectors/api/m365-folders.ts diff --git a/packages/collaboration/src/features/connectors/api/m365-folders.ts b/packages/collaboration/src/features/connectors/api/m365-folders.ts new file mode 100644 index 0000000000..671d9827a6 --- /dev/null +++ b/packages/collaboration/src/features/connectors/api/m365-folders.ts @@ -0,0 +1,155 @@ +import { z } from "@semoss/ui/next"; +import { callPixel, type InsightActions, pixel } from "@/lib/pixel"; + +export const topicM365FolderSchema = z.object({ + linkId: z.string(), + kind: z.enum(["drive_folder", "channel_files"]), + displayName: z.string(), + driveId: z.string().optional().nullable(), + itemId: z.string().optional().nullable(), + teamId: z.string().optional().nullable(), + channelId: z.string().optional().nullable(), + siteId: z.string().optional().nullable(), + webUrl: z.string().optional().nullable(), + state: z.enum(["ok", "no_access"]), + checkedAt: z.string().optional().nullable(), +}); +export type TopicM365Folder = z.infer; + +const folderPointerSchema = z.object({ + kind: z.enum(["drive_folder", "channel_files"]), + displayName: z.string(), + driveId: z.string().optional(), + itemId: z.string().optional(), + teamId: z.string().optional(), + channelId: z.string().optional(), + siteId: z.string().optional(), + webUrl: z.string().optional(), +}); +export type FolderPointer = z.infer; + +// OneDrive folder browser +const oneDriveFolderSchema = z.object({ + id: z.string(), + name: z.string(), + driveId: z.string(), + itemId: z.string(), + webUrl: z.string().optional(), +}); +export type OneDriveFolder = z.infer; + +const oneDriveFoldersSchema = z.object({ + files: z.array(oneDriveFolderSchema), +}); + +// Teams browser +const teamSchema = z.object({ + id: z.string(), + displayName: z.string(), +}); +export type Team = z.infer; + +const teamsListSchema = z.object({ + teams: z.array(teamSchema), +}); + +const teamChannelSchema = z.object({ + id: z.string(), + displayName: z.string(), + webUrl: z.string().optional(), +}); +export type TeamChannel = z.infer; + +const channelsListSchema = z.object({ + channels: z.array(teamChannelSchema), +}); + +const listLinksSchema = z.object({ + topicId: z.string(), + folders: z.array(topicM365FolderSchema), +}); + +const linkResultSchema = z.object({ + topicId: z.string(), + folders: z.array(topicM365FolderSchema), +}); + +/** List the M365 folders linked to a topic (OneDrive, Teams channels). */ +export async function listTopicM365Folders( + actions: InsightActions, + topicId: string, +): Promise { + return ( + await callPixel( + actions, + pixel("BrainListTopicM365Folders", { topicId }), + listLinksSchema, + ) + ).folders; +} + +/** List OneDrive folders available to the user (from their drive and shared folders). */ +export async function listAvailableOneDriveFolders( + actions: InsightActions, +): Promise { + return ( + await callPixel( + actions, + pixel("MicrosoftOneDriveListFiles"), + oneDriveFoldersSchema, + ) + ).files; +} + +/** List Microsoft Teams available to the user. */ +export async function listAvailableTeams( + actions: InsightActions, +): Promise { + return ( + await callPixel( + actions, + pixel("MicrosoftTeamsListTeams"), + teamsListSchema, + ) + ).teams; +} + +/** List channels in a Microsoft Teams team. */ +export async function listTeamChannels( + actions: InsightActions, + teamId: string, +): Promise { + return ( + await callPixel( + actions, + pixel("MicrosoftTeamsListChannels", { teamId }), + channelsListSchema, + ) + ).channels; +} + +/** Link an M365 folder (OneDrive or Teams channel) to a topic. */ +export function linkTopicM365Folder( + actions: InsightActions, + topicId: string, + folder: FolderPointer, +): Promise { + return callPixel( + actions, + pixel("BrainLinkTopicM365Folder", { topicId, ...folder }), + linkResultSchema, + ).then((result) => result.folders); +} + +/** Unlink an M365 folder from a topic. */ +export function unlinkTopicM365Folder( + actions: InsightActions, + topicId: string, + linkId: string, +): Promise { + return callPixel( + actions, + pixel("BrainLinkTopicM365Folder", { topicId, linkId, remove: true }), + linkResultSchema, + ).then((result) => result.folders); +} From d261f89a2ec40a46087a31b29e23e21d6dafa2bc Mon Sep 17 00:00:00 2001 From: oshahid Date: Tue, 17 Nov 2026 20:57:30 +0530 Subject: [PATCH 2/5] feat: add Microsoft 365 folder integration to topic details and API --- .../collaboration/components/topic-detail.tsx | 6 + .../components/topic-m365-folders.tsx | 463 ++++++++++++++++++ .../state/collaboration.types.ts | 6 + .../features/connectors/api/m365-folders.ts | 22 +- 4 files changed, 482 insertions(+), 15 deletions(-) create mode 100644 packages/collaboration/src/features/collaboration/components/topic-m365-folders.tsx diff --git a/packages/collaboration/src/features/collaboration/components/topic-detail.tsx b/packages/collaboration/src/features/collaboration/components/topic-detail.tsx index 1d8234c679..4bec9a4040 100644 --- a/packages/collaboration/src/features/collaboration/components/topic-detail.tsx +++ b/packages/collaboration/src/features/collaboration/components/topic-detail.tsx @@ -56,6 +56,7 @@ import { TopicEditor } from "./topic-editor"; import { TopicFiles } from "./topic-files"; import { TopicFolders } from "./topic-folders"; import { TopicIgnoredPeople } from "./topic-ignored-people"; +import { TopicM365Folders } from "./topic-m365-folders"; import { TopicMatchesDialog } from "./topic-matches-dialog"; import { TopicPeoplePopover } from "./topic-people-popover"; import { TopicPersonMenu } from "./topic-person-menu"; @@ -848,6 +849,11 @@ export function TopicDetail() { folders={topic.folders ?? []} isSample={topic.isSample} /> + + ) : ( + + ); +} + +function FolderList({ folders }: { folders: TopicM365Folder[] }) { + if (!folders || !folders.length) return null; + return ( +
+

Microsoft 365 folders

+
+ {folders.map((folder) => ( + + + ))} +
+
+ ); +} + +function LiveTopicM365Folders({ + topicId, + folders, + isSample, +}: TopicM365FoldersProps) { + const { actions } = useInsight(); + const updates = useWorkUpdates(); + // the page's copy until the next reload brings the saved links + const [linked, setLinked] = useState(null); + const [teams, setTeams] = useState(null); + const [channels, setChannels] = useState(null); + const [onedriveFolders, setOnedriveFolders] = useState< + OneDriveFolder[] | null + >(null); + const [selectedTeam, setSelectedTeam] = useState(null); + const [isOpen, setIsOpen] = useState(false); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(""); + const [note, setNote] = useState(""); + const shown = linked ?? folders; + + async function open(next: boolean) { + setIsOpen(next); + if (!next) return; + setError(""); + setNote(""); + // Load data for both tabs when opening + try { + const [teamsList, foldersList] = await Promise.all([ + listAvailableTeams(actions), + listAvailableOneDriveFolders(actions), + ]); + setTeams(teamsList); + setOnedriveFolders(foldersList); + } catch (cause: unknown) { + setError( + cause instanceof Error + ? cause.message + : "Could not read your folders.", + ); + } + } + + async function loadChannels(teamId: string) { + try { + setChannels(null); + setSelectedTeam(teamId); + const channelsList = await listTeamChannels(actions, teamId); + setChannels(channelsList); + } catch (cause: unknown) { + setError( + cause instanceof Error + ? cause.message + : "Could not read channels for this team.", + ); + } + } + + async function linkFolder(pointer: FolderPointer, remove: boolean) { + setBusy(true); + setError(""); + setNote(""); + try { + // a topic made on this page is known to the server by another id + const serverId = updates + ? await updates.serverId(topicId) + : topicId; + const result = remove + ? await unlinkTopicM365Folder( + actions, + serverId, + pointer.linkId || "", + ) + : await linkTopicM365Folder(actions, serverId, pointer); + setLinked(result); + setIsOpen(false); + updates?.refresh(); + } catch (cause: unknown) { + setError( + cause instanceof Error ? cause.message : "That did not work.", + ); + } finally { + setBusy(false); + } + } + + const linkedIds = new Set(shown.map((folder) => folder.linkId)); + + return ( +
+
+

Microsoft 365 folders

+ {!isSample && ( + void open(next)} + > + + + + + setSelectedTeam(null)} + > + + + Teams + + + OneDrive + + + + + {!teams && !error && ( +
+ +
+ )} + {error && !teams && ( + + + {error} + + + )} + {teams && teams.length === 0 && ( +

+ No teams found. +

+ )} + {teams && + teams.length > 0 && + !selectedTeam && ( + + + + {teams.map((team) => ( + + void loadChannels( + team.id, + ) + } + > + + + { + team.displayName + } + + + ))} + + + )} + {selectedTeam && channels !== null && ( +
+ + + + + {channels.length === 0 && ( + + No channels found. + + )} + {channels.map((channel) => ( + + void linkFolder( + { + kind: "channel_files", + teamId: selectedTeam, + channelId: + channel.id, + displayName: + channel.displayName, + } as FolderPointer, + false, + ) + } + > + + + { + channel.displayName + } + + {linkedIds.has( + channel.id, + ) && " - linked"} + + ))} + + +
+ )} +
+ + + {!onedriveFolders && !error && ( +
+ +
+ )} + {error && !onedriveFolders && ( + + + {error} + + + )} + {onedriveFolders && + onedriveFolders.length === 0 && ( +

+ No folders found. +

+ )} + {onedriveFolders && + onedriveFolders.length > 0 && ( + + + + {onedriveFolders.map( + (folder) => ( + + void linkFolder( + { + kind: "drive_folder", + driveId: + folder.driveId, + itemId: folder.id, + displayName: + folder.name, + } as FolderPointer, + false, + ) + } + > + + + { + folder.name + } + + {linkedIds.has( + folder.id, + ) && + " - linked"} + + ), + )} + + + )} +
+
+
+
+ )} +
+ {shown.length > 0 && ( +
+ {shown.map((folder) => ( + + + ))} +
+ )} +

+ {shown.length + ? "Files from these folders are available in this topic's chat." + : "Link a Teams channel or OneDrive folder to explore its files in this topic's chat."} +

+ {note &&

{note}

} + {error && ( + + {error} + + )} +
+ ); +} diff --git a/packages/collaboration/src/features/collaboration/state/collaboration.types.ts b/packages/collaboration/src/features/collaboration/state/collaboration.types.ts index 7dc63c2dea..fec8022e8a 100644 --- a/packages/collaboration/src/features/collaboration/state/collaboration.types.ts +++ b/packages/collaboration/src/features/collaboration/state/collaboration.types.ts @@ -1,3 +1,4 @@ +import type { TopicM365Folder as ApiTopicM365Folder } from "@/features/connectors/api/m365-folders"; import type { SourceAttachment } from "@/features/connectors/types"; import type { DisplayBody } from "@/features/email/message-body"; @@ -49,6 +50,9 @@ export interface TopicFile { webUrl?: string; } +/** A Microsoft 365 folder (Teams channel or OneDrive folder) linked to a topic. */ +export type TopicM365Folder = ApiTopicM365Folder; + export interface Topic { id: string; name: string; @@ -65,6 +69,8 @@ export interface Topic { folders?: { id: string; path: string; origin?: string | null }[]; /** OneDrive and Teams files the owner linked to the topic. */ files?: TopicFile[]; + /** Microsoft 365 folders (Teams channels, OneDrive folders) linked to the topic. */ + m365Folders?: TopicM365Folder[]; stats: { threads: number; openItems: number; lastActivity: string }; isSample: boolean; } diff --git a/packages/collaboration/src/features/connectors/api/m365-folders.ts b/packages/collaboration/src/features/connectors/api/m365-folders.ts index 671d9827a6..a64459f7df 100644 --- a/packages/collaboration/src/features/connectors/api/m365-folders.ts +++ b/packages/collaboration/src/features/connectors/api/m365-folders.ts @@ -19,6 +19,7 @@ export type TopicM365Folder = z.infer; const folderPointerSchema = z.object({ kind: z.enum(["drive_folder", "channel_files"]), displayName: z.string(), + linkId: z.string().optional(), driveId: z.string().optional(), itemId: z.string().optional(), teamId: z.string().optional(), @@ -82,7 +83,7 @@ export async function listTopicM365Folders( return ( await callPixel( actions, - pixel("BrainListTopicM365Folders", { topicId }), + pixel("ListTopicM365Folders", { topicId }), listLinksSchema, ) ).folders; @@ -93,11 +94,7 @@ export async function listAvailableOneDriveFolders( actions: InsightActions, ): Promise { return ( - await callPixel( - actions, - pixel("MicrosoftOneDriveListFiles"), - oneDriveFoldersSchema, - ) + await callPixel(actions, pixel("ListDriveFiles"), oneDriveFoldersSchema) ).files; } @@ -105,13 +102,8 @@ export async function listAvailableOneDriveFolders( export async function listAvailableTeams( actions: InsightActions, ): Promise { - return ( - await callPixel( - actions, - pixel("MicrosoftTeamsListTeams"), - teamsListSchema, - ) - ).teams; + return (await callPixel(actions, pixel("ListTeams"), teamsListSchema)) + .teams; } /** List channels in a Microsoft Teams team. */ @@ -122,7 +114,7 @@ export async function listTeamChannels( return ( await callPixel( actions, - pixel("MicrosoftTeamsListChannels", { teamId }), + pixel("ListChannels", { teamId }), channelsListSchema, ) ).channels; @@ -136,7 +128,7 @@ export function linkTopicM365Folder( ): Promise { return callPixel( actions, - pixel("BrainLinkTopicM365Folder", { topicId, ...folder }), + pixel("LinkTopicM365Folder", { topicId, ...folder }), linkResultSchema, ).then((result) => result.folders); } From 1c2c29e8510b58884e93b81f2713983f06bcef90 Mon Sep 17 00:00:00 2001 From: oshahid Date: Wed, 18 Nov 2026 11:13:06 +0530 Subject: [PATCH 3/5] feat: update pixel event names for M365 folder and team operations --- .../features/connectors/api/m365-folders.ts | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/collaboration/src/features/connectors/api/m365-folders.ts b/packages/collaboration/src/features/connectors/api/m365-folders.ts index a64459f7df..fa016ed78b 100644 --- a/packages/collaboration/src/features/connectors/api/m365-folders.ts +++ b/packages/collaboration/src/features/connectors/api/m365-folders.ts @@ -83,7 +83,7 @@ export async function listTopicM365Folders( return ( await callPixel( actions, - pixel("ListTopicM365Folders", { topicId }), + pixel("BrainListTopicM365Folders", { topicId }), listLinksSchema, ) ).folders; @@ -94,7 +94,11 @@ export async function listAvailableOneDriveFolders( actions: InsightActions, ): Promise { return ( - await callPixel(actions, pixel("ListDriveFiles"), oneDriveFoldersSchema) + await callPixel( + actions, + pixel("MicrosoftOneDriveListFiles"), + oneDriveFoldersSchema, + ) ).files; } @@ -102,8 +106,13 @@ export async function listAvailableOneDriveFolders( export async function listAvailableTeams( actions: InsightActions, ): Promise { - return (await callPixel(actions, pixel("ListTeams"), teamsListSchema)) - .teams; + return ( + await callPixel( + actions, + pixel("MicrosoftTeamsListTeams"), + teamsListSchema, + ) + ).teams; } /** List channels in a Microsoft Teams team. */ @@ -114,7 +123,7 @@ export async function listTeamChannels( return ( await callPixel( actions, - pixel("ListChannels", { teamId }), + pixel("MicrosoftTeamsListChannels", { teamId }), channelsListSchema, ) ).channels; @@ -128,7 +137,7 @@ export function linkTopicM365Folder( ): Promise { return callPixel( actions, - pixel("LinkTopicM365Folder", { topicId, ...folder }), + pixel("BrainLinkTopicM365Folder", { topicId, ...folder }), linkResultSchema, ).then((result) => result.folders); } From f9295d3e91c6c0bc43cd8ced8e5a826af9b8b033 Mon Sep 17 00:00:00 2001 From: oshahid Date: Thu, 19 Nov 2026 14:45:03 +0530 Subject: [PATCH 4/5] feat: implement mapping for linked M365 folders in topic details --- .../components/topic-m365-folders.tsx | 15 ++++++++++--- .../features/collaboration/live/live-state.ts | 21 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/packages/collaboration/src/features/collaboration/components/topic-m365-folders.tsx b/packages/collaboration/src/features/collaboration/components/topic-m365-folders.tsx index bee26410c0..0e860a53b1 100644 --- a/packages/collaboration/src/features/collaboration/components/topic-m365-folders.tsx +++ b/packages/collaboration/src/features/collaboration/components/topic-m365-folders.tsx @@ -1,5 +1,5 @@ import { FolderOpen, Plus, X } from "lucide-react"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import { useInsight } from "@semoss/sdk/react"; import { Alert, @@ -85,8 +85,10 @@ function LiveTopicM365Folders({ }: TopicM365FoldersProps) { const { actions } = useInsight(); const updates = useWorkUpdates(); - // the page's copy until the next reload brings the saved links - const [linked, setLinked] = useState(null); + // Local state tracks changes after linking; initialized with the prop value from backend + const [linked, setLinked] = useState( + folders && folders.length > 0 ? folders : null, + ); const [teams, setTeams] = useState(null); const [channels, setChannels] = useState(null); const [onedriveFolders, setOnedriveFolders] = useState< @@ -99,6 +101,13 @@ function LiveTopicM365Folders({ const [note, setNote] = useState(""); const shown = linked ?? folders; + // Sync linked state when folders prop changes (e.g., after page reload with backend data) + useEffect(() => { + if (folders && folders.length > 0 && !linked) { + setLinked(folders); + } + }, [folders, linked]); + async function open(next: boolean) { setIsOpen(next); if (!next) return; diff --git a/packages/collaboration/src/features/collaboration/live/live-state.ts b/packages/collaboration/src/features/collaboration/live/live-state.ts index 0dbc59dae9..f9ffbeed1d 100644 --- a/packages/collaboration/src/features/collaboration/live/live-state.ts +++ b/packages/collaboration/src/features/collaboration/live/live-state.ts @@ -278,6 +278,26 @@ export function mapTopicFiles(rows: unknown): TopicFile[] { })); } +/** A topic's linked M365 folders, as BrainGetTopic and BrainLinkTopicM365Folder answer them. */ +export function mapTopicM365Folders( + rows: unknown, +): import("@/features/connectors/api/m365-folders").TopicM365Folder[] { + return list(rows).map((folder) => ({ + linkId: str(folder.linkId), + kind: + (folder.kind as "drive_folder" | "channel_files") ?? "drive_folder", + displayName: str(folder.displayName), + driveId: opt(folder.driveId), + itemId: opt(folder.itemId), + teamId: opt(folder.teamId), + channelId: opt(folder.channelId), + siteId: opt(folder.siteId), + webUrl: opt(folder.webUrl), + state: (folder.state as "ok" | "no_access") ?? "ok", + checkedAt: opt(folder.checkedAt), + })); +} + export function mapTopic(row: Row): Topic { const stats = (row.stats ?? {}) as Row; return { @@ -311,6 +331,7 @@ export function mapTopic(row: Row): Topic { origin: opt(folder.origin), })), files: mapTopicFiles(row.files), + m365Folders: mapTopicM365Folders(row.m365Folders), stats: { threads: Number(stats.threads ?? 0), openItems: Number(stats.openItems ?? 0), From fb8da1c43f511c2b8afff6e25031ba233ba3eaa1 Mon Sep 17 00:00:00 2001 From: oshahid Date: Thu, 19 Nov 2026 19:34:22 +0530 Subject: [PATCH 5/5] feat: add isFolder property to OneDrive folder schema and filter folders in LiveTopicM365Folders --- .../collaboration/components/topic-m365-folders.tsx | 8 ++++++-- .../src/features/connectors/api/m365-folders.ts | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/collaboration/src/features/collaboration/components/topic-m365-folders.tsx b/packages/collaboration/src/features/collaboration/components/topic-m365-folders.tsx index 0e860a53b1..ca3e10af36 100644 --- a/packages/collaboration/src/features/collaboration/components/topic-m365-folders.tsx +++ b/packages/collaboration/src/features/collaboration/components/topic-m365-folders.tsx @@ -115,12 +115,16 @@ function LiveTopicM365Folders({ setNote(""); // Load data for both tabs when opening try { - const [teamsList, foldersList] = await Promise.all([ + const [teamsList, allFolders] = await Promise.all([ listAvailableTeams(actions), listAvailableOneDriveFolders(actions), ]); setTeams(teamsList); - setOnedriveFolders(foldersList); + // Filter to show only folders, exclude files + const foldersOnly = allFolders.filter( + (item) => item.isFolder === true, + ); + setOnedriveFolders(foldersOnly); } catch (cause: unknown) { setError( cause instanceof Error diff --git a/packages/collaboration/src/features/connectors/api/m365-folders.ts b/packages/collaboration/src/features/connectors/api/m365-folders.ts index fa016ed78b..d10ffaa4d0 100644 --- a/packages/collaboration/src/features/connectors/api/m365-folders.ts +++ b/packages/collaboration/src/features/connectors/api/m365-folders.ts @@ -36,6 +36,7 @@ const oneDriveFolderSchema = z.object({ driveId: z.string(), itemId: z.string(), webUrl: z.string().optional(), + isFolder: z.boolean().optional(), }); export type OneDriveFolder = z.infer;