diff --git a/actions/backdatePost.ts b/actions/backdatePost.ts deleted file mode 100644 index f0d688fc..00000000 --- a/actions/backdatePost.ts +++ /dev/null @@ -1,86 +0,0 @@ -"use server"; - -import { AtpBaseClient, PubLeafletDocument } from "lexicons/api"; -import { restoreOAuthSession, OAuthSessionError } from "src/atproto-oauth"; -import { getIdentityData } from "actions/getIdentityData"; -import { supabaseServerClient } from "supabase/serverClient"; -import { Json } from "supabase/database.types"; -import { AtUri } from "@atproto/syntax"; - -type BackdateResult = - | { success: true; publishedAt: string } - | { success: false; error?: OAuthSessionError | string }; - -export async function backdatePost({ - uri, - publishedAt, -}: { - uri: string; - publishedAt: string; -}): Promise { - let identity = await getIdentityData(); - if (!identity || !identity.atp_did) { - return { - success: false, - error: "Not authenticated", - }; - } - - const sessionResult = await restoreOAuthSession(identity.atp_did); - if (!sessionResult.ok) { - return { success: false, error: sessionResult.error }; - } - let credentialSession = sessionResult.value; - let agent = new AtpBaseClient( - credentialSession.fetchHandler.bind(credentialSession), - ); - - // Get the existing document - let { data: existingDoc } = await supabaseServerClient - .from("documents") - .select("*") - .eq("uri", uri) - .single(); - - if (!existingDoc) { - return { success: false, error: "Document not found" }; - } - - let record = existingDoc.data as PubLeafletDocument.Record; - - // Check if the user is the author - if (record.author !== identity.atp_did) { - return { success: false, error: "Not authorized" }; - } - - let aturi = new AtUri(uri); - - // Update the record with the new publishedAt date - let updatedRecord: PubLeafletDocument.Record = { - ...record, - publishedAt, - }; - - // Update the record on ATP - let result = await agent.com.atproto.repo.putRecord({ - repo: credentialSession.did!, - rkey: aturi.rkey, - record: updatedRecord, - collection: record.$type, - validate: false, - }); - - // Optimistically write to our db - let { error } = await supabaseServerClient - .from("documents") - .update({ - data: updatedRecord as Json, - }) - .eq("uri", uri); - - if (error) { - return { success: false, error: error.message }; - } - - return { success: true, publishedAt }; -} diff --git a/actions/publishToPublication.ts b/actions/publishToPublication.ts index 04717287..b5164848 100644 --- a/actions/publishToPublication.ts +++ b/actions/publishToPublication.ts @@ -201,7 +201,7 @@ export async function publishToPublication({ } }), publishedAt: - existingRecord?.publishedAt || publishedAt || new Date().toISOString(), + publishedAt || existingRecord?.publishedAt || new Date().toISOString(), }; // Keep the same rkey if updating an existing document diff --git a/app/[leaflet_id]/actions/PublishButton.tsx b/app/[leaflet_id]/actions/PublishButton.tsx index f0140132..ee739b60 100644 --- a/app/[leaflet_id]/actions/PublishButton.tsx +++ b/app/[leaflet_id]/actions/PublishButton.tsx @@ -95,6 +95,12 @@ const UpdateButton = () => { tx.get("publication_cover_image"), ); + // Get localPublishedAt from Replicache state + let localPublishedAt = useSubscribe(rep, (tx) => + tx.get("publication_local_published_at"), + ); + console.log("local: " + localPublishedAt); + return ( { description: currentDescription, tags: currentTags, cover_image: coverImage, + ...(localPublishedAt && { publishedAt: localPublishedAt }), }); setIsLoading(false); mutate(); diff --git a/app/[leaflet_id]/publish/PublishPost.tsx b/app/[leaflet_id]/publish/PublishPost.tsx index ca82eaec..7aa301fe 100644 --- a/app/[leaflet_id]/publish/PublishPost.tsx +++ b/app/[leaflet_id]/publish/PublishPost.tsx @@ -92,8 +92,7 @@ const PublishPostForm = ( ); // Use Replicache tags only when we have a draft - const hasDraft = props.hasDraft; - const currentTags = hasDraft + const currentTags = props.hasDraft ? Array.isArray(replicacheTags) ? replicacheTags : [] @@ -101,7 +100,7 @@ const PublishPostForm = ( // Update tags via Replicache mutation or local state depending on context const handleTagsChange = async (newTags: string[]) => { - if (hasDraft) { + if (props.hasDraft) { await rep?.mutate.updatePublicationDraft({ tags: newTags, }); diff --git a/components/Pages/Backdater.tsx b/components/Pages/Backdater.tsx index 8cc5eb99..e630efd6 100644 --- a/components/Pages/Backdater.tsx +++ b/components/Pages/Backdater.tsx @@ -4,8 +4,10 @@ import { useState } from "react"; import { timeAgo } from "src/utils/timeAgo"; import { Popover } from "components/Popover"; import { Separator } from "react-aria-components"; +import { useReplicache } from "src/replicache"; export const Backdater = (props: { publishedAt: string }) => { + let { rep } = useReplicache(); let [localPublishedAt, setLocalPublishedAt] = useState( new Date(props.publishedAt), ); @@ -16,7 +18,7 @@ export const Backdater = (props: { publishedAt: string }) => { let currentTime = `${new Date().getHours().toString().padStart(2, "0")}:${new Date().getMinutes().toString().padStart(2, "0")}`; - const handleTimeChange = (time: string) => { + const handleTimeChange = async (time: string) => { setTimeValue(time); const [hours, minutes] = time.split(":").map((str) => parseInt(str, 10)); const newDate = new Date(localPublishedAt); @@ -27,10 +29,18 @@ export const Backdater = (props: { publishedAt: string }) => { if (newDate > currentDate) { setLocalPublishedAt(currentDate); setTimeValue(currentTime); - } else setLocalPublishedAt(newDate); + await rep?.mutate.updatePublicationDraft({ + localPublishedAt: currentDate.toISOString(), + }); + } else { + setLocalPublishedAt(newDate); + await rep?.mutate.updatePublicationDraft({ + localPublishedAt: newDate.toISOString(), + }); + } }; - const handleDateChange = (date: Date | undefined) => { + const handleDateChange = async (date: Date | undefined) => { if (!date) return; const [hours, minutes] = timeValue .split(":") @@ -43,9 +53,16 @@ export const Backdater = (props: { publishedAt: string }) => { if (newDate > currentDate) { setLocalPublishedAt(currentDate); setTimeValue(currentTime); - } else setLocalPublishedAt(newDate); + await rep?.mutate.updatePublicationDraft({ + localPublishedAt: currentDate.toISOString(), + }); + } else { + setLocalPublishedAt(newDate); + await rep?.mutate.updatePublicationDraft({ + localPublishedAt: newDate.toISOString(), + }); + } }; - console.log(localPublishedAt); return ( = async (args, ctx) => { await ctx.runOnServer(async (serverCtx) => { console.log("updating"); @@ -670,13 +671,14 @@ const updatePublicationDraft: Mutation<{ } }); await ctx.runOnClient(async ({ tx }) => { - if (args.title !== undefined) - await tx.set("publication_title", args.title); + if (args.title !== undefined) await tx.set("publication_title", args.title); if (args.description !== undefined) await tx.set("publication_description", args.description); if (args.tags !== undefined) await tx.set("publication_tags", args.tags); if (args.cover_image !== undefined) await tx.set("publication_cover_image", args.cover_image); + if (args.localPublishedAt !== undefined) + await tx.set("publication_local_published_at", args.localPublishedAt); }); };