Skip to content

Commit

Permalink
リファクタ
Browse files Browse the repository at this point in the history
  • Loading branch information
eatski committed Aug 14, 2024
1 parent 80dbd2a commit d127c59
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
27 changes: 8 additions & 19 deletions src/app/stories/[storyId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { brand } from "@/common/texts";
import { uaToDevice } from "@/common/util/device";
import { Play } from "@/components/play";
import { StoryDescription } from "@/components/storyDescription";
import type { Story } from "@/server/model/story";
Expand All @@ -15,10 +14,10 @@ import {
unpublishStory,
} from "@/server/services/story/publishStory";
import { postStoryEvalution } from "@/server/services/storyEvalution/post";
import { get } from "@vercel/edge-config";
import { getQuestionLimitation } from "@/server/services/user/limitation";
import { Metadata } from "next";
import { revalidateTag } from "next/cache";
import { cookies, headers } from "next/headers";
import { cookies } from "next/headers";
import { notFound } from "next/navigation";
import { cache } from "react";
import { z } from "zod";
Expand Down Expand Up @@ -160,24 +159,14 @@ export default async function StoryPage({ params: { storyId } }: StoryProps) {
story={story}
fetchCanPlay={async () => {
"use server";
const thankyouCookie = cookies().get("thankyou");
if (
thankyouCookie &&
thankyouCookie.value === process.env.THANKYOU_CODE
) {
return {
canPlay: true,
};
}
const questionLimitation = questionLimitationSchema.parse(
await get("questionLimitation"),
);

const device = getDevice();
if (questionLimitation.desktopOnly && device !== "desktop") {
const limited = await getQuestionLimitation({
device: getDevice(),
getCookie: (key) => cookies().get(key)?.value || null,
});
if (limited) {
return {
canPlay: false,
reason: "desktop_only",
reason: limited,
};
}
return {
Expand Down
29 changes: 29 additions & 0 deletions src/server/services/user/limitation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { get } from "@vercel/edge-config";
import { z } from "zod";
import { Device } from "../../../common/util/device";

const questionLimitationSchema = z.object({
desktopOnly: z.boolean(),
});
export const getQuestionLimitation = async ({
getCookie,
device,
}: {
getCookie: (key: string) => string | null;
device: Device;
}) => {
const thankyouCodeCookie = getCookie("thankyouCode");
if (thankyouCodeCookie && thankyouCodeCookie === process.env.THANKYOU_CODE) {
return false;
}

const questionLimitation = questionLimitationSchema.parse(
await get("questionLimitation"),
);

if (questionLimitation.desktopOnly && device !== "desktop") {
return "desktop_only";
}

return false;
};

0 comments on commit d127c59

Please sign in to comment.