Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions aima-front/app/history/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ export default function HistoryPage() {
const [error, setError] = useState<string | null>(null);

useEffect(() => {
setLoading(true);
setError(null);

getActivityLogs(100)
.then(({ activity_logs }) => setLogs(activity_logs))
.catch((e) => {
Expand Down
24 changes: 18 additions & 6 deletions aima-front/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@ export default function HomePage() {
const [checked, setChecked] = useState(false);

useEffect(() => {
const id = getUserId();
if (!id) {
router.replace("/onboarding");
return;
}
setChecked(true);
let cancelled = false;

const checkAuth = async () => {
const id = await getUserId();
if (!id) {
router.replace("/onboarding");
return;
}
Comment on lines +15 to +20
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getUserId は現状 number | null を返す同期関数(lib/userStore.ts)なので、ここで async + await にすると不要にマイクロタスク境界が入ります。その結果、cleanup 後でも router.replace が走り得て cancelled フラグの意図(アンマウント後の副作用抑止)とズレるので、同期呼び出しに戻すか、少なくとも router.replacecancelled を見て抑止するようにしてください。

Copilot uses AI. Check for mistakes.
if (!cancelled) {
setChecked(true);
}
};

void checkAuth();

return () => {
cancelled = true;
};
}, [router]);

const handleLogout = () => {
Expand Down
51 changes: 28 additions & 23 deletions aima-front/app/recipes/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { useEffect, useMemo, useState } from "react";
import { useRouter, useSearchParams } from "next/navigation";
import { createRecommendation } from "@/lib/api";
import type { Recipe, DurationMin, Mood } from "@/lib/types";
import type { Recipe, DurationMin, Mood, Weather } from "@/lib/types";

function parseDuration(v: string | null): DurationMin {
const n = Number(v);
Expand All @@ -13,6 +13,9 @@ function parseDuration(v: string | null): DurationMin {
function parseMood(v: string | null): Mood {
return v === "energetic" || v === "neutral" || v === "calm" ? v : "neutral";
}
function parseWeather(v: string | null): Weather {
return v === "sunny" || v === "rainy" || v === "cloudy" ? v : "sunny";
}

const moodLabel: Record<Mood, string> = {
energetic: "元気に動きたい",
Expand All @@ -30,10 +33,7 @@ export default function RecipesPage() {
[params]
);
const mood = useMemo(() => parseMood(params.get("mood")), [params]);
const weather = useMemo(
() => (params.get("weather") as import("@/lib/types").Weather) ?? "sunny",
[params]
);
const weather = useMemo(() => parseWeather(params.get("weather")), [params]);

const [recipes, setRecipes] = useState<Recipe[]>([]);
const [loading, setLoading] = useState(true);
Expand All @@ -43,28 +43,33 @@ export default function RecipesPage() {
useEffect(() => {
let cancelled = false;

setLoading(true);
setError(null);
setSelectedRecipeId(null);

createRecommendation({ duration_min: duration, mood, weather })
.then((res) => {
if (cancelled) return;
setRecipes(res.recipes);
})
.catch(() => {
if (cancelled) return;
setError("レシピの取得に失敗しました");
})
.finally(() => {
if (cancelled) return;
setLoading(false);
});
const fetchRecipes = async () => {
setLoading(true);
setError(null);
setSelectedRecipeId(null);

try {
const res = await createRecommendation({ duration_min: duration, mood, weather });
if (!cancelled) {
setRecipes(res.recipes);
}
} catch {
if (!cancelled) {
setError("レシピの取得に失敗しました");
}
} finally {
if (!cancelled) {
setLoading(false);
}
}
};

void fetchRecipes();

return () => {
cancelled = true;
};
}, [duration, mood]);
}, [duration, mood, weather]);

const goReview = () => {
if (!selectedRecipeId) return;
Expand Down