Skip to content

Commit

Permalink
Set Correct Initial Data for Difference Calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
NPJigaK committed Oct 21, 2023
1 parent b8837ef commit 1d99fdf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
7 changes: 4 additions & 3 deletions components/AppContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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, {
Expand Down
8 changes: 7 additions & 1 deletion lib/accessTwitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion lib/debugLogger.ts
Original file line number Diff line number Diff line change
@@ -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;
};
Expand Down

0 comments on commit 1d99fdf

Please sign in to comment.