tangled
alpha
login
or
join now
leaflet.pub
/
leaflet
289
fork
atom
a tool for shared writing and social publishing
289
fork
atom
overview
issues
28
pulls
pipelines
wire up updating pubish date on published post
cozylittle.house
2 months ago
c29ddc1c
cf5110e7
+24
-27
3 changed files
expand all
collapse all
unified
split
app
[leaflet_id]
actions
PublishButton.tsx
components
Pages
Backdater.tsx
PublicationMetadata.tsx
+6
-6
app/[leaflet_id]/actions/PublishButton.tsx
···
24
24
import { DotLoader } from "components/utils/DotLoader";
25
25
import { PubLeafletPublication } from "lexicons/api";
26
26
import { useParams, useRouter, useSearchParams } from "next/navigation";
27
27
-
import { useState, useMemo } from "react";
27
27
+
import { useState, useMemo, useEffect } from "react";
28
28
import { useIsMobile } from "src/hooks/isMobile";
29
29
import { useReplicache, useEntity } from "src/replicache";
30
30
import { useSubscribe } from "src/replicache/useSubscribe";
···
40
40
import { moveLeafletToPublication } from "actions/publications/moveLeafletToPublication";
41
41
import { AddTiny } from "components/Icons/AddTiny";
42
42
import { OAuthErrorMessage, isOAuthSessionError } from "components/OAuthError";
43
43
+
import { useLocalPublishedAt } from "components/Pages/Backdater";
43
44
44
45
export const PublishButton = (props: { entityID: string }) => {
45
46
let { data: pub } = useLeafletPublicationData();
···
95
96
tx.get<string | null>("publication_cover_image"),
96
97
);
97
98
98
98
-
// Get localPublishedAt from Replicache state
99
99
-
let localPublishedAt = useSubscribe(rep, (tx) =>
100
100
-
tx.get<string | null>("publication_local_published_at"),
99
99
+
// Get local published at from Replicache (session-only state, not persisted to DB)
100
100
+
let publishedAt = useLocalPublishedAt((s) =>
101
101
+
pub?.doc ? s[pub?.doc] : undefined,
101
102
);
102
102
-
console.log("local: " + localPublishedAt);
103
103
104
104
return (
105
105
<ActionButton
···
117
117
description: currentDescription,
118
118
tags: currentTags,
119
119
cover_image: coverImage,
120
120
-
...(localPublishedAt && { publishedAt: localPublishedAt }),
120
120
+
publishedAt: publishedAt?.toISOString(),
121
121
});
122
122
setIsLoading(false);
123
123
mutate();
+15
-20
components/Pages/Backdater.tsx
···
1
1
"use client";
2
2
import { DatePicker, TimePicker } from "components/DatePicker";
3
3
-
import { useState } from "react";
3
3
+
import { useMemo, useState } from "react";
4
4
import { timeAgo } from "src/utils/timeAgo";
5
5
import { Popover } from "components/Popover";
6
6
import { Separator } from "react-aria-components";
7
7
import { useReplicache } from "src/replicache";
8
8
+
import { create } from "zustand";
8
9
9
9
-
export const Backdater = (props: { publishedAt: string }) => {
10
10
+
export const useLocalPublishedAt = create<{ [uri: string]: Date }>(() => ({}));
11
11
+
export const Backdater = (props: { publishedAt: string; docURI: string }) => {
10
12
let { rep } = useReplicache();
11
11
-
let [localPublishedAt, setLocalPublishedAt] = useState(
12
12
-
new Date(props.publishedAt),
13
13
+
let localPublishedAtDate = useLocalPublishedAt((s) =>
14
14
+
s[props.docURI] ? s[props.docURI] : null,
15
15
+
);
16
16
+
let localPublishedAt = useMemo(
17
17
+
() => localPublishedAtDate || new Date(props.publishedAt),
18
18
+
[localPublishedAtDate, props.publishedAt],
13
19
);
14
20
15
21
let [timeValue, setTimeValue] = useState(
···
27
33
28
34
let currentDate = new Date();
29
35
if (newDate > currentDate) {
30
30
-
setLocalPublishedAt(currentDate);
36
36
+
useLocalPublishedAt.setState({ [props.docURI]: currentDate });
31
37
setTimeValue(currentTime);
32
32
-
await rep?.mutate.updatePublicationDraft({
33
33
-
localPublishedAt: currentDate.toISOString(),
34
34
-
});
35
38
} else {
36
36
-
setLocalPublishedAt(newDate);
37
37
-
await rep?.mutate.updatePublicationDraft({
38
38
-
localPublishedAt: newDate.toISOString(),
39
39
-
});
39
39
+
useLocalPublishedAt.setState({ [props.docURI]: newDate });
40
40
}
41
41
};
42
42
···
51
51
52
52
let currentDate = new Date();
53
53
if (newDate > currentDate) {
54
54
-
setLocalPublishedAt(currentDate);
54
54
+
useLocalPublishedAt.setState({ [props.docURI]: currentDate });
55
55
+
55
56
setTimeValue(currentTime);
56
56
-
await rep?.mutate.updatePublicationDraft({
57
57
-
localPublishedAt: currentDate.toISOString(),
58
58
-
});
59
57
} else {
60
60
-
setLocalPublishedAt(newDate);
61
61
-
await rep?.mutate.updatePublicationDraft({
62
62
-
localPublishedAt: newDate.toISOString(),
63
63
-
});
58
58
+
useLocalPublishedAt.setState({ [props.docURI]: newDate });
64
59
}
65
60
};
66
61
+3
-1
components/Pages/PublicationMetadata.tsx
···
99
99
<div className="flex gap-2 items-center">
100
100
<p className="text-sm text-tertiary">
101
101
Published{" "}
102
102
-
{publishedAt && <Backdater publishedAt={publishedAt} />}
102
102
+
{publishedAt && (
103
103
+
<Backdater publishedAt={publishedAt} docURI={pub.doc} />
104
104
+
)}
103
105
</p>
104
106
105
107
<Link