Skip to content

Commit

Permalink
Fix invalid date parsing on lemmy 0.18.4 (#723)
Browse files Browse the repository at this point in the history
Resolves #616

Co-authored-by: Alexander Harding <[email protected]>
  • Loading branch information
alexwasserman and aeharding authored Sep 14, 2023
1 parent 777e066 commit d8e83e4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/features/inbox/messages/Time.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { differenceInDays, differenceInHours, format } from "date-fns";
import { fixLemmyDateString } from "../../../helpers/date";

interface TimeProps {
date: string;
}

export default function Time({ date: dateStr }: TimeProps) {
const date = new Date(`${dateStr}Z`);
const date = new Date(fixLemmyDateString(dateStr));

if (differenceInDays(new Date(), date) > 6) {
return <>{format(date, "PP")}</>;
Expand Down
10 changes: 7 additions & 3 deletions src/features/labels/Ago.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { formatDistanceToNowStrict } from "date-fns";
import { fixLemmyDateString } from "../../helpers/date";

interface AgoProps {
date: string;
Expand All @@ -10,9 +11,12 @@ export default function Ago({ date, className }: AgoProps) {
}

export function formatRelative(date: string): string {
const relativeDate = formatDistanceToNowStrict(new Date(`${date}Z`), {
addSuffix: false,
});
const relativeDate = formatDistanceToNowStrict(
new Date(fixLemmyDateString(date)),
{
addSuffix: false,
},
);

return getRelativeDateString(relativeDate);
}
Expand Down
5 changes: 4 additions & 1 deletion src/features/labels/Edited.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { pencil } from "ionicons/icons";
import { MouseEvent, useMemo } from "react";
import { formatRelative } from "./Ago";
import styled from "@emotion/styled";
import { fixLemmyDateString } from "../../helpers/date";

const Container = styled.div`
display: flex;
Expand Down Expand Up @@ -46,7 +47,9 @@ export default function Edited({ item, showDate, className }: EditedProps) {

present({
header: `Edited ${formatRelative(edited)} Ago`,
message: `Last edited on ${new Date(`${edited}Z`).toLocaleTimeString()}`,
message: `Last edited on ${new Date(
fixLemmyDateString(edited),
).toLocaleTimeString()}`,
buttons: ["OK"],
});
}
Expand Down
5 changes: 3 additions & 2 deletions src/features/user/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import PostCommentFeed, {
isPost,
} from "../feed/PostCommentFeed";
import { handleSelector, jwtSelector } from "../auth/authSlice";
import { fixLemmyDateString } from "../../helpers/date";

export const InsetIonItem = styled(IonItem)`
--background: var(--ion-tab-bar-background, var(--ion-color-step-50, #fff));
Expand Down Expand Up @@ -118,6 +119,6 @@ export default function Profile({ person }: ProfileProps) {
}

function getCreatedDate(item: PostCommentItem): number {
if (isPost(item)) return Date.parse(`${item.post.published}Z`);
return Date.parse(`${item.comment.published}Z`);
if (isPost(item)) return Date.parse(fixLemmyDateString(item.post.published));
return Date.parse(fixLemmyDateString(item.comment.published));
}
3 changes: 2 additions & 1 deletion src/features/user/Scores.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { formatNumber } from "../../helpers/number";
import Ago from "../labels/Ago";
import { useIonAlert } from "@ionic/react";
import { formatDistanceToNowStrict } from "date-fns";
import { fixLemmyDateString } from "../../helpers/date";

const Container = styled.div`
display: flex;
Expand Down Expand Up @@ -35,7 +36,7 @@ export default function Scores({ aggregates, accountCreated }: ScoreProps) {
const [present] = useIonAlert();

const relativeDate = formatDistanceToNowStrict(
new Date(`${accountCreated}Z`),
new Date(fixLemmyDateString(accountCreated)),
{
addSuffix: false,
},
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Lemmy <= 0.18.3 has a bug where the Z isn't appended
*
* @param rawLemmyDateString Bugged lemmy date string
* @returns Consistent date string, ready to be passed to Date
*/
export function fixLemmyDateString(rawLemmyDateString: string): string {
if (rawLemmyDateString.endsWith("Z")) return rawLemmyDateString;

return `${rawLemmyDateString}Z`;
}

0 comments on commit d8e83e4

Please sign in to comment.