react-iframe-bridge 1.0.0 → 2.0.0

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.
Files changed (143) hide show
  1. package/README.md +0 -7
  2. package/{lib-cjs/components/ErrorPage.d.ts → lib/components/error_page.d.ts} +1 -0
  3. package/lib/components/error_page.d.ts.map +1 -0
  4. package/lib/components/error_page.js +15 -0
  5. package/lib/components/error_page.js.map +1 -0
  6. package/lib/components/home/{Home.d.ts → home.d.ts} +1 -0
  7. package/lib/components/home/home.d.ts.map +1 -0
  8. package/lib/components/home/home.js +44 -0
  9. package/lib/components/home/home.js.map +1 -0
  10. package/{lib-cjs/components/home/HomeContext.d.ts → lib/components/home/home_context.d.ts} +2 -1
  11. package/lib/components/home/home_context.d.ts.map +1 -0
  12. package/lib/components/home/{HomeContext.js → home_context.js} +4 -3
  13. package/lib/components/home/home_context.js.map +1 -0
  14. package/{lib-cjs/components/home/HomeHeader.d.ts → lib/components/home/home_header.d.ts} +1 -0
  15. package/lib/components/home/home_header.d.ts.map +1 -0
  16. package/lib/components/home/home_header.js +16 -0
  17. package/lib/components/home/home_header.js.map +1 -0
  18. package/lib/components/home/{HomeIframe.d.ts → home_iframe.d.ts} +1 -0
  19. package/lib/components/home/home_iframe.d.ts.map +1 -0
  20. package/lib/components/home/{HomeIframe.js → home_iframe.js} +8 -2
  21. package/lib/components/home/home_iframe.js.map +1 -0
  22. package/{lib-cjs/components/home/HomeNoSample.d.ts → lib/components/home/home_no_sample.d.ts} +1 -0
  23. package/lib/components/home/home_no_sample.d.ts.map +1 -0
  24. package/lib/components/home/{HomeNoSample.js → home_no_sample.js} +3 -2
  25. package/lib/components/home/home_no_sample.js.map +1 -0
  26. package/{lib-cjs/components/home/HomeSamples.d.ts → lib/components/home/home_samples.d.ts} +1 -0
  27. package/lib/components/home/home_samples.d.ts.map +1 -0
  28. package/lib/components/home/home_samples.js +29 -0
  29. package/lib/components/home/home_samples.js.map +1 -0
  30. package/{lib-cjs/components/home/HomeSelector.d.ts → lib/components/home/home_selector.d.ts} +1 -0
  31. package/lib/components/home/home_selector.d.ts.map +1 -0
  32. package/lib/components/home/home_selector.js +15 -0
  33. package/lib/components/home/home_selector.js.map +1 -0
  34. package/{lib-cjs/components/Input.d.ts → lib/components/input.d.ts} +3 -2
  35. package/lib/components/input.d.ts.map +1 -0
  36. package/lib/components/input.js +14 -0
  37. package/lib/components/input.js.map +1 -0
  38. package/{lib-cjs/components/LoadingFull.d.ts → lib/components/loading_full.d.ts} +1 -0
  39. package/lib/components/loading_full.d.ts.map +1 -0
  40. package/lib/components/loading_full.js +12 -0
  41. package/lib/components/loading_full.js.map +1 -0
  42. package/lib/components/{Spinner.d.ts → spinner.d.ts} +3 -1
  43. package/lib/components/spinner.d.ts.map +1 -0
  44. package/lib/components/spinner.js +18 -0
  45. package/lib/components/spinner.js.map +1 -0
  46. package/lib/contexts/{iframeBridge.d.ts → iframe_bridge.d.ts} +2 -1
  47. package/lib/contexts/iframe_bridge.d.ts.map +1 -0
  48. package/lib/contexts/{iframeBridge.js → iframe_bridge.js} +5 -6
  49. package/lib/contexts/iframe_bridge.js.map +1 -0
  50. package/lib/contexts/roc.d.ts +1 -0
  51. package/lib/contexts/roc.d.ts.map +1 -0
  52. package/lib/contexts/roc.js +1 -0
  53. package/lib/contexts/roc.js.map +1 -0
  54. package/{lib-cjs/hooks/localStorage.d.ts → lib/hooks/local_storage.d.ts} +1 -0
  55. package/lib/hooks/local_storage.d.ts.map +1 -0
  56. package/lib/hooks/{localStorage.js → local_storage.js} +2 -1
  57. package/lib/hooks/local_storage.js.map +1 -0
  58. package/{lib-cjs/hooks/useRocQuery.d.ts → lib/hooks/use_roc_query.d.ts} +1 -0
  59. package/lib/hooks/use_roc_query.d.ts.map +1 -0
  60. package/lib/hooks/{useRocQuery.js → use_roc_query.js} +2 -1
  61. package/lib/hooks/use_roc_query.js.map +1 -0
  62. package/lib/index.d.ts +4 -3
  63. package/lib/index.d.ts.map +1 -0
  64. package/lib/index.js +4 -3
  65. package/lib/index.js.map +1 -0
  66. package/lib/types/db.d.ts +5 -0
  67. package/lib/types/db.d.ts.map +1 -0
  68. package/lib/types/db.js +1 -0
  69. package/lib/types/db.js.map +1 -0
  70. package/lib/types/util.d.ts +1 -0
  71. package/lib/types/util.d.ts.map +1 -0
  72. package/lib/types/util.js +1 -0
  73. package/lib/types/util.js.map +1 -0
  74. package/{lib-cjs/utils/localStorage.d.ts → lib/utils/local_storage.d.ts} +1 -0
  75. package/lib/utils/local_storage.d.ts.map +1 -0
  76. package/lib/utils/{localStorage.js → local_storage.js} +1 -0
  77. package/lib/utils/local_storage.js.map +1 -0
  78. package/package.json +7 -7
  79. package/src/components/error_page.tsx +39 -0
  80. package/src/components/home/home.tsx +98 -0
  81. package/src/components/home/home_context.tsx +101 -0
  82. package/src/components/home/home_header.tsx +29 -0
  83. package/src/components/home/home_iframe.tsx +73 -0
  84. package/src/components/home/home_no_sample.tsx +16 -0
  85. package/src/components/home/home_samples.tsx +62 -0
  86. package/src/components/home/home_selector.tsx +23 -0
  87. package/src/components/input.tsx +31 -0
  88. package/src/components/loading_full.tsx +19 -0
  89. package/src/components/spinner.tsx +44 -0
  90. package/src/contexts/iframe_bridge.tsx +234 -0
  91. package/src/contexts/roc.tsx +23 -0
  92. package/src/hooks/local_storage.ts +45 -0
  93. package/src/hooks/use_roc_query.ts +75 -0
  94. package/src/index.ts +7 -0
  95. package/src/types/db.ts +81 -0
  96. package/src/types/util.ts +3 -0
  97. package/src/utils/local_storage.ts +35 -0
  98. package/lib/components/ErrorPage.d.ts +0 -8
  99. package/lib/components/ErrorPage.js +0 -4
  100. package/lib/components/Input.d.ts +0 -10
  101. package/lib/components/Input.js +0 -5
  102. package/lib/components/LoadingFull.d.ts +0 -1
  103. package/lib/components/LoadingFull.js +0 -5
  104. package/lib/components/Spinner.js +0 -5
  105. package/lib/components/home/Home.js +0 -22
  106. package/lib/components/home/HomeContext.d.ts +0 -20
  107. package/lib/components/home/HomeHeader.d.ts +0 -1
  108. package/lib/components/home/HomeHeader.js +0 -10
  109. package/lib/components/home/HomeNoSample.d.ts +0 -1
  110. package/lib/components/home/HomeSamples.d.ts +0 -1
  111. package/lib/components/home/HomeSamples.js +0 -23
  112. package/lib/components/home/HomeSelector.d.ts +0 -5
  113. package/lib/components/home/HomeSelector.js +0 -5
  114. package/lib/hooks/localStorage.d.ts +0 -13
  115. package/lib/hooks/useRocQuery.d.ts +0 -13
  116. package/lib/utils/localStorage.d.ts +0 -3
  117. package/lib-cjs/components/ErrorPage.js +0 -7
  118. package/lib-cjs/components/Input.js +0 -11
  119. package/lib-cjs/components/LoadingFull.js +0 -11
  120. package/lib-cjs/components/Spinner.d.ts +0 -4
  121. package/lib-cjs/components/Spinner.js +0 -11
  122. package/lib-cjs/components/home/Home.d.ts +0 -26
  123. package/lib-cjs/components/home/Home.js +0 -28
  124. package/lib-cjs/components/home/HomeContext.js +0 -54
  125. package/lib-cjs/components/home/HomeHeader.js +0 -16
  126. package/lib-cjs/components/home/HomeIframe.d.ts +0 -5
  127. package/lib-cjs/components/home/HomeIframe.js +0 -37
  128. package/lib-cjs/components/home/HomeNoSample.js +0 -14
  129. package/lib-cjs/components/home/HomeSamples.js +0 -29
  130. package/lib-cjs/components/home/HomeSelector.js +0 -11
  131. package/lib-cjs/contexts/iframeBridge.d.ts +0 -33
  132. package/lib-cjs/contexts/iframeBridge.js +0 -130
  133. package/lib-cjs/contexts/roc.d.ts +0 -8
  134. package/lib-cjs/contexts/roc.js +0 -20
  135. package/lib-cjs/hooks/localStorage.js +0 -38
  136. package/lib-cjs/hooks/useRocQuery.js +0 -45
  137. package/lib-cjs/index.d.ts +0 -3
  138. package/lib-cjs/index.js +0 -12
  139. package/lib-cjs/types/db.d.ts +0 -78
  140. package/lib-cjs/types/db.js +0 -2
  141. package/lib-cjs/types/util.d.ts +0 -6
  142. package/lib-cjs/types/util.js +0 -2
  143. package/lib-cjs/utils/localStorage.js +0 -34
@@ -1,13 +0,0 @@
1
- /**
2
- * Like `useState`, but initializing from `localStorage` if available and saving
3
- * to `localStorage` everytime the state is changed.
4
- * @param key localStorage key. Will be appended to the `react-iframe-bridge-` prefix.
5
- * @param initialValue Value to use if the storage is empty.
6
- */
7
- export declare function useLocalStorage<ValueType>(key: string, initialValue: ValueType): readonly [ValueType, (value: ValueType) => void];
8
- /**
9
- * Save the provided value to `localStorage` everytime it changes.
10
- * @param key localStorage key. Will be appended to the `react-iframe-bridge-` prefix.
11
- * @param value Value to save.
12
- */
13
- export declare function useSaveToLocalStorage(key: string, value: unknown): void;
@@ -1,13 +0,0 @@
1
- import type { IQueryResult } from 'rest-on-couch-client';
2
- export type RocQueryResult<T> = IQueryResult<[string, string], T>;
3
- interface RocQueryState<T = unknown> {
4
- loading: boolean;
5
- error: null | Error;
6
- result: null | Array<RocQueryResult<T>>;
7
- }
8
- type RocQueryHookResult<T> = RocQueryState<T>;
9
- interface RocQueryHookOptions {
10
- mine?: boolean;
11
- }
12
- export declare function useRocQuery<T = unknown>(viewName: string, options?: RocQueryHookOptions): RocQueryHookResult<T>;
13
- export {};
@@ -1,3 +0,0 @@
1
- declare let getItem: (key: string) => unknown | null;
2
- declare let setItem: (key: string, value: unknown) => void;
3
- export { getItem, setItem };
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = ErrorPage;
4
- const jsx_runtime_1 = require("react/jsx-runtime");
5
- function ErrorPage(props) {
6
- return ((0, jsx_runtime_1.jsx)("div", { className: "max-w-2xl m-auto md:max-w-4xl", children: (0, jsx_runtime_1.jsx)("div", { className: "flex justify-between px-2 pt-4", children: (0, jsx_runtime_1.jsxs)("div", { className: "min-w-0", children: [(0, jsx_runtime_1.jsx)("h1", { className: "text-5xl font-bold sm:mt-16 text-primary-900", children: props.title }), (0, jsx_runtime_1.jsx)("h2", { className: "mt-16 text-lg sm:mt-8", children: props.subtitle }), (0, jsx_runtime_1.jsx)("div", { className: "mt-4", children: props.children })] }) }) }));
7
- }
@@ -1,11 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.default = Input;
7
- const jsx_runtime_1 = require("react/jsx-runtime");
8
- const clsx_1 = __importDefault(require("clsx"));
9
- function Input(props) {
10
- return ((0, jsx_runtime_1.jsx)("input", { name: props.name, type: "text", className: (0, clsx_1.default)('appearance-none border border-neutral-600 bg-white px-3 py-2 text-base leading-none', props.className), value: props.value, readOnly: props.readOnly, onChange: props.onChange }));
11
- }
@@ -1,11 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.default = LoadingFull;
7
- const jsx_runtime_1 = require("react/jsx-runtime");
8
- const Spinner_1 = __importDefault(require("./Spinner"));
9
- function LoadingFull() {
10
- return ((0, jsx_runtime_1.jsx)("div", { className: "flex items-center justify-center w-full h-full", children: (0, jsx_runtime_1.jsx)(Spinner_1.default, { className: "w-10 h-10 text-alternative-500" }) }));
11
- }
@@ -1,4 +0,0 @@
1
- export interface SpinnerProps {
2
- className?: string;
3
- }
4
- export default function Spinner(props: SpinnerProps): import("react/jsx-runtime").JSX.Element;
@@ -1,11 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.default = Spinner;
7
- const jsx_runtime_1 = require("react/jsx-runtime");
8
- const clsx_1 = __importDefault(require("clsx"));
9
- function Spinner(props) {
10
- return ((0, jsx_runtime_1.jsxs)("svg", { className: (0, clsx_1.default)('animate-spin', props.className), xmlns: "http://www.w3.org/2000/svg", fill: "none", viewBox: "0 0 24 24", children: [(0, jsx_runtime_1.jsx)("circle", { className: "opacity-25", cx: "12", cy: "12", r: "10", stroke: "currentColor", strokeWidth: "4" }), (0, jsx_runtime_1.jsx)("path", { className: "opacity-75", fill: "currentColor", d: "M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" })] }));
11
- }
@@ -1,26 +0,0 @@
1
- export interface HomeProps {
2
- /**
3
- * URL of the rest-on-couch instance.
4
- * @default 'http://localhost:3000/api/fake-roc'
5
- */
6
- rocUrl?: string;
7
- /**
8
- * Name of the rest-on-couch database.
9
- * @default 'eln'
10
- */
11
- database?: string;
12
- /**
13
- * Base url loaded by the iframe
14
- */
15
- baseUrl?: string;
16
- /**
17
- * Default path the iframe should load, if no path is found in local storage
18
- */
19
- defaultPath?: string;
20
- /**
21
- * Opt out of selecting a sample / selecting no sample before loading the iframe.
22
- * The sample selection UI will be hidden and the iframe will automatically load without a selected sample.
23
- */
24
- noSampleSelection?: boolean;
25
- }
26
- export declare function Home(props: HomeProps): import("react/jsx-runtime").JSX.Element;
@@ -1,28 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Home = Home;
7
- const jsx_runtime_1 = require("react/jsx-runtime");
8
- const react_1 = require("react");
9
- const HomeContext_1 = require("./HomeContext");
10
- const HomeHeader_1 = __importDefault(require("./HomeHeader"));
11
- const HomeIframe_1 = __importDefault(require("./HomeIframe"));
12
- const HomeNoSample_1 = __importDefault(require("./HomeNoSample"));
13
- const HomeSamples_1 = __importDefault(require("./HomeSamples"));
14
- function Home(props) {
15
- const { baseUrl, noSampleSelection, rocUrl, database, defaultPath } = props;
16
- return ((0, jsx_runtime_1.jsx)(HomeContext_1.HomeContextProvider, { rocUrl: rocUrl, database: database, defaultPath: defaultPath, children: (0, jsx_runtime_1.jsx)(HomeInternal, { noSampleSelection: noSampleSelection, baseUrl: baseUrl }) }));
17
- }
18
- function HomeInternal(props) {
19
- const homeDispatch = (0, HomeContext_1.useHomeDispatchContext)();
20
- (0, react_1.useEffect)(() => {
21
- if (props.noSampleSelection) {
22
- homeDispatch({
23
- type: 'OPEN_NO_SAMPLE',
24
- });
25
- }
26
- }, [props.noSampleSelection, homeDispatch]);
27
- return ((0, jsx_runtime_1.jsxs)("div", { className: "flex flex-col w-screen h-screen", children: [(0, jsx_runtime_1.jsx)(HomeHeader_1.default, {}), (0, jsx_runtime_1.jsxs)("div", { className: "flex flex-row flex-1 mt-2 border-t border-neutral-300 min-h-0", children: [!props.noSampleSelection && ((0, jsx_runtime_1.jsxs)("div", { className: "flex flex-col w-48 px-2 pt-4 space-y-3 border-r border-neutral-300 overflow-auto", children: [(0, jsx_runtime_1.jsx)(HomeNoSample_1.default, {}), (0, jsx_runtime_1.jsx)(HomeSamples_1.default, {})] })), (0, jsx_runtime_1.jsx)(HomeIframe_1.default, { baseUrl: props.baseUrl })] })] }));
28
- }
@@ -1,54 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HomeContextProvider = HomeContextProvider;
4
- exports.useHomeContext = useHomeContext;
5
- exports.useHomeDispatchContext = useHomeDispatchContext;
6
- const jsx_runtime_1 = require("react/jsx-runtime");
7
- const immer_1 = require("immer");
8
- const react_1 = require("react");
9
- const roc_1 = require("../../contexts/roc");
10
- const localStorage_1 = require("../../hooks/localStorage");
11
- const localStorage_2 = require("../../utils/localStorage");
12
- function getInitialHomeContext(config = {}) {
13
- const { rocUrl = 'http://localhost:3000/api/fake-roc', database = 'eln', defaultPath = '/dev/base-page', } = config;
14
- return {
15
- rocUrl,
16
- database,
17
- iframePath: (0, localStorage_2.getItem)('dev-home-iframe-path') || defaultPath,
18
- iframeMode: 'closed',
19
- selectedSample: null,
20
- };
21
- }
22
- const homeReducer = (0, immer_1.produce)((state, action) => {
23
- switch (action.type) {
24
- case 'OPEN_NO_SAMPLE':
25
- state.iframeMode = 'no-sample';
26
- state.selectedSample = null;
27
- break;
28
- case 'SELECT_SAMPLE':
29
- state.iframeMode = 'sample';
30
- state.selectedSample = action.payload;
31
- break;
32
- case 'SET_IFRAME_PAGE':
33
- state.iframePath = action.payload;
34
- break;
35
- default:
36
- throw new Error('unreachable');
37
- }
38
- });
39
- const homeContext = (0, react_1.createContext)(getInitialHomeContext());
40
- const homeDispatchContext = (0, react_1.createContext)(() => {
41
- // noop
42
- });
43
- function HomeContextProvider(props) {
44
- const { children, ...initial } = props;
45
- const [homeState, dispatch] = (0, react_1.useReducer)(homeReducer, initial, getInitialHomeContext);
46
- (0, localStorage_1.useSaveToLocalStorage)('dev-home-iframe-path', homeState.iframePath);
47
- return ((0, jsx_runtime_1.jsx)(homeContext.Provider, { value: homeState, children: (0, jsx_runtime_1.jsx)(homeDispatchContext.Provider, { value: dispatch, children: (0, jsx_runtime_1.jsx)(roc_1.RocProvider, { url: homeState.rocUrl, database: homeState.database, children: children }) }) }));
48
- }
49
- function useHomeContext() {
50
- return (0, react_1.useContext)(homeContext);
51
- }
52
- function useHomeDispatchContext() {
53
- return (0, react_1.useContext)(homeDispatchContext);
54
- }
@@ -1,16 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.default = HomeHeader;
7
- const jsx_runtime_1 = require("react/jsx-runtime");
8
- const Input_1 = __importDefault(require("../Input"));
9
- const HomeContext_1 = require("./HomeContext");
10
- function HomeHeader() {
11
- const { rocUrl, database, iframePath } = (0, HomeContext_1.useHomeContext)();
12
- const dispatch = (0, HomeContext_1.useHomeDispatchContext)();
13
- return ((0, jsx_runtime_1.jsxs)("header", { className: "flex flex-row p-2 space-x-4", children: [(0, jsx_runtime_1.jsx)(Input_1.default, { name: "rocUrl", className: "flex-1", value: rocUrl, readOnly: true }), (0, jsx_runtime_1.jsx)(Input_1.default, { name: "database", value: database, readOnly: true }), (0, jsx_runtime_1.jsx)(Input_1.default, { name: "iframe-page", value: iframePath, className: "flex-1", onChange: (event) => {
14
- dispatch({ type: 'SET_IFRAME_PAGE', payload: event.target.value });
15
- } })] }));
16
- }
@@ -1,5 +0,0 @@
1
- interface HomeIframeProps {
2
- baseUrl?: string;
3
- }
4
- export default function HomeIframe(props: HomeIframeProps): import("react/jsx-runtime").JSX.Element;
5
- export {};
@@ -1,37 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = HomeIframe;
4
- const jsx_runtime_1 = require("react/jsx-runtime");
5
- // https://github.com/import-js/eslint-plugin-import/issues/1810
6
- const main_1 = require("iframe-bridge/main");
7
- const react_1 = require("react");
8
- const HomeContext_1 = require("./HomeContext");
9
- function HomeIframe(props) {
10
- const { baseUrl } = props;
11
- const { database, iframePath, rocUrl, selectedSample, iframeMode } = (0, HomeContext_1.useHomeContext)();
12
- const [windowId, setWindowId] = (0, react_1.useState)();
13
- (0, react_1.useEffect)(() => {
14
- (0, main_1.registerHandler)('admin', (message) => {
15
- switch (message.type) {
16
- case 'admin.connect': {
17
- setWindowId(message.windowID);
18
- break;
19
- }
20
- default:
21
- throw new Error('unreachable');
22
- }
23
- });
24
- }, []);
25
- (0, react_1.useEffect)(() => {
26
- if (windowId === undefined)
27
- return;
28
- (0, main_1.postMessage)('tab.data', {
29
- couchDB: {
30
- url: rocUrl,
31
- database,
32
- },
33
- uuid: selectedSample,
34
- }, windowId);
35
- }, [windowId, database, rocUrl, selectedSample]);
36
- return ((0, jsx_runtime_1.jsx)("div", { className: "flex items-center justify-center flex-1", children: iframeMode !== 'closed' ? ((0, jsx_runtime_1.jsx)("iframe", { allowFullScreen: true, src: `${baseUrl || ''}${iframePath}`, className: "w-full h-full" }, selectedSample)) : ((0, jsx_runtime_1.jsx)("div", { children: "Please select something" })) }));
37
- }
@@ -1,14 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.default = HomeNoSample;
7
- const jsx_runtime_1 = require("react/jsx-runtime");
8
- const HomeContext_1 = require("./HomeContext");
9
- const HomeSelector_1 = __importDefault(require("./HomeSelector"));
10
- function HomeNoSample() {
11
- const { iframeMode } = (0, HomeContext_1.useHomeContext)();
12
- const dispatch = (0, HomeContext_1.useHomeDispatchContext)();
13
- return ((0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)(HomeSelector_1.default, { onClick: () => dispatch({ type: 'OPEN_NO_SAMPLE' }), selected: iframeMode === 'no-sample', text: "No sample" }) }));
14
- }
@@ -1,29 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.default = HomeSamples;
7
- const jsx_runtime_1 = require("react/jsx-runtime");
8
- const useRocQuery_1 = require("../../hooks/useRocQuery");
9
- const Spinner_1 = __importDefault(require("../Spinner"));
10
- const HomeContext_1 = require("./HomeContext");
11
- const HomeSelector_1 = __importDefault(require("./HomeSelector"));
12
- function HomeSamples() {
13
- const { loading, error, result } = (0, useRocQuery_1.useRocQuery)('sample_toc');
14
- if (error) {
15
- throw error;
16
- }
17
- return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("h1", { className: "mb-4 text-lg font-bold text-center", children: "Sample TOC" }), (0, jsx_runtime_1.jsx)("div", { className: "flex-1", children: loading || !result ? (0, jsx_runtime_1.jsx)(Loading, {}) : (0, jsx_runtime_1.jsx)(SampleToc, { samples: result }) })] }));
18
- }
19
- function SampleToc(props) {
20
- const { selectedSample } = (0, HomeContext_1.useHomeContext)();
21
- const dispatch = (0, HomeContext_1.useHomeDispatchContext)();
22
- function selectSample(id) {
23
- dispatch({ type: 'SELECT_SAMPLE', payload: id });
24
- }
25
- return ((0, jsx_runtime_1.jsx)("div", { className: "space-y-2", children: props.samples.map((sample) => ((0, jsx_runtime_1.jsx)(HomeSelector_1.default, { selected: sample.id === selectedSample, text: sample.value.reference, onClick: () => selectSample(sample.id) }, sample.id))) }));
26
- }
27
- function Loading() {
28
- return ((0, jsx_runtime_1.jsx)("div", { className: "flex justify-center mt-8", children: (0, jsx_runtime_1.jsx)(Spinner_1.default, { className: "w-8 h-8 text-alternative-500" }) }));
29
- }
@@ -1,11 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.default = HomeSelector;
7
- const jsx_runtime_1 = require("react/jsx-runtime");
8
- const clsx_1 = __importDefault(require("clsx"));
9
- function HomeSelector(props) {
10
- return ((0, jsx_runtime_1.jsx)("div", { className: (0, clsx_1.default)('p-1 border rounded cursor-pointer border-neutral-400', props.selected && 'bg-primary-50 shadow-inner'), onClick: props.onClick, children: props.text }));
11
- }
@@ -1,33 +0,0 @@
1
- import type { ReactNode } from 'react';
2
- import type { RocDocument } from 'rest-on-couch-client';
3
- import { Roc } from 'rest-on-couch-client';
4
- import type { SampleEntryContent, SampleEntryId } from '../types/db';
5
- export declare function useIframeBridgeContext<PublicUserInfo = unknown, PrivateUserInfo = unknown>(): IframeBridgeReadyContextType<PublicUserInfo, PrivateUserInfo>;
6
- export declare function useIframeBridgeSample(): RocDocument<SampleEntryContent, SampleEntryId>;
7
- interface IframeBridgeReadyContextTypeBase<PublicUserInfo, PrivateUserInfo> {
8
- state: 'ready';
9
- data: IframeDataMessage;
10
- roc: Roc<PublicUserInfo, PrivateUserInfo>;
11
- }
12
- interface IframeBridgeReadyContextTypeWithSample<PublicUserInfo, PrivateUserInfo> extends IframeBridgeReadyContextTypeBase<PublicUserInfo, PrivateUserInfo> {
13
- hasSample: true;
14
- sample: RocDocument<SampleEntryContent, SampleEntryId>;
15
- }
16
- interface IframeBridgeReadyContextTypeWithoutSample<PublicUserInfo, PrivateUserInfo> extends IframeBridgeReadyContextTypeBase<PublicUserInfo, PrivateUserInfo> {
17
- hasSample: false;
18
- sample: null;
19
- }
20
- type IframeBridgeReadyContextType<PublicUserInfo = unknown, PrivateUserInfo = unknown> = IframeBridgeReadyContextTypeWithSample<PublicUserInfo, PrivateUserInfo> | IframeBridgeReadyContextTypeWithoutSample<PublicUserInfo, PrivateUserInfo>;
21
- interface IframeDataMessage {
22
- couchDB: {
23
- url: string;
24
- database: string;
25
- };
26
- uuid: string;
27
- }
28
- export declare function IframeBridgeProvider(props: {
29
- children: ReactNode;
30
- requireSample?: boolean;
31
- allowStandalone?: boolean;
32
- }): import("react/jsx-runtime").JSX.Element;
33
- export {};
@@ -1,130 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.useIframeBridgeContext = useIframeBridgeContext;
7
- exports.useIframeBridgeSample = useIframeBridgeSample;
8
- exports.IframeBridgeProvider = IframeBridgeProvider;
9
- const jsx_runtime_1 = require("react/jsx-runtime");
10
- // https://github.com/import-js/eslint-plugin-import/issues/1810
11
- const iframe_1 = require("iframe-bridge/iframe");
12
- const immer_1 = require("immer");
13
- const react_1 = require("react");
14
- const rest_on_couch_client_1 = require("rest-on-couch-client");
15
- const ErrorPage_1 = __importDefault(require("../components/ErrorPage"));
16
- const LoadingFull_1 = __importDefault(require("../components/LoadingFull"));
17
- const iframeBridgeContext =
18
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
- (0, react_1.createContext)(null);
20
- function useIframeBridgeContext() {
21
- const context = (0, react_1.useContext)(iframeBridgeContext);
22
- if (!context) {
23
- throw new Error('Iframe bridge context is not ready');
24
- }
25
- return context;
26
- }
27
- function useIframeBridgeSample() {
28
- const context = useIframeBridgeContext();
29
- if (!context.sample) {
30
- throw new Error('No sample in context');
31
- }
32
- return context.sample;
33
- }
34
- const iframeBridgeReducer = (0, immer_1.produce)((state, action) => {
35
- switch (action.type) {
36
- case 'RECEIVE_DATA': {
37
- state.data = action.payload;
38
- state.roc = new rest_on_couch_client_1.Roc(action.payload.couchDB);
39
- if (action.payload.uuid) {
40
- state.state = 'loading';
41
- state.hasSample = true;
42
- state.sample = null;
43
- }
44
- else {
45
- state.state = 'ready';
46
- }
47
- break;
48
- }
49
- case 'SET_SAMPLE': {
50
- state.sample = action.payload;
51
- state.state = 'ready';
52
- break;
53
- }
54
- case 'STANDALONE_TIMEOUT': {
55
- state.state = 'standalone-error';
56
- break;
57
- }
58
- default:
59
- throw new Error('unreachable');
60
- }
61
- });
62
- const initialState = {
63
- state: 'initial',
64
- data: null,
65
- roc: null,
66
- hasSample: false,
67
- sample: null,
68
- };
69
- function IframeBridgeProvider(props) {
70
- const [state, dispatch] = (0, react_1.useReducer)(iframeBridgeReducer, initialState);
71
- (0, react_1.useEffect)(() => {
72
- (0, iframe_1.onMessage)((message) => {
73
- switch (message.type) {
74
- case 'tab.data': {
75
- dispatch({ type: 'RECEIVE_DATA', payload: message.message });
76
- break;
77
- }
78
- case 'tab.focus': {
79
- // Ignore this event. Happens in C6H6 when an already opened tab is
80
- // refocused.
81
- break;
82
- }
83
- default:
84
- // eslint-disable-next-line no-console
85
- console.error(message);
86
- throw new Error('unreachable');
87
- }
88
- });
89
- (0, iframe_1.ready)();
90
- }, []);
91
- (0, react_1.useEffect)(() => {
92
- if (!props.allowStandalone && state.state === 'initial') {
93
- const timeout = setTimeout(() => {
94
- dispatch({ type: 'STANDALONE_TIMEOUT' });
95
- }, 3000);
96
- return () => clearTimeout(timeout);
97
- }
98
- }, [props.allowStandalone, state.state]);
99
- (0, react_1.useEffect)(() => {
100
- if (!state.roc || !state.data?.uuid)
101
- return;
102
- let cancelled = false;
103
- const document = state.roc.getDocument(state.data.uuid);
104
- document
105
- .fetch()
106
- .then(() => {
107
- if (!cancelled) {
108
- dispatch({ type: 'SET_SAMPLE', payload: document });
109
- }
110
- })
111
- .catch((error) => {
112
- // TODO: handle error
113
- // eslint-disable-next-line no-console
114
- console.error(error);
115
- });
116
- return () => {
117
- cancelled = true;
118
- };
119
- }, [state.roc, state.data]);
120
- if (state.state === 'standalone-error') {
121
- return ((0, jsx_runtime_1.jsx)(ErrorPage_1.default, { title: "Invalid access", subtitle: "This page cannot be accessed without iframe-bridge." }));
122
- }
123
- if (state.state !== 'ready') {
124
- return ((0, jsx_runtime_1.jsx)("div", { className: "w-screen h-screen", children: (0, jsx_runtime_1.jsx)(LoadingFull_1.default, {}) }));
125
- }
126
- if (!state.hasSample && props.requireSample) {
127
- return ((0, jsx_runtime_1.jsx)(ErrorPage_1.default, { title: "Invalid access", subtitle: "This page must be accessed with a sample." }));
128
- }
129
- return ((0, jsx_runtime_1.jsx)(iframeBridgeContext.Provider, { value: state, children: props.children }));
130
- }
@@ -1,8 +0,0 @@
1
- import type { ReactNode } from 'react';
2
- import { Roc } from 'rest-on-couch-client';
3
- export declare function useRoc<PublicUserInfo = unknown, PrivateUserInfo = unknown>(): Roc<PublicUserInfo, PrivateUserInfo>;
4
- export declare function RocProvider(props: {
5
- children: ReactNode;
6
- url: string;
7
- database: string;
8
- }): import("react/jsx-runtime").JSX.Element;
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useRoc = useRoc;
4
- exports.RocProvider = RocProvider;
5
- const jsx_runtime_1 = require("react/jsx-runtime");
6
- const react_1 = require("react");
7
- const rest_on_couch_client_1 = require("rest-on-couch-client");
8
- const rocContext = (0, react_1.createContext)(null);
9
- function useRoc() {
10
- const roc = (0, react_1.useContext)(rocContext);
11
- if (!roc) {
12
- throw new Error('missing roc');
13
- }
14
- return roc;
15
- }
16
- function RocProvider(props) {
17
- const { url, database, children } = props;
18
- const roc = (0, react_1.useMemo)(() => new rest_on_couch_client_1.Roc({ url, database }), [url, database]);
19
- return (0, jsx_runtime_1.jsx)(rocContext.Provider, { value: roc, children: children });
20
- }
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useLocalStorage = useLocalStorage;
4
- exports.useSaveToLocalStorage = useSaveToLocalStorage;
5
- const react_1 = require("react");
6
- const localStorage_1 = require("../utils/localStorage");
7
- /**
8
- * Like `useState`, but initializing from `localStorage` if available and saving
9
- * to `localStorage` everytime the state is changed.
10
- * @param key localStorage key. Will be appended to the `react-iframe-bridge-` prefix.
11
- * @param initialValue Value to use if the storage is empty.
12
- */
13
- function useLocalStorage(key, initialValue) {
14
- const [storedValue, setStoredValue] = (0, react_1.useState)(() => {
15
- // Get from local storage by key.
16
- const item = (0, localStorage_1.getItem)(key);
17
- // Parse stored json or if none return initialValue.
18
- return item ? item : initialValue;
19
- });
20
- // Return a wrapped version of useState's setter function that persists the new value to localStorage.
21
- const setValue = (0, react_1.useCallback)((value) => {
22
- // Save state.
23
- setStoredValue(value);
24
- // Save to local storage.
25
- (0, localStorage_1.setItem)(key, value);
26
- }, [key]);
27
- return [storedValue, setValue];
28
- }
29
- /**
30
- * Save the provided value to `localStorage` everytime it changes.
31
- * @param key localStorage key. Will be appended to the `react-iframe-bridge-` prefix.
32
- * @param value Value to save.
33
- */
34
- function useSaveToLocalStorage(key, value) {
35
- (0, react_1.useEffect)(() => {
36
- (0, localStorage_1.setItem)(key, value);
37
- }, [key, value]);
38
- }
@@ -1,45 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useRocQuery = useRocQuery;
4
- const react_1 = require("react");
5
- const roc_1 = require("../contexts/roc");
6
- function rocQueryReducer(state, action) {
7
- switch (action.type) {
8
- case 'LOAD':
9
- return {
10
- ...state,
11
- error: null,
12
- loading: true,
13
- };
14
- case 'SET_RESULT':
15
- return {
16
- loading: false,
17
- error: null,
18
- result: action.value,
19
- };
20
- case 'ERROR':
21
- return { loading: false, error: action.value, result: null };
22
- default:
23
- throw new Error('unreachable');
24
- }
25
- }
26
- function useRocQuery(viewName, options = {}) {
27
- const { mine = false } = options;
28
- const roc = (0, roc_1.useRoc)();
29
- const [state, dispatch] = (0, react_1.useReducer)(rocQueryReducer, {
30
- loading: true,
31
- error: null,
32
- result: null,
33
- });
34
- (0, react_1.useEffect)(() => {
35
- dispatch({ type: 'LOAD' });
36
- const query = roc.getQuery(viewName, { mine });
37
- query
38
- .fetch()
39
- .then((result) => dispatch({ type: 'SET_RESULT', value: result }))
40
- .catch((err) => {
41
- dispatch({ type: 'ERROR', value: err });
42
- });
43
- }, [roc, viewName, mine]);
44
- return state;
45
- }
@@ -1,3 +0,0 @@
1
- export { Home } from './components/home/Home';
2
- export { useRoc, RocProvider } from './contexts/roc';
3
- export { useIframeBridgeContext, useIframeBridgeSample, IframeBridgeProvider, } from './contexts/iframeBridge';
package/lib-cjs/index.js DELETED
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.IframeBridgeProvider = exports.useIframeBridgeSample = exports.useIframeBridgeContext = exports.RocProvider = exports.useRoc = exports.Home = void 0;
4
- var Home_1 = require("./components/home/Home");
5
- Object.defineProperty(exports, "Home", { enumerable: true, get: function () { return Home_1.Home; } });
6
- var roc_1 = require("./contexts/roc");
7
- Object.defineProperty(exports, "useRoc", { enumerable: true, get: function () { return roc_1.useRoc; } });
8
- Object.defineProperty(exports, "RocProvider", { enumerable: true, get: function () { return roc_1.RocProvider; } });
9
- var iframeBridge_1 = require("./contexts/iframeBridge");
10
- Object.defineProperty(exports, "useIframeBridgeContext", { enumerable: true, get: function () { return iframeBridge_1.useIframeBridgeContext; } });
11
- Object.defineProperty(exports, "useIframeBridgeSample", { enumerable: true, get: function () { return iframeBridge_1.useIframeBridgeSample; } });
12
- Object.defineProperty(exports, "IframeBridgeProvider", { enumerable: true, get: function () { return iframeBridge_1.IframeBridgeProvider; } });