Skip to content

Commit

Permalink
Merge pull request #264 from ayanami77/refac/timer
Browse files Browse the repository at this point in the history
  • Loading branch information
ayanami77 committed May 20, 2024
2 parents b06ddce + 599eeb6 commit 940c182
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Set up Node.js ${{ matrix.node-version }}
uses: pnpm/action-setup@v2
with:
version: 9
version: 9.0.4

- name: Install dependencies
run: pnpm i --frozen-lockfile
Expand Down
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/*"]
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"version": "0.0.0",
"type": "module",
"packageManager": "pnpm@9.1.1",
"packageManager": "pnpm@9.0.4",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
Expand Down
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
22 changes: 8 additions & 14 deletions src/features/quests/components/QuestBoardTimer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,27 @@ type Props = {
startedAt: string;
};

const constructTime = (hh: number, mm: number, ss: number) => {
return `${String(hh)}時間${String(mm).padStart(2, "0")}${String(ss).padStart(2, "0")}秒`;
};

export const QuestBoardTimer: FC<Props> = ({ startsAt, isStarted, minutes, startedAt }) => {
const { baseTime } = calcBaseTime(startsAt, isStarted, minutes, startedAt);

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

const [hh, setHH] = useState(0 < diffHH ? diffHH : 0);
const [mm, setMM] = useState(0 < diffMM ? diffMM : 0);
const [ss, setSS] = useState(0 < diffSS ? diffSS : 0);
const [time, setTime] = useState(constructTime(diffHH, diffMM, diffSS));

useInterval(() => {
const { baseTime, status } = calcBaseTime(startsAt, getIsStarted(startedAt), minutes, startedAt);

if (status !== FORCE_STOP) {
const { diffHH, diffMM, diffSS } = calcDiffTimeBetweenNowAndTargetTime(baseTime);
setHH(diffHH);
setMM(diffMM);
setSS(diffSS);
setTime(constructTime(diffHH, diffMM, diffSS));
} else {
setHH(0);
setMM(0);
setSS(0);
setTime(constructTime(0, 0, 0));
}
}, 1000);

return (
<span className="text-2xl text-rhyth-light-blue tracking-wider">
{String(hh)}時間{String(mm).padStart(2, "0")}{String(ss).padStart(2, "0")}
</span>
);
return <span className="text-2xl text-rhyth-light-blue tracking-wider">{time}</span>;
};
2 changes: 1 addition & 1 deletion src/features/quests/funcs/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const calcBaseTime = (
}

// クエスト解放中前後30分
if (!isStarted && beforeDiffMM < 0) {
if (!isStarted && -30 <= beforeDiffMM && beforeDiffMM < 0) {
return {
baseTime: formatDateTimeWithAddMinutes(startsAt, 15),
status: OPEN,
Expand Down

0 comments on commit 940c182

Please sign in to comment.