Skip to content

Commit

Permalink
Bug Fix: #9 - realtimePosts는 SWR의 revalidateOnFocus가 적용되지 않으므로fromNow…
Browse files Browse the repository at this point in the history
… 값을 1분마다 다시 계산
  • Loading branch information
yeoularu committed Apr 12, 2024
1 parent cd7e920 commit 5d77488
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 35 deletions.
6 changes: 4 additions & 2 deletions src/hooks/usePostById.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import likeCountAtom from '@/atoms/likeCount';
import { Post } from '@/types/post';
import { fromNow } from '@/utils/dayjs';
import { getFromNow } from '@/utils/dayjs';
import { supabase } from '@/utils/supabaseClient';
import { useAtomValue } from 'jotai';
import useSWR from 'swr';
Expand All @@ -27,7 +27,9 @@ export default function usePostById(id?: string | number) {
);

const likeCountMap = useAtomValue(likeCountAtom);
const post = data ? { ...data, fromNow: fromNow(data.inserted_at) } : null;
const post = data
? { ...data, fromNow: getFromNow(data.inserted_at) }
: null;

if (post) {
const likeCount = likeCountMap.get(post.id);
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/usePosts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import likeCountAtom from '@/atoms/likeCount';
import { Post } from '@/types/post';
import { fromNow } from '@/utils/dayjs';
import { getFromNow } from '@/utils/dayjs';
import { supabase } from '@/utils/supabaseClient';
import { useAtomValue } from 'jotai';
import useSWRInfinite from 'swr/infinite';
Expand Down Expand Up @@ -39,7 +39,7 @@ export default function usePosts() {
? data.flatMap((page) =>
page.map((post) => ({
...post,
fromNow: fromNow(post.inserted_at),
fromNow: getFromNow(post.inserted_at),
})),
)
: [];
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/usePostsByIds.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import likeCountAtom from '@/atoms/likeCount';
import { Post } from '@/types/post';
import { fromNow } from '@/utils/dayjs';
import { getFromNow } from '@/utils/dayjs';
import { supabase } from '@/utils/supabaseClient';
import { useAtomValue } from 'jotai';
import useSWR from 'swr';
Expand All @@ -22,7 +22,7 @@ export default function usePostsByIds(ids: number[]) {
const posts = data
? data.map((post) => ({
...post,
fromNow: fromNow(post.inserted_at),
fromNow: getFromNow(post.inserted_at),
}))
: [];

Expand Down
4 changes: 2 additions & 2 deletions src/hooks/usePostsBySourceName.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import likeCountAtom from '@/atoms/likeCount';
import { Post } from '@/types/post';
import { fromNow } from '@/utils/dayjs';
import { getFromNow } from '@/utils/dayjs';
import { supabase } from '@/utils/supabaseClient';
import { useAtomValue } from 'jotai';
import useSWRInfinite from 'swr/infinite';
Expand Down Expand Up @@ -52,7 +52,7 @@ export default function usePostsBySourceName(og_source: string) {
? data.flatMap((page) =>
page.map((post) => ({
...post,
fromNow: fromNow(post.inserted_at),
fromNow: getFromNow(post.inserted_at),
})),
)
: [];
Expand Down
18 changes: 16 additions & 2 deletions src/hooks/useRealtimePosts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import likeCountAtom from '@/atoms/likeCount';
import realtimePostsAtom from '@/atoms/realtimePosts';
import { Post } from '@/types/post';
import { fromNow } from '@/utils/dayjs';
import { getFromNow } from '@/utils/dayjs';
import { supabase } from '@/utils/supabaseClient';
import { useAtom, useAtomValue } from 'jotai';
import { useEffect } from 'react';
Expand All @@ -23,7 +23,7 @@ export default function useRealtimePosts() {
(payload) => {
const post = {
...payload.new,
fromNow: fromNow(payload.new.inserted_at),
fromNow: getFromNow(payload.new.inserted_at),
} as Post;

setPosts((prev) => [post, ...prev]);
Expand Down Expand Up @@ -56,4 +56,18 @@ export default function useRealtimePosts() {
// setPosts가 수행되므로 posts를 의존성 배열에서 제외.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [likeCountMap]);

useEffect(() => {
const interval = setInterval(() => {
setPosts(
posts.map((post) => {
const newFromNow = getFromNow(post.inserted_at);
if (post.fromNow !== newFromNow) {
return { ...post, fromNow: newFromNow };
} else return post;
}),
);
}, 60000);
return () => clearInterval(interval);
});
}
50 changes: 25 additions & 25 deletions src/utils/dayjs.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import updateLocale from "dayjs/plugin/updateLocale";
import "dayjs/locale/ko";
import dayjs from 'dayjs';
import 'dayjs/locale/ko';
import relativeTime from 'dayjs/plugin/relativeTime';
import updateLocale from 'dayjs/plugin/updateLocale';

dayjs.extend(relativeTime);
dayjs.extend(updateLocale);

dayjs.updateLocale("ko", {
relativeTime: {
future: "%s 후",
past: "%s 전",
s: "몇 초",
m: "1분",
mm: "%d분",
h: "한 시간",
hh: "%d시간",
d: "하루",
dd: "%d일",
M: "한 달",
MM: "%d개월",
y: "일 년",
yy: "%d년",
},
dayjs.updateLocale('ko', {
relativeTime: {
future: '%s 후',
past: '%s 전',
s: '몇 초',
m: '1분',
mm: '%d분',
h: '한 시간',
hh: '%d시간',
d: '하루',
dd: '%d일',
M: '한 달',
MM: '%d개월',
y: '일 년',
yy: '%d년',
},
});

dayjs.locale("ko");
dayjs.locale('ko');

const fromNow = (date: string): string => {
return dayjs(date).fromNow();
const getFromNow = (date: string): string => {
return dayjs(date).fromNow();
};

const getKoreanFormat = (date: string): string => {
return dayjs(date).format("YYYY년 M월 D일 HH:mm");
return dayjs(date).format('YYYY년 M월 D일 HH:mm');
};

export { fromNow, getKoreanFormat };
export { getFromNow, getKoreanFormat };

0 comments on commit 5d77488

Please sign in to comment.