Skip to content
Open
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
2 changes: 1 addition & 1 deletion apps/obsidian/src/components/ImportNodesModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { StrictMode, useState, useEffect, useCallback } from "react";
import type DiscourseGraphPlugin from "../index";
import type { ImportableNode, GroupWithNodes } from "~/types";
import { getUserNameById } from "~/utils/typeUtils";
import { getAvailableGroupIds } from "@repo/database/lib/groups";
import {
fetchUserNames,
getAvailableGroupIds,
getPublishedNodesForGroups,
getLocalNodeInstanceIds,
getSpaceNameFromIds,
Expand Down
2 changes: 1 addition & 1 deletion apps/obsidian/src/components/PublishGroupDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
publishToSelectedGroupWithNotice,
withPublishedState,
} from "~/utils/publishGroupSelection";
import type { MyGroup } from "~/utils/importNodes";
import type { MyGroup } from "@repo/database/lib/groups";

type PublishGroupDropdownProps = {
plugin: DiscourseGraphPlugin;
Expand Down
48 changes: 0 additions & 48 deletions apps/obsidian/src/utils/importNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,54 +21,6 @@ import {
import { createTemplateFile } from "./templates";
import { resolveFolderForSpaceUri } from "./importFolderMetadata";

export type MyGroup = {
id: string;
name: string;
};

export const getAvailableGroupIds = async (
client: DGSupabaseClient,
): Promise<string[]> => {
const { data, error } = await client
.from("group_membership")
.select("group_id")
.eq("member_id", (await client.auth.getUser()).data.user?.id || "");

if (error) {
console.error("Error fetching groups:", error);
throw new Error(`Failed to fetch groups: ${error.message}`);
}

return (data || []).map((g) => g.group_id);
};

export const getMyGroups = async (
client: DGSupabaseClient,
): Promise<MyGroup[]> => {
const userId = (await client.auth.getUser()).data.user?.id ?? "";
const { data, error } = await client
.from("group_membership")
.select("group_id, my_groups!group_id(name)")
.eq("member_id", userId);

if (error) {
console.error("Error fetching groups:", error);
throw new Error(`Failed to fetch groups: ${error.message}`);
}

return (data ?? [])
.filter(
(row): row is { group_id: string; my_groups: { name: string | null } } =>
typeof row.group_id === "string" &&
row.my_groups !== null &&
typeof row.my_groups === "object",
)
.map((row) => ({
id: row.group_id,
name: row.my_groups.name ?? row.group_id,
}));
};

type PublishedNode = {
source_local_id: string;
space_id: number;
Expand Down
6 changes: 3 additions & 3 deletions apps/obsidian/src/utils/publishGroupSelection.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Notice, type FrontMatterCache, type TFile } from "obsidian";
import type DiscourseGraphPlugin from "~/index";
import { PublishGroupSuggestModal } from "~/components/PublishGroupSuggestModal";
import {
getAvailableGroupIds,
getMyGroups,
type MyGroup,
} from "~/utils/importNodes";
} from "@repo/database/lib/groups";
import type DiscourseGraphPlugin from "~/index";
import { PublishGroupSuggestModal } from "~/components/PublishGroupSuggestModal";
import { getLoggedInClient } from "~/utils/supabaseContext";
import {
getPublishedToGroups,
Expand Down
2 changes: 1 addition & 1 deletion apps/obsidian/src/utils/publishNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
type RelationsFile,
} from "./relationsStore";
import type { RelationInstance } from "~/types";
import { getAvailableGroupIds } from "./importNodes";
import { getAvailableGroupIds } from "@repo/database/lib/groups";
import {
syncAllNodesAndRelations,
syncPublishedNodeAssets,
Expand Down
2 changes: 1 addition & 1 deletion apps/obsidian/src/utils/templateImport.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/naming-convention -- Supabase query results use snake_case column names */
import type { Json } from "@repo/database/dbTypes";
import { getAvailableGroupIds } from "@repo/database/lib/groups";
import type DiscourseGraphPlugin from "~/index";
import {
fetchUserNames,
getAvailableGroupIds,
getSpaceNameFromIds,
getSpaceUris,
} from "./importNodes";
Expand Down
188 changes: 188 additions & 0 deletions apps/roam/src/components/Export.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ import getDiscourseRelations, {
} from "~/utils/getDiscourseRelations";
import { AddReferencedNodeType } from "./canvas/DiscourseRelationShape/DiscourseRelationTool";
import posthog from "posthog-js";
import { getMyGroups, type MyGroup } from "@repo/database/lib/groups";
import {
publishNodesToGroups,
type PublishNode,
} from "~/utils/publishNodesToGroups";
import { getLoggedInClient, getSupabaseContext } from "~/utils/supabaseContext";
import { isSyncEnabled } from "~/components/settings/utils/accessors";

const ExportProgress = ({ id }: { id: string }) => {
const [progress, setProgress] = useState(0);
Expand Down Expand Up @@ -213,6 +220,30 @@ const ExportDialog: ExportDialogComponent = ({

const [canSendToGitHub, setCanSendToGitHub] = useState(false);

const syncEnabled = useMemo(() => isSyncEnabled(), []);
const [myGroups, setMyGroups] = useState<MyGroup[]>([]);
const [groupsLoading, setGroupsLoading] = useState(false);
const [groupsLoaded, setGroupsLoaded] = useState(false);
const [selectedGroupIds, setSelectedGroupIds] = useState<string[]>([]);
const [groupsError, setGroupsError] = useState("");
const [publishError, setPublishError] = useState("");

const publishableNodes = useMemo(
() =>
syncEnabled
? results
.map((r) => {
const node = findDiscourseNode({ uid: r.uid });
return node && node.backedBy === "user"
? { uid: r.uid, type: node.type }
: null;
})
.filter((n): n is PublishNode => n !== null)
: [],
[results, syncEnabled],
);
const nonDiscourseCount = results.length - publishableNodes.length;

const writeFileToRepo = async ({
filename,
content,
Expand Down Expand Up @@ -764,6 +795,98 @@ const ExportDialog: ExportDialogComponent = ({
});
}
};
useEffect(() => {
if (
!syncEnabled ||
!isOpen ||
selectedTabId !== "publish" ||
groupsLoaded ||
groupsLoading
)
return;
setGroupsLoading(true);
void (async () => {
try {
const client = await getLoggedInClient();
if (!client) throw new Error("Could not connect to sync.");
const groups = await getMyGroups(client);
setMyGroups(groups);
} catch (e) {
setGroupsError((e as Error).message || "Failed to load groups.");
} finally {
setGroupsLoading(false);
setGroupsLoaded(true);
}
})();
}, [syncEnabled, isOpen, selectedTabId, groupsLoaded, groupsLoading]);

const handlePublish = async () => {
setPublishError("");
setLoading(true);
try {
const client = await getLoggedInClient();
const context = await getSupabaseContext();
if (!client || !context) throw new Error("Could not connect to sync.");
const {
publishedNodeUids,
skippedUnsyncedUids,
okGroupIds,
failedGroupIds,
} = await publishNodesToGroups({
client,
spaceId: context.spaceId,
groupIds: selectedGroupIds,
nodes: publishableNodes,
});
posthog.capture("Export Dialog: Publish", {
groupCount: okGroupIds.length,
publishedNodeCount: publishedNodeUids.length,
skippedUnsyncedCount: skippedUnsyncedUids.length,
nonDiscourseCount,
failedGroupCount: failedGroupIds.length,
});
const hasPublishedNodes = publishedNodeUids.length > 0;
const messages = hasPublishedNodes
? [
`Published ${publishedNodeUids.length} node${
publishedNodeUids.length === 1 ? "" : "s"
} to ${okGroupIds.length} group${
okGroupIds.length === 1 ? "" : "s"
}.`,
]
: ["No nodes were published."];
if (skippedUnsyncedUids.length)
messages.push(
`${skippedUnsyncedUids.length} not synced yet — try again shortly.`,
);
if (nonDiscourseCount)
messages.push(`${nonDiscourseCount} skipped (not discourse nodes).`);
if (failedGroupIds.length)
messages.push(
`${failedGroupIds.length} group${
failedGroupIds.length === 1 ? "" : "s"
} failed.`,
);
renderToast({
content: messages.join(" "),
intent:
failedGroupIds.length || !hasPublishedNodes ? "warning" : "success",
id: "query-builder-publish-success",
});
if (hasPublishedNodes) onClose();
} catch (e) {
internalError({
error: e as Error,
type: "Publish Dialog Failed",
userMessage:
"Looks like there was an error publishing. The team has been notified.",
});
setPublishError((e as Error).message);
} finally {
setLoading(false);
}
};

const ExportPanel = (
<>
<div className={Classes.DIALOG_BODY}>
Expand Down Expand Up @@ -1042,6 +1165,68 @@ const ExportDialog: ExportDialogComponent = ({
</>
);

const PublishPanel = (
<>
<div className={Classes.DIALOG_BODY}>
{groupsLoading || !groupsLoaded ? (
<div className="my-2.5">Loading groups…</div>
) : groupsError ? (
<div className="my-2.5">{groupsError}</div>
) : myGroups.length === 0 ? (
<div className="my-2.5">
You are not a member of any sharing group.
</div>
) : (
<>
<Label>Publish to group(s)</Label>
{myGroups.map((group) => (
<Checkbox
key={group.id}
checked={selectedGroupIds.includes(group.id)}
label={group.name}
onChange={(e) => {
const { checked } = e.target as HTMLInputElement;
setSelectedGroupIds((prev) =>
checked
? [...prev, group.id]
: prev.filter((id) => id !== group.id),
);
}}
/>
))}
<div className="mt-2.5">
{`Publishing ${publishableNodes.length} discourse node${
publishableNodes.length === 1 ? "" : "s"
}`}
{nonDiscourseCount > 0 &&
` (${nonDiscourseCount} non-discourse result${
nonDiscourseCount === 1 ? "" : "s"
} will be skipped)`}
</div>
</>
)}
</div>
<div className={Classes.DIALOG_FOOTER}>
<div className={Classes.DIALOG_FOOTER_ACTIONS}>
<span style={{ color: "darkred" }}>{publishError}</span>
<Button text={"Cancel"} intent={Intent.NONE} onClick={onClose} />
<Button
text={"Publish"}
intent={Intent.PRIMARY}
onClick={() => void handlePublish()}
loading={loading}
disabled={
loading ||
selectedGroupIds.length === 0 ||
publishableNodes.length === 0
}
style={{ minWidth: 64 }}
/>
</div>
</div>
</>
);

return (
<>
<Dialog
Expand All @@ -1067,6 +1252,9 @@ const ExportDialog: ExportDialogComponent = ({
>
<Tab id="sendto" title="Send To" panel={SendToPanel} />
<Tab id="export" title="Export" panel={ExportPanel} />
{syncEnabled && (
<Tab id="publish" title="Publish" panel={PublishPanel} />
)}
</Tabs>
</Dialog>
<ExportProgress id={exportId} />
Expand Down
Loading