From 1d99fdfa5df0a9753cfdb39bfdefecc35c781e43 Mon Sep 17 00:00:00 2001 From: NPJigaK Date: Sun, 22 Oct 2023 04:50:54 +0900 Subject: [PATCH] Set Correct Initial Data for Difference Calculation --- components/AppContainer.tsx | 7 ++++--- lib/accessTwitch.ts | 8 +++++++- lib/debugLogger.ts | 5 ++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/components/AppContainer.tsx b/components/AppContainer.tsx index 8c98254..3f69a56 100644 --- a/components/AppContainer.tsx +++ b/components/AppContainer.tsx @@ -18,7 +18,7 @@ import RefreshListsButton from "./RefreshListsButton"; import CheckDoneButton from "./CheckDoneButton"; import { useNowAllFollowers } from "@/lib/accessTwitch"; import { debugLogger } from "@/lib/debugLogger"; -import { lastCheckedDateKey } from "@/lib/constants"; +import { lastCheckedDateKey, storedAllFollowersKey } from "@/lib/constants"; export default function AppContainer() { debugLogger("AppContainer"); @@ -105,10 +105,11 @@ export default function AppContainer() { useEffect(() => { debugLogger("onChecked();"); - if (!localStorage.getItem(lastCheckedDateKey)) { + if (!localStorage.getItem(storedAllFollowersKey)) { onChecked(); } - }, [onChecked]); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); function formatToLocaleString(date: Date): string { return date.toLocaleString(undefined, { diff --git a/lib/accessTwitch.ts b/lib/accessTwitch.ts index 62b25c8..eddb59a 100644 --- a/lib/accessTwitch.ts +++ b/lib/accessTwitch.ts @@ -110,10 +110,16 @@ export const useNowAllFollowers = () => { const resJson = await res.json(); const followers = await fetchFollowers(resJson.data[0].id); + if (!localStorage.getItem(storedAllFollowersKey)) { + // addedUsers の差分が全てのユーザーにならないように、初期値はfollowers + localStorage.setItem(storedAllFollowersKey, JSON.stringify(followers)); + } + const result = await findDifference( followers, - JSON.parse(localStorage.getItem(storedAllFollowersKey) ?? "[]") + JSON.parse(localStorage.getItem(storedAllFollowersKey) as string) ); + debugLogger(result.removedUsers); debugLogger(result.addedUsers); setNowAllFollowers(followers); diff --git a/lib/debugLogger.ts b/lib/debugLogger.ts index ed1a184..2750f60 100644 --- a/lib/debugLogger.ts +++ b/lib/debugLogger.ts @@ -1,7 +1,10 @@ const isDebugMode = (): boolean => { if (typeof window !== "undefined") { // Make it server-side compatible. - return window.location.hash.includes("debug"); + return ( + window.location.hash.includes("debug") || + window.location.hostname === "localhost" + ); } return false; };