From edc9fadfee267dd9366dc451bb8588b711ae6ea9 Mon Sep 17 00:00:00 2001 From: Jared Vu Date: Tue, 7 Nov 2023 20:28:42 -0800 Subject: [PATCH] Add TestFlags provider and class (#132) * Add TestFlags provider and class * import order nit * markets is an object and not array * Use null coalescing op --- package.json | 1 + pnpm-lock.yaml | 7 +++--- src/App.tsx | 2 ++ src/hooks/useTestFlags.tsx | 46 +++++++++++++++++++++++++++++++++++++ src/lib/abacus/websocket.ts | 22 ++++++++++++++++-- 5 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 src/hooks/useTestFlags.tsx diff --git a/package.json b/package.json index 953a8dc..d2fef95 100644 --- a/package.json +++ b/package.json @@ -96,6 +96,7 @@ "long": "^5.2.3", "luxon": "^3.3.0", "qr-code-styling": "1.6.0-rc.1", + "query-string": "^8.1.0", "react": "^18.2.0", "react-aria": "^3.25.0", "react-dom": "^18.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index de16fa6..acb4342 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -197,6 +197,9 @@ dependencies: qr-code-styling: specifier: 1.6.0-rc.1 version: 1.6.0-rc.1 + query-string: + specifier: ^8.1.0 + version: 8.1.0 react: specifier: ^18.2.0 version: 18.2.0 @@ -7875,7 +7878,6 @@ packages: /decode-uri-component@0.4.1: resolution: {integrity: sha512-+8VxcR21HhTy8nOt6jf20w0c9CADrw1O8d+VZ/YzzCt4bJ3uBjw+D1q2osAB8RnpwwaeYBxy0HyKQxD5JBMuuQ==} engines: {node: '>=14.16'} - dev: true /deep-eql@4.1.3: resolution: {integrity: sha512-WaEtAOpRA1MQ0eohqZjpGD8zdI0Ovsm8mmFhaDN8dvDZzyoUMcYDnf5Y6iu7HTXxf8JDS23qWa4a+hKCDyOPzw==} @@ -8945,7 +8947,6 @@ packages: /filter-obj@5.1.0: resolution: {integrity: sha512-qWeTREPoT7I0bifpPUXtxkZJ1XJzxWtfoWWkdVGqa+eCr3SHW/Ocp89o8vLvbUuQnadybJpjOKu4V+RwO6sGng==} engines: {node: '>=14.16'} - dev: true /find-cache-dir@3.3.2: resolution: {integrity: sha512-wXZV5emFEjrridIgED11OoUKLxiYjAcqot/NJdAkOhlJ+vGzwhOAfcG5OX1jP+S0PcjEn8bdMJv+g2jwQ3Onig==} @@ -11876,7 +11877,6 @@ packages: decode-uri-component: 0.4.1 filter-obj: 5.1.0 split-on-first: 3.0.0 - dev: true /queue-microtask@1.2.3: resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==} @@ -12701,7 +12701,6 @@ packages: /split-on-first@3.0.0: resolution: {integrity: sha512-qxQJTx2ryR0Dw0ITYyekNQWpz6f8dGd7vffGNflQQ3Iqj9NJ6qiZ7ELpZsJ/QBhIVAiDfXdag3+Gp8RvWa62AA==} engines: {node: '>=12'} - dev: true /split2@4.2.0: resolution: {integrity: sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==} diff --git a/src/App.tsx b/src/App.tsx index 0de3dda..d9cf2a1 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -23,6 +23,7 @@ import { LocalNotificationsProvider } from '@/hooks/useLocalNotifications'; import { RestrictionProvider } from '@/hooks/useRestrictions'; import { SubaccountProvider } from '@/hooks/useSubaccount'; import { SquidProvider } from '@/hooks/useSquid'; +import { TestFlagsProvider } from '@/hooks/useTestFlags'; import { GuardedMobileRoute } from '@/components/GuardedMobileRoute'; @@ -123,6 +124,7 @@ const providers = [ wrapProvider(QueryClientProvider, { client: queryClient }), wrapProvider(GrazProvider), wrapProvider(WagmiConfig, { config }), + wrapProvider(TestFlagsProvider), wrapProvider(LocaleProvider), wrapProvider(RestrictionProvider), wrapProvider(DydxProvider), diff --git a/src/hooks/useTestFlags.tsx b/src/hooks/useTestFlags.tsx new file mode 100644 index 0000000..3aedaee --- /dev/null +++ b/src/hooks/useTestFlags.tsx @@ -0,0 +1,46 @@ +import { createContext, useContext, useEffect, useState } from 'react'; +import { useLocation } from 'react-router-dom'; +import queryString from 'query-string'; + +class TestFlags { + public queryParams: queryString.ParsedQuery; + + constructor() { + this.queryParams = {}; + } + + setQueryParams(flags: queryString.ParsedQuery) { + this.queryParams = flags; + } + + get displayInitializingMarkets() { + return !!this.queryParams.displayInitializingMarkets; + } +} + +export const testFlags = new TestFlags(); + +const useTestFlagsContext = () => { + const [queryParams, setQueryParams] = useState>({}); + const location = useLocation(); + + useEffect(() => { + const parsedQueryParams = queryString.parse(location.search.substring(-1)); + testFlags.setQueryParams(parsedQueryParams); + setQueryParams(parsedQueryParams); + }, []); + + return { + ...queryParams, + }; +}; + +type TestFlagsContextType = ReturnType; +const TestFlagsContext = createContext({} as TestFlagsContextType); +TestFlagsContext.displayName = 'TestFlags'; + +export const TestFlagsProvider = ({ ...props }) => ( + +); + +export const useTestFlags = () => useContext(TestFlagsContext); diff --git a/src/lib/abacus/websocket.ts b/src/lib/abacus/websocket.ts index 7bd41de..8843c4e 100644 --- a/src/lib/abacus/websocket.ts +++ b/src/lib/abacus/websocket.ts @@ -9,11 +9,12 @@ import { PONG_MESSAGE_TYPE, } from '@/constants/websocket'; +import { lastSuccessfulWebsocketRequestByOrigin } from '@/hooks/useAnalytics'; +import { testFlags } from '@/hooks/useTestFlags'; + import { subscriptionsByChannelId } from '@/lib/tradingView/dydxfeed/cache'; import { mapCandle } from '@/lib/tradingView/utils'; -import { lastSuccessfulWebsocketRequestByOrigin } from '@/hooks/useAnalytics'; - import { log } from '../telemetry'; const RECONNECT_INTERVAL_MS = 10_000; @@ -144,6 +145,23 @@ class AbacusWebsocket implements Omit { + const status = contents.markets[market].status; + if (status === 'INITIALIZING') { + contents.markets[market].status = 'ONLINE'; + } + }); + + this.receivedCallback?.(JSON.stringify(parsedMessage)); + } + + break; + } default: { break; }