Skip to content

Commit

Permalink
refac: timerのロジックのリファクタリング
Browse files Browse the repository at this point in the history
  • Loading branch information
ayanami77 committed May 20, 2024
1 parent b06ddce commit b329d9e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
"lineWidth": 120
},
"files": {
"ignore": ["src/routeTree.gen.ts"]
"ignore": ["src/routeTree.gen.ts", "src/Rhythmate-Service/*"]
}
}
8 changes: 3 additions & 5 deletions src/features/common/hooks/useInterval.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { useEffect } from "react";

const useInterval = (callback: any, delay?: number | null) => {
const useInterval = (callback: () => void, delay: number) => {
useEffect(() => {
if (delay !== null) {
const interval = setInterval(() => callback(), delay || 0);
return () => clearInterval(interval);
}
const interval = setInterval(() => callback(), delay || 0);
return () => clearInterval(interval);
}, [callback, delay]);
};

Expand Down
12 changes: 4 additions & 8 deletions src/features/quests/components/QuestBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const QuestBoard: FC<Props> = (props) => {
currentQuest.minutes,
currentQuest.startedAt,
);

const [questStatus, setQuestStatus] = useState<QuestStatus>(status);

useInterval(() => {
Expand All @@ -42,19 +41,16 @@ export const QuestBoard: FC<Props> = (props) => {

const { diffHH, diffMM, diffSS } = calcDiffTimeBetweenNowAndTargetTime(baseTime);

if (status === FORCE_STOP && questStatus !== FORCE_STOP) {
// クエスト強制終了へ切り替える
if (status === FORCE_STOP) {
setQuestStatus(FORCE_STOP);
}
// クエスト解放へ切り替える
if (diffHH === diffMM && diffMM === diffSS && questStatus === CLOSED) {
if (diffHH === 0 && diffMM === 0 && diffSS === 0 && questStatus === CLOSED) {
setQuestStatus(OPEN);
}
// クエスト集中へ切り替える
if (diffHH === diffMM && diffMM === diffSS && questStatus === OPEN) {
setQuestStatus(ENGAGED);
}
// クエスト終了へ切り替える
if (diffHH === diffMM && diffMM === diffSS && questStatus === ENGAGED) {
if (diffHH === 0 && diffMM === 0 && diffSS === 0 && questStatus === ENGAGED) {
setQuestStatus(DONE);
}
}, 1000);
Expand Down

0 comments on commit b329d9e

Please sign in to comment.