@influenzanet/participant-webapp-kit 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +107 -2
- package/dist/ParticipantApp.d.ts +34 -0
- package/dist/ParticipantApp.js +38 -0
- package/dist/defaultTexts.d.ts +16 -0
- package/dist/defaultTexts.js +38 -0
- package/dist/env.d.ts +8 -0
- package/dist/env.js +8 -0
- package/dist/extensions/BlogPosts/BlogPosts.css +78 -0
- package/dist/extensions/BlogPosts/BlogPosts.d.ts +13 -0
- package/dist/extensions/BlogPosts/BlogPosts.js +69 -0
- package/dist/extensions/IframeEmbed.d.ts +36 -0
- package/dist/extensions/IframeEmbed.js +100 -0
- package/dist/extensions/index.d.ts +14 -0
- package/dist/extensions/index.js +12 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +4 -0
- package/dist/locales/en/linkresolvers.json +19 -0
- package/dist/locales/it/linkresolvers.json +19 -0
- package/dist/render.d.ts +17 -0
- package/dist/render.js +23 -0
- package/dist/runtime.d.ts +8 -0
- package/dist/runtime.js +8 -0
- package/dist/vite/index.d.ts +29 -0
- package/dist/vite/index.js +128 -0
- package/package.json +83 -4
package/README.md
CHANGED
|
@@ -1,3 +1,108 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @influenzanet/participant-webapp-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The shared kit of the Influenzanet participant web apps, currently used in influweb, infectieradar-be,
|
|
4
|
+
gripenet-pt, grippenet-ch).
|
|
5
|
+
|
|
6
|
+
| What | Export |
|
|
7
|
+
|---|---|
|
|
8
|
+
| Bootstrap (i18n, store, Suspense, fonts, icons, Bootstrap JS) | `renderParticipantApp()` |
|
|
9
|
+
| AppCore wrapper (instance override, default language, extensions) | `<ParticipantApp>` |
|
|
10
|
+
| Generic page extensions | `iframeEmbed`, `blogPosts` (registered by default) |
|
|
11
|
+
| Vite config | `@influenzanet/participant-webapp-kit/vite` |
|
|
12
|
+
|
|
13
|
+
Files that must physically live in each country repo (Dockerfile, nginx, CI, `vite.config.ts`,
|
|
14
|
+
`index.html`, ...) come from the copier template
|
|
15
|
+
[influenzanet/participant-webapp-template](https://github.com/influenzanet/participant-webapp-template).
|
|
16
|
+
|
|
17
|
+
## Use in a country app
|
|
18
|
+
|
|
19
|
+
`src/index.tsx`:
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import { renderParticipantApp } from "@influenzanet/participant-webapp-kit";
|
|
23
|
+
import { it } from "date-fns/locale";
|
|
24
|
+
|
|
25
|
+
import "./index.scss";
|
|
26
|
+
import * as appConfig from "./configs/appConfig.json";
|
|
27
|
+
import * as footerConfig from "./configs/footer.json";
|
|
28
|
+
import * as headerConfig from "./configs/header.json";
|
|
29
|
+
import * as navbarConfig from "./configs/navbar.json";
|
|
30
|
+
import * as pagesConfig from "./configs/pages.json";
|
|
31
|
+
|
|
32
|
+
renderParticipantApp({
|
|
33
|
+
appConfig, footerConfig, headerConfig, navbarConfig, pagesConfig,
|
|
34
|
+
dateLocales: [{ code: "it", locale: it, format: "dd/MM/yyyy" }],
|
|
35
|
+
// optional, country specific:
|
|
36
|
+
extensions: [{ name: "myWidget", component: MyWidget }],
|
|
37
|
+
beforeRender: () => registerMyReducers(),
|
|
38
|
+
children: <CookieBanner />,
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
`vite.config.ts`:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { defineConfig } from "vite";
|
|
46
|
+
import { influenzanetVite } from "@influenzanet/participant-webapp-kit/vite";
|
|
47
|
+
|
|
48
|
+
export default defineConfig(influenzanetVite({
|
|
49
|
+
proxyTarget: "https://influweb.staging.influenzanet.info",
|
|
50
|
+
wsProxyPaths: ["/dashboard", "/_stcore"],
|
|
51
|
+
// other backends, prefix stripped: /results-proxy/a.html -> <bucket>/a.html
|
|
52
|
+
pathProxies: { "/results-proxy": "https://storage.googleapis.com/my-bucket" },
|
|
53
|
+
}));
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Env vars keep the CRA names (`REACT_APP_*`) because case-web-app-core reads them; they're
|
|
57
|
+
available as `process.env.REACT_APP_X` in code and `%REACT_APP_X%` in `index.html`.
|
|
58
|
+
|
|
59
|
+
## Default texts
|
|
60
|
+
|
|
61
|
+
Texts of the pages every country has (for now the newsletter and weekly reminder unsubscribe
|
|
62
|
+
pages) ship with the kit, in `src/locales/<language>/<namespace>.json` (`en` and `it`), and are
|
|
63
|
+
added under the country's own locale files when the app starts. A country does not copy them:
|
|
64
|
+
to change one, put the same key in its own `public/assets/locales/<language>/<namespace>.json`,
|
|
65
|
+
which always wins. Other languages fall back to the fallback language for the keys they lack.
|
|
66
|
+
|
|
67
|
+
## Extensions
|
|
68
|
+
|
|
69
|
+
Used from `pages.json` items (case-web-app-core 2.7.x matches on `config.type`):
|
|
70
|
+
|
|
71
|
+
```json
|
|
72
|
+
{ "itemKey": "results", "config": { "type": "extension",
|
|
73
|
+
"config": { "type": "iframeEmbed", "url": "/dashboard/?lang={lang}", "title": "Results" } } }
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### `iframeEmbed`
|
|
77
|
+
|
|
78
|
+
| prop | default | |
|
|
79
|
+
|---|---|---|
|
|
80
|
+
| `url` | | page url, `{lang}` → current language. Use the exact URL the page serves (Streamlit: `/dashboard/`, with the slash): a redirect to another origin makes the iframe cross-origin and resizing stops |
|
|
81
|
+
| `baseUrl` | | legacy: same as `url: "<baseUrl>?lang={lang}"` |
|
|
82
|
+
| `autoResize` | `true` | resize to content with iframe-resizer v5 (embedded page must load `@iframe-resizer/child`); hidden behind a spinner until its size settles |
|
|
83
|
+
| `height`, `minHeight` | `100vh` | used when `autoResize: false` |
|
|
84
|
+
| `stylesheet` | | same-origin pages: stylesheet injected into the iframe on load |
|
|
85
|
+
| `title` | `Results` | |
|
|
86
|
+
| `settleMs`, `maxWaitMs`, `placeholderHeight` | 700, 10000, 300 | auto-resize reveal tuning |
|
|
87
|
+
|
|
88
|
+
### `blogPosts`
|
|
89
|
+
|
|
90
|
+
Latest posts from a Ghost blog. Props `ghostUrl`, `apiKey`, `blogUrl`, `limit` (3), defaults from `REACT_APP_GHOST_URL` / `REACT_APP_GHOST_CONTENT_API_KEY`. Texts from the page's locale namespace: `<itemKey>.title|readMore|viewAll|error|empty`.
|
|
91
|
+
|
|
92
|
+
A country extension with the same name replaces the kit one, pass `includeDefaultExtensions: false` to register none of them.
|
|
93
|
+
|
|
94
|
+
## Develop
|
|
95
|
+
|
|
96
|
+
```sh
|
|
97
|
+
yarn install
|
|
98
|
+
yarn build # dist/
|
|
99
|
+
# try it in a country app without publishing:
|
|
100
|
+
cd ../participant-webapp-influweb && yarn add file:../participant-webapp-kit
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
To release a new version:
|
|
104
|
+
- bump `version`
|
|
105
|
+
- update `CHANGELOG.md`
|
|
106
|
+
- tag `vX.Y.Z` (or `vX.Y.Z-staging.N`) and push;
|
|
107
|
+
|
|
108
|
+
the workflow publishes to npm (`latest` / `staging`).
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { AppCore, AppConfig, FooterConfig, HeaderConfig, NavbarConfig, PagesConfig } from "@influenzanet/case-web-app-core";
|
|
3
|
+
export type Extension = {
|
|
4
|
+
name: string;
|
|
5
|
+
component: React.FunctionComponent<any>;
|
|
6
|
+
};
|
|
7
|
+
export type DateLocale = {
|
|
8
|
+
code: string;
|
|
9
|
+
locale: any;
|
|
10
|
+
format: string;
|
|
11
|
+
};
|
|
12
|
+
type AppCoreProps = React.ComponentProps<typeof AppCore>;
|
|
13
|
+
export interface ParticipantAppProps {
|
|
14
|
+
appConfig: AppConfig | object;
|
|
15
|
+
headerConfig: HeaderConfig | object;
|
|
16
|
+
navbarConfig: NavbarConfig | object;
|
|
17
|
+
pagesConfig: PagesConfig | object;
|
|
18
|
+
footerConfig: FooterConfig | object;
|
|
19
|
+
dateLocales: DateLocale[];
|
|
20
|
+
/** Country extensions. A name that matches a kit extension replaces it. */
|
|
21
|
+
extensions?: Extension[];
|
|
22
|
+
/** Set to false to register only the country's own extensions. */
|
|
23
|
+
includeDefaultExtensions?: boolean;
|
|
24
|
+
/** Overrides appConfig.instanceId. Defaults to REACT_APP_DEFAULT_INSTANCE. */
|
|
25
|
+
instanceId?: string;
|
|
26
|
+
/** Language used when i18n has none yet. Defaults to REACT_APP_DEFAULT_LANGUAGE. */
|
|
27
|
+
defaultLanguage?: string;
|
|
28
|
+
/** Any other AppCore prop (customHeader, customSurveyResponseComponents, ...). */
|
|
29
|
+
appCoreProps?: Partial<AppCoreProps>;
|
|
30
|
+
/** Rendered after AppCore: cookie banners, trackers, ... */
|
|
31
|
+
children?: React.ReactNode;
|
|
32
|
+
}
|
|
33
|
+
declare const ParticipantApp: React.FC<ParticipantAppProps>;
|
|
34
|
+
export default ParticipantApp;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import React, { useEffect, useMemo } from "react";
|
|
3
|
+
import { useTranslation } from "react-i18next";
|
|
4
|
+
import { AppCore, } from "@influenzanet/case-web-app-core";
|
|
5
|
+
import { env } from "./env";
|
|
6
|
+
import { defaultExtensions } from "./extensions";
|
|
7
|
+
// `import * as cfg from "./configs/x.json"` yields a module namespace; accept both shapes.
|
|
8
|
+
const unwrap = (m) => (m && m.default ? m.default : m);
|
|
9
|
+
const ParticipantApp = (props) => {
|
|
10
|
+
var _a;
|
|
11
|
+
const { i18n } = useTranslation();
|
|
12
|
+
const defaultLanguage = (_a = props.defaultLanguage) !== null && _a !== void 0 ? _a : env.defaultLanguage;
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (!i18n.language && defaultLanguage) {
|
|
15
|
+
i18n.changeLanguage(defaultLanguage);
|
|
16
|
+
}
|
|
17
|
+
}, [i18n, i18n.language, defaultLanguage]);
|
|
18
|
+
const appConfig = useMemo(() => {
|
|
19
|
+
var _a;
|
|
20
|
+
const cfg = { ...unwrap(props.appConfig) };
|
|
21
|
+
const instanceId = (_a = props.instanceId) !== null && _a !== void 0 ? _a : env.defaultInstance;
|
|
22
|
+
if (instanceId) {
|
|
23
|
+
cfg.instanceId = instanceId;
|
|
24
|
+
}
|
|
25
|
+
return cfg;
|
|
26
|
+
}, [props.appConfig, props.instanceId]);
|
|
27
|
+
const extensions = useMemo(() => {
|
|
28
|
+
var _a;
|
|
29
|
+
const own = (_a = props.extensions) !== null && _a !== void 0 ? _a : [];
|
|
30
|
+
if (props.includeDefaultExtensions === false)
|
|
31
|
+
return own;
|
|
32
|
+
const ownNames = new Set(own.map((e) => e.name));
|
|
33
|
+
// core picks the first match, so country extensions go first
|
|
34
|
+
return [...own, ...defaultExtensions.filter((e) => !ownNames.has(e.name))];
|
|
35
|
+
}, [props.extensions, props.includeDefaultExtensions]);
|
|
36
|
+
return (_jsxs(React.Fragment, { children: [_jsx(AppCore, { ...props.appCoreProps, appConfig: appConfig, headerConfig: unwrap(props.headerConfig), navbarConfig: unwrap(props.navbarConfig), pagesConfig: unwrap(props.pagesConfig), footerConfig: unwrap(props.footerConfig), dateLocales: props.dateLocales, extensions: extensions }), props.children] }));
|
|
37
|
+
};
|
|
38
|
+
export default ParticipantApp;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { i18n as I18n } from "i18next";
|
|
2
|
+
/** Texts by language, then namespace: `{ it: { linkresolvers: { ... } } }`. */
|
|
3
|
+
export type DefaultTexts = Record<string, Record<string, object>>;
|
|
4
|
+
/**
|
|
5
|
+
* The texts of the pages every country app has (src/locales/<language>/<namespace>.json). A
|
|
6
|
+
* country gets them without copying them into its public/ folder, and replaces any of them by
|
|
7
|
+
* putting the same key in its own locale file.
|
|
8
|
+
*/
|
|
9
|
+
export declare const kitTexts: DefaultTexts;
|
|
10
|
+
/**
|
|
11
|
+
* Fills the keys a country's locale files lack with the given texts, once i18n has loaded each
|
|
12
|
+
* namespace, or failed to (the country has no file for it). The country's own keys are never
|
|
13
|
+
* replaced. The texts are added while i18n records the load, before the components waiting
|
|
14
|
+
* for the namespace render. Call it right after i18n is initialised.
|
|
15
|
+
*/
|
|
16
|
+
export declare function addDefaultTexts(defaults: DefaultTexts, instance?: I18n): void;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import i18n from "i18next";
|
|
2
|
+
import enLinkResolvers from "./locales/en/linkresolvers.json";
|
|
3
|
+
import itLinkResolvers from "./locales/it/linkresolvers.json";
|
|
4
|
+
/**
|
|
5
|
+
* The texts of the pages every country app has (src/locales/<language>/<namespace>.json). A
|
|
6
|
+
* country gets them without copying them into its public/ folder, and replaces any of them by
|
|
7
|
+
* putting the same key in its own locale file.
|
|
8
|
+
*/
|
|
9
|
+
export const kitTexts = {
|
|
10
|
+
en: { linkresolvers: enLinkResolvers },
|
|
11
|
+
it: { linkresolvers: itLinkResolvers },
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Fills the keys a country's locale files lack with the given texts, once i18n has loaded each
|
|
15
|
+
* namespace, or failed to (the country has no file for it). The country's own keys are never
|
|
16
|
+
* replaced. The texts are added while i18n records the load, before the components waiting
|
|
17
|
+
* for the namespace render. Call it right after i18n is initialised.
|
|
18
|
+
*/
|
|
19
|
+
export function addDefaultTexts(defaults, instance = i18n) {
|
|
20
|
+
let adding = false;
|
|
21
|
+
const fill = (lng, ns) => {
|
|
22
|
+
var _a;
|
|
23
|
+
const texts = (_a = defaults[lng]) === null || _a === void 0 ? void 0 : _a[ns];
|
|
24
|
+
// addResourceBundle announces itself as "added" too: ignore our own addition.
|
|
25
|
+
if (!texts || adding)
|
|
26
|
+
return;
|
|
27
|
+
adding = true;
|
|
28
|
+
try {
|
|
29
|
+
// A copy: i18n keeps the objects it is given, and a later load merges into them.
|
|
30
|
+
instance.addResourceBundle(lng, ns, JSON.parse(JSON.stringify(texts)), true, false);
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
adding = false;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
instance.store.on("added", fill);
|
|
37
|
+
instance.on("failedLoading", fill);
|
|
38
|
+
}
|
package/dist/env.d.ts
ADDED
package/dist/env.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const env = {
|
|
2
|
+
defaultLanguage: process.env.REACT_APP_DEFAULT_LANGUAGE,
|
|
3
|
+
fallbackLanguage: process.env.REACT_APP_FALLBACK_LANGUAGE,
|
|
4
|
+
contentUrl: process.env.REACT_APP_CONTENT_URL,
|
|
5
|
+
defaultInstance: process.env.REACT_APP_DEFAULT_INSTANCE,
|
|
6
|
+
ghostUrl: process.env.REACT_APP_GHOST_URL,
|
|
7
|
+
ghostContentApiKey: process.env.REACT_APP_GHOST_CONTENT_API_KEY,
|
|
8
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/* BlogPosts extension styles. Plain CSS (prefixed) so it ships from a library. */
|
|
2
|
+
.ifn-blog-card {
|
|
3
|
+
transition: transform 180ms ease, box-shadow 180ms ease;
|
|
4
|
+
overflow: hidden;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.ifn-blog-card:hover,
|
|
8
|
+
.ifn-blog-card:focus-visible {
|
|
9
|
+
transform: translateY(-3px);
|
|
10
|
+
box-shadow: 0 0.75rem 1.5rem rgba(0, 0, 0, 0.12) !important;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
.ifn-blog-image-wrap {
|
|
14
|
+
position: relative;
|
|
15
|
+
width: 100%;
|
|
16
|
+
aspect-ratio: 16 / 9;
|
|
17
|
+
overflow: hidden;
|
|
18
|
+
background-color: #f1f1f1;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.ifn-blog-image {
|
|
22
|
+
position: absolute;
|
|
23
|
+
inset: 0;
|
|
24
|
+
width: 100%;
|
|
25
|
+
height: 100%;
|
|
26
|
+
object-fit: cover;
|
|
27
|
+
transition: transform 300ms ease;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
.ifn-blog-card:hover .ifn-blog-image {
|
|
31
|
+
transform: scale(1.04);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.ifn-blog-read-more {
|
|
35
|
+
color: var(--bs-primary, #870a30);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.ifn-blog-view-all {
|
|
39
|
+
color: var(--bs-primary, #870a30);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.ifn-blog-view-all:hover {
|
|
43
|
+
text-decoration: underline !important;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.ifn-blog-skeleton {
|
|
47
|
+
background-color: #fff;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
.ifn-blog-skeleton-image {
|
|
51
|
+
width: 100%;
|
|
52
|
+
aspect-ratio: 16 / 9;
|
|
53
|
+
background: linear-gradient(90deg, #eee 0%, #f5f5f5 50%, #eee 100%);
|
|
54
|
+
background-size: 200% 100%;
|
|
55
|
+
animation: ifn-blog-shimmer 1.4s ease-in-out infinite;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.ifn-blog-skeleton-line {
|
|
59
|
+
height: 0.75rem;
|
|
60
|
+
border-radius: 0.25rem;
|
|
61
|
+
margin-bottom: 0.5rem;
|
|
62
|
+
background: linear-gradient(90deg, #eee 0%, #f5f5f5 50%, #eee 100%);
|
|
63
|
+
background-size: 200% 100%;
|
|
64
|
+
animation: ifn-blog-shimmer 1.4s ease-in-out infinite;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.ifn-blog-skeleton-line-short {
|
|
68
|
+
width: 60%;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
@keyframes ifn-blog-shimmer {
|
|
72
|
+
0% {
|
|
73
|
+
background-position: 200% 0;
|
|
74
|
+
}
|
|
75
|
+
100% {
|
|
76
|
+
background-position: -200% 0;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import "./BlogPosts.css";
|
|
3
|
+
export interface BlogPostsProps {
|
|
4
|
+
pageKey: string;
|
|
5
|
+
itemKey: string;
|
|
6
|
+
className?: string;
|
|
7
|
+
ghostUrl?: string;
|
|
8
|
+
apiKey?: string;
|
|
9
|
+
blogUrl?: string;
|
|
10
|
+
limit?: number;
|
|
11
|
+
}
|
|
12
|
+
declare const BlogPosts: React.FC<BlogPostsProps>;
|
|
13
|
+
export default BlogPosts;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { useTranslation } from "react-i18next";
|
|
4
|
+
import "./BlogPosts.css";
|
|
5
|
+
import { env } from "../../env";
|
|
6
|
+
const cx = (...c) => c.filter(Boolean).join(" ");
|
|
7
|
+
const BlogPosts = (props) => {
|
|
8
|
+
var _a, _b, _c, _d;
|
|
9
|
+
const { t, i18n } = useTranslation([props.pageKey, "global"]);
|
|
10
|
+
const [posts, setPosts] = useState(null);
|
|
11
|
+
const [errored, setErrored] = useState(false);
|
|
12
|
+
const ghostUrl = (_a = props.ghostUrl) !== null && _a !== void 0 ? _a : env.ghostUrl;
|
|
13
|
+
const apiKey = (_b = props.apiKey) !== null && _b !== void 0 ? _b : env.ghostContentApiKey;
|
|
14
|
+
const blogUrl = (_c = props.blogUrl) !== null && _c !== void 0 ? _c : ghostUrl;
|
|
15
|
+
const limit = (_d = props.limit) !== null && _d !== void 0 ? _d : 3;
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
if (!ghostUrl || !apiKey)
|
|
18
|
+
return;
|
|
19
|
+
const fields = "id,title,url,feature_image,excerpt,custom_excerpt,published_at,reading_time";
|
|
20
|
+
const url = `${ghostUrl.replace(/\/$/, "")}/ghost/api/content/posts/` +
|
|
21
|
+
`?key=${encodeURIComponent(apiKey)}` +
|
|
22
|
+
`&limit=${limit}` +
|
|
23
|
+
`&fields=${fields}` +
|
|
24
|
+
`&order=published_at%20desc`;
|
|
25
|
+
let cancelled = false;
|
|
26
|
+
fetch(url)
|
|
27
|
+
.then((r) => {
|
|
28
|
+
if (!r.ok)
|
|
29
|
+
throw new Error(`HTTP ${r.status}`);
|
|
30
|
+
return r.json();
|
|
31
|
+
})
|
|
32
|
+
.then((data) => {
|
|
33
|
+
var _a;
|
|
34
|
+
if (!cancelled)
|
|
35
|
+
setPosts((_a = data.posts) !== null && _a !== void 0 ? _a : []);
|
|
36
|
+
})
|
|
37
|
+
.catch(() => {
|
|
38
|
+
if (!cancelled)
|
|
39
|
+
setErrored(true);
|
|
40
|
+
});
|
|
41
|
+
return () => {
|
|
42
|
+
cancelled = true;
|
|
43
|
+
};
|
|
44
|
+
}, [ghostUrl, apiKey, limit]);
|
|
45
|
+
if (!ghostUrl || !apiKey)
|
|
46
|
+
return null;
|
|
47
|
+
const sectionTitle = t(`${props.itemKey}.title`, "Latest from the blog");
|
|
48
|
+
const readMoreLabel = t(`${props.itemKey}.readMore`, "Read more");
|
|
49
|
+
const viewAllLabel = t(`${props.itemKey}.viewAll`, "Visit our blog");
|
|
50
|
+
const errorLabel = t(`${props.itemKey}.error`, "Couldn't load blog posts right now.");
|
|
51
|
+
const emptyLabel = t(`${props.itemKey}.empty`, "No posts yet.");
|
|
52
|
+
const formatDate = (iso) => {
|
|
53
|
+
try {
|
|
54
|
+
return new Date(iso).toLocaleDateString(i18n.language, {
|
|
55
|
+
year: "numeric",
|
|
56
|
+
month: "long",
|
|
57
|
+
day: "numeric",
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return "";
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
return (_jsxs("section", { className: cx("py-4", props.className), "aria-label": sectionTitle, children: [_jsxs("div", { className: "d-flex flex-wrap align-items-baseline justify-content-between mb-3 gap-2", children: [_jsx("h2", { className: "h4 m-0", children: sectionTitle }), blogUrl && (_jsxs("a", { href: blogUrl, target: "_blank", rel: "noopener noreferrer", className: cx("text-decoration-none fw-bold", "ifn-blog-view-all"), children: [viewAllLabel, " ", _jsx("span", { "aria-hidden": true, children: "\u2192" })] }))] }), errored && (_jsx("p", { className: "text-muted small mb-0", children: errorLabel })), !errored && posts === null && (_jsx("div", { className: "row g-3", children: Array.from({ length: limit }).map((_, i) => (_jsx("div", { className: "col-12 col-md-4", children: _jsxs("div", { className: cx("card h-100 border-0 shadow-sm", "ifn-blog-skeleton"), "aria-hidden": true, children: [_jsx("div", { className: "ifn-blog-skeleton-image" }), _jsxs("div", { className: "card-body", children: [_jsx("div", { className: "ifn-blog-skeleton-line" }), _jsx("div", { className: cx("ifn-blog-skeleton-line", "ifn-blog-skeleton-line-short") })] })] }) }, i))) })), !errored && posts && posts.length === 0 && (_jsx("p", { className: "text-muted small mb-0", children: emptyLabel })), !errored && posts && posts.length > 0 && (_jsx("div", { className: "row g-3", children: posts.map((post) => {
|
|
65
|
+
const excerpt = post.custom_excerpt || post.excerpt || "";
|
|
66
|
+
return (_jsx("div", { className: "col-12 col-md-4", children: _jsxs("a", { href: post.url, target: "_blank", rel: "noopener noreferrer", className: cx("card h-100 border-0 shadow-sm text-decoration-none text-reset", "ifn-blog-card"), children: [post.feature_image && (_jsx("div", { className: "ifn-blog-image-wrap", children: _jsx("img", { src: post.feature_image, alt: "", className: "ifn-blog-image", loading: "lazy" }) })), _jsxs("div", { className: "card-body d-flex flex-column", children: [_jsx("h3", { className: "h6 fw-bold mb-2", children: post.title }), excerpt && (_jsx("p", { className: "small text-muted mb-3 flex-grow-1", children: excerpt })), _jsxs("div", { className: "d-flex justify-content-between align-items-center small text-muted mt-auto", children: [_jsx("span", { children: formatDate(post.published_at) }), post.reading_time > 0 && (_jsxs("span", { children: [post.reading_time, " min"] }))] })] }), _jsx("div", { className: cx("card-footer bg-transparent border-0 pt-0 pb-3", "ifn-blog-read-more"), children: _jsxs("span", { className: "fw-bold small", children: [readMoreLabel, " ", _jsx("span", { "aria-hidden": true, children: "\u2192" })] }) })] }) }, post.id));
|
|
67
|
+
}) }))] }));
|
|
68
|
+
};
|
|
69
|
+
export default BlogPosts;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Page extension that embeds an external page (results dashboard, ...) in an iframe.
|
|
4
|
+
*
|
|
5
|
+
* pages.json:
|
|
6
|
+
* { "type": "extension", "config": { "type": "iframeEmbed", "url": "/dashboard/?lang={lang}" } }
|
|
7
|
+
*
|
|
8
|
+
* `{lang}` in the url is replaced with the current language.
|
|
9
|
+
*/
|
|
10
|
+
export interface IframeEmbedProps {
|
|
11
|
+
/** Page url; `{lang}` is replaced with the current i18n language. */
|
|
12
|
+
url?: string;
|
|
13
|
+
/** Legacy form: becomes `${baseUrl}?lang={lang}`. */
|
|
14
|
+
baseUrl?: string;
|
|
15
|
+
title?: string;
|
|
16
|
+
className?: string;
|
|
17
|
+
/**
|
|
18
|
+
* true (default): size the iframe to its content with iframe-resizer. The embedded
|
|
19
|
+
* page must load @iframe-resizer/child v5.
|
|
20
|
+
* false: fixed size from `height` / `minHeight`.
|
|
21
|
+
*/
|
|
22
|
+
autoResize?: boolean;
|
|
23
|
+
height?: string | number;
|
|
24
|
+
minHeight?: string | number;
|
|
25
|
+
/** Same-origin pages only: stylesheet url injected into the iframe document on load. */
|
|
26
|
+
stylesheet?: string;
|
|
27
|
+
/** autoResize: reveal once the size has been stable this long (ms). Default 700. */
|
|
28
|
+
settleMs?: number;
|
|
29
|
+
/** autoResize: reveal anyway after this long (ms). Default 10000. */
|
|
30
|
+
maxWaitMs?: number;
|
|
31
|
+
/** autoResize: height of the loading box (px). Default 300. */
|
|
32
|
+
placeholderHeight?: number;
|
|
33
|
+
}
|
|
34
|
+
export declare const buildIframeUrl: (props: Pick<IframeEmbedProps, "url" | "baseUrl">, lang: string) => string;
|
|
35
|
+
declare const IframeEmbed: React.FC<IframeEmbedProps>;
|
|
36
|
+
export default IframeEmbed;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
|
+
import { useTranslation } from "react-i18next";
|
|
4
|
+
import iframeResize from "@iframe-resizer/parent";
|
|
5
|
+
// True while images inside the iframe are still loading; false when we can't look inside
|
|
6
|
+
// (cross-origin), in which case the settle timer alone decides.
|
|
7
|
+
const imagesPending = (iframe) => {
|
|
8
|
+
var _a, _b;
|
|
9
|
+
try {
|
|
10
|
+
return Array.from((_b = (_a = iframe.contentDocument) === null || _a === void 0 ? void 0 : _a.images) !== null && _b !== void 0 ? _b : []).some((img) => !img.complete);
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
export const buildIframeUrl = (props, lang) => {
|
|
17
|
+
var _a;
|
|
18
|
+
const template = (_a = props.url) !== null && _a !== void 0 ? _a : (props.baseUrl ? `${props.baseUrl}?lang={lang}` : "");
|
|
19
|
+
return template.split("{lang}").join(encodeURIComponent(lang !== null && lang !== void 0 ? lang : ""));
|
|
20
|
+
};
|
|
21
|
+
const IframeEmbed = (props) => {
|
|
22
|
+
var _a, _b, _c, _d, _e;
|
|
23
|
+
const { i18n } = useTranslation();
|
|
24
|
+
const ref = useRef(null);
|
|
25
|
+
const autoResize = props.autoResize !== false;
|
|
26
|
+
const [ready, setReady] = useState(!autoResize);
|
|
27
|
+
const settleMs = (_a = props.settleMs) !== null && _a !== void 0 ? _a : 700;
|
|
28
|
+
const maxWaitMs = (_b = props.maxWaitMs) !== null && _b !== void 0 ? _b : 10000;
|
|
29
|
+
const placeholderHeight = (_c = props.placeholderHeight) !== null && _c !== void 0 ? _c : 300;
|
|
30
|
+
const url = buildIframeUrl(props, i18n.language);
|
|
31
|
+
// Streamlit-like pages draw in pieces, so the iframe is resized many times while
|
|
32
|
+
// loading. Keep it hidden in a fixed-height box until its size has been stable for
|
|
33
|
+
// settleMs (and, when same-origin, its images have loaded), then show it once.
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
const el = ref.current;
|
|
36
|
+
if (!el || !autoResize)
|
|
37
|
+
return;
|
|
38
|
+
const reveal = () => setReady(true);
|
|
39
|
+
const maxWait = setTimeout(reveal, maxWaitMs);
|
|
40
|
+
let settle;
|
|
41
|
+
const waitForSettle = () => {
|
|
42
|
+
clearTimeout(settle);
|
|
43
|
+
settle = setTimeout(() => (imagesPending(el) ? waitForSettle() : reveal()), settleMs);
|
|
44
|
+
};
|
|
45
|
+
iframeResize({
|
|
46
|
+
license: "GPLv3",
|
|
47
|
+
log: false,
|
|
48
|
+
// the first resize ('init') is the page's empty shell, before any content arrives
|
|
49
|
+
onResized: ({ type }) => {
|
|
50
|
+
if (type !== "init")
|
|
51
|
+
waitForSettle();
|
|
52
|
+
},
|
|
53
|
+
}, el);
|
|
54
|
+
return () => {
|
|
55
|
+
var _a, _b;
|
|
56
|
+
clearTimeout(settle);
|
|
57
|
+
clearTimeout(maxWait);
|
|
58
|
+
(_b = (_a = el.iFrameResizer) === null || _a === void 0 ? void 0 : _a.disconnect) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
59
|
+
};
|
|
60
|
+
}, [autoResize, settleMs, maxWaitMs]);
|
|
61
|
+
const onLoad = () => {
|
|
62
|
+
var _a;
|
|
63
|
+
if (!props.stylesheet)
|
|
64
|
+
return;
|
|
65
|
+
try {
|
|
66
|
+
const doc = (_a = ref.current) === null || _a === void 0 ? void 0 : _a.contentDocument;
|
|
67
|
+
if (!doc)
|
|
68
|
+
return;
|
|
69
|
+
const link = doc.createElement("link");
|
|
70
|
+
link.rel = "stylesheet";
|
|
71
|
+
link.href = new URL(props.stylesheet, window.location.origin).href;
|
|
72
|
+
doc.head.appendChild(link);
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
console.warn("iframeEmbed: could not inject stylesheet into iframe:", err);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const iframeStyle = autoResize
|
|
79
|
+
? { width: "1px", minWidth: "100%", border: "none", opacity: ready ? 1 : 0, transition: "opacity 0.3s ease" }
|
|
80
|
+
: {
|
|
81
|
+
width: "100%",
|
|
82
|
+
height: (_d = props.height) !== null && _d !== void 0 ? _d : "100vh",
|
|
83
|
+
minHeight: props.minHeight,
|
|
84
|
+
border: "none",
|
|
85
|
+
display: "block",
|
|
86
|
+
};
|
|
87
|
+
return (_jsxs("div", { className: props.className, style: {
|
|
88
|
+
position: "relative",
|
|
89
|
+
height: ready ? undefined : placeholderHeight,
|
|
90
|
+
overflow: ready ? undefined : "hidden",
|
|
91
|
+
}, children: [!ready && (_jsx("div", { style: {
|
|
92
|
+
position: "absolute",
|
|
93
|
+
inset: 0,
|
|
94
|
+
display: "flex",
|
|
95
|
+
alignItems: "center",
|
|
96
|
+
justifyContent: "center",
|
|
97
|
+
minHeight: placeholderHeight,
|
|
98
|
+
}, children: _jsx("div", { className: "spinner-border text-primary", role: "status", children: _jsx("span", { className: "visually-hidden", children: "Loading..." }) }) })), _jsx("iframe", { ref: ref, id: "iframe-target", src: url, title: (_e = props.title) !== null && _e !== void 0 ? _e : "Results", onLoad: onLoad, style: iframeStyle })] }));
|
|
99
|
+
};
|
|
100
|
+
export default IframeEmbed;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
export declare const IframeEmbed: (props: any) => React.FunctionComponentElement<{
|
|
3
|
+
fallback: null;
|
|
4
|
+
}>;
|
|
5
|
+
export declare const BlogPosts: (props: any) => React.FunctionComponentElement<{
|
|
6
|
+
fallback: null;
|
|
7
|
+
}>;
|
|
8
|
+
/** Extension names usable in pages.json (`"config": { "type": "<name>", ... }`). */
|
|
9
|
+
export declare const defaultExtensions: {
|
|
10
|
+
name: string;
|
|
11
|
+
component: (props: any) => React.FunctionComponentElement<{
|
|
12
|
+
fallback: null;
|
|
13
|
+
}>;
|
|
14
|
+
}[];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React, { Suspense } from "react";
|
|
2
|
+
// Lazy so a country that doesn't use an extension doesn't pay for it in the main bundle.
|
|
3
|
+
const LazyIframeEmbed = React.lazy(() => import("./IframeEmbed"));
|
|
4
|
+
const LazyBlogPosts = React.lazy(() => import("./BlogPosts/BlogPosts"));
|
|
5
|
+
const lazy = (C) => (props) => React.createElement(Suspense, { fallback: null }, React.createElement(C, props));
|
|
6
|
+
export const IframeEmbed = lazy(LazyIframeEmbed);
|
|
7
|
+
export const BlogPosts = lazy(LazyBlogPosts);
|
|
8
|
+
/** Extension names usable in pages.json (`"config": { "type": "<name>", ... }`). */
|
|
9
|
+
export const defaultExtensions = [
|
|
10
|
+
{ name: "iframeEmbed", component: IframeEmbed },
|
|
11
|
+
{ name: "blogPosts", component: BlogPosts },
|
|
12
|
+
];
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { renderParticipantApp } from "./render";
|
|
2
|
+
export type { RenderOptions } from "./render";
|
|
3
|
+
export { default as ParticipantApp } from "./ParticipantApp";
|
|
4
|
+
export type { ParticipantAppProps, Extension, DateLocale } from "./ParticipantApp";
|
|
5
|
+
export { defaultExtensions, IframeEmbed, BlogPosts } from "./extensions";
|
|
6
|
+
export type { IframeEmbedProps } from "./extensions/IframeEmbed";
|
|
7
|
+
export type { BlogPostsProps } from "./extensions/BlogPosts/BlogPosts";
|
|
8
|
+
export { kitTexts, addDefaultTexts } from "./defaultTexts";
|
|
9
|
+
export type { DefaultTexts } from "./defaultTexts";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"unsubscribe": {
|
|
3
|
+
"newsletter": {
|
|
4
|
+
"title": "Unsubscribe from the newsletter",
|
|
5
|
+
"info": "Confirm to stop receiving the newsletter.",
|
|
6
|
+
"success": "Done: you will no longer receive the newsletter. You can subscribe again at any time in your account settings."
|
|
7
|
+
},
|
|
8
|
+
"weekly": {
|
|
9
|
+
"title": "Stop the weekly reminder",
|
|
10
|
+
"info": "Confirm to stop receiving the weekly reminder. You can still fill in the questionnaire whenever you like.",
|
|
11
|
+
"success": "Done: you will no longer receive the weekly reminder. You can turn it back on at any time in your account settings."
|
|
12
|
+
},
|
|
13
|
+
"confirmBtn": "Confirm",
|
|
14
|
+
"retryBtn": "Try again",
|
|
15
|
+
"homeBtn": "Go to the home page",
|
|
16
|
+
"invalidLink": "This link is not valid or has expired. You can change your subscriptions in your account settings after logging in.",
|
|
17
|
+
"error": "Something went wrong and nothing was changed. Please try again in a few minutes."
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"unsubscribe": {
|
|
3
|
+
"newsletter": {
|
|
4
|
+
"title": "Disiscriviti dalla newsletter",
|
|
5
|
+
"info": "Conferma per non ricevere più la newsletter.",
|
|
6
|
+
"success": "Fatto: non riceverai più la newsletter. Puoi iscriverti di nuovo in qualsiasi momento dalle impostazioni del tuo account."
|
|
7
|
+
},
|
|
8
|
+
"weekly": {
|
|
9
|
+
"title": "Disattiva il promemoria settimanale",
|
|
10
|
+
"info": "Conferma per non ricevere più il promemoria settimanale. Potrai comunque compilare il questionario quando vuoi.",
|
|
11
|
+
"success": "Fatto: non riceverai più il promemoria settimanale. Puoi riattivarlo in qualsiasi momento dalle impostazioni del tuo account."
|
|
12
|
+
},
|
|
13
|
+
"confirmBtn": "Conferma",
|
|
14
|
+
"retryBtn": "Riprova",
|
|
15
|
+
"homeBtn": "Vai alla pagina iniziale",
|
|
16
|
+
"invalidLink": "Il link non è valido o è scaduto. Puoi modificare le tue iscrizioni dalle impostazioni del tuo account, dopo aver effettuato l'accesso.",
|
|
17
|
+
"error": "Si è verificato un errore e non è stato modificato nulla. Riprova tra qualche minuto."
|
|
18
|
+
}
|
|
19
|
+
}
|
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import "./runtime";
|
|
2
|
+
import { ParticipantAppProps } from "./ParticipantApp";
|
|
3
|
+
export interface RenderOptions extends ParticipantAppProps {
|
|
4
|
+
/** Defaults to REACT_APP_FALLBACK_LANGUAGE, then "en". */
|
|
5
|
+
fallbackLanguage?: string;
|
|
6
|
+
/** Where locales are fetched from. Defaults to REACT_APP_CONTENT_URL + "/locales". */
|
|
7
|
+
localeUrl?: string;
|
|
8
|
+
/** Runs after i18n init and before the first render: register reducers, local-storage hooks, ... */
|
|
9
|
+
beforeRender?: () => void;
|
|
10
|
+
/** DOM element id to mount into. Default "root". */
|
|
11
|
+
rootId?: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Boots a participant web app: i18n (with the kit's default texts under the country's), redux
|
|
15
|
+
* store, Suspense fallback, and the ParticipantApp (AppCore + kit extensions + country extensions).
|
|
16
|
+
*/
|
|
17
|
+
export declare function renderParticipantApp(options: RenderOptions): void;
|
package/dist/render.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React, { Suspense } from "react";
|
|
3
|
+
import ReactDOM from "react-dom";
|
|
4
|
+
import { Provider } from "react-redux";
|
|
5
|
+
import { store, initI18n } from "@influenzanet/case-web-app-core";
|
|
6
|
+
import { LoadingPlaceholder } from "@influenzanet/case-web-ui";
|
|
7
|
+
import "./runtime";
|
|
8
|
+
import { env } from "./env";
|
|
9
|
+
import ParticipantApp from "./ParticipantApp";
|
|
10
|
+
import { addDefaultTexts, kitTexts } from "./defaultTexts";
|
|
11
|
+
/**
|
|
12
|
+
* Boots a participant web app: i18n (with the kit's default texts under the country's), redux
|
|
13
|
+
* store, Suspense fallback, and the ParticipantApp (AppCore + kit extensions + country extensions).
|
|
14
|
+
*/
|
|
15
|
+
export function renderParticipantApp(options) {
|
|
16
|
+
var _a, _b, _c, _d;
|
|
17
|
+
const { fallbackLanguage, localeUrl, beforeRender, rootId, ...appProps } = options;
|
|
18
|
+
const defaultLanguage = (_b = (_a = appProps.defaultLanguage) !== null && _a !== void 0 ? _a : env.defaultLanguage) !== null && _b !== void 0 ? _b : "en";
|
|
19
|
+
initI18n(defaultLanguage, (_c = fallbackLanguage !== null && fallbackLanguage !== void 0 ? fallbackLanguage : env.fallbackLanguage) !== null && _c !== void 0 ? _c : "en", localeUrl !== null && localeUrl !== void 0 ? localeUrl : `${(_d = env.contentUrl) !== null && _d !== void 0 ? _d : "/assets"}/locales`);
|
|
20
|
+
addDefaultTexts(kitTexts);
|
|
21
|
+
beforeRender === null || beforeRender === void 0 ? void 0 : beforeRender();
|
|
22
|
+
ReactDOM.render(_jsx(React.StrictMode, { children: _jsx(Provider, { store: store, children: _jsx(Suspense, { fallback: _jsx(LoadingPlaceholder, { color: "secondary", minHeight: "100vh" }), children: _jsx(ParticipantApp, { ...appProps, defaultLanguage: defaultLanguage }) }) }) }), document.getElementById(rootId !== null && rootId !== void 0 ? rootId : "root"));
|
|
23
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import "@fontsource/open-sans";
|
|
2
|
+
import "@fontsource/open-sans/400-italic.css";
|
|
3
|
+
import "@fontsource/open-sans/700.css";
|
|
4
|
+
import "@fortawesome/fontawesome-free/js/solid";
|
|
5
|
+
import "@fortawesome/fontawesome-free/js/brands";
|
|
6
|
+
import "@fortawesome/fontawesome-free/js/fontawesome";
|
|
7
|
+
import "bootstrap/dist/js/bootstrap.bundle";
|
|
8
|
+
export {};
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Global side effects every participant app needs: fonts, icons, Bootstrap JS.
|
|
2
|
+
import "@fontsource/open-sans";
|
|
3
|
+
import "@fontsource/open-sans/400-italic.css";
|
|
4
|
+
import "@fontsource/open-sans/700.css";
|
|
5
|
+
import "@fortawesome/fontawesome-free/js/solid";
|
|
6
|
+
import "@fortawesome/fontawesome-free/js/brands";
|
|
7
|
+
import "@fortawesome/fontawesome-free/js/fontawesome";
|
|
8
|
+
import "bootstrap/dist/js/bootstrap.bundle";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type UserConfig, type ConfigEnv } from "vite";
|
|
2
|
+
export interface InfluenzanetViteOptions {
|
|
3
|
+
/** Backend the dev server proxies API calls to (e.g. https://influweb.staging.influenzanet.info). */
|
|
4
|
+
proxyTarget?: string;
|
|
5
|
+
/** Paths proxied to proxyTarget. Default ["/api"]. */
|
|
6
|
+
proxyPaths?: string[];
|
|
7
|
+
/** Paths proxied with websockets enabled (e.g. a Streamlit dashboard). Default []. */
|
|
8
|
+
wsProxyPaths?: string[];
|
|
9
|
+
/**
|
|
10
|
+
* Prefix -> URL proxies to other backends, prefix stripped:
|
|
11
|
+
* { "/results-proxy": "https://storage.googleapis.com/bucket" } sends
|
|
12
|
+
* /results-proxy/a.html to https://storage.googleapis.com/bucket/a.html.
|
|
13
|
+
*/
|
|
14
|
+
pathProxies?: Record<string, string>;
|
|
15
|
+
/** Build output directory. Default "dist". */
|
|
16
|
+
outDir?: string;
|
|
17
|
+
/** Extra Vite config, deep-merged last. */
|
|
18
|
+
config?: UserConfig;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Shared Vite config for participant web apps.
|
|
22
|
+
*
|
|
23
|
+
* - REACT_APP_* env vars (CRA naming, used by case-web-app-core) are exposed as
|
|
24
|
+
* process.env.REACT_APP_* and as %REACT_APP_*% in index.html.
|
|
25
|
+
* - REACT_APP_VERSION / REACT_APP_SURVEY_ENGINE_VERSION come from package.json.
|
|
26
|
+
* - SCSS can import from node_modules (with or without the CRA "~" prefix).
|
|
27
|
+
*/
|
|
28
|
+
export declare function influenzanetVite(options?: InfluenzanetViteOptions): ({ mode }: ConfigEnv) => UserConfig;
|
|
29
|
+
export default influenzanetVite;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import react from "@vitejs/plugin-react";
|
|
5
|
+
import { loadEnv, mergeConfig } from "vite";
|
|
6
|
+
const KIT = "@influenzanet/participant-webapp-kit";
|
|
7
|
+
const readJson = (path) => JSON.parse(readFileSync(path, "utf-8"));
|
|
8
|
+
// The version actually installed, falling back to the range in package.json.
|
|
9
|
+
const installedVersion = (root, pkg, name) => {
|
|
10
|
+
try {
|
|
11
|
+
const req = createRequire(join(root, "package.json"));
|
|
12
|
+
return readJson(req.resolve(`${name}/package.json`)).version;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return pkg.dependencies?.[name];
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
// CRA leftovers in index.html: %PUBLIC_URL% is always "" (apps are served from /).
|
|
19
|
+
const publicUrlPlugin = () => ({
|
|
20
|
+
name: "influenzanet:public-url",
|
|
21
|
+
transformIndexHtml: (html) => html.split("%PUBLIC_URL%").join(""),
|
|
22
|
+
});
|
|
23
|
+
/**
|
|
24
|
+
* Shared Vite config for participant web apps.
|
|
25
|
+
*
|
|
26
|
+
* - REACT_APP_* env vars (CRA naming, used by case-web-app-core) are exposed as
|
|
27
|
+
* process.env.REACT_APP_* and as %REACT_APP_*% in index.html.
|
|
28
|
+
* - REACT_APP_VERSION / REACT_APP_SURVEY_ENGINE_VERSION come from package.json.
|
|
29
|
+
* - SCSS can import from node_modules (with or without the CRA "~" prefix).
|
|
30
|
+
*/
|
|
31
|
+
export function influenzanetVite(options = {}) {
|
|
32
|
+
return ({ mode }) => {
|
|
33
|
+
const root = process.cwd();
|
|
34
|
+
const pkg = existsSync(join(root, "package.json")) ? readJson(join(root, "package.json")) : {};
|
|
35
|
+
const env = loadEnv(mode, root, "REACT_APP_");
|
|
36
|
+
const defines = Object.fromEntries(Object.entries(env).map(([k, v]) => [`process.env.${k}`, JSON.stringify(v)]));
|
|
37
|
+
const proxy = {};
|
|
38
|
+
if (options.proxyTarget) {
|
|
39
|
+
const common = {
|
|
40
|
+
target: options.proxyTarget,
|
|
41
|
+
changeOrigin: true,
|
|
42
|
+
secure: false,
|
|
43
|
+
// A backend redirect to its own absolute URL (e.g. Streamlit: /dashboard -> https://staging/dashboard/)
|
|
44
|
+
// would take the browser off the dev server: an iframe then becomes cross-origin, so
|
|
45
|
+
// iframe-resizer ignores it and cookies are dropped. Rewrite such redirects back to us.
|
|
46
|
+
autoRewrite: true,
|
|
47
|
+
protocolRewrite: "http",
|
|
48
|
+
};
|
|
49
|
+
for (const p of options.proxyPaths ?? ["/api"]) {
|
|
50
|
+
proxy[p] = { ...common };
|
|
51
|
+
}
|
|
52
|
+
for (const p of options.wsProxyPaths ?? []) {
|
|
53
|
+
proxy[p] = { ...common, ws: true };
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
for (const [prefix, url] of Object.entries(options.pathProxies ?? {})) {
|
|
57
|
+
proxy[prefix] = {
|
|
58
|
+
target: url,
|
|
59
|
+
changeOrigin: true,
|
|
60
|
+
rewrite: (path) => path.slice(prefix.length) || "/",
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
const base = {
|
|
64
|
+
plugins: [react(), publicUrlPlugin()],
|
|
65
|
+
envPrefix: ["VITE_", "REACT_APP_"],
|
|
66
|
+
define: {
|
|
67
|
+
"process.env.NODE_ENV": JSON.stringify(mode === "production" ? "production" : "development"),
|
|
68
|
+
"process.env.PUBLIC_URL": JSON.stringify(""),
|
|
69
|
+
...defines,
|
|
70
|
+
// replaces the CRA $npm_package_* magic
|
|
71
|
+
"process.env.REACT_APP_VERSION": JSON.stringify(pkg.version),
|
|
72
|
+
"process.env.REACT_APP_SURVEY_ENGINE_VERSION": JSON.stringify(installedVersion(root, pkg, "survey-engine")),
|
|
73
|
+
// catch-all: any other process.env.X evaluates to undefined
|
|
74
|
+
"process.env": "{}",
|
|
75
|
+
},
|
|
76
|
+
resolve: {
|
|
77
|
+
preserveSymlinks: true,
|
|
78
|
+
// one copy of these, even when the kit or core are linked locally
|
|
79
|
+
dedupe: [
|
|
80
|
+
"react",
|
|
81
|
+
"react-dom",
|
|
82
|
+
"react-redux",
|
|
83
|
+
"react-i18next",
|
|
84
|
+
"i18next",
|
|
85
|
+
"@influenzanet/case-web-app-core",
|
|
86
|
+
"@influenzanet/case-web-ui",
|
|
87
|
+
],
|
|
88
|
+
// CRA-style "~pkg/..." imports in SCSS
|
|
89
|
+
alias: [{ find: /^~(?!\/)(.*)$/, replacement: "$1" }],
|
|
90
|
+
},
|
|
91
|
+
optimizeDeps: {
|
|
92
|
+
// the kit ships ESM + CSS imports; let Vite transform it like app code
|
|
93
|
+
exclude: [KIT],
|
|
94
|
+
// Vite doesn't crawl excluded packages at startup: list what the kit imports so
|
|
95
|
+
// it's pre-bundled once (one shared copy, no re-optimize + reload on first load)
|
|
96
|
+
include: [
|
|
97
|
+
"react",
|
|
98
|
+
"react-dom",
|
|
99
|
+
"react/jsx-runtime",
|
|
100
|
+
"react-redux",
|
|
101
|
+
"react-i18next",
|
|
102
|
+
"@influenzanet/case-web-app-core",
|
|
103
|
+
"@influenzanet/case-web-ui",
|
|
104
|
+
`${KIT} > @iframe-resizer/parent`,
|
|
105
|
+
"bootstrap/dist/js/bootstrap.bundle",
|
|
106
|
+
"@fortawesome/fontawesome-free/js/solid",
|
|
107
|
+
"@fortawesome/fontawesome-free/js/brands",
|
|
108
|
+
"@fortawesome/fontawesome-free/js/fontawesome",
|
|
109
|
+
],
|
|
110
|
+
},
|
|
111
|
+
css: {
|
|
112
|
+
preprocessorOptions: {
|
|
113
|
+
scss: { loadPaths: ["node_modules"] },
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
build: {
|
|
117
|
+
outDir: options.outDir ?? "dist",
|
|
118
|
+
// hashed bundles go to /static (nginx caches them immutably), away from the
|
|
119
|
+
// /assets content folder (configs, locales, images) that is served no-cache
|
|
120
|
+
assetsDir: "static",
|
|
121
|
+
sourcemap: false,
|
|
122
|
+
},
|
|
123
|
+
server: { proxy },
|
|
124
|
+
};
|
|
125
|
+
return options.config ? mergeConfig(base, options.config) : base;
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
export default influenzanetVite;
|
package/package.json
CHANGED
|
@@ -1,6 +1,85 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@influenzanet/participant-webapp-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Shared code for Influenzanet participant web apps: bootstrap, Vite preset and generic page extensions on top of case-web-app-core",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/influenzanet/participant-webapp-kit.git"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"sideEffects": [
|
|
12
|
+
"*.css",
|
|
13
|
+
"./dist/runtime.js"
|
|
14
|
+
],
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./vite": {
|
|
23
|
+
"types": "./dist/vite/index.d.ts",
|
|
24
|
+
"import": "./dist/vite/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"clean": "rm -rf dist",
|
|
33
|
+
"build": "yarn clean && tsc -p tsconfig.json && tsc -p tsconfig.vite.json && node scripts/copy-css.mjs",
|
|
34
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.vite.json --noEmit",
|
|
35
|
+
"test": "vitest run",
|
|
36
|
+
"prepublishOnly": "yarn build"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@fontsource/open-sans": "^4.5.14",
|
|
40
|
+
"@fortawesome/fontawesome-free": "^5.15.4",
|
|
41
|
+
"@iframe-resizer/parent": "^5.5.9"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@influenzanet/case-web-app-core": ">=2.7.8",
|
|
45
|
+
"@influenzanet/case-web-ui": ">=1.15.1",
|
|
46
|
+
"@vitejs/plugin-react": ">=4",
|
|
47
|
+
"bootstrap": "^5.1.3",
|
|
48
|
+
"i18next": ">=21",
|
|
49
|
+
"react": "^17.0.2",
|
|
50
|
+
"react-dom": "^17.0.2",
|
|
51
|
+
"react-i18next": ">=11",
|
|
52
|
+
"react-redux": "^7.2.8",
|
|
53
|
+
"vite": ">=5"
|
|
54
|
+
},
|
|
55
|
+
"peerDependenciesMeta": {
|
|
56
|
+
"@vitejs/plugin-react": {
|
|
57
|
+
"optional": true
|
|
58
|
+
},
|
|
59
|
+
"vite": {
|
|
60
|
+
"optional": true
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"@influenzanet/case-web-app-core": "2.7.8",
|
|
65
|
+
"@influenzanet/case-web-ui": "1.15.1",
|
|
66
|
+
"@types/node": "^22.0.0",
|
|
67
|
+
"@types/react": "^17.0.2",
|
|
68
|
+
"@types/react-dom": "^17.0.2",
|
|
69
|
+
"@vitejs/plugin-react": "^5.0.0",
|
|
70
|
+
"bootstrap": "^5.1.3",
|
|
71
|
+
"i18next": "^21.7.1",
|
|
72
|
+
"react": "^17.0.2",
|
|
73
|
+
"react-dom": "^17.0.2",
|
|
74
|
+
"react-i18next": "^11.16.8",
|
|
75
|
+
"react-redux": "^7.2.8",
|
|
76
|
+
"redux": "^4.2.0",
|
|
77
|
+
"survey-engine": "1.2.1",
|
|
78
|
+
"typescript": "^5.6.0",
|
|
79
|
+
"vite": "^6.4.2",
|
|
80
|
+
"vitest": "^3.2.0"
|
|
81
|
+
},
|
|
82
|
+
"resolutions": {
|
|
83
|
+
"@types/react": "^17.0.2"
|
|
84
|
+
}
|
|
85
|
+
}
|