Skip to content

Commit

Permalink
fix: リロード時に背景色が必ずpinkになってしまう問題の修正 & 静的レンダリングされる箇所に動的な要素を入れてしまった問題を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
shun-shobon committed Nov 9, 2024
1 parent a4f917b commit 06d97b8
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 76 deletions.
72 changes: 35 additions & 37 deletions app/features/profile/useMyProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@ import type { Json } from "~/libs/database";
import { supabase } from "~/libs/supabase";
import { useSession } from "../../hooks/useSession";

export async function myProfileFetcher([_, userId]: [string, string]) {
if (!userId) return null;

const { data: rawProfile } = await supabase
.from("profiles_with_stats")
.select(
"user_id, display_name, high_score, play_count, rank, character_setting",
)
.eq("user_id", userId)
.limit(1)
.single();

if (!rawProfile) return null;

const characterSetting = deserializeCharacterSetting(
rawProfile.character_setting,
);

const profile: Profile = {
id: rawProfile.user_id,
displayName: rawProfile.display_name,
playCount: rawProfile.play_count,
highScore: rawProfile.high_score,
rank: rawProfile.rank,
characterSetting,
};

return profile;
}

interface UseMyProfile {
myProfile: Profile | null;
error: unknown;
Expand All @@ -24,43 +54,11 @@ export function useMyProfile(): UseMyProfile {
error,
mutate,
isValidating,
} = useSWR(
["/profile/me", session?.user.id],
async ([_, userId]) => {
if (!userId) return null;

const { data: rawProfile } = await supabase
.from("profiles_with_stats")
.select(
"user_id, display_name, high_score, play_count, rank, character_setting",
)
.eq("user_id", userId)
.limit(1)
.single();

if (!rawProfile) return null;

const characterSetting = deserializeCharacterSetting(
rawProfile.character_setting,
);

const profile: Profile = {
id: rawProfile.user_id,
displayName: rawProfile.display_name,
playCount: rawProfile.play_count,
highScore: rawProfile.high_score,
rank: rawProfile.rank,
characterSetting,
};

return profile;
},
{
suspense: true,
fallbackData: null,
refreshInterval: 1000 * 10,
},
);
} = useSWR(["/profile/me", session?.user.id], myProfileFetcher, {
suspense: true,
fallbackData: null,
refreshInterval: 1000 * 10,
});

const updateDisplayName = useCallback(
async (displayName: string) => {
Expand Down
26 changes: 12 additions & 14 deletions app/hooks/useSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@ import useSWRSubscription from "swr/subscription";
import type { SWRSubscriptionOptions } from "swr/subscription";
import { supabase } from "~/libs/supabase";

export async function sessionFetcher() {
const {
data: { session },
} = await supabase.auth.getSession();

return session;
}

export function useSession(): Session | null {
const key = "session";
const { data: initialSession } = useSWRImmutable(
key,
async () => {
const {
data: { session },
} = await supabase.auth.getSession();

return session;
},
{
suspense: true,
fallbackData: null,
},
);
const { data: initialSession } = useSWRImmutable(key, sessionFetcher, {
suspense: true,
fallbackData: null,
});

const { data: session } = useSWRSubscription(
key,
Expand Down
41 changes: 28 additions & 13 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ import {
} from "@remix-run/react";
import { cva } from "class-variance-authority";
import { type ReactNode, Suspense } from "react";
import { preload } from "swr";
import { Analytics } from "./components/Analytics";
import { Background } from "./components/Background";
import { BottomNav } from "./components/BottomNav";
import { Button } from "./components/Button";
import { Patterns } from "./components/Patterns";
import { ThemeProvider } from "./components/Theme";
import { useMyProfile } from "./features/profile/useMyProfile";
import {
myProfileFetcher,
useMyProfile,
} from "./features/profile/useMyProfile";
import { sessionFetcher } from "./hooks/useSession";
import { cn } from "./libs/utils";

export const appThemes = cva(
Expand All @@ -32,9 +37,6 @@ const PATH_THEME_MAP: Record<string, "pink" | "cyan" | "emerald" | "yellow"> = {
};

export function Layout({ children }: { children: React.ReactNode }) {
const location = useLocation();
const theme = PATH_THEME_MAP[location.pathname] ?? "pink";

return (
<html lang="ja">
<head>
Expand Down Expand Up @@ -67,14 +69,7 @@ export function Layout({ children }: { children: React.ReactNode }) {
<link rel="manifest" href="/manifest.webmanifest" />
</head>
<body className={appThemes()}>
<ThemeProvider theme={theme}>
<Patterns />
<Background />
{children}
<Suspense fallback={null}>
<Nav path={location.pathname} />
</Suspense>
</ThemeProvider>
{children}
<ScrollRestoration />
<Scripts />
</body>
Expand All @@ -101,8 +96,28 @@ function Nav({ path }: NavProps): ReactNode {
);
}

export async function clientLoader() {
const session = await preload("session", sessionFetcher);

await preload(["/profile/me", session?.user.id], myProfileFetcher);

return null;
}

export default function App() {
return <Outlet />;
const location = useLocation();
const theme = PATH_THEME_MAP[location.pathname] ?? "pink";

return (
<ThemeProvider theme={theme}>
<Patterns />
<Background />
<Outlet />
<Suspense fallback={null}>
<Nav path={location.pathname} />
</Suspense>
</ThemeProvider>
);
}

export function HydrateFallback(): ReactNode {
Expand Down
26 changes: 14 additions & 12 deletions app/routes/_auth.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { Outlet, useNavigate } from "@remix-run/react";
import { type ReactNode, useLayoutEffect } from "react";
import { useSession } from "~/hooks/useSession";
import { Outlet } from "@remix-run/react";
import { redirect } from "@remix-run/react";
import type { ReactNode } from "react";
import { preload } from "swr";
import { sessionFetcher, useSession } from "~/hooks/useSession";

// _auth.**.tsx のパスへのアクセスは必ずここで前処理される
export default function Layout(): ReactNode {
const navigate = useNavigate();
const session = useSession();
export async function clientLoader() {
const session = await preload("session", sessionFetcher);
if (!session) {
return redirect("/");
}

useLayoutEffect(() => {
if (!session) {
navigate("/");
}
}, [session, navigate]);
return null;
}

// _auth.**.tsx のパスへのアクセスは必ずここで前処理される
export default function Layout(): ReactNode {
return <Outlet />;
}

0 comments on commit 06d97b8

Please sign in to comment.