-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add a basic package page. Fixes #1444 * Address review comments.
- Loading branch information
Showing
7 changed files
with
212 additions
and
9 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
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,19 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import 'lit/experimental-hydrate-support.js'; | ||
import {hydrate} from 'lit/experimental-hydrate.js'; | ||
import {renderPackagePage} from './shell.js'; | ||
|
||
const data = ( | ||
globalThis as unknown as {__ssrData: Parameters<typeof renderPackagePage>} | ||
).__ssrData; | ||
|
||
// We need to hydrate the whole page to remove any defer-hydration attributes. | ||
// We could also remove the attribute manually, or not use deferhydration, but | ||
// instead manually assign the data into the <wco-package-page> element, and | ||
// time imports so that automatic element hydration happend after. | ||
hydrate(renderPackagePage(...data), document.body); |
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,12 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import './wco-package-page.js'; | ||
import type {PackageData} from './wco-package-page.js'; | ||
import {html} from 'lit'; | ||
|
||
export const renderPackagePage = (packageData: PackageData) => | ||
html`<wco-package-page .packageData=${packageData}></wco-package-page>`; |
68 changes: 68 additions & 0 deletions
68
packages/site-client/src/pages/package/wco-package-page.ts
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,68 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import {CustomElement} from '@webcomponents/catalog-api/lib/_schema.js'; | ||
import {html, css} from 'lit'; | ||
import {customElement, property} from 'lit/decorators.js'; | ||
import {unsafeHTML} from 'lit/directives/unsafe-html.js'; | ||
|
||
import {WCOPage} from '../../shared/wco-page.js'; | ||
import '../catalog/wco-element-card.js'; | ||
|
||
export interface PackageData { | ||
name: string; | ||
description: string; | ||
version: string; | ||
elements: CustomElement[]; | ||
} | ||
|
||
@customElement('wco-package-page') | ||
export class WCOPackagePage extends WCOPage { | ||
static styles = [ | ||
WCOPage.styles, | ||
css` | ||
h1 { | ||
display: inline-block; | ||
} | ||
.elements { | ||
display: grid; | ||
grid-template-columns: repeat(4, 200px); | ||
grid-template-rows: auto; | ||
grid-auto-columns: 200px; | ||
grid-auto-rows: 200px; | ||
gap: 8px; | ||
} | ||
`, | ||
]; | ||
|
||
@property({attribute: false}) | ||
packageData?: PackageData; | ||
|
||
renderContent() { | ||
if (this.packageData === undefined) { | ||
return this.fullScreenError('No package to display'); | ||
} | ||
|
||
return html` | ||
<div> | ||
<h1>${this.packageData.name}</h1> | ||
v${this.packageData.version} | ||
</div> | ||
<div>${unsafeHTML(this.packageData.description)}</div> | ||
<div class="elements"> | ||
${this.packageData.elements.map((e) => { | ||
return html`<wco-element-card .element=${e}></wco-element-card>`; | ||
})} | ||
</div> | ||
`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
'wco-package-page': WCOPackagePage; | ||
} | ||
} |
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
96 changes: 96 additions & 0 deletions
96
packages/site-server/src/lib/catalog/routes/package/package-route.ts
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,96 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
// This must be imported before lit | ||
import {renderPage} from '@webcomponents/internal-site-templates/lib/base.js'; | ||
|
||
import {DefaultContext, DefaultState, ParameterizedContext} from 'koa'; | ||
import {Readable} from 'stream'; | ||
import {gql} from '@apollo/client/core/index.js'; | ||
import Router from '@koa/router'; | ||
|
||
import {renderPackagePage} from '@webcomponents/internal-site-client/lib/pages/package/shell.js'; | ||
import {client} from '../../graphql.js'; | ||
import {PackageData} from '@webcomponents/internal-site-client/lib/pages/package/wco-package-page.js'; | ||
import {marked} from 'marked'; | ||
|
||
export const handlePackageRoute = async ( | ||
context: ParameterizedContext< | ||
DefaultState, | ||
DefaultContext & Router.RouterParamContext<DefaultState, DefaultContext>, | ||
unknown | ||
> | ||
) => { | ||
const {params} = context; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const packageName = params['name']!; | ||
|
||
// TODO (justinfagnani): To make this type-safe, we need to write | ||
// a query .graphql document and generate a TypedDocumentNode from it. | ||
const result = await client.query({ | ||
query: gql`{ | ||
package(packageName: "${packageName}") { | ||
... on ReadablePackageInfo { | ||
name | ||
description | ||
version { | ||
... on ReadablePackageVersion { | ||
version | ||
description | ||
customElements { | ||
tagName | ||
package | ||
declaration | ||
customElementExport | ||
declaration | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}`, | ||
}); | ||
|
||
if (result.errors !== undefined && result.errors.length > 0) { | ||
throw new Error(result.errors.map((e) => e.message).join('\n')); | ||
} | ||
const {data} = result; | ||
const packageVersion = data.package?.version; | ||
if (packageVersion === undefined) { | ||
// TODO: 404 | ||
throw new Error(`No such package version: ${packageName}`); | ||
} | ||
|
||
// Set location because wco-nav-bar reads pathname from it. URL isn't | ||
// exactly a Location, but it's close enough for read-only uses | ||
globalThis.location = new URL(context.URL.href) as unknown as Location; | ||
|
||
const responseData: PackageData = { | ||
name: packageName, | ||
description: marked(data.package.description ?? ''), | ||
version: packageVersion.version, | ||
elements: packageVersion.customElements, | ||
}; | ||
|
||
context.type = 'html'; | ||
context.status = 200; | ||
context.body = Readable.from( | ||
renderPage( | ||
{ | ||
title: `${packageName}`, | ||
initScript: '/js/package/boot.js', | ||
content: renderPackagePage(responseData), | ||
initialData: [responseData], | ||
}, | ||
{ | ||
// We need to defer elements from hydrating so that we can | ||
// manually provide data to the element in element-hydrate.js | ||
deferHydration: true, | ||
} | ||
) | ||
); | ||
}; |