Skip to content

Commit 4c0cdfe

Browse files
refactor to TS rest of partial views, preview and info view
1 parent 9cb6da2 commit 4c0cdfe

File tree

11 files changed

+195
-49
lines changed

11 files changed

+195
-49
lines changed

src/registry/routes/component-info.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const parseAuthor = require('parse-author');
44
const _ = require('lodash');
55

66
const getComponentFallback = require('./helpers/get-component-fallback');
7-
const infoView = require('../views/info');
7+
const infoView = require('../views/info').default;
88
const isUrlDiscoverable = require('./helpers/is-url-discoverable').default;
99
const urlBuilder = require('../domain/url-builder');
1010

src/registry/routes/component-preview.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const _ = require('lodash');
44

55
const getComponentFallback = require('./helpers/get-component-fallback');
6-
const previewView = require('../views/preview');
6+
const previewView = require('../views/preview').default;
77
const urlBuilder = require('../domain/url-builder');
88

99
function componentPreview(err, req, res, component, templates) {
@@ -25,6 +25,7 @@ function componentPreview(err, req, res, component, templates) {
2525
return res.send(
2626
previewView({
2727
component,
28+
// @ts-ignore
2829
dependencies: Object.keys(component.dependencies || {}),
2930
href: res.conf.baseUrl,
3031
liveReload,

src/registry/views/info.js renamed to src/registry/views/info.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
1-
module.exports = vm => {
2-
const componentAuthor = require('./partials/component-author')(vm);
3-
const componentParameters = require('./partials/component-parameters')(vm);
4-
const componentState = require('./partials/component-state')(vm);
5-
const componentVersions = require('./partials/component-versions')(vm);
6-
const infoJS = require('./static/info').default;
7-
const layout = require('./partials/layout').default(vm);
8-
const property = require('./partials/property').default();
9-
const isTemplateLegacy = require('../../utils/is-template-legacy').default;
1+
import { Component } from '../../types';
2+
3+
import getComponentAuthor from './partials/component-author';
4+
import getComponentParameters from './partials/component-parameters';
5+
import getComponentState from './partials/component-state';
6+
import getComponentVersions from './partials/component-versions';
7+
import infoJS from './static/info';
8+
import getLayout from './partials/layout';
9+
import getProperty from './partials/property';
10+
import isTemplateLegacy from '../../utils/is-template-legacy';
11+
12+
interface Vm {
13+
parsedAuthor: { name?: string; email?: string; url?: string };
14+
component: Component;
15+
dependencies: string[];
16+
href: string;
17+
sandBoxDefaultQs: string;
18+
title: string;
19+
repositoryUrl: string;
20+
}
21+
22+
export default function info(vm: Vm): string {
23+
const componentAuthor = getComponentAuthor(vm);
24+
const componentParameters = getComponentParameters(vm);
25+
const componentState = getComponentState(vm);
26+
const componentVersions = getComponentVersions(vm);
27+
const layout = getLayout(vm);
28+
const property = getProperty();
1029

1130
const showArray = (title, arr) =>
1231
property(title, !!arr && arr.length > 0 ? arr.join(', ') : 'none');
@@ -64,4 +83,4 @@ module.exports = vm => {
6483
</script>`;
6584

6685
return layout({ content, scripts });
67-
};
86+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
module.exports = vm => () => {
1+
const componentAuthor = (vm: {
2+
parsedAuthor: {
3+
name?: string;
4+
email?: string;
5+
url?: string;
6+
};
7+
}) => (): string => {
28
const author = vm.parsedAuthor;
39
let placeholder = '';
410

@@ -11,16 +17,14 @@ module.exports = vm => () => {
1117
}
1218

1319
if (author.email) {
14-
placeholder += `<span><a href="mailto:${author.email}">${
15-
author.email
16-
}</a>&nbsp;</span>`;
20+
placeholder += `<span><a href="mailto:${author.email}">${author.email}</a>&nbsp;</span>`;
1721
}
1822

1923
if (author.url) {
20-
placeholder += `<span><a href="${author.url}" target="_blank">${
21-
author.url
22-
}</a></span>`;
24+
placeholder += `<span><a href="${author.url}" target="_blank">${author.url}</a></span>`;
2325
}
2426

2527
return `<div class="field"><p>Author:</p>${placeholder}</div>`;
2628
};
29+
30+
export default componentAuthor;

src/registry/views/partials/component-parameters.js renamed to src/registry/views/partials/component-parameters.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
module.exports = ({ component }) => () => {
1+
import { Component } from '../../../types';
2+
3+
const componentParameters = ({
4+
component
5+
}: {
6+
component: Component;
7+
}) => (): string => {
28
let parameters = `<h3>Parameters</h3>`;
39

410
if (!component.oc.parameters) {
@@ -12,9 +18,7 @@ module.exports = ({ component }) => () => {
1218
: '';
1319
const defaultParam =
1420
!param.mandatory && param.default
15-
? `<br /><br /><span class="bold">Default:</span><span>${
16-
param.default
17-
}</span>`
21+
? `<br /><br /><span class="bold">Default:</span><span>${param.default}</span>`
1822
: '';
1923

2024
return `<div class="row">
@@ -41,9 +45,11 @@ module.exports = ({ component }) => () => {
4145
<div class="row header">
4246
<div class="parameter">Parameters</div>
4347
<div class="parameter-description"></div>
44-
</div>
48+
</div>
4549
${rows}
4650
</div>`;
4751

4852
return parameters;
4953
};
54+
55+
export default componentParameters;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
module.exports = ({ component }) => () =>
1+
import { Component } from '../../../types';
2+
3+
const componentState = ({
4+
component
5+
}: {
6+
component: Component;
7+
}) => (): string =>
28
!component.oc.state
39
? ''
410
: `<span class="details-state">
511
<span class="component-state-${component.oc.state.toLowerCase()}">
612
${component.oc.state}
713
</span>
814
</span>`;
15+
16+
export default componentState;

src/registry/views/partials/component-versions.js renamed to src/registry/views/partials/component-versions.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
module.exports = ({ component }) => () => {
1+
import { Component } from '../../../types';
2+
3+
const componentVersions = ({
4+
component
5+
}: {
6+
component: Component;
7+
}) => (): string => {
28
const componentOption = version =>
39
`<option value="${version}"${
410
version === component.version ? ' selected="selected"' : ''
@@ -8,3 +14,5 @@ module.exports = ({ component }) => () => {
814
.map(componentOption)
915
.join('')}</select>`;
1016
};
17+
18+
export default componentVersions;

src/registry/views/partials/components-templates.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function componentsTemplates(vm: VM): string {
1111
}: {
1212
type: string;
1313
version: string;
14-
externals: Array<{ global: string; url: string }>;
14+
externals: Array<{ global: string | string[]; url: string }>;
1515
}) => {
1616
const externalLinks = externals.map(externalLink).join(', ');
1717
const externalsLabel = externalLinks ? `(Externals: ${externalLinks})` : '';

src/registry/views/partials/property.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const property = () => (
22
display: string,
3-
value?: Date | string,
3+
value?: Date | string | number,
44
linked?: boolean
55
): string => {
66
if (!value) return '';

src/registry/views/preview.js renamed to src/registry/views/preview.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
module.exports = vm => {
1+
import { Component, TemplateInfo } from '../../types';
2+
3+
export default function preview(vm: {
4+
href: string;
5+
component: Component;
6+
qs: string;
7+
liveReload: string;
8+
templates: TemplateInfo[];
9+
}): string {
210
const baseUrl = vm.href.replace('http://', '//').replace('https://', '//');
311
const { name, version } = vm.component;
412
const componentHref = `${baseUrl}${name}/${version}/${vm.qs}`;
513

614
return `<!DOCTYPE html>
715
<html>
816
<head>
9-
<style>
17+
<style>
1018
html, body {
1119
width: 100%;
1220
height: 100%;
@@ -20,10 +28,10 @@ module.exports = vm => {
2028
<body>
2129
<oc-component href="${componentHref}"></oc-component>
2230
<script>window.oc=window.oc||{};oc.conf=oc.conf||{};oc.conf.templates=(oc.conf.templates||[]).concat(${JSON.stringify(
23-
vm.templates
24-
)});</script>
31+
vm.templates
32+
)});</script>
2533
<script src="${baseUrl}oc-client/client.js"></script>
2634
${vm.liveReload}
2735
</body>
2836
</html>`;
29-
};
37+
}

0 commit comments

Comments
 (0)