{
- Save as Draft
-
+
+ {
+ if (!selectedPub) return;
+ e.preventDefault();
+ if (selectedPub === "create") return;
+ router.push(
+ `${permission_token.id}/publish?publication_uri=${encodeURIComponent(selectedPub)}&title=${encodeURIComponent(title)}&description=${encodeURIComponent(description)}`,
+ );
+ }}
+ >
Next{selectedPub === "create" && ": Create Pub!"}
@@ -142,33 +195,54 @@ const PublishToPublicationButton = (props: { entityID: string }) => {
);
};
-const PostDetailsForm = (props: { entityID: string }) => {
- let [description, setDescription] = useState("");
-
- let rootPage = useEntity(props.entityID, "root/page")[0].data.value;
- let firstBlock = useBlocks(rootPage)[0];
- let firstBlockText = useEntity(firstBlock?.value, "block/text")?.data.value;
+const SaveAsDraftButton = (props: {
+ selectedPub: string | undefined;
+ leafletId: string;
+ metadata: { title: string; description: string };
+ entitiesToDelete: string[];
+}) => {
+ let { mutate } = useLeafletPublicationData();
+ let { rep } = useReplicache();
+ let [isLoading, setIsLoading] = useState(false);
- const leafletTitle = useMemo(() => {
- if (!firstBlockText) return "Untitled";
- let doc = new Y.Doc();
- const update = base64.toByteArray(firstBlockText);
- Y.applyUpdate(doc, update);
- let nodes = doc.getXmlElement("prosemirror").toArray();
- return YJSFragmentToString(nodes[0]) || "Untitled";
- }, [firstBlockText]);
+ return (
+ {
+ if (!props.selectedPub) return;
+ if (props.selectedPub === "create") return;
+ e.preventDefault();
+ setIsLoading(true);
+ await moveLeafletToPublication(
+ props.leafletId,
+ props.selectedPub,
+ props.metadata,
+ props.entitiesToDelete,
+ );
+ await Promise.all([rep?.pull(), mutate()]);
+ setIsLoading(false);
+ }}
+ >
+ {isLoading ? : "Save as Draft"}
+
+ );
+};
+const PostDetailsForm = (props: {
+ title: string;
+ description: string;
+ setDescription: (d: string) => void;
+}) => {
return (
Post Details
-
+
setDescription(e.currentTarget.value)}
+ onChange={(e) => props.setDescription(e.currentTarget.value)}
/>
@@ -271,3 +345,40 @@ const PubOption = (props: {
);
};
+
+let useTitle = (entityID: string) => {
+ let rootPage = useEntity(entityID, "root/page")[0].data.value;
+ let firstBlock = useBlocks(rootPage)[0]?.value;
+
+ let firstBlockText = useEntity(firstBlock, "block/text")?.data.value;
+
+ const leafletTitle = useMemo(() => {
+ if (!firstBlockText) return "Untitled";
+ let doc = new Y.Doc();
+ const update = base64.toByteArray(firstBlockText);
+ Y.applyUpdate(doc, update);
+ let nodes = doc.getXmlElement("prosemirror").toArray();
+ return YJSFragmentToString(nodes[0]) || "Untitled";
+ }, [firstBlockText]);
+
+ let secondBlock = useBlocks(rootPage)[1];
+ let secondBlockTextValue = useEntity(secondBlock.value, "block/text")?.data
+ .value;
+ const secondBlockText = useMemo(() => {
+ if (!secondBlockTextValue) return "";
+ let doc = new Y.Doc();
+ const update = base64.toByteArray(secondBlockTextValue);
+ Y.applyUpdate(doc, update);
+ let nodes = doc.getXmlElement("prosemirror").toArray();
+ return YJSFragmentToString(nodes[0]) || "";
+ }, [firstBlockText]);
+
+ let entitiesToDelete = useMemo(() => {
+ let etod = [firstBlock];
+ if (secondBlockText.trim() === "" && secondBlock.type === "text")
+ etod.push(secondBlock.value);
+ return etod;
+ }, [firstBlock, secondBlockText, secondBlock]);
+
+ return { title: leafletTitle, entitiesToDelete };
+};
diff --git a/app/[leaflet_id]/publish/page.tsx b/app/[leaflet_id]/publish/page.tsx
index 578afb69..592990ff 100644
--- a/app/[leaflet_id]/publish/page.tsx
+++ b/app/[leaflet_id]/publish/page.tsx
@@ -13,6 +13,11 @@ export const fetchCache = "force-no-store";
type Props = {
// this is now a token id not leaflet! Should probs rename
params: Promise<{ leaflet_id: string }>;
+ searchParams: Promise<{
+ publication_uri: string;
+ title: string;
+ description: string;
+ }>;
};
export default async function PublishLeafletPage(props: Props) {
let leaflet_id = (await props.params).leaflet_id;
@@ -32,7 +37,20 @@ export default async function PublishLeafletPage(props: Props) {
.eq("id", leaflet_id)
.single();
let rootEntity = data?.root_entity;
- if (!data || !rootEntity || !data.leaflets_in_publications[0])
+ let publication = data?.leaflets_in_publications[0]?.publications;
+ if (!publication) {
+ let pub_uri = (await props.searchParams).publication_uri;
+ if (!pub_uri) return;
+ console.log(decodeURIComponent(pub_uri));
+ let { data, error } = await supabaseServerClient
+ .from("publications")
+ .select("*, documents_in_publications(count)")
+ .eq("uri", decodeURIComponent(pub_uri))
+ .single();
+ console.log(error);
+ publication = data;
+ }
+ if (!data || !rootEntity || !publication)
return (
missin something
@@ -42,7 +60,12 @@ export default async function PublishLeafletPage(props: Props) {
let identity = await getIdentityData();
if (!identity || !identity.atp_did) return null;
- let pub = data.leaflets_in_publications[0];
+ let title =
+ data.leaflets_in_publications[0]?.title ||
+ decodeURIComponent((await props.searchParams).title);
+ let description =
+ data.leaflets_in_publications[0]?.description ||
+ decodeURIComponent((await props.searchParams).description);
let agent = new AtpAgent({ service: "https://public.api.bsky.app" });
let profile = await agent.getProfile({ actor: identity.atp_did });
@@ -57,11 +80,11 @@ export default async function PublishLeafletPage(props: Props) {
leaflet_id={leaflet_id}
root_entity={rootEntity}
profile={profile.data}
- title={pub.title}
- publication_uri={pub.publication}
- description={pub.description}
- record={pub.publications?.record as PubLeafletPublication.Record}
- posts_in_pub={pub.publications?.documents_in_publications[0].count}
+ title={title}
+ description={description}
+ publication_uri={publication.uri}
+ record={publication.record as PubLeafletPublication.Record}
+ posts_in_pub={publication.documents_in_publications[0].count}
/>
);