Skip to content

Commit

Permalink
Merge branch 'master' into instagraph
Browse files Browse the repository at this point in the history
  • Loading branch information
kevkevinpal authored Oct 10, 2023
2 parents 5a712bd + c19f6d6 commit 440e091
Show file tree
Hide file tree
Showing 9 changed files with 435 additions and 339 deletions.
430 changes: 215 additions & 215 deletions build/assets/index-f81f55db.js → build/assets/index-3f187897.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>Second Brain</title>
<script type="module" crossorigin src="/assets/index-f81f55db.js"></script>
<script type="module" crossorigin src="/assets/index-3f187897.js"></script>
<link rel="stylesheet" href="/assets/index-516c5c0e.css">
</head>
<body style="background: #000">
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sphinx-nav",
"version": "0.1.77",
"version": "0.1.78",
"private": true,
"dependencies": {
"@emotion/react": "^11.11.1",
Expand Down
45 changes: 26 additions & 19 deletions src/components/AddNodeModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Stack } from '@mui/material'
import { clsx } from 'clsx'
import { Lsat } from 'lsat-js'
import { ReactElement, useState } from 'react'
import { FieldValues, FormProvider, useForm } from 'react-hook-form'
import { MdCheckCircle, MdClose, MdInfo, MdKeyboardBackspace, MdWarning } from 'react-icons/md'
Expand All @@ -8,9 +9,9 @@ import { toast } from 'react-toastify'
import * as sphinx from 'sphinx-bridge-kevkevinpal'
import styled from 'styled-components'
import { Button } from '~/components/Button'
import { BaseModal } from '~/components/Modal'
import { Flex } from '~/components/common/Flex'
import { Text } from '~/components/common/Text'
import { BaseModal } from '~/components/Modal'
import {
DOCUMENT,
GITHUB_REPOSITORY,
Expand All @@ -30,11 +31,12 @@ import { useModal } from '~/stores/useModalStore'
import { FetchRadarResponse, SubmitErrRes } from '~/types'
import { colors } from '~/utils/colors'
import { getLSat } from '~/utils/getLSat'
import { payLsat } from '~/utils/payLsat'
import { executeIfProd } from '~/utils/tests'
import { timeToMilliseconds } from '~/utils/timeToMilliseconds'
import { useDataStore } from '../../stores/useDataStore/index'
import StyledSelect from '../Select'
import { ToastMessage } from '../common/Toast/toastMessage'
import StyledSelect from '../Select'
import { Document } from './Document'
import { GithubRepository } from './GithubRepository'
import { RSSFeed } from './RSSFeed'
Expand Down Expand Up @@ -76,7 +78,12 @@ const notify = (message: string) => {
})
}

const handleSubmit = async (data: FieldValues, close: () => void, sourceType: string, successCallback: () => void) => {
const handleSubmit = async (
data: FieldValues,
close: () => void,
sourceType: string,
successCallback: () => void,
): Promise<void> => {
const body: { [index: string]: unknown } = {}

if (sourceType === LINK) {
Expand Down Expand Up @@ -145,7 +152,7 @@ const handleSubmit = async (data: FieldValues, close: () => void, sourceType: st
}
}

let lsatToken: string | null = null
let lsatToken = ''

// skipping this for end to end test because it requires a sphinx-relay to be connected
await executeIfProd(async () => {
Expand All @@ -155,25 +162,15 @@ const handleSubmit = async (data: FieldValues, close: () => void, sourceType: st

body.pubkey = enable?.pubkey

lsatToken = await getLSat('adding_node')

if (!lsatToken) {
throw new Error('An error occured calling getLSat')
}
lsatToken = await getLSat()
})

const endPoint = CONTENT_TYPES.includes(sourceType) ? 'add_node' : 'radar'

try {
const res: SubmitErrRes = await api.post(
`/${endPoint}`,
JSON.stringify(body),
(lsatToken
? {
Authorization: lsatToken,
}
: {}) as HeadersInit,
)
const res: SubmitErrRes = await api.post(`/${endPoint}`, JSON.stringify(body), {
Authorization: lsatToken,
})

if (res.error) {
const { message } = res.error
Expand All @@ -187,7 +184,17 @@ const handleSubmit = async (data: FieldValues, close: () => void, sourceType: st

notify(NODE_ADD_SUCCESS)
close()
} catch (err: unknown) {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
if (err.status === 402) {
const lsat = Lsat.fromHeader(err.headers.get('www-authenticate'))

await payLsat(lsat)

await handleSubmit(data, close, sourceType, successCallback)
}

if (err instanceof Error) {
notify(NODE_ADD_ERROR)
close()
Expand Down
4 changes: 4 additions & 0 deletions src/network/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { API_URL } from '~/constants'
const request = async <Res>(url: string, config?: RequestInit): Promise<Res> => {
const response = await fetch(url, config)

if (!response.ok) {
throw response
}

return response.json()
}

Expand Down
130 changes: 93 additions & 37 deletions src/network/fetchGraphData/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Lsat } from 'lsat-js'
import { Vector3 } from 'three'

// import { getNodeColorByType } from '~/components/Universe/Graph/constant'
import {
AWS_IMAGE_BUCKET_URL,
CLOUDFRONT_IMAGE_BUCKET_URL,
NODE_RELATIVE_HIGHLIGHT_COLORS,
isDevelopment,
isE2E,
NODE_RELATIVE_HIGHLIGHT_COLORS,
} from '~/constants'
import { mock } from '~/mocks/getMockGraphData/mockResponse'
import { api } from '~/network/api'
Expand All @@ -23,6 +23,7 @@ import {
} from '~/types'
import { getLSat } from '~/utils/getLSat'
import { getMaxSuperficialWeightPerNodeType, getSuperficialNodeWeight } from '~/utils/getSuperficialNodeWeight'
import { payLsat } from '~/utils/payLsat'
import { getGraphDataPositions } from './const'

type guestMapChild = {
Expand Down Expand Up @@ -68,7 +69,7 @@ export const fetchGraphData = async (search: string) => {
}
}

const fetchNodes = async (search: string) => {
const fetchNodes = async (search: string): Promise<FetchDataResponse> => {
if (!search) {
try {
const response = await api.get<FetchDataResponse>(`/prediction/content/latest`)
Expand All @@ -87,15 +88,26 @@ const fetchNodes = async (search: string) => {
return response
}

const lsatToken = await getLSat('searching')
const lsatToken = await getLSat()

if (!lsatToken) {
throw new Error('An error occured calling getLSat')
}
try {
const response = await api.get<FetchDataResponse>(`/v2/search?word=${search}`, {
Authorization: lsatToken,
})

return api.get<FetchDataResponse>(`/v2/search?word=${search}`, {
Authorization: lsatToken,
})
return response
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (error.status === 402) {
const lsat = Lsat.fromHeader(error.headers.get('www-authenticate'))

await payLsat(lsat)

return fetchNodes(search)
}

throw error
}
}

export const getTrends = async () => {
Expand All @@ -114,7 +126,10 @@ export const getTrends = async () => {
* }
*/

export const getSentimentData = async (args?: { topic: string; cutoff_date: string }) => {
export const getSentimentData = async (args?: {
topic: string
cutoff_date: string
}): Promise<FetchSentimentResponse> => {
const search = args && new URLSearchParams(args)

const endpoint = search ? `/sentiments?${search.toString()}` : '/sentiments'
Expand All @@ -126,49 +141,90 @@ export const getSentimentData = async (args?: { topic: string; cutoff_date: stri
return response
}

const lsatToken = await getLSat('sentiments', search?.toString())
const lsatToken = await getLSat()

if (!lsatToken) {
throw new Error('An error occured calling getLSat')
}
try {
const response = await api.get<FetchSentimentResponse>(endpoint, {
Authorization: lsatToken,
})

const response = await api.get<FetchSentimentResponse>(endpoint, {
Authorization: lsatToken,
})
return response

return response
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (error.status === 402) {
const lsat = Lsat.fromHeader(error.headers.get('www-authenticate'))

export const postTeachMe = async (data: TeachData) => {
const lsatToken = await getLSat('teachme')
await payLsat(lsat)

if (!lsatToken) {
throw new Error('An error occured calling getLSat')
}
return getSentimentData(args)
}

return api.post(`/teachme`, JSON.stringify(data), { Authorization: lsatToken })
throw error
}
}

export const postInstagraph = async (data: TeachData) => {

Check failure on line 167 in src/network/fetchGraphData/index.ts

View workflow job for this annotation

GitHub Actions / eslint-run

'postInstagraph' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.

Check failure on line 167 in src/network/fetchGraphData/index.ts

View workflow job for this annotation

GitHub Actions / craco-build-run

'postInstagraph' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
console.log(data)

const lsatToken = await getLSat('instagraph')
const lsatToken = await getLSat()

if (!lsatToken) {
throw new Error('An error occured calling getLSat')
}

return api.post(`/instagraph`, JSON.stringify(data), { Authorization: lsatToken })
try {
return api.post(`/instagraph`, JSON.stringify(data), { Authorization: lsatToken })

// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (error.status === 402) {
const lsat = Lsat.fromHeader(error.headers.get('www-authenticate'))

await payLsat(lsat)

return postInstagraph(data)
}

throw error
}
}

export const postAskQuestion = async (data: QuestionData) => {
const lsatToken = await getLSat('ask_question')

if (!lsatToken) {
throw new Error('An error occured calling getLSat')
export const postTeachMe = async (data: TeachData): Promise<void> => {
const lsatToken = await getLSat()

try {
return api.post(`/teachme`, JSON.stringify(data), { Authorization: lsatToken })

// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (error.status === 402) {
const lsat = Lsat.fromHeader(error.headers.get('www-authenticate'))

await payLsat(lsat)

return postTeachMe(data)
}

throw error
}
}

export const postAskQuestion = async (data: QuestionData): Promise<void> => {
const lsatToken = await getLSat()

try {
return api.post(`/ask_question`, JSON.stringify(data), { Authorization: lsatToken })

return api.post(`/ask_question`, JSON.stringify(data), { Authorization: lsatToken })
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (error.status === 402) {
const lsat = Lsat.fromHeader(error.headers.get('www-authenticate'))

await payLsat(lsat)

return postAskQuestion(data)
}

throw error
}
}

export const getAdminId = async (tribeId: string) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getLSat/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ describe('assertNever', () => {
* @jest-environment-options {"url": "https://jestjs.io/"}
* */
it('should assert a message with the correct message', async () => {
expect(await getLSat('searching', 'utxo')).toBe(null)
expect(await getLSat()).toBe('')
})
})
Loading

0 comments on commit 440e091

Please sign in to comment.