-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
123 additions
and
25 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 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,90 @@ | ||
import { ActionArgs, SerializeFrom, createCookie } from "@remix-run/node"; | ||
import { useRouteLoaderData } from "@remix-run/react"; | ||
import { z } from "zod"; | ||
import { useShortcut } from "~/components/use-shortcut"; | ||
import { config } from "~/config.server"; | ||
import { loader } from "~/root"; | ||
|
||
// Schema | ||
const preivewModeSchema = z.object({ | ||
enabled: z.boolean(), | ||
secret: z.string(), | ||
}); | ||
export type PreviewMode = z.infer<typeof preivewModeSchema>; | ||
|
||
// Cookie | ||
const cookie = createCookie("preview-mode"); | ||
export async function serializePreviewModeToCookie(previewMode: PreviewMode) { | ||
return await cookie.serialize(previewMode); | ||
} | ||
async function safeParsePreviewModeCookie(cookieHeader: string | null) { | ||
const value = await cookie.parse(cookieHeader); | ||
const parsed = preivewModeSchema.safeParse(value); | ||
if (parsed.success) return parsed.data; | ||
else return undefined; | ||
} | ||
|
||
// Getter | ||
export async function isPreviewMode(fromRequest: Request) { | ||
return (await getPreviewMode(fromRequest))?.enabled ?? false; | ||
} | ||
export async function getPreviewMode( | ||
fromRequest: Request | ||
): Promise<PreviewMode | undefined> { | ||
// First. always use the url | ||
const previewSecretFromUrl = new URL(fromRequest.url).searchParams.get( | ||
"preview" | ||
); | ||
const previewModeEnabledFromUrl = | ||
previewSecretFromUrl === config.previewSecret; | ||
if (previewModeEnabledFromUrl) { | ||
return { | ||
enabled: true, | ||
secret: previewSecretFromUrl, | ||
}; | ||
} | ||
|
||
// Second. use the cookie, if the secret is correct of course | ||
const previewMode = await safeParsePreviewModeCookie( | ||
fromRequest.headers.get("cookie") | ||
); | ||
if (previewMode?.secret === config.previewSecret) { | ||
return previewMode; | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
export const action = async ({ request }: ActionArgs) => { | ||
const body = preivewModeSchema.parse(await request.json()); | ||
if (body.secret !== config.previewSecret) { | ||
return new Response("Unauthorized", { status: 401 }); | ||
} | ||
|
||
const headers = { "Set-Cookie": await serializePreviewModeToCookie(body) }; | ||
return new Response("OK", { status: 200, headers }); | ||
}; | ||
|
||
export function PreviewMode() { | ||
const { previewMode } = useRouteLoaderData("root") as SerializeFrom< | ||
typeof loader | ||
>; | ||
async function togglePreviewMode() { | ||
const secret = | ||
previewMode?.secret ?? window.prompt("Fyll inn preview secret") ?? ""; | ||
const previewModeToSend: PreviewMode = { | ||
enabled: !previewMode?.enabled, | ||
secret, | ||
}; | ||
const response = await fetch(`/api/preview-mode`, { | ||
method: "POST", | ||
body: JSON.stringify(previewModeToSend), | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
if (response.ok) { | ||
window.location.reload(); | ||
} | ||
} | ||
useShortcut("pp", togglePreviewMode); | ||
return null; | ||
} |
This file contains 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 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