-
Notifications
You must be signed in to change notification settings - Fork 387
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
10 changed files
with
238 additions
and
4 deletions.
There are no files selected for viewing
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,74 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
import packageJson from "../../package.json"; | ||
import { getClassNameFactory } from "@/core/lib"; | ||
|
||
import styles from "./styles.module.css"; | ||
|
||
const { version } = packageJson; | ||
|
||
const getClassName = getClassNameFactory("ReleaseSwitcher", styles); | ||
|
||
export const ReleaseSwitcher = () => { | ||
const isCanary = process.env.NEXT_PUBLIC_IS_CANARY === "true" || false; | ||
const isLatest = process.env.NEXT_PUBLIC_IS_LATEST === "true" || false; | ||
|
||
const currentValue = isCanary ? "canary" : isLatest ? "" : version; | ||
|
||
const [options, setOptions] = useState<{ value: string; label: string }[]>([ | ||
{ | ||
label: "canary", | ||
value: "canary", | ||
}, | ||
...(isCanary | ||
? [] | ||
: [ | ||
{ | ||
label: isLatest ? `v${version} (latest)` : `v${version}`, | ||
value: "", | ||
}, | ||
]), | ||
]); | ||
|
||
useEffect(() => { | ||
fetch("/api/releases").then(async (res) => { | ||
const { releases } = await res.json(); | ||
|
||
if (releases.length === 0) return; | ||
|
||
const releaseOptions = releases.map((release) => ({ | ||
label: release.name.split("releases/")[1], | ||
value: release.name.split("releases/v")[1], // remove the leading `v` | ||
})); | ||
|
||
releaseOptions[0].label = `${releaseOptions[0].label} (latest)`; | ||
releaseOptions[0].value = ""; | ||
|
||
setOptions([{ label: "canary", value: "canary" }, ...releaseOptions]); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<select | ||
className={getClassName()} | ||
value={currentValue} | ||
onChange={(e) => { | ||
const newHref = e.currentTarget.value | ||
? `https://puckeditor.com/v/${e.currentTarget.value}` | ||
: "https://puckeditor.com"; | ||
|
||
if (window.parent) { | ||
window.parent.location.href = newHref; | ||
} else { | ||
window.location.href = newHref; | ||
} | ||
}} | ||
> | ||
{options.map((option) => ( | ||
<option key={option.value} value={option.value}> | ||
{option.label} | ||
</option> | ||
))} | ||
</select> | ||
); | ||
}; |
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,15 @@ | ||
.ReleaseSwitcher { | ||
appearance: none; /* Safari */ | ||
background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='100' height='100' fill='%23c3c3c3'><polygon points='0,0 100,0 50,50'/></svg>") | ||
no-repeat; | ||
background-size: 12px; | ||
background-position: calc(100% - 12px) calc(50% + 3px); | ||
background-repeat: no-repeat; | ||
background-color: var(--puck-color-grey-10); | ||
border-radius: 100px; | ||
color: black; | ||
padding-left: 16px; | ||
padding-right: 16px; | ||
height: 33px; /* Magic number to align with Nextra search */ | ||
width: 156px; | ||
} |
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 |
---|---|---|
@@ -1,8 +1,39 @@ | ||
import React from "react"; | ||
import React, { useEffect } from "react"; | ||
|
||
import type { AppProps } from "next/app"; | ||
import "../styles.css"; | ||
import { useRouter } from "next/router"; | ||
import { Message } from "./v/[[...fullPath]]"; | ||
|
||
export default function DocsApp({ Component, pageProps }: AppProps) { | ||
const router = useRouter(); | ||
|
||
useEffect(() => { | ||
if (!window.parent) return; | ||
|
||
const message: Message = { | ||
type: "routeChange", | ||
title: window.document.title, | ||
}; | ||
|
||
window.parent.postMessage(message); | ||
|
||
const handleRouteChange = (url) => { | ||
const message: Message = { | ||
type: "routeChange", | ||
url, | ||
title: window.document.title, | ||
}; | ||
|
||
window.parent.postMessage(message); | ||
}; | ||
|
||
router.events.on("routeChangeComplete", handleRouteChange); | ||
|
||
return () => { | ||
router.events.off("routeChangeComplete", handleRouteChange); | ||
}; | ||
}, []); | ||
|
||
export default function MyApp({ Component, pageProps }: AppProps) { | ||
return <Component {...pageProps} />; | ||
} |
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,41 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import { LRUCache } from "lru-cache"; | ||
|
||
type ResponseData = { | ||
releases: object; | ||
}; | ||
|
||
const cache = new LRUCache({ | ||
ttl: 1000 * 60 * 2, // 2 minutes | ||
ttlAutopurge: true, | ||
}); | ||
|
||
/** | ||
* Proxy GitHub and rely on Next.js cache to prevent rate limiting | ||
*/ | ||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<ResponseData> | ||
) { | ||
const cached = cache.get("releases"); | ||
|
||
if (cached) { | ||
res.status(200).json({ releases: cached }); | ||
|
||
return; | ||
} | ||
|
||
const data = [{ name: "releases/v0.12.0", protected: false }]; | ||
|
||
const releases: { name: string; protected: boolean }[] = data | ||
.filter( | ||
(item) => | ||
item.name.indexOf("releases") === 0 && | ||
item.name.indexOf(`v0.11.`) === -1 // Filter out any release branches before v0.12.0 | ||
) | ||
.reverse(); | ||
|
||
res.status(200).json({ releases }); | ||
|
||
cache.set("releases", releases); | ||
} |
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,56 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
export type Message = { | ||
type: "routeChange"; | ||
url?: string; | ||
title: string; | ||
}; | ||
|
||
export default function Version({ path, version }) { | ||
useEffect(() => { | ||
const handleMessageReceived = (event: MessageEvent) => { | ||
if (event.data.type === "routeChange") { | ||
const routeChange = event.data as Message; | ||
|
||
if (routeChange.url) { | ||
window.history.pushState({}, "", `/v/${version}${routeChange.url}`); | ||
} | ||
|
||
window.document.title = `${routeChange.title} [${ | ||
version !== "canary" ? `v${version}` : version | ||
}]`; | ||
} | ||
}; | ||
|
||
window.addEventListener("message", handleMessageReceived); | ||
|
||
return () => window.removeEventListener("message", handleMessageReceived); | ||
}, []); | ||
|
||
const versionSlug = version.replace(/\./g, ""); | ||
|
||
const src = | ||
version === "canary" | ||
? `https://puck-docs-git-canary-measured.vercel.app` | ||
: `https://puck-docs-git-releases-v${versionSlug}-measured.vercel.app`; | ||
|
||
return ( | ||
<iframe | ||
src={src} | ||
style={{ | ||
top: 0, | ||
left: 0, | ||
height: "100%", | ||
width: "100%", | ||
position: "fixed", | ||
border: 0, | ||
}} | ||
></iframe> | ||
); | ||
} | ||
|
||
export function getServerSideProps(ctx) { | ||
const [version, ...path] = ctx.query.fullPath; | ||
|
||
return { props: { path: path.join("/"), version } }; | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"packages": [ | ||
"apps/docs", | ||
"packages/core", | ||
"packages/create-puck-app", | ||
"packages/plugin-heading-analyzer" | ||
|
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