-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: useFetchData カスタムフック抽出とコンポーネント移行 #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6be6a35
feat: useFetchData カスタムフックを追加
max747 38bd3cf
refactor: AppLayout を useFetchData に移行
max747 aa29f98
refactor: QuestView を useFetchData に移行
max747 4ca6f33
refactor: ReporterSummary を useFetchData に移行
max747 b3003c9
refactor: EventItemSummaryPage を useFetchData に移行
max747 b2b4425
test: useFetchData のテストを追加
max747 20386fd
docs: useFetchData に JSDoc コメントを追加
max747 5cae42c
merge: main を取り込み、EventItemSummaryPage のコンフリクトを解消
max747 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // @vitest-environment jsdom | ||
| import { renderHook, waitFor } from "@testing-library/react"; | ||
| import { afterEach, describe, expect, test, vi } from "vitest"; | ||
| import { useFetchData } from "./useFetchData"; | ||
|
|
||
| describe("useFetchData", () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| test("初期状態は loading: true, data: initialData", () => { | ||
| const fetcher = vi.fn((_signal: AbortSignal) => new Promise<string>(() => {})); | ||
| const { result } = renderHook(() => useFetchData(fetcher, [], "initial")); | ||
| expect(result.current.loading).toBe(true); | ||
| expect(result.current.data).toBe("initial"); | ||
| expect(result.current.error).toBeNull(); | ||
| }); | ||
|
|
||
| test("fetch 成功時に data が更新され loading が false になる", async () => { | ||
| const fetcher = vi.fn((_signal: AbortSignal) => Promise.resolve("fetched")); | ||
| const { result } = renderHook(() => useFetchData(fetcher, [], "initial")); | ||
| await waitFor(() => expect(result.current.loading).toBe(false)); | ||
| expect(result.current.data).toBe("fetched"); | ||
| expect(result.current.error).toBeNull(); | ||
| }); | ||
|
|
||
| test("fetch 失敗時に error が設定され loading が false になる", async () => { | ||
| const fetcher = vi.fn((_signal: AbortSignal) => Promise.reject(new Error("fetch failed"))); | ||
| const { result } = renderHook(() => useFetchData(fetcher, [], "initial")); | ||
| await waitFor(() => expect(result.current.loading).toBe(false)); | ||
| expect(result.current.error).toBe("fetch failed"); | ||
| expect(result.current.data).toBe("initial"); | ||
| }); | ||
|
|
||
| test("AbortError は error に設定されない", async () => { | ||
| const abortError = new DOMException("Aborted", "AbortError"); | ||
| const fetcher = vi.fn((_signal: AbortSignal) => Promise.reject(abortError)); | ||
| const { result } = renderHook(() => useFetchData(fetcher, [], "initial")); | ||
| await waitFor(() => expect(result.current.loading).toBe(false)); | ||
| expect(result.current.error).toBeNull(); | ||
| }); | ||
|
|
||
| test("deps が変わると fetcher が再実行される", async () => { | ||
| let questId = "q1"; | ||
| const fetcher = vi.fn((_signal: AbortSignal) => Promise.resolve(`data-${questId}`)); | ||
| const { result, rerender } = renderHook(() => useFetchData(fetcher, [questId], "initial")); | ||
| await waitFor(() => expect(result.current.data).toBe("data-q1")); | ||
|
|
||
| questId = "q2"; | ||
| rerender(); | ||
| await waitFor(() => expect(result.current.data).toBe("data-q2")); | ||
| }); | ||
|
|
||
| test("アンマウント時に fetch が中断される", async () => { | ||
| let aborted = false; | ||
| const fetcher = vi.fn( | ||
| (signal: AbortSignal) => | ||
| new Promise<string>((resolve) => { | ||
| signal.addEventListener("abort", () => { | ||
| aborted = true; | ||
| }); | ||
| setTimeout(() => resolve("done"), 1000); | ||
| }), | ||
| ); | ||
| const { unmount } = renderHook(() => useFetchData(fetcher, [], "initial")); | ||
| unmount(); | ||
| expect(aborted).toBe(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import type React from "react"; | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| /** | ||
| * AbortController・loading/error 状態管理を共通化する汎用フェッチフック。 | ||
| * @param fetcher AbortSignal を受け取り Promise を返す非同期関数 | ||
| * @param deps fetcher の再実行トリガーとなる依存配列(呼び出し側が管理する) | ||
| * @param initialData フェッチ完了前に返す初期値 | ||
| * @returns data・loading・error の状態オブジェクト | ||
| */ | ||
| export function useFetchData<T>( | ||
| fetcher: (signal: AbortSignal) => Promise<T>, | ||
| deps: React.DependencyList, | ||
| initialData: T, | ||
| ): { data: T; loading: boolean; error: string | null } { | ||
| const [data, setData] = useState<T>(initialData); | ||
| const [loading, setLoading] = useState(true); | ||
| const [error, setError] = useState<string | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| const controller = new AbortController(); | ||
| setLoading(true); | ||
| setError(null); | ||
| fetcher(controller.signal) | ||
| .then(setData) | ||
| .catch((e: unknown) => { | ||
| if (e instanceof DOMException && e.name === "AbortError") return; | ||
| setError(e instanceof Error ? e.message : String(e)); | ||
| }) | ||
| .finally(() => { | ||
| if (!controller.signal.aborted) setLoading(false); | ||
| }); | ||
| return () => controller.abort(); | ||
| // biome-ignore lint/correctness/useExhaustiveDependencies: caller controls deps | ||
| }, deps); | ||
|
|
||
| return { data, loading, error }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing JSDoc documentation. The codebase consistently uses JSDoc comments for exported functions and custom hooks (see useSortState, useToggleSet, formatNote, etc.). Add a JSDoc comment describing the hook's purpose, parameters, and behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ご指摘のとおりです。useSortState・useToggleSet と同様に JSDoc コメントを追加しました。