@luxonis/depthai-viewer-common 0.0.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/src/index.ts ADDED
@@ -0,0 +1,27 @@
1
+ export * from './utils/functions.js';
2
+
3
+ export * from './components/ButtonLink.js';
4
+ export * from './components/Link.js';
5
+ export * from './components/Pipeline.js';
6
+ export * from './components/Resizable.js';
7
+ export * from './components/Streams.js';
8
+ export * from './components/CTASubLabel.js';
9
+
10
+ export * from './constants/connection.js';
11
+ export * from './constants/url.js';
12
+
13
+ export * from './dai-connection/connection.js';
14
+ export * from './dai-connection/decoder.worker.js';
15
+
16
+ export * from './hooks/connection.js';
17
+ export * from './hooks/navigation.js';
18
+ export * from './hooks/search.js';
19
+
20
+ export * from './utils/dynamic-base-worker.js';
21
+ export * from './utils/functions.js';
22
+ export * from './utils/sorting.js';
23
+ export * from './utils/url.js';
24
+
25
+ export * from './providers/DataProvider.js';
26
+ export * from './providers/ConnectionProvider.js';
27
+ export * from './constants/streams.js';
@@ -0,0 +1,39 @@
1
+ import { VisualizerContext } from '@luxonis/visualizer-protobuf';
2
+ import { type ConnectionData, useCreateConnection } from '../hooks/connection.js';
3
+ import { useData } from './DataProvider.js';
4
+ import type { DAIService } from '../dai-connection/connection.js';
5
+ import { noop } from '../utils/functions.js';
6
+ import React from 'react';
7
+
8
+ export const ConnectionContext = React.createContext<ConnectionData>({
9
+ daiConnection: null,
10
+ connected: false,
11
+ topics: [],
12
+ pipeline: undefined,
13
+ isPipelineLoading: false,
14
+ setIsPipelineLoading: noop,
15
+ connections: [],
16
+ toggleTopic: noop,
17
+ });
18
+
19
+ export const ConnectionProvider: React.FC<
20
+ React.PropsWithChildren<{ activeServices: DAIService[] }>
21
+ > = ({ children, activeServices }) => {
22
+ const { connectionType, wsUrl, token, clientId } = useData();
23
+
24
+ const connectionData = useCreateConnection({
25
+ type: connectionType,
26
+ token,
27
+ clientId,
28
+ connectionUrl: wsUrl,
29
+ activeServices,
30
+ });
31
+
32
+ return (
33
+ <VisualizerContext connections={connectionData.connections}>
34
+ <ConnectionContext.Provider value={connectionData}>{children}</ConnectionContext.Provider>
35
+ </VisualizerContext>
36
+ );
37
+ };
38
+
39
+ export const useConnection = () => React.useContext(ConnectionContext);
@@ -0,0 +1,71 @@
1
+ import React from 'react';
2
+ import { DEFAULT_WS_URL, IS_LOCALHOST, LOCALHOST_WS_URL } from '../constants/url.js';
3
+ import { useFlatSearch } from '../hooks/search.js';
4
+ import { noop } from '../utils/functions.js';
5
+
6
+ export type DataContext = {
7
+ columns: number | null;
8
+ changeColumns: (columns: number | null) => void;
9
+ changeWsUrl: (url: string) => void;
10
+ connectionType: 'ws' | 'webrtc';
11
+ wsUrl: string;
12
+ token: string;
13
+ clientId: string;
14
+ };
15
+
16
+ const DataContext = React.createContext<DataContext>({
17
+ columns: null,
18
+ connectionType: 'ws',
19
+ wsUrl: DEFAULT_WS_URL,
20
+ changeColumns: noop,
21
+ changeWsUrl: noop,
22
+ token: '',
23
+ clientId: '',
24
+ });
25
+
26
+ export const useData = () => React.useContext(DataContext);
27
+
28
+ export const DataProvider: React.FC<React.PropsWithChildren> = ({ children }) => {
29
+ const { search, updateSearch } = useFlatSearch({
30
+ defaultInit: { layout: '0', t: '', cid: '' },
31
+ });
32
+ const [columns, setColumns] = React.useState<number | null>(() => {
33
+ return search.layout === '0' ? null : Number(search.layout);
34
+ });
35
+ const [connectionUrl, setConnectionUrl] = React.useState(
36
+ search.t && search.cid ? '' : IS_LOCALHOST ? LOCALHOST_WS_URL : DEFAULT_WS_URL,
37
+ );
38
+
39
+ const changeColumns = React.useCallback(
40
+ (columns: number | null) => {
41
+ setColumns(columns);
42
+ updateSearch('layout', columns ?? 0);
43
+ },
44
+ [updateSearch],
45
+ );
46
+
47
+ const changeWsUrl = React.useCallback(
48
+ (url: string) => {
49
+ setConnectionUrl(url);
50
+ updateSearch('t', null);
51
+ updateSearch('cid', null);
52
+ },
53
+ [updateSearch],
54
+ );
55
+
56
+ return (
57
+ <DataContext.Provider
58
+ value={{
59
+ token: search.t as string,
60
+ clientId: search.cid as string,
61
+ wsUrl: connectionUrl,
62
+ connectionType: search.t && search.cid ? 'webrtc' : 'ws',
63
+ columns,
64
+ changeColumns,
65
+ changeWsUrl,
66
+ }}
67
+ >
68
+ {children}
69
+ </DataContext.Provider>
70
+ );
71
+ };
@@ -0,0 +1,10 @@
1
+ export function makeColumns<T>(list: T[], columns: number | null): T[][] {
2
+ if (!columns) {
3
+ return [list];
4
+ }
5
+ const result: T[][] = [];
6
+ for (let i = 0; i < list.length; i += columns) {
7
+ result.push(list.slice(i, i + columns));
8
+ }
9
+ return result;
10
+ }
@@ -0,0 +1,21 @@
1
+ export class DynamicBaseWorker extends Worker {
2
+ public DEBUG_IS_DYNAMIC_POLYFILL = true;
3
+
4
+ constructor(scriptUrl: string | URL, options?: WorkerOptions & { basePath?: string }) {
5
+ const url = new URL(scriptUrl);
6
+ if (options?.basePath !== undefined) {
7
+ url.pathname = `${options.basePath}${url.pathname}`;
8
+ } else {
9
+ url.pathname = `${window.__basePath}${url.pathname}`;
10
+ }
11
+ super(url.toString(), options);
12
+ }
13
+
14
+ public postMessage(
15
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
16
+ message: any,
17
+ transfer?: Transferable[] | StructuredSerializeOptions,
18
+ ): void {
19
+ super.postMessage(message, transfer as Parameters<Worker['postMessage']>['1']);
20
+ }
21
+ }
@@ -0,0 +1,3 @@
1
+ export const noop = () => {
2
+ /* no-op */
3
+ };
@@ -0,0 +1,42 @@
1
+ import type { Topic } from '../hooks/connection.js';
2
+ import { StreamTypes } from '../constants/streams.js';
3
+
4
+ const PreferredStreamOrder: string[] = [
5
+ StreamTypes.Encoded,
6
+ StreamTypes.Raw,
7
+ StreamTypes.Disparity,
8
+ StreamTypes.PointCloud,
9
+ StreamTypes.LeftEncoded,
10
+ StreamTypes.LeftRaw,
11
+ StreamTypes.RightEncoded,
12
+ StreamTypes.RightRaw,
13
+ ];
14
+
15
+ export function sortStreamsDefault(streams: Topic[]) {
16
+ return streams.sort((a, b) => {
17
+ const indexA = PreferredStreamOrder.indexOf(a.name);
18
+ const indexB = PreferredStreamOrder.indexOf(b.name);
19
+
20
+ if (indexA === -1 && indexB === -1) {
21
+ if (indexA === -1) {
22
+ console.warn('No index for stream', a.name);
23
+ }
24
+
25
+ if (indexB === -1) {
26
+ console.warn('No index for stream', b.name);
27
+ }
28
+
29
+ return 0;
30
+ }
31
+
32
+ if (indexA === -1) {
33
+ return 1;
34
+ }
35
+
36
+ if (indexB === -1) {
37
+ return -1;
38
+ }
39
+
40
+ return indexA - indexB;
41
+ });
42
+ }
@@ -0,0 +1,8 @@
1
+ export function parseUrl(pathname: string) {
2
+ const segments = pathname.split('/').filter(Boolean);
3
+
4
+ const page = segments.at(-1) === 'pipeline' ? 'pipeline' : 'streams';
5
+
6
+ const basePath = segments.slice(0, page === 'pipeline' ? -1 : undefined).join('/');
7
+ return { basePath, page };
8
+ }
@@ -0,0 +1,7 @@
1
+ declare global {
2
+ interface Window {
3
+ __basePath: string;
4
+ }
5
+ }
6
+
7
+ export {};
package/tsconfig.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "compilerOptions": {
3
+ "lib": [
4
+ "DOM",
5
+ "DOM.Iterable",
6
+ "ESNext"
7
+ ],
8
+ "tsBuildInfoFile": "../../node_modules/.tmp/tsconfig-common.tsbuildinfo",
9
+ "isolatedModules": true,
10
+ "esModuleInterop": true,
11
+ "moduleResolution": "node16",
12
+ "resolveJsonModule": true,
13
+ "importsNotUsedAsValues": "remove",
14
+ "target": "ESNext",
15
+ "strict": true,
16
+ "forceConsistentCasingInFileNames": true,
17
+ "sourceMap": true,
18
+ "composite": true,
19
+ "incremental": true,
20
+ "allowJs": true,
21
+ "skipLibCheck": true,
22
+ "downlevelIteration": true,
23
+ "declaration": true,
24
+ "declarationMap": true,
25
+ "importHelpers": true,
26
+ "maxNodeModuleJsDepth": 1,
27
+ "jsx": "react-jsx",
28
+ "module": "node16",
29
+ "outDir": "./dist",
30
+ "rootDir": "./src"
31
+ },
32
+ "include": [
33
+ "src/**/*.ts",
34
+ "src/**/*.tsx"
35
+ ],
36
+ "references": []
37
+ }