Skip to content

Commit

Permalink
fix(cypress): test
Browse files Browse the repository at this point in the history
  • Loading branch information
Shoaibdev7 committed Oct 28, 2024
1 parent 0dbc9c7 commit 9a7e6c3
Showing 1 changed file with 152 additions and 152 deletions.
304 changes: 152 additions & 152 deletions src/components/Auth/index.tsx
Original file line number Diff line number Diff line change
@@ -1,152 +1,152 @@
import { PropsWithChildren, useCallback, useEffect, useState } from 'react'
import * as sphinx from 'sphinx-bridge'
import styled from 'styled-components'
import { Flex } from '~/components/common/Flex'
import { Text } from '~/components/common/Text'
import { isDevelopment, isE2E } from '~/constants'
import { getIsAdmin } from '~/network/auth'
import { useDataStore } from '~/stores/useDataStore'
import { useFeatureFlagStore } from '~/stores/useFeatureFlagStore'
import { useUserStore } from '~/stores/useUserStore'
import { sphinxBridge } from '~/testSphinxBridge'
import { updateBudget } from '~/utils'
import { isAndroid, isWebView } from '~/utils/isWebView'
import { Splash } from '../App/Splash'

export const AuthGuard = ({ children }: PropsWithChildren) => {
const [unAuthorized, setUnauthorized] = useState(false)
const { setBudget, setIsAdmin, setPubKey, setIsAuthenticated, setSwarmUiUrl } = useUserStore((s) => s)
const { splashDataLoading } = useDataStore((s) => s)
const [renderMainPage, setRenderMainPage] = useState(false)

const {
setTrendingTopicsFeatureFlag,
setQueuedSourcesFeatureFlag,
setCustomSchemaFeatureFlag,
setRealtimeGraphFeatureFlag,
setChatInterfaceFeatureFlag,
setFastFiltersFeatureFlag,
} = useFeatureFlagStore((s) => s)

const handleAuth = useCallback(async () => {
localStorage.removeItem('admin')
localStorage.removeItem('signature')

let sphinxEnable

try {
if (!isE2E) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
sphinxEnable = await sphinx.enable()
} else {
sphinxEnable = await sphinxBridge.enable()
}

sessionStorage.setItem('isSphinx', sphinxEnable ? 'true' : 'false')

setPubKey(sphinxEnable?.pubkey)
} catch (error) {
setPubKey('')
}

if (isE2E || isDevelopment) {
setIsAuthenticated(true)
}
}, [setPubKey, setIsAuthenticated])

const handleIsAdmin = useCallback(async () => {
try {
const res = await getIsAdmin()

if (res.data) {
const isAdmin = !!res.data.isAdmin

localStorage.setItem('admin', JSON.stringify({ isAdmin }))

if (isAdmin && res.data.swarmUiUrl) {
setSwarmUiUrl(res.data.swarmUiUrl)
}

setIsAdmin(isAdmin)
setTrendingTopicsFeatureFlag(res.data.trendingTopics)
setQueuedSourcesFeatureFlag(res.data.queuedSources)
setCustomSchemaFeatureFlag(res.data.customSchema)
setRealtimeGraphFeatureFlag(res.data.realtimeGraph || false)
setChatInterfaceFeatureFlag(res.data.chatInterface || false)
setFastFiltersFeatureFlag(res.data.fastFilters || false)
}

setIsAuthenticated(true)
setRenderMainPage(true)
} catch (error) {
/* not an admin */
setUnauthorized(true)
}
}, [
setIsAuthenticated,
setIsAdmin,
setTrendingTopicsFeatureFlag,
setQueuedSourcesFeatureFlag,
setCustomSchemaFeatureFlag,
setRealtimeGraphFeatureFlag,
setChatInterfaceFeatureFlag,
setFastFiltersFeatureFlag,
setSwarmUiUrl,
])

// auth checker
useEffect(() => {
const init = async () => {
if (isWebView() || isE2E || isAndroid()) {
try {
if (isAndroid()) {
// eslint-disable-next-line no-promise-executor-return
await new Promise((r) => setTimeout(r, 5000))
}

await handleAuth()
} catch (error) {
console.log(error)
}
}

await updateBudget(setBudget)

await handleIsAdmin()
}

init()
}, [handleAuth, handleIsAdmin, setBudget])

const message = 'This is a private Graph, Contact Admin'

if (unAuthorized) {
return (
<StyledFlex>
<StyledText>{message}</StyledText>
</StyledFlex>
)
}

return (
<>
{splashDataLoading && <Splash />}
{renderMainPage && children}
</>
)
}

const StyledText = styled(Text)`
font-size: 5rem;
font-weight: 600;
font-family: 'Barlow';
text-align: center;
`

const StyledFlex = styled(Flex)`
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
`
import { PropsWithChildren, useCallback, useEffect, useState } from 'react'
import * as sphinx from 'sphinx-bridge'
import styled from 'styled-components'
import { Flex } from '~/components/common/Flex'
import { Text } from '~/components/common/Text'
import { isDevelopment, isE2E } from '~/constants'
import { getIsAdmin } from '~/network/auth'
import { useDataStore } from '~/stores/useDataStore'
import { useFeatureFlagStore } from '~/stores/useFeatureFlagStore'
import { useUserStore } from '~/stores/useUserStore'
import { sphinxBridge } from '~/testSphinxBridge'
import { updateBudget } from '~/utils'
import { isAndroid, isWebView } from '~/utils/isWebView'
import { Splash } from '../App/Splash'

export const AuthGuard = ({ children }: PropsWithChildren) => {
const [unAuthorized, setUnauthorized] = useState(false)
const { setBudget, setIsAdmin, setPubKey, setIsAuthenticated, setSwarmUiUrl } = useUserStore((s) => s)
const { splashDataLoading } = useDataStore((s) => s)
const [renderMainPage, setRenderMainPage] = useState(false)

const {
setTrendingTopicsFeatureFlag,
setQueuedSourcesFeatureFlag,
setCustomSchemaFeatureFlag,
setRealtimeGraphFeatureFlag,
setChatInterfaceFeatureFlag,
setFastFiltersFeatureFlag,
} = useFeatureFlagStore((s) => s)

const handleAuth = useCallback(async () => {
localStorage.removeItem('admin')
localStorage.removeItem('signature')

let sphinxEnable

try {
if (!isE2E) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
sphinxEnable = await sphinx.enable()
} else {
sphinxEnable = await sphinxBridge.enable()
}

sessionStorage.setItem('isSphinx', sphinxEnable ? 'true' : 'false')

setPubKey(sphinxEnable?.pubkey)
} catch (error) {
setPubKey('')
}

if (isE2E || isDevelopment) {
setIsAuthenticated(true)
}
}, [setPubKey, setIsAuthenticated])

const handleIsAdmin = useCallback(async () => {
try {
const res = await getIsAdmin()

if (res.data) {
const isAdmin = !!res.data.isAdmin

localStorage.setItem('admin', JSON.stringify({ isAdmin }))

if (isAdmin && res.data.swarmUiUrl) {
setSwarmUiUrl(res.data.swarmUiUrl)
}

setIsAdmin(isAdmin)
setTrendingTopicsFeatureFlag(res.data.trendingTopics)
setQueuedSourcesFeatureFlag(res.data.queuedSources)
setCustomSchemaFeatureFlag(res.data.customSchema)
setRealtimeGraphFeatureFlag(res.data.realtimeGraph || false)
setChatInterfaceFeatureFlag(res.data.chatInterface || true)
setFastFiltersFeatureFlag(res.data.fastFilters || false)
}

setIsAuthenticated(true)
setRenderMainPage(true)
} catch (error) {
/* not an admin */
setUnauthorized(true)
}
}, [
setIsAuthenticated,
setIsAdmin,
setTrendingTopicsFeatureFlag,
setQueuedSourcesFeatureFlag,
setCustomSchemaFeatureFlag,
setRealtimeGraphFeatureFlag,
setChatInterfaceFeatureFlag,
setFastFiltersFeatureFlag,
setSwarmUiUrl,
])

// auth checker
useEffect(() => {
const init = async () => {
if (isWebView() || isE2E || isAndroid()) {
try {
if (isAndroid()) {
// eslint-disable-next-line no-promise-executor-return
await new Promise((r) => setTimeout(r, 5000))
}

await handleAuth()
} catch (error) {
console.log(error)

Check warning on line 110 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / eslint-run

Unexpected console statement

Check warning on line 110 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement

Check warning on line 110 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addSource.cy.ts)

Unexpected console statement

Check warning on line 110 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/sourcesTable/sourcesTable.cy.ts)

Unexpected console statement

Check warning on line 110 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/curationTable/curation.cy.ts)

Unexpected console statement

Check warning on line 110 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/admin/topics.cy.ts)

Unexpected console statement

Check warning on line 110 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/createGeneratedEdges/createGeneratedEdges.cy.ts)

Unexpected console statement

Check warning on line 110 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/checkEnv.cy.ts)

Unexpected console statement

Check warning on line 110 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/admin/signin.cy.ts)

Unexpected console statement

Check warning on line 110 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addWebpage.cy.ts)

Unexpected console statement

Check warning on line 110 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addTweet.cy.ts)

Unexpected console statement

Check warning on line 110 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addYoutube.cy.ts)

Unexpected console statement

Check warning on line 110 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/trendingTopics/trendingTopics.cy.ts)

Unexpected console statement

Check warning on line 110 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/seeLatest/latest.cy.ts)

Unexpected console statement

Check warning on line 110 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addNode/addNodeType.cy.ts)

Unexpected console statement
}
}

await updateBudget(setBudget)

await handleIsAdmin()
}

init()
}, [handleAuth, handleIsAdmin, setBudget])

const message = 'This is a private Graph, Contact Admin'

if (unAuthorized) {
return (
<StyledFlex>
<StyledText>{message}</StyledText>
</StyledFlex>
)
}

return (
<>
{splashDataLoading && <Splash />}
{renderMainPage && children}
</>
)
}

const StyledText = styled(Text)`
font-size: 5rem;
font-weight: 600;
font-family: 'Barlow';
text-align: center;
`

const StyledFlex = styled(Flex)`
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
`

0 comments on commit 9a7e6c3

Please sign in to comment.