Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Open in IDX buttons to astro.new #80

Merged
merged 4 commits into from
Nov 25, 2024
Merged
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
19 changes: 14 additions & 5 deletions src/components/RepoCard.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type Props = Example & { aboveTheFold: boolean };
const {
title,
sourceUrl,
idxUrl,
stackblitzUrl,
codesandboxUrl,
gitpodUrl,
Expand Down Expand Up @@ -46,26 +47,34 @@ const headingId = `template-${createAstroTemplate}`;
</p>
<hr class="border-astro-gray-100/10" />
<div
class="flex h-10 w-max min-w-0 max-w-full divide-x divide-astro-gray-600 rounded-full bg-blue-purple-gradient"
class="relative flex h-10 w-max min-w-0 max-w-full divide-x divide-astro-gray-600 rounded-full bg-blue-purple-gradient"
>
<a
href={stackblitzUrl}
href={idxUrl}
target="_blank"
rel="noopener noreferrer"
class="flex items-center gap-2 rounded-l-full bg-black/15 px-4 text-sm transition hover:backdrop-brightness-75"
>
<Icon name="stackblitz" size={16} aria-hidden="true" /> Open in StackBlitz
<Icon name="idx" size={16} aria-hidden="true" /> Open in IDX
</a>
<details class="relative z-10" data-card-options>
<details class="z-10" data-card-options>
<summary
class="flex h-full cursor-pointer list-none items-center rounded-r-full bg-black/15 px-2 transition hover:backdrop-brightness-75"
>
<span class="sr-only">More options</span>
<Icon name="chevron-down" size={20} aria-hidden="true" />
</summary>
<ul
class="absolute right-0 top-full mt-2 flex w-max flex-col rounded bg-white p-2 text-astro-gray-700 shadow-md"
class="absolute left-0 top-full mt-2 flex w-max flex-col rounded bg-white p-2 text-astro-gray-700 shadow-md"
>
<li>
<a
href={stackblitzUrl}
class="flex flex-row items-center gap-2 rounded-sm px-3 py-2 hover:bg-blue-purple-gradient hover:text-white"
>
<Icon name="stackblitz" size={20} aria-hidden="true" /> Open in StackBlitz
</a>
</li>
<li>
<a
href={gitpodUrl}
Expand Down
2 changes: 2 additions & 0 deletions src/data/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface Example {
name: string;
title: string;
sourceUrl: string;
idxUrl: string;
stackblitzUrl: string;
codesandboxUrl: string;
gitpodUrl: string;
Expand All @@ -57,6 +58,7 @@ function toExample(exampleData: ExampleDataWithRepo, ref: string): Example {
return {
name,
sourceUrl: `/${name}${suffix}?on=github`,
idxUrl: `/${name}${suffix}?on=idx`,
stackblitzUrl: `/${name}${suffix}?on=stackblitz`,
codesandboxUrl: `/${name}${suffix}?on=codesandbox`,
gitpodUrl: `/${name}${suffix}?on=gitpod`,
Expand Down
3 changes: 3 additions & 0 deletions src/icons/idx.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/icons/stackblitz.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 55 additions & 11 deletions src/pages/[...rest].ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { APIContext, APIRoute } from 'astro';
import { fromStarlightName, isStarlightName } from '../utils/constants.js';
import { toTemplateName, toTitle } from '../data/examples-shared.js';
import { fromStarlightName, isStarlightName, toStarlightName } from '../utils/constants.js';
import {
type ExampleData,
astroContentUrl,
Expand All @@ -12,7 +13,7 @@ export const prerender = false;
type CachedExample = {
name: string;
github: string;
netlify: string;
idx: string;
stackblitz: string;
codesandbox: string;
gitpod: string;
Expand All @@ -21,15 +22,56 @@ type CachedExample = {
const examplesCache = new Map<string, CachedExample[]>();
let starlightExamplesCache: CachedExample[] | undefined = undefined;

function toCachedExample({ name, html_url }: ExampleData): CachedExample {
const githubUrl = new URL(html_url);
/** Generate a placeholder workspace name for IDX. Must be no longer than 20 characters. */
function idxProjectName(example: ExampleData, repo: string) {
const fullTitle = toTitle(
repo === 'withastro/starlight' ? toStarlightName(example.name) : example.name,
)
// Remove parentheticals
.replace(/\([^)]+\)/, '')
.trim();

if (fullTitle.length > 20) return `${fullTitle.slice(0, 19)}…`;
if (fullTitle.length < 13) return `Astro: ${fullTitle}`;
return fullTitle;
}

/**
* Generate a URL to create a new IDX workspace for the given example.
*
* @param example GitHub REST API repository contents entry for this template.
* @param repo The GitHub repo identifier for this template, e.g. `withastro/astro`.
* @param ref The GitHub branch to use: `latest` or `next`.
*/
function idxUrl(example: ExampleData, repo: string, ref = 'latest') {
const url = new URL('https://idx.google.com/new');
// Select the Astro template to use when starting up IDX.
url.searchParams.set('astroTemplate', toTemplateName({ ...example, repo }));
// Pre-fill the IDX wizard with a project name based on the selected template.
const title = idxProjectName(example, repo);
url.searchParams.set('name', title);
// Tell IDX where the template files are located. IDX parses this greedily so it MUST COME LAST.
const templateUrl = `https://github.com/withastro/astro.new/tree/main/.idx-templates/${ref}`;
url.searchParams.set('template', templateUrl);
return url.href;
}

/**
* Create a map of URLs that open this example on different services.
*
* @param example GitHub REST API repository contents entry for this template.
* @param repo The GitHub repo identifier for this template, e.g. `withastro/astro`.
* @param ref The GitHub branch to use: `latest` or `next`.
*/
function toCachedExample(example: ExampleData, repo: string, ref: string): CachedExample {
const githubUrl = new URL(example.html_url);
return {
name,
github: html_url,
netlify: 'https://astro.build',
name: example.name,
github: example.html_url,
idx: idxUrl(example, repo, ref),
stackblitz: `https://stackblitz.com/github${githubUrl.pathname}`,
codesandbox: `https://codesandbox.io/p/sandbox/github${githubUrl.pathname}`,
gitpod: `https://gitpod.io/#${html_url}`,
gitpod: `https://gitpod.io/#${example.html_url}`,
};
}

Expand All @@ -48,7 +90,7 @@ async function getStarlightExamples() {
}

starlightExamplesCache = examples.flatMap((example) =>
example.size > 0 ? [] : toCachedExample(example),
example.size > 0 ? [] : toCachedExample(example, 'withastro/starlight', 'latest'),
);

return starlightExamplesCache;
Expand All @@ -70,7 +112,9 @@ async function getExamples(ref = 'latest') {
throw new Error(`Unable to fetch templates from GitHub`);
}

const values = examples.flatMap((example) => (example.size > 0 ? [] : toCachedExample(example)));
const values = examples.flatMap((example) =>
example.size > 0 ? [] : toCachedExample(example, 'withastro/astro', ref),
);

examplesCache.set(ref, values);

Expand Down Expand Up @@ -108,7 +152,7 @@ async function validateRef(name: string) {
}

type Platform = typeof PLATFORMS extends Set<infer T> ? T : never;
const PLATFORMS = new Set(['stackblitz', 'codesandbox', 'netlify', 'github', 'gitpod'] as const);
const PLATFORMS = new Set(['idx', 'stackblitz', 'codesandbox', 'github', 'gitpod'] as const);
function isPlatform(name: string): name is Platform {
return PLATFORMS.has(name as Platform);
}
Expand Down