quidproquo-web-react 0.0.263 → 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/lib/commonjs/api/hooks/index.d.ts +1 -0
- package/lib/commonjs/api/hooks/index.js +1 -0
- package/lib/commonjs/api/hooks/useApiRequestActionProcessor.d.ts +5 -0
- package/lib/commonjs/api/hooks/useApiRequestActionProcessor.js +22 -0
- package/lib/commonjs/api/hooks/useAuthenticatedNetworkRequest.js +2 -2
- package/lib/commonjs/api/hooks/useNetworkRequest.js +2 -2
- package/lib/commonjs/auth/hooks/useRefreshTokens.js +11 -3
- package/lib/commonjs/dateTime/DateTime.d.ts +28 -0
- package/lib/commonjs/dateTime/DateTime.js +57 -0
- package/lib/commonjs/dateTime/hooks/index.d.ts +1 -0
- package/lib/commonjs/dateTime/hooks/index.js +17 -0
- package/lib/commonjs/dateTime/hooks/useTimeAgo.d.ts +6 -0
- package/lib/commonjs/dateTime/hooks/useTimeAgo.js +26 -0
- package/lib/commonjs/dateTime/index.d.ts +2 -0
- package/lib/commonjs/dateTime/index.js +18 -0
- package/lib/commonjs/dateTime/logic/formatTimeAgo.d.ts +11 -0
- package/lib/commonjs/dateTime/logic/formatTimeAgo.js +49 -0
- package/lib/commonjs/dateTime/logic/index.d.ts +1 -0
- package/lib/commonjs/dateTime/logic/index.js +17 -0
- package/lib/commonjs/hooks/asmj/QpqContextProvider.d.ts +2 -1
- package/lib/commonjs/hooks/asmj/QpqContextProvider.js +6 -1
- package/lib/commonjs/hooks/asmj/QpqRuntimeEffectCatcher.d.ts +2 -2
- package/lib/commonjs/hooks/index.d.ts +1 -0
- package/lib/commonjs/hooks/index.js +1 -0
- package/lib/commonjs/hooks/useComputed.d.ts +1 -0
- package/lib/commonjs/hooks/useComputed.js +7 -0
- package/lib/commonjs/index.d.ts +1 -0
- package/lib/commonjs/index.js +1 -0
- package/lib/commonjs/websocket/WebSocketAuthSync/WebSocketAuthContext.d.ts +1 -0
- package/lib/commonjs/websocket/WebSocketAuthSync/WebSocketAuthContext.js +5 -0
- package/lib/commonjs/websocket/WebSocketAuthSync/WebSocketAuthSync.js +3 -3
- package/lib/commonjs/websocket/WebSocketAuthSync/hooks/index.d.ts +1 -0
- package/lib/commonjs/websocket/WebSocketAuthSync/hooks/index.js +1 -0
- package/lib/commonjs/websocket/WebSocketAuthSync/hooks/useWebSocketIsAuthenticated.d.ts +1 -0
- package/lib/commonjs/websocket/WebSocketAuthSync/hooks/useWebSocketIsAuthenticated.js +9 -0
- package/lib/commonjs/websocket/WebSocketAuthSync/hooks/useWebsocketAuthSync.d.ts +1 -1
- package/lib/commonjs/websocket/WebSocketAuthSync/hooks/useWebsocketAuthSync.js +11 -8
- package/lib/commonjs/websocket/WebSocketAuthSync/index.d.ts +1 -0
- package/lib/commonjs/websocket/WebSocketAuthSync/index.js +1 -0
- package/lib/commonjs/websocket/hooks/useSubscribeToWebSocketEvent.js +4 -2
- package/lib/commonjs/websocket/hooks/useSubscribeToWebsocket.js +4 -2
- package/lib/esm/api/hooks/index.d.ts +1 -0
- package/lib/esm/api/hooks/index.js +1 -0
- package/lib/esm/api/hooks/useApiRequestActionProcessor.d.ts +5 -0
- package/lib/esm/api/hooks/useApiRequestActionProcessor.js +15 -0
- package/lib/esm/api/hooks/useAuthenticatedNetworkRequest.js +1 -1
- package/lib/esm/api/hooks/useNetworkRequest.js +1 -1
- package/lib/esm/auth/hooks/useRefreshTokens.js +11 -3
- package/lib/esm/dateTime/DateTime.d.ts +28 -0
- package/lib/esm/dateTime/DateTime.js +54 -0
- package/lib/esm/dateTime/hooks/index.d.ts +1 -0
- package/lib/esm/dateTime/hooks/index.js +1 -0
- package/lib/esm/dateTime/hooks/useTimeAgo.d.ts +6 -0
- package/lib/esm/dateTime/hooks/useTimeAgo.js +22 -0
- package/lib/esm/dateTime/index.d.ts +2 -0
- package/lib/esm/dateTime/index.js +2 -0
- package/lib/esm/dateTime/logic/formatTimeAgo.d.ts +11 -0
- package/lib/esm/dateTime/logic/formatTimeAgo.js +44 -0
- package/lib/esm/dateTime/logic/index.d.ts +1 -0
- package/lib/esm/dateTime/logic/index.js +1 -0
- package/lib/esm/hooks/asmj/QpqContextProvider.d.ts +2 -1
- package/lib/esm/hooks/asmj/QpqContextProvider.js +4 -0
- package/lib/esm/hooks/asmj/QpqRuntimeEffectCatcher.d.ts +2 -2
- package/lib/esm/hooks/index.d.ts +1 -0
- package/lib/esm/hooks/index.js +1 -0
- package/lib/esm/hooks/useComputed.d.ts +1 -0
- package/lib/esm/hooks/useComputed.js +4 -0
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/websocket/WebSocketAuthSync/WebSocketAuthContext.d.ts +1 -0
- package/lib/esm/websocket/WebSocketAuthSync/WebSocketAuthContext.js +2 -0
- package/lib/esm/websocket/WebSocketAuthSync/WebSocketAuthSync.js +4 -4
- package/lib/esm/websocket/WebSocketAuthSync/hooks/index.d.ts +1 -0
- package/lib/esm/websocket/WebSocketAuthSync/hooks/index.js +1 -0
- package/lib/esm/websocket/WebSocketAuthSync/hooks/useWebSocketIsAuthenticated.d.ts +1 -0
- package/lib/esm/websocket/WebSocketAuthSync/hooks/useWebSocketIsAuthenticated.js +5 -0
- package/lib/esm/websocket/WebSocketAuthSync/hooks/useWebsocketAuthSync.d.ts +1 -1
- package/lib/esm/websocket/WebSocketAuthSync/hooks/useWebsocketAuthSync.js +14 -11
- package/lib/esm/websocket/WebSocketAuthSync/index.d.ts +1 -0
- package/lib/esm/websocket/WebSocketAuthSync/index.js +1 -0
- package/lib/esm/websocket/hooks/useSubscribeToWebSocketEvent.js +4 -2
- package/lib/esm/websocket/hooks/useSubscribeToWebsocket.js +4 -2
- package/package.json +10 -8
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ActionProcessorListResolver } from 'quidproquo-core';
|
|
2
|
+
export type UseApiRequestActionProcessorOptions = {
|
|
3
|
+
resolveServiceBaseUrl?: (service: string) => string;
|
|
4
|
+
};
|
|
5
|
+
export declare const useApiRequestActionProcessor: (options?: UseApiRequestActionProcessorOptions) => ActionProcessorListResolver;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createApiRequestActionProcessor } from 'quidproquo-actionprocessor-web';
|
|
2
|
+
import { useMemo, useRef } from 'react';
|
|
3
|
+
import { useAuthAccessToken } from '../../auth/hooks';
|
|
4
|
+
import { useBaseUrlResolvers } from '../../baseUrl/hooks';
|
|
5
|
+
export const useApiRequestActionProcessor = (options) => {
|
|
6
|
+
const accessToken = useAuthAccessToken();
|
|
7
|
+
const { getApiUrl } = useBaseUrlResolvers();
|
|
8
|
+
// Hold the token in a ref so the processor closure always reads the current value.
|
|
9
|
+
const tokenRef = useRef(accessToken);
|
|
10
|
+
tokenRef.current = accessToken;
|
|
11
|
+
return useMemo(() => createApiRequestActionProcessor({
|
|
12
|
+
getHeaders: () => (tokenRef.current ? { Authorization: `Bearer ${tokenRef.current}` } : {}),
|
|
13
|
+
resolveServiceBaseUrl: options?.resolveServiceBaseUrl ?? (() => getApiUrl()),
|
|
14
|
+
}), [options?.resolveServiceBaseUrl, getApiUrl]);
|
|
15
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { preformNetworkRequest } from 'quidproquo-
|
|
1
|
+
import { preformNetworkRequest } from 'quidproquo-webserver';
|
|
2
2
|
import { useAuthAccessToken } from '../../auth';
|
|
3
3
|
import { useFastCallback } from '../../hooks';
|
|
4
4
|
export const useAuthenticatedNetworkRequest = (payload) => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { preformNetworkRequest } from 'quidproquo-
|
|
1
|
+
import { preformNetworkRequest } from 'quidproquo-webserver';
|
|
2
2
|
import { useFastCallback } from '../../hooks';
|
|
3
3
|
export const useNetworkRequest = (payload) => {
|
|
4
4
|
const requeser = useFastCallback(() => preformNetworkRequest(payload));
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
|
+
import { useFastCallback } from '../../hooks/useFastCallback';
|
|
2
3
|
export const useRefreshTokens = (authenticationInfo, refreshTokens) => {
|
|
4
|
+
const stableRefreshTokens = useFastCallback(refreshTokens);
|
|
3
5
|
const refresh = () => {
|
|
4
6
|
if (authenticationInfo && authenticationInfo.refreshToken && authenticationInfo.expiresAt) {
|
|
5
7
|
const now = new Date().toISOString();
|
|
@@ -9,12 +11,12 @@ export const useRefreshTokens = (authenticationInfo, refreshTokens) => {
|
|
|
9
11
|
const refreshTime = timeToExpire - bufferTime;
|
|
10
12
|
if (refreshTime > 0) {
|
|
11
13
|
return setTimeout(() => {
|
|
12
|
-
|
|
14
|
+
stableRefreshTokens(authenticationInfo);
|
|
13
15
|
}, refreshTime);
|
|
14
16
|
}
|
|
15
17
|
else {
|
|
16
18
|
// If the token is already expired or very close to expiration, refresh immediately
|
|
17
|
-
|
|
19
|
+
stableRefreshTokens(authenticationInfo);
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
return null;
|
|
@@ -27,5 +29,11 @@ export const useRefreshTokens = (authenticationInfo, refreshTokens) => {
|
|
|
27
29
|
clearTimeout(timerId);
|
|
28
30
|
}
|
|
29
31
|
};
|
|
30
|
-
|
|
32
|
+
// Key on the stable primitives that actually drive a refresh, not the
|
|
33
|
+
// authenticationInfo object itself. Callers commonly derive that object
|
|
34
|
+
// fresh on every render (e.g. an unmemoized selector), so depending on its
|
|
35
|
+
// reference would re-run this effect every render — and when the token is
|
|
36
|
+
// expired/near-expiry the "refresh immediately" branch would then fire a
|
|
37
|
+
// network refresh on every render, an unbounded loop.
|
|
38
|
+
}, [authenticationInfo?.expiresAt, authenticationInfo?.refreshToken]);
|
|
31
39
|
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type DateInput = {
|
|
2
|
+
isoDateTime: string;
|
|
3
|
+
} | {
|
|
4
|
+
unixTimestampMs: number;
|
|
5
|
+
} | {
|
|
6
|
+
date: Date;
|
|
7
|
+
};
|
|
8
|
+
export type DateTimeProps = DateInput & {
|
|
9
|
+
/** Locale(s) for both the absolute and relative formatting. Defaults to the runtime locale. */
|
|
10
|
+
locale?: string | string[];
|
|
11
|
+
/** Options for the absolute date/time. Defaults to a short date + time. */
|
|
12
|
+
dateTimeFormatOptions?: Intl.DateTimeFormatOptions;
|
|
13
|
+
/** Show the "x minutes ago" label alongside the absolute time. Defaults to true. */
|
|
14
|
+
showTimeAgo?: boolean;
|
|
15
|
+
/** Hide the date portion of the absolute time. Defaults to false. */
|
|
16
|
+
hideDate?: boolean;
|
|
17
|
+
/** Hide the time portion of the absolute time. Defaults to false. */
|
|
18
|
+
hideTime?: boolean;
|
|
19
|
+
/** Hide the "x minutes ago" label. Takes precedence over showTimeAgo. Defaults to false. */
|
|
20
|
+
hideTimeAgo?: boolean;
|
|
21
|
+
/** Render each part on its own line instead of inline. Defaults to true. */
|
|
22
|
+
isMultiline?: boolean;
|
|
23
|
+
/** What to render when the date is missing/invalid (e.g. an empty isoDateTime). Defaults to "—". */
|
|
24
|
+
placeholder?: React.ReactNode;
|
|
25
|
+
className?: string;
|
|
26
|
+
};
|
|
27
|
+
export declare const DateTime: import("react").NamedExoticComponent<DateTimeProps>;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { memo, useMemo } from 'react';
|
|
3
|
+
import { useTimeAgo } from './hooks';
|
|
4
|
+
const DEFAULT_DATE_OPTIONS = {
|
|
5
|
+
day: '2-digit',
|
|
6
|
+
month: '2-digit',
|
|
7
|
+
year: '2-digit',
|
|
8
|
+
};
|
|
9
|
+
const DEFAULT_TIME_OPTIONS = {
|
|
10
|
+
hour: '2-digit',
|
|
11
|
+
minute: '2-digit',
|
|
12
|
+
};
|
|
13
|
+
const DATE_OPTION_KEYS = ['weekday', 'era', 'year', 'month', 'day'];
|
|
14
|
+
const TIME_OPTION_KEYS = ['hour', 'minute', 'second', 'dayPeriod', 'timeZoneName'];
|
|
15
|
+
const pickOptions = (options, keys) => {
|
|
16
|
+
if (!options) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
const picked = keys.reduce((acc, key) => {
|
|
20
|
+
if (options[key] !== undefined) {
|
|
21
|
+
acc[key] = options[key];
|
|
22
|
+
}
|
|
23
|
+
return acc;
|
|
24
|
+
}, {});
|
|
25
|
+
return Object.keys(picked).length > 0 ? picked : undefined;
|
|
26
|
+
};
|
|
27
|
+
const toDate = (props) => {
|
|
28
|
+
if ('isoDateTime' in props) {
|
|
29
|
+
return new Date(props.isoDateTime);
|
|
30
|
+
}
|
|
31
|
+
if ('unixTimestampMs' in props) {
|
|
32
|
+
return new Date(props.unixTimestampMs);
|
|
33
|
+
}
|
|
34
|
+
return props.date;
|
|
35
|
+
};
|
|
36
|
+
const component = (props) => {
|
|
37
|
+
const { locale, dateTimeFormatOptions, showTimeAgo = true, hideDate = false, hideTime = false, hideTimeAgo = false, isMultiline = true, placeholder = '—', className } = props;
|
|
38
|
+
// Stable primitive key so a fresh props object doesn't churn the memo.
|
|
39
|
+
const dateKey = 'isoDateTime' in props ? props.isoDateTime : 'unixTimestampMs' in props ? props.unixTimestampMs : props.date.getTime();
|
|
40
|
+
const date = useMemo(() => toDate(props), [dateKey]);
|
|
41
|
+
const isValidDate = !Number.isNaN(date.getTime());
|
|
42
|
+
const formattedDate = useMemo(() => (hideDate ? '' : date.toLocaleDateString(locale, pickOptions(dateTimeFormatOptions, DATE_OPTION_KEYS) ?? DEFAULT_DATE_OPTIONS)), [date, locale, dateTimeFormatOptions, hideDate]);
|
|
43
|
+
const formattedTime = useMemo(() => (hideTime ? '' : date.toLocaleTimeString(locale, pickOptions(dateTimeFormatOptions, TIME_OPTION_KEYS) ?? DEFAULT_TIME_OPTIONS)), [date, locale, dateTimeFormatOptions, hideTime]);
|
|
44
|
+
const timeAgo = useTimeAgo(date, locale);
|
|
45
|
+
const formattedDateTime = [formattedDate, formattedTime].filter(Boolean).join(' ');
|
|
46
|
+
const displayTimeAgo = showTimeAgo && !hideTimeAgo;
|
|
47
|
+
// A missing/invalid date (e.g. an empty isoDateTime) renders the placeholder instead of
|
|
48
|
+
// an "Invalid Date" string or crashing the relative-time formatter.
|
|
49
|
+
if (!isValidDate) {
|
|
50
|
+
return _jsx("time", { className: className, children: placeholder });
|
|
51
|
+
}
|
|
52
|
+
return (_jsx("time", { dateTime: date.toISOString(), title: date.toLocaleString(locale), className: className, children: isMultiline ? (_jsxs(_Fragment, { children: [formattedDateTime ? _jsx("div", { children: formattedDateTime }) : null, displayTimeAgo ? _jsx("div", { children: timeAgo }) : null] })) : (_jsxs(_Fragment, { children: [formattedDateTime, displayTimeAgo ? ` (${timeAgo})` : ''] })) }));
|
|
53
|
+
};
|
|
54
|
+
export const DateTime = memo(component);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useTimeAgo';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useTimeAgo';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a relative time phrase for `date` (e.g. "5 minutes ago") that
|
|
3
|
+
* updates itself over time. The re-render cadence adapts to the age of the
|
|
4
|
+
* date, so recent times tick every second and older ones only update hourly.
|
|
5
|
+
*/
|
|
6
|
+
export declare const useTimeAgo: (date: Date, locale?: string | string[]) => string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { formatTimeAgo, getTimeAgoUpdateIntervalMs } from 'quidproquo-web';
|
|
2
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
3
|
+
/**
|
|
4
|
+
* Returns a relative time phrase for `date` (e.g. "5 minutes ago") that
|
|
5
|
+
* updates itself over time. The re-render cadence adapts to the age of the
|
|
6
|
+
* date, so recent times tick every second and older ones only update hourly.
|
|
7
|
+
*/
|
|
8
|
+
export const useTimeAgo = (date, locale) => {
|
|
9
|
+
const [now, setNow] = useState(() => new Date());
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
let timeoutId;
|
|
12
|
+
const schedule = () => {
|
|
13
|
+
timeoutId = setTimeout(() => {
|
|
14
|
+
setNow(new Date());
|
|
15
|
+
schedule();
|
|
16
|
+
}, getTimeAgoUpdateIntervalMs(date, new Date()));
|
|
17
|
+
};
|
|
18
|
+
schedule();
|
|
19
|
+
return () => clearTimeout(timeoutId);
|
|
20
|
+
}, [date]);
|
|
21
|
+
return useMemo(() => formatTimeAgo(date, now, locale), [date, now, locale]);
|
|
22
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats the distance between `date` and `now` as a relative phrase
|
|
3
|
+
* (e.g. "5 minutes ago", "in 2 hours") using the native Intl.RelativeTimeFormat.
|
|
4
|
+
* Past dates are negative, future dates are positive.
|
|
5
|
+
*/
|
|
6
|
+
export declare const formatTimeAgo: (date: Date, now?: Date, locale?: string | string[]) => string;
|
|
7
|
+
/**
|
|
8
|
+
* How long until the relative phrase could next change, so callers can
|
|
9
|
+
* re-render no more often than necessary.
|
|
10
|
+
*/
|
|
11
|
+
export declare const getTimeAgoUpdateIntervalMs: (date: Date, now?: Date) => number;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Each amount is "how many of this unit make up one of the next unit".
|
|
2
|
+
const DIVISIONS = [
|
|
3
|
+
{ amount: 60, unit: 'seconds' },
|
|
4
|
+
{ amount: 60, unit: 'minutes' },
|
|
5
|
+
{ amount: 24, unit: 'hours' },
|
|
6
|
+
{ amount: 7, unit: 'days' },
|
|
7
|
+
{ amount: 4.34524, unit: 'weeks' },
|
|
8
|
+
{ amount: 12, unit: 'months' },
|
|
9
|
+
{ amount: Number.POSITIVE_INFINITY, unit: 'years' },
|
|
10
|
+
];
|
|
11
|
+
/**
|
|
12
|
+
* Formats the distance between `date` and `now` as a relative phrase
|
|
13
|
+
* (e.g. "5 minutes ago", "in 2 hours") using the native Intl.RelativeTimeFormat.
|
|
14
|
+
* Past dates are negative, future dates are positive.
|
|
15
|
+
*/
|
|
16
|
+
export const formatTimeAgo = (date, now = new Date(), locale) => {
|
|
17
|
+
const formatter = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' });
|
|
18
|
+
// Seconds between the two dates; negative for the past.
|
|
19
|
+
let duration = (date.getTime() - now.getTime()) / 1000;
|
|
20
|
+
for (const division of DIVISIONS) {
|
|
21
|
+
if (Math.abs(duration) < division.amount) {
|
|
22
|
+
return formatter.format(Math.round(duration), division.unit);
|
|
23
|
+
}
|
|
24
|
+
duration /= division.amount;
|
|
25
|
+
}
|
|
26
|
+
return formatter.format(Math.round(duration), 'years');
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* How long until the relative phrase could next change, so callers can
|
|
30
|
+
* re-render no more often than necessary.
|
|
31
|
+
*/
|
|
32
|
+
export const getTimeAgoUpdateIntervalMs = (date, now = new Date()) => {
|
|
33
|
+
const seconds = Math.abs((date.getTime() - now.getTime()) / 1000);
|
|
34
|
+
if (seconds < 60) {
|
|
35
|
+
return 1000; // seconds tick over
|
|
36
|
+
}
|
|
37
|
+
if (seconds < 60 * 60) {
|
|
38
|
+
return 60 * 1000; // minutes tick over
|
|
39
|
+
}
|
|
40
|
+
if (seconds < 60 * 60 * 24) {
|
|
41
|
+
return 60 * 60 * 1000; // hours tick over
|
|
42
|
+
}
|
|
43
|
+
return 60 * 60 * 24 * 1000; // days or coarser
|
|
44
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './formatTimeAgo';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './formatTimeAgo';
|
|
@@ -6,5 +6,6 @@ interface QpqContextProviderProps<T> {
|
|
|
6
6
|
children: ReactNode;
|
|
7
7
|
}
|
|
8
8
|
export declare const useQpqContextValues: () => QpqContext<any>;
|
|
9
|
-
export declare const
|
|
9
|
+
export declare const useQpqContextValue: <T>(contextIdentifier: QpqContextIdentifier<T>) => T;
|
|
10
|
+
export declare const QpqContextProvider: <T>({ contextIdentifier, value, children }: QpqContextProviderProps<T>) => import("react").JSX.Element;
|
|
10
11
|
export {};
|
|
@@ -5,6 +5,10 @@ const qpqReactContext = createContext({});
|
|
|
5
5
|
export const useQpqContextValues = () => {
|
|
6
6
|
return useContext(qpqReactContext);
|
|
7
7
|
};
|
|
8
|
+
export const useQpqContextValue = (contextIdentifier) => {
|
|
9
|
+
const context = useQpqContextValues();
|
|
10
|
+
return contextIdentifier.uniqueName in context ? context[contextIdentifier.uniqueName] : contextIdentifier.defaultValue;
|
|
11
|
+
};
|
|
8
12
|
export const QpqContextProvider = ({ contextIdentifier, value, children }) => {
|
|
9
13
|
// Get the parent context value
|
|
10
14
|
const parentContext = useContext(qpqReactContext);
|
|
@@ -7,8 +7,8 @@ type QpqRuntimeEffectCatcher = {
|
|
|
7
7
|
};
|
|
8
8
|
export declare const QpqRuntimeEffectCatcherComponent: <TState, TAction, TApi extends QpqApi>({ children, runtime, name, }: QpqRuntimeEffectCatcher & {
|
|
9
9
|
children: ReactNode;
|
|
10
|
-
}) => import("react
|
|
10
|
+
}) => import("react").JSX.Element;
|
|
11
11
|
export declare const QpqRuntimeEffectCatcher: import("react").MemoExoticComponent<(<TState, TAction, TApi extends QpqApi>({ children, runtime, name, }: QpqRuntimeEffectCatcher & {
|
|
12
12
|
children: ReactNode;
|
|
13
|
-
}) => import("react
|
|
13
|
+
}) => import("react").JSX.Element)>;
|
|
14
14
|
export {};
|
package/lib/esm/hooks/index.d.ts
CHANGED
package/lib/esm/hooks/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useComputed<TState extends object, T>(state: TState, selector: (state: TState) => T): T;
|
package/lib/esm/index.d.ts
CHANGED
package/lib/esm/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const WebSocketAuthContext: import("react").Context<boolean>;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { memo } from 'react';
|
|
3
3
|
import { useWebsocketAuthSync, useWebsocketPingPong } from './hooks';
|
|
4
|
+
import { WebSocketAuthContext } from './WebSocketAuthContext';
|
|
4
5
|
const component = ({ children, accessToken }) => {
|
|
5
|
-
useWebsocketAuthSync(accessToken);
|
|
6
|
+
const isAuthenticated = useWebsocketAuthSync(accessToken);
|
|
6
7
|
// Keep the active websocket alive
|
|
7
8
|
useWebsocketPingPong();
|
|
8
|
-
|
|
9
|
-
return _jsx(_Fragment, { children: children });
|
|
9
|
+
return (_jsx(WebSocketAuthContext.Provider, { value: isAuthenticated, children: children }));
|
|
10
10
|
};
|
|
11
11
|
export const WebSocketAuthSync = memo(component);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useWebSocketIsAuthenticated: () => boolean;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { AuthenticationInfo } from 'quidproquo-core';
|
|
2
|
-
export declare const useWebsocketAuthSync: (accessToken: AuthenticationInfo["accessToken"]) =>
|
|
2
|
+
export declare const useWebsocketAuthSync: (accessToken: AuthenticationInfo["accessToken"]) => boolean;
|
|
@@ -1,33 +1,36 @@
|
|
|
1
1
|
import { WebsocketServiceEvent } from 'quidproquo-web';
|
|
2
|
-
import { WebSocketQueueClientMessageEventType, } from 'quidproquo-webserver';
|
|
3
|
-
import { useEffect } from 'react';
|
|
2
|
+
import { WebSocketQueueClientMessageEventType, WebSocketQueueServerMessageEventType, } from 'quidproquo-webserver';
|
|
3
|
+
import { useEffect, useState } from 'react';
|
|
4
4
|
import { useFastCallback } from '../../../hooks';
|
|
5
|
-
import { useSubscribeToWebsocket, useWebsocketApi, useWebsocketSendEvent } from '../../hooks';
|
|
5
|
+
import { useSubscribeToWebsocket, useSubscribeToWebSocketEvent, useWebsocketApi, useWebsocketSendEvent } from '../../hooks';
|
|
6
6
|
export const useWebsocketAuthSync = (accessToken) => {
|
|
7
7
|
const sendMessage = useWebsocketSendEvent();
|
|
8
8
|
const websocketApi = useWebsocketApi();
|
|
9
|
+
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
9
10
|
const updateAuthTokens = useFastCallback(() => {
|
|
10
11
|
if (!websocketApi?.isConnected()) {
|
|
11
12
|
return;
|
|
12
13
|
}
|
|
13
14
|
if (accessToken) {
|
|
14
|
-
|
|
15
|
+
sendMessage({
|
|
15
16
|
type: WebSocketQueueClientMessageEventType.Authenticate,
|
|
16
17
|
payload: {
|
|
17
18
|
accessToken: accessToken,
|
|
18
19
|
},
|
|
19
|
-
};
|
|
20
|
-
sendMessage(authMessage);
|
|
20
|
+
});
|
|
21
21
|
}
|
|
22
|
-
else {
|
|
23
|
-
|
|
22
|
+
else if (isAuthenticated) {
|
|
23
|
+
sendMessage({
|
|
24
24
|
type: WebSocketQueueClientMessageEventType.Unauthenticate,
|
|
25
|
-
};
|
|
26
|
-
sendMessage(authMessage);
|
|
25
|
+
});
|
|
27
26
|
}
|
|
28
27
|
});
|
|
28
|
+
useSubscribeToWebSocketEvent(WebSocketQueueServerMessageEventType.Authenticated, () => setIsAuthenticated(true));
|
|
29
|
+
useSubscribeToWebSocketEvent(WebSocketQueueServerMessageEventType.Unauthenticated, () => setIsAuthenticated(false));
|
|
29
30
|
// Sync the tokens in on open
|
|
30
31
|
useSubscribeToWebsocket(WebsocketServiceEvent.OPEN, updateAuthTokens);
|
|
32
|
+
useSubscribeToWebsocket(WebsocketServiceEvent.CLOSE, () => setIsAuthenticated(false));
|
|
31
33
|
// Sync the tokens when they change
|
|
32
|
-
useEffect(updateAuthTokens, [accessToken, websocketApi]);
|
|
34
|
+
useEffect(updateAuthTokens, [accessToken, websocketApi, isAuthenticated]);
|
|
35
|
+
return isAuthenticated;
|
|
33
36
|
};
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
|
+
import { useFastCallback } from '../../hooks';
|
|
2
3
|
import { useWebsocketApi } from './useWebsocketApi';
|
|
3
4
|
export const useSubscribeToWebSocketEvent = (subscriptionType, callback) => {
|
|
5
|
+
const stableCallback = useFastCallback(callback);
|
|
4
6
|
const websocketService = useWebsocketApi();
|
|
5
7
|
useEffect(() => {
|
|
6
|
-
const subscriptionHandle = websocketService?.subscribeToEvent(subscriptionType,
|
|
8
|
+
const subscriptionHandle = websocketService?.subscribeToEvent(subscriptionType, stableCallback);
|
|
7
9
|
return () => {
|
|
8
10
|
if (subscriptionHandle) {
|
|
9
11
|
websocketService?.unsubscribe(subscriptionHandle);
|
|
10
12
|
}
|
|
11
13
|
};
|
|
12
|
-
}, [websocketService, subscriptionType,
|
|
14
|
+
}, [websocketService, subscriptionType, stableCallback]);
|
|
13
15
|
};
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
|
+
import { useFastCallback } from '../../hooks';
|
|
2
3
|
import { useWebsocketApi } from './useWebsocketApi';
|
|
3
4
|
export const useSubscribeToWebsocket = (subscriptionType, callback) => {
|
|
5
|
+
const stableCallback = useFastCallback(callback);
|
|
4
6
|
const websocketService = useWebsocketApi();
|
|
5
7
|
useEffect(() => {
|
|
6
|
-
const handle = websocketService?.subscribe(subscriptionType,
|
|
8
|
+
const handle = websocketService?.subscribe(subscriptionType, stableCallback);
|
|
7
9
|
return () => {
|
|
8
10
|
if (handle) {
|
|
9
11
|
websocketService?.unsubscribe(handle);
|
|
10
12
|
}
|
|
11
13
|
};
|
|
12
|
-
}, [websocketService, subscriptionType,
|
|
14
|
+
}, [websocketService, subscriptionType, stableCallback]);
|
|
13
15
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "quidproquo-web-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./lib/commonjs/index.js",
|
|
6
6
|
"module": "./lib/esm/index.js",
|
|
@@ -32,8 +32,10 @@
|
|
|
32
32
|
},
|
|
33
33
|
"homepage": "https://github.com/joe-coady/quidproquo#readme",
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"
|
|
36
|
-
"
|
|
35
|
+
"@testing-library/dom": "^10.4.1",
|
|
36
|
+
"@testing-library/react": "^16.3.2",
|
|
37
|
+
"quidproquo-core": "0.1.1",
|
|
38
|
+
"quidproquo-tsconfig": "0.1.1",
|
|
37
39
|
"typescript": "^5.8.2"
|
|
38
40
|
},
|
|
39
41
|
"peerDependencies": {
|
|
@@ -41,10 +43,10 @@
|
|
|
41
43
|
},
|
|
42
44
|
"dependencies": {
|
|
43
45
|
"jotai": "^2.12.1",
|
|
44
|
-
"
|
|
45
|
-
"quidproquo-web": "0.
|
|
46
|
-
"quidproquo-
|
|
47
|
-
"quidproquo-
|
|
48
|
-
"
|
|
46
|
+
"quidproquo-actionprocessor-node": "0.1.1",
|
|
47
|
+
"quidproquo-actionprocessor-web": "0.1.1",
|
|
48
|
+
"quidproquo-web": "0.1.1",
|
|
49
|
+
"quidproquo-webserver": "0.1.1",
|
|
50
|
+
"symbol-observable": "^4.0.0"
|
|
49
51
|
}
|
|
50
52
|
}
|