Skip to content

Commit 82c4365

Browse files
committed
dayjs
1 parent 82e044b commit 82c4365

6 files changed

Lines changed: 23 additions & 20 deletions

File tree

lib/queries/direct-contacts-query.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { DirectContact, useKeysQuery, useNostrFetchQuery } from "../nostr";
33
import { Event, Kind } from "nostr-tools";
44
import { useContext } from "react";
55
import { ChatContext } from "../chat-context-provider";
6-
import { isAfter } from "date-fns";
6+
import dayjs from "dayjs";
77

88
function convertEventToDirectContacts(
99
events: Event[],
@@ -95,7 +95,7 @@ export function useDirectContactsQuery() {
9595
if (
9696
a.lastSeenDate instanceof Date &&
9797
b.lastSeenDate instanceof Date &&
98-
isAfter(a.lastSeenDate, b.lastSeenDate)
98+
dayjs(a.lastSeenDate).isAfter(dayjs(b.lastSeenDate))
9999
) {
100100
return -1;
101101
}

lib/queries/unread-count-query.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from "../nostr";
77
import { useMessagesQuery } from "./messages-query";
88
import { useMemo } from "react";
9-
import { isAfter } from "date-fns";
9+
import dayjs from "dayjs";
1010

1111
export function useUnreadCountQuery(
1212
contact?: DirectContact,
@@ -20,7 +20,7 @@ export function useUnreadCountQuery(
2020
const computeUnread = (lastSeenDate: Date) => {
2121
const firstMessageAfterLastSeenDate = messages.findIndex(
2222
(m) =>
23-
isAfter(new Date(m.created * 1000), lastSeenDate) &&
23+
dayjs(m.created * 1000).isAfter(dayjs(lastSeenDate)) &&
2424
m.creator !== publicKey,
2525
);
2626
if (firstMessageAfterLastSeenDate > -1) {

lib/utils/format-message-time.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { format } from "date-fns";
1+
import dayjs from "dayjs";
2+
import advancedFormat from "dayjs/plugin/advancedFormat";
3+
4+
dayjs.extend(advancedFormat);
25

36
export const formatMessageTime = (unixTs: number) =>
4-
format(new Date(unixTs), "h:mm a");
7+
dayjs(unixTs).format("h:mm a");
58

69
export const formatMessageDate = (unixTs: number) =>
7-
format(new Date(unixTs), "dddd, MMMM Do");
10+
dayjs(unixTs).format("dddd, MMMM Do");

lib/utils/get-relative-date.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import { differenceInDays, differenceInHours, differenceInYears, format, getDay } from "date-fns";
1+
import dayjs from "dayjs";
22

33
export function getRelativeDate(timestamp?: number) {
44
if (!timestamp) {
55
return "";
66
}
77

8-
const date = new Date(timestamp * 1000);
9-
const now = new Date();
8+
const date = dayjs.unix(timestamp);
9+
const now = dayjs();
1010

11-
if (getDay(now) === getDay(date) && differenceInHours(now, date) <= 24) {
12-
return format(date, "HH:mm");
13-
} else if (differenceInDays(now, date) <= 7) {
14-
return format(date, "EEE");
15-
} else if (differenceInYears(now, date) === 0) {
16-
return format(date, "dd.MM");
11+
if (now.day() === date.day() && now.diff(date, "hour") <= 24) {
12+
return date.format("HH:mm");
13+
} else if (now.diff(date, "day") <= 7) {
14+
return date.format("ddd");
15+
} else if (now.diff(date, "year") === 0) {
16+
return date.format("DD.MM");
1717
} else {
18-
return format(date, "dd.MM.yyyy");
18+
return date.format("DD.MM.YYYY");
1919
}
2020
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ecency/ns-query",
33
"description": "React-query based Nostr protocol SDK for Ecency vision and mobile",
4-
"version": "1.2.11-next",
4+
"version": "1.2.12-next",
55
"repository": "https://github.com/ecency/ns-query",
66
"author": "ildar.timerbaev <dkildar@gmail.com>",
77
"license": "MIT",
@@ -39,7 +39,7 @@
3939
"peerDependencies": {
4040
"@tanstack/react-query": "^5.15.0",
4141
"axios": "1.6.2",
42-
"date-fns": "^2.30.0",
42+
"dayjs": "^1.11.10",
4343
"react": "^18.3.1"
4444
},
4545
"dependencies": {

vite.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default defineConfig({
2121
"react",
2222
"react/jsx-runtime",
2323
"@tanstack/react-query",
24-
"date-fns",
24+
"dayjs",
2525
"crypto",
2626
],
2727
},

0 commit comments

Comments
 (0)