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
10 changes: 5 additions & 5 deletions apps/roam/src/components/Export.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ import { AddReferencedNodeType } from "./canvas/DiscourseRelationShape/Discourse
import posthog from "posthog-js";
import { getMyGroups, type MyGroup } from "@repo/database/lib/groups";
import {
publishNodesToGroups,
type PublishNode,
publishNodeUidsWithTypeToGroups,
type NodeUidWithType,
} from "~/utils/publishNodesToGroups";
import { getLoggedInClient, getSupabaseContext } from "~/utils/supabaseContext";
import { isSyncEnabled } from "~/components/settings/utils/accessors";
Expand Down Expand Up @@ -248,7 +248,7 @@ const ExportDialog: ExportDialogComponent = ({
? { uid: r.uid, type: node.type }
: null;
})
.filter((n): n is PublishNode => n !== null)
.filter((n): n is NodeUidWithType => n !== null)
: [],
[results, syncEnabled],
);
Expand Down Expand Up @@ -843,11 +843,11 @@ const ExportDialog: ExportDialogComponent = ({
skippedUnsyncedUids,
okGroupIds,
failedGroupIds,
} = await publishNodesToGroups({
} = await publishNodeUidsWithTypeToGroups({
client,
spaceId: context.spaceId,
groupIds: selectedGroupIds,
nodes: publishableNodes,
nodeUids: publishableNodes,
});
posthog.capture("Export Dialog: Publish", {
groupCount: okGroupIds.length,
Expand Down
27 changes: 23 additions & 4 deletions apps/roam/src/utils/publishNodesToGroups.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { CrossAppNode } from "@repo/database/crossAppContracts";
import type { DGSupabaseClient } from "@repo/database/lib/client";
import { getAvailableGroupIds } from "@repo/database/lib/groups";
import { nodeUidsWithTypeToCrossApp } from "./roamToCrossAppConverters";

export type PublishNode = {
export type NodeUidWithType = {
uid: string;
type: string;
};
Expand Down Expand Up @@ -37,7 +39,7 @@ export const publishNodesToGroups = async ({
client: DGSupabaseClient;
spaceId: number;
groupIds: string[];
nodes: PublishNode[];
nodes: CrossAppNode[];
}): Promise<PublishNodesResult> => {
const result: PublishNodesResult = {
publishedNodeUids: [],
Expand All @@ -57,7 +59,7 @@ export const publishNodesToGroups = async ({
);
if (targetGroupIds.length === 0) return result;

const uids = [...new Set(nodes.map((node) => node.uid))];
const uids = [...new Set(nodes.map((node) => node.localId))];

const syncedRes = await client
.from("my_concepts")
Expand All @@ -77,7 +79,9 @@ export const publishNodesToGroups = async ({
// Required dependency: the node-type schema concept, when it is synced too.
const types = [
...new Set(
nodes.filter((node) => syncedUids.has(node.uid)).map((node) => node.type),
nodes
.filter((node) => syncedUids.has(node.localId))
.map((node) => node.nodeType),
),
];
const schemaRes = await client
Expand Down Expand Up @@ -125,3 +129,18 @@ export const publishNodesToGroups = async ({
result.publishedNodeUids = result.okGroupIds.length > 0 ? syncedNodeUids : [];
return result;
};

export const publishNodeUidsWithTypeToGroups = async ({
client,
spaceId,
groupIds,
nodeUids,
}: {
client: DGSupabaseClient;
spaceId: number;
groupIds: string[];
nodeUids: NodeUidWithType[];
}): Promise<PublishNodesResult> => {
const nodes = await nodeUidsWithTypeToCrossApp(nodeUids);
return await publishNodesToGroups({ client, spaceId, groupIds, nodes });
};
52 changes: 52 additions & 0 deletions apps/roam/src/utils/roamToCrossAppConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { CrossAppNode } from "@repo/database/crossAppContracts";
import type { RoamFullContentNode } from "./convertRoamNodeToFullContent";
import type { DiscourseNode } from "./getDiscourseNodes";
import type { TreeNode, ViewType } from "roamjs-components/types";
import type { NodeUidWithType } from "~/utils/publishNodesToGroups";
import type { Json } from "@repo/database/dbTypes";
import { toMarkdown } from "./pageToMarkdown";
import getFullTreeByParentUid from "roamjs-components/queries/getFullTreeByParentUid";
import getPageViewType from "roamjs-components/queries/getPageViewType";
Expand Down Expand Up @@ -64,3 +66,53 @@ export const fullContentNodeToCrossApp = (
},
};
};

export const nodeUidsWithTypeToCrossApp = async (
nodes: NodeUidWithType[],
): Promise<CrossAppNode[]> => {
const typesByUid = Object.fromEntries(nodes.map((n) => [n.uid, n.type]));
const nodeRows = (await window.roamAlphaAPI.data.async.pull_many(
`[:block/uid :create/user :create/time :edit/time :page/edit-time :node/title]`,
nodes.map((n) => [":block/uid", n.uid]),
)) as Record<string, Json>[];
const userEids = [
...new Set(
nodeRows.map(
(r) => (r[":create/user"] as Record<string, number>)[":db/id"],
),
),
];
const userRows = await window.roamAlphaAPI.data.async.pull_many(
`[:db/id :user/uid]`,
// @ts-expect-error array of dbIds is valid
userEids,
);
const userUidByEid = Object.fromEntries(
userRows.map((r) => [r[":db/id"] as number, r[":user/uid"] as string]),
);
const results = nodeRows.map((row) => {
const uid = row[":block/uid"] as string;
const userUid =
userUidByEid[(row[":create/user"] as Record<string, number>)[":db/id"]];

return {
localId: uid,
nodeType: typesByUid[uid],
authorId: userUid,
createdAt: new Date((row[":create/time"] as number) || Date.now()),
modifiedAt: new Date(
Math.max(
row[":edit/time"] as number,
row[":page/edit-time"] as number,
) || Date.now(),
),
Comment thread
maparent marked this conversation as resolved.
content: {
direct: {
localId: uid,
value: row[":node/title"] as string,
},
},
};
});
return results;
};