Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Instagraph added changes to open the side bar view #478

Merged
merged 16 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"react-toastify": "^8.2.0",
"react-twitter-embed": "^4.0.4",
"react-twitter-widgets": "^1.11.0",
"reactflow": "^11.9.2",
"recharts": "^2.4.3",
"socket.io-client": "^4.6.1",
"sphinx-bridge-kevkevinpal": "0.2.58",
Expand Down Expand Up @@ -82,7 +83,7 @@
"prettier": "npx prettier --config prettier.config.js --check ./src",
"postinstall": "husky install",
"typecheck": "tsc --noEmit",
"lint": "eslint src --max-warnings 30"
"lint": "eslint src --max-warnings 22"
},
"eslintConfig": {
"extends": [
Expand All @@ -97,7 +98,7 @@
],
"coverageThreshold": {
"global": {
"lines": 28
"lines": 20
}
},
"moduleNameMapper": {
Expand Down Expand Up @@ -191,6 +192,6 @@
]
},
"coverage": {
"min": 35.1
"min": 30
}
}
8 changes: 6 additions & 2 deletions src/components/App/Helper/AskQuestion/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
})
}
} catch (error) {
console.error(error)

Check warning on line 112 in src/components/App/Helper/AskQuestion/index.tsx

View workflow job for this annotation

GitHub Actions / eslint-run

Unexpected console statement
}
}

Expand All @@ -133,7 +133,7 @@
}

return (
<>
<AskQuestionContainer>
<Flex>
<Flex>
<FormControl component="div">
Expand Down Expand Up @@ -190,10 +190,14 @@
<MdSend color={canSubmit ? colors.white : colors.gray300} size="18" />
</IconWrapper>
</TextAreaWrapper>
</>
</AskQuestionContainer>
)
}

const AskQuestionContainer = styled.div`
margin-top: 20px;
`

const StyledTextarea = styled(TextareaAutosize)`
background: ${colors.inputBg1};
max-width: 100%;
Expand Down
128 changes: 88 additions & 40 deletions src/components/App/Helper/TeachMe/index.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import { styled } from '@mui/material'
import { useCallback, useEffect, useRef } from 'react'
import styled from 'styled-components'
import { PropagateLoader } from 'react-spinners'
import { toast } from 'react-toastify'
import { Socket } from 'socket.io-client'
import * as sphinx from 'sphinx-bridge-kevkevinpal'
import { Button } from '~/components/Button'
import { Button } from '@mui/material'
import { Flex } from '~/components/common/Flex'
import { Text } from '~/components/common/Text'
import { ToastMessage } from '~/components/common/Toast/toastMessage'
import useSocket from '~/hooks/useSockets'
import { postTeachMe } from '~/network/fetchGraphData'
import { postTeachMe, postInstagraph } from '~/network/fetchGraphData'
import { useAppStore } from '~/stores/useAppStore'
import { useDataStore } from '~/stores/useDataStore'
import { useTeachStore } from '~/stores/useTeachStore'
import { useTeachStore, InstagraphResponse } from '~/stores/useTeachStore'
import { colors } from '~/utils/colors'
import { AskQuestion } from '../AskQuestion'
import ReactFlow from 'reactflow'

import 'reactflow/dist/style.css'

type ResponseType = {
tutorial: string
}

export const TeachMe = () => {
const data = useDataStore((s) => s.data)
const searchTerm = useAppStore((s) => s.currentSearch)
const [data, setTeachMe] = useDataStore((s) => [s.data, s.setTeachMe])
const [searchTerm, setSideBarOpen] = useAppStore((s) => [s.currentSearch, s.setSidebarOpen])
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can move setTeachMe into the teachMe Store


const isSocketSet: { current: boolean } = useRef<boolean>(false)
const socket: Socket | null = useSocket()

const [teachMeAnswer, setTeachMeAnswer, hasTeachingInProgress, setHasTeachingInProgress] = useTeachStore((s) => [
s.teachMeAnswer,
s.setTeachMeAnswer,
s.hasTeachingInProgress,
s.setHasTeachingInProgress,
])
const [setTeachMeAnswer, setHasTeachingInProgress, setInstagraphAnswer, setHasInstagraphInProgress] = useTeachStore(
(s) => [s.setTeachMeAnswer, s.setHasTeachingInProgress, s.setInstagraphAnswer, s.setHasInstagraphInProgress],
)

const handleTeachMe = useCallback(
(response: ResponseType) => {
Expand All @@ -46,6 +46,18 @@ export const TeachMe = () => {
[setTeachMeAnswer],
)

const handleInstagraph = useCallback(
(response: InstagraphResponse) => {
setInstagraphAnswer(response)

toast(<ToastMessage message="Instagraph is ready" />, {
position: toast.POSITION.BOTTOM_CENTER,
type: 'success',
})
},
[setInstagraphAnswer],
)

useEffect(() => {
if (isSocketSet.current) {
return
Expand All @@ -58,11 +70,22 @@ export const TeachMe = () => {
isSocketSet.current = true
}
}
}, [socket, handleTeachMe])

if (handleInstagraph) {
if (socket) {
socket.on('instagraphhook', handleInstagraph)

isSocketSet.current = true
}
}
}, [socket, handleTeachMe, handleInstagraph])

const handleTutorialStart = async () => {
if (searchTerm) {
setHasTeachingInProgress(true)
setHasInstagraphInProgress(true)
setSideBarOpen(true)
setTeachMe(true)

try {
const nodesWithTranscript = data?.nodes.filter((i) => i.text)
Expand All @@ -84,45 +107,70 @@ export const TeachMe = () => {
toast(<ToastMessage message="We started preparing tutorial for you" />, {
type: 'success',
})

await postInstagraph({
term: searchTerm,
transcripts,
})

toast(<ToastMessage message="We started preparing an instagraph for you" />, {
type: 'success',
})
} catch (error: unknown) {
setHasTeachingInProgress(false)
setHasInstagraphInProgress(false)
}
}
}

return <ButtonStyled onClick={() => handleTutorialStart()}>Teach me</ButtonStyled>
}

const TeachMeLoader = ({ text }: { text: string }) => (
<Flex align="center" justify="center" py={12}>
<Flex align="center" py={12}>
<PropagateLoader color={colors.white} />
</Flex>
<Flex align="center" py={12}>
<Text>{text}</Text>
</Flex>
</Flex>
)

export const TeachMeText = () => {
const [teachMeAnswer, hasTeachingInProgress, instgraphAnswser, hasInstagraphInProgress] = useTeachStore((s) => [
s.teachMeAnswer,
s.hasTeachingInProgress,
s.instgraphAnswser,
s.hasInstagraphInProgress,
])

const showInstagraph: boolean = !hasInstagraphInProgress && !!instgraphAnswser?.edges && !!instgraphAnswser?.nodes

return (
<Container>
<>
{showInstagraph ? (
<ReactFlow edges={instgraphAnswser?.edges} nodes={instgraphAnswser?.nodes} />
) : (
<TeachMeLoader text="Generating instagraph" />
)}
{!hasTeachingInProgress ? (
<>
{!teachMeAnswer ? (
<Flex py={8}>
<Button kind="big" onClick={() => handleTutorialStart()}>
Teach me
</Button>
</Flex>
) : (
<>
<Flex>
<Text>{teachMeAnswer}</Text>
</Flex>
<AskQuestion />
</>
)}
<TeachMeAnswerFlex>
<Text>{teachMeAnswer}</Text>
</TeachMeAnswerFlex>
<AskQuestion />
</>
) : (
<Flex align="center" justify="center" py={12}>
<Flex align="center" py={12}>
<PropagateLoader color={colors.white} />
</Flex>
<Flex align="center" py={12}>
<Text>Generating tutorial</Text>
</Flex>
</Flex>
<TeachMeLoader text="Generating tutorial" />
)}
</Container>
</>
)
}

const Container = styled('div')(() => ({
marginTop: 'auto',
}))
const ButtonStyled = styled(Button)``

const TeachMeAnswerFlex = styled(Flex)`
padding-left: 10px;
padding-right: 10px;
`
8 changes: 7 additions & 1 deletion src/components/App/SideBar/SelectedNodeView/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { memo } from 'react'
import { useSelectedNode } from '~/stores/useDataStore'
import { useSelectedNode, useDataStore } from '~/stores/useDataStore'
import { TextType } from '../../Helper/AskQuestion/Text'
import { AudioClip } from '../AudioClip'
import { Creator } from '../Creator'
Expand All @@ -11,10 +11,16 @@ import { Topic } from '../Topic'
import { TwitData } from '../TwitData'
import { Twitter } from '../Twitter'
import { YouTube } from '../YouTube'
import { TeachMeText } from '../../Helper/TeachMe'

// eslint-disable-next-line no-underscore-dangle
const _View = () => {
const selectedNode = useSelectedNode()
const [showTeachMe] = useDataStore((s) => [s.showTeachMe])
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again I can put this in the teachMe store


if (showTeachMe) {
return <TeachMeText />
}

switch (selectedNode?.node_type) {
case 'twitter':
Expand Down
50 changes: 28 additions & 22 deletions src/components/App/SideBar/SidebarSubView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,43 @@ import styled from 'styled-components'
import ChevronLeftIcon from '~/components/Icons/ChevronLeftIcon'
import CloseIcon from '~/components/Icons/CloseIcon'
import { Flex } from '~/components/common/Flex'
import { useAppStore } from '~/stores/useAppStore'
import { useDataStore } from '~/stores/useDataStore'
import { colors } from '~/utils/colors'
import { SelectedNodeView } from '../SelectedNodeView'

type Props = { open: boolean }

export const SideBarSubView = ({ open }: Props) => {
const [setSidebarOpen] = useAppStore((s) => [s.setSidebarOpen])
const setSelectedNode = useDataStore((s) => s.setSelectedNode)
const [setSelectedNode, setTeachMe, showTeachMe] = useDataStore((s) => [
s.setSelectedNode,
s.setTeachMe,
s.showTeachMe,
])

return (
<>
<Slide direction="right" in={open} mountOnEnter unmountOnExit>
<Wrapper>
<ScrollWrapper>
<SelectedNodeView />
</ScrollWrapper>
<CloseButton onClick={() => setSelectedNode(null)}>
<CloseIcon />
</CloseButton>
<CollapseButton
onClick={() => {
setSidebarOpen(false)
}}
>
<ChevronLeftIcon />
</CollapseButton>
</Wrapper>
</Slide>
</>
<Slide direction="right" in={open} mountOnEnter style={{ width: showTeachMe ? '700px' : '' }} unmountOnExit>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might want to consider mobile view since this is 700px which might not translate well to a mobile device

<Wrapper>
<ScrollWrapper>
<SelectedNodeView />
</ScrollWrapper>
<CloseButton
onClick={() => {
setSelectedNode(null)
setTeachMe(false)
}}
>
<CloseIcon />
</CloseButton>
<CollapseButton
onClick={() => {
setSelectedNode(null)
setTeachMe(false)
}}
>
<ChevronLeftIcon />
</CollapseButton>
</Wrapper>
</Slide>
)
}

Expand Down
Loading
Loading