Skip to content

Commit

Permalink
🐛 Fix hydration error
Browse files Browse the repository at this point in the history
  • Loading branch information
journey-ad committed Jul 23, 2024
1 parent f63f955 commit 677b67f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 27 deletions.
10 changes: 3 additions & 7 deletions app/api/graphql/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,15 +406,11 @@ FROM

return {
...data,
updated_at: Math.floor(new Date(data.updated_at).getTime() / 1000),
updated_at: Math.floor(data.updated_at / 1000),
latest_torrent: {
...data.latest_torrent,
created_at: Math.floor(
new Date(data.latest_torrent.created_at).getTime() / 1000,
),
updated_at: Math.floor(
new Date(data.latest_torrent.updated_at).getTime() / 1000,
),
created_at: Math.floor(data.latest_torrent.created_at / 1000),
updated_at: Math.floor(data.latest_torrent.updated_at / 1000),
},
};
} catch (error) {
Expand Down
4 changes: 1 addition & 3 deletions app/error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ export default function Error({
useEffect(() => {
// Log the error to an error reporting service
/* eslint-disable no-console */
console.error(error);
console.error("error", error.message, error.digest);
}, [error]);

console.error("error", error.message, error.digest);

const t = useTranslations("ERROR_MESSAGE");

return (
Expand Down
33 changes: 24 additions & 9 deletions components/DetailContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
useDisclosure,
} from "@nextui-org/react";
import { useTranslations } from "next-intl";
import { useEffect, useState } from "react";
import { useEffect, useState, Suspense } from "react";

import { TorrentItemProps } from "@/types";
import {
Expand All @@ -29,6 +29,7 @@ import useBreakpoint from "@/hooks/useBreakpoints";
import FileList from "@/components/FileList";
import { CopyIcon } from "@/components/icons";
import EmblaCarousel from "@/components//EmblaCarousel";
import { useHydration } from "@/hooks/useHydration";

const Preview = ({
linkInfo,
Expand Down Expand Up @@ -122,6 +123,8 @@ export const DetailContent = ({
const t = useTranslations();
const { isXs } = useBreakpoint();

const hydrated = useHydration();

return (
<>
{/* Torrent name */}
Expand All @@ -145,10 +148,16 @@ export const DetailContent = ({
{t("Search.file_count")}
{data.files.length}
</span>
<span>
{t("Search.created_at")}
{formatDate(data.created_at, t("COMMON.DATE_FORMAT"))}
</span>
<Suspense key={hydrated ? "load" : "loading"}>
<span>
{t("Search.created_at")}
{formatDate(
data.created_at,
t("COMMON.DATE_FORMAT"),
!hydrated,
)}
</span>
</Suspense>
<span>
{t("Search.hash")}
<span className="border rounded-sm px-1 font-mono bg-gray-100 dark:bg-inherit dark:border-slate-800">
Expand Down Expand Up @@ -216,10 +225,16 @@ export const DetailContent = ({
{t("Search.file_count")}
{data.files.length}
</span>
<span>
{t("Search.created_at")}
{formatDate(data.created_at, t("COMMON.DATE_FORMAT"))}
</span>
<Suspense key={hydrated ? "load" : "loading"}>
<span>
{t("Search.created_at")}
{formatDate(
data.created_at,
t("COMMON.DATE_FORMAT"),
!hydrated,
)}
</span>
</Suspense>
</div>
</CardFooter>
</Card>
Expand Down
2 changes: 1 addition & 1 deletion components/FloatTool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const SwitchLanguage = ({ noBg = false }: { noBg?: boolean }) => {
setlang(key);
Cookie.set("NEXT_LOCALE", Array.from(key)[0], {
path: "/",
expires: new Date(new Date().setFullYear(new Date().getFullYear() + 1)),
expires: 365,
});

if (typeof window !== "undefined") {
Expand Down
15 changes: 10 additions & 5 deletions components/SearchResultsItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import React from "react";
import { Suspense } from "react";
import {
Card,
CardHeader,
Expand All @@ -23,6 +23,7 @@ import {
} from "@/utils";
import FileList from "@/components/FileList";
import { SEARCH_DISPLAY_FILES_MAX } from "@/config/constant";
import { useHydration } from "@/hooks/useHydration";

export default function SearchResultsItem({
item,
Expand All @@ -40,6 +41,8 @@ export default function SearchResultsItem({

const t = useTranslations();

const hydrated = useHydration();

return (
<Card className="w-full bg-opacity-80 dark:brightness-95">
<CardHeader className="flex gap-3 bg-gray-100 dark:bg-slate-800">
Expand Down Expand Up @@ -89,10 +92,12 @@ export default function SearchResultsItem({
{t("Search.file_count")}
{data.files.length}
</span>
<span>
{t("Search.created_at")}
{formatDate(data.created_at, t("COMMON.DATE_FORMAT"))}
</span>
<Suspense key={hydrated ? "load" : "loading"}>
<span>
{t("Search.created_at")}
{formatDate(data.created_at, t("COMMON.DATE_FORMAT"), !hydrated)}
</span>
</Suspense>
</div>
</CardFooter>
</Card>
Expand Down
11 changes: 11 additions & 0 deletions hooks/useHydration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useState, useEffect } from "react";

export function useHydration() {
const [hydrated, setHydrated] = useState(false);

useEffect(() => {
setHydrated(true);
}, []);

return hydrated;
}
12 changes: 10 additions & 2 deletions utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,16 @@ export function formatByteSize(bytes: number | string) {
return `${convertedSize} ${units[digitGroups]}`;
}

export function formatDate(ts: number, format = "YYYY-MM-DD HH:mm:ss") {
return dayjs.unix(ts).format(format);
export function formatDate(
ts: number,
format = "YYYY-MM-DD HH:mm:ss",
utc = false,
) {
let dateStr = dayjs.unix(ts).format(format);

if (utc) dateStr += " (UTC)";

return dateStr;
}

export function getSizeColor(size: number | string) {
Expand Down

0 comments on commit 677b67f

Please sign in to comment.