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: Update frontend title after endpoint #691

Merged
merged 8 commits into from
Dec 14, 2023
30 changes: 4 additions & 26 deletions src/components/App/AppBar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,21 @@
import { useEffect, useState } from 'react'
import styled from 'styled-components'
import { Flex } from '~/components/common/Flex'
import { Text } from '~/components/common/Text'
import { Stats } from '~/components/Stats'
import { getAboutData, TAboutParams } from '~/network/fetchSourcesData'
import { useAppStore } from '~/stores/useAppStore'
import { colors } from '~/utils/colors'
import { media } from '~/utils/media'

const defaultData = {
description: '',
mission_statement: '',
search_term: '',
title: '',
}

export const AppBar = () => {
const [initialValues, setInitialValues] = useState<TAboutParams>(defaultData)

useEffect(() => {
const init = async () => {
try {
const response = await getAboutData()

setInitialValues(response)
} catch (error) {
console.warn(error)
}
}

init()
}, [])
const appMetaData = useAppStore((s) => s.appMetaData)

return (
<Header>
<TitleWrapper>
<>
{initialValues.title && (
{appMetaData.title && (
<Text className="title" color="white">
{initialValues.title}
{appMetaData.title}
</Text>
)}
</>
Expand Down
44 changes: 5 additions & 39 deletions src/components/App/SecondarySidebar/About/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { useEffect, useState } from 'react'
import { ClipLoader } from 'react-spinners'
import styled from 'styled-components'
import { Flex } from '~/components/common/Flex'
import { Text } from '~/components/common/Text'
import { getAboutData, TAboutParams } from '~/network/fetchSourcesData'
import { useAppStore } from '~/stores/useAppStore'
import { useUserStore } from '~/stores/useUserStore'
import { colors } from '~/utils/colors'
import { AboutAdminView } from './AdminView'
Expand All @@ -16,50 +14,18 @@ export const requiredRule = {
},
}

const defaultData = {
description: '',
mission_statement: '',
search_term: '',
title: '',
}

export const About = () => {
const [isAdmin] = useUserStore((s) => [s.isAdmin])
const [loading, setLoading] = useState(false)
const [initialValues, setInitialValues] = useState<TAboutParams>(defaultData)

useEffect(() => {
const init = async () => {
setLoading(true)

try {
const response = await getAboutData()

setInitialValues(response)
} catch (error) {
console.warn(error)
} finally {
setLoading(false)
}
}

init()
}, [])
const appMetaData = useAppStore((s) => s.appMetaData)

return (
<Wrapper align="stretch" direction="column" justify="flex-end">
<Heading align="center" direction="row" justify="space-between">
<Text className="title">About</Text>
</Heading>
{loading ? (
<ContentWrapper align="center" justify="center">
<ClipLoader />
</ContentWrapper>
) : (
<ContentWrapper align="stretch" justify="flex-start">
{!isAdmin ? <CommonView initialValues={initialValues} /> : <AboutAdminView initialValues={initialValues} />}
</ContentWrapper>
)}
<ContentWrapper align="stretch" justify="flex-start">
{!isAdmin ? <CommonView initialValues={appMetaData} /> : <AboutAdminView initialValues={appMetaData} />}
</ContentWrapper>
</Wrapper>
)
}
Expand Down
9 changes: 5 additions & 4 deletions src/components/App/Splash/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import styled from 'styled-components'
import { Flex } from '~/components/common/Flex'
import { Text } from '~/components/common/Text'
import { TAboutParams, getAboutData, getStats } from '~/network/fetchSourcesData'
import { useAppStore } from '~/stores/useAppStore'
import { colors } from '~/utils'
import { SphereAnimation } from './SpiningSphere'
import { AnimatedTextContent } from './animated'
import { Message, initialAboutState, initialMessageData } from './constants'
import { Message, initialMessageData } from './constants'

type Props = {
handleLoading: React.Dispatch<React.SetStateAction<boolean>>
}

export const Splash = memo(({ handleLoading }: Props) => {
const [about, setAbout] = useState<TAboutParams>(initialAboutState)
const [message, setMessage] = useState<Message>(initialMessageData)
const [progress, setProgress] = useState(0)
const [appMetaData, setAppMetaData] = [useAppStore((s) => s.appMetaData), useAppStore((s) => s.setAppMetaData)]

useEffect(() => {
let timeoutId: NodeJS.Timeout
Expand All @@ -34,7 +35,7 @@ export const Splash = memo(({ handleLoading }: Props) => {

setProgress((prev) => prev + 15)

setAbout(aboutResponse)
setAppMetaData(aboutResponse)

setMessage(
initialMessageData.map(({ key, ...rest }) => ({
Expand Down Expand Up @@ -75,7 +76,7 @@ export const Splash = memo(({ handleLoading }: Props) => {
<SphereAnimation />
<Flex style={{ color: colors.white }}>
<TitleWrapper>
{about.title && <Text className="title">{about.title}</Text>}
{appMetaData.title && <Text className="title">{appMetaData.title}</Text>}
<Text className="subtitle">Second Brain</Text>
</TitleWrapper>
<LinearProgress color="inherit" sx={{ my: 1.75, height: '2px' }} value={progress} variant="determinate" />
Expand Down
8 changes: 7 additions & 1 deletion src/components/SettingsModal/SettingsView/General/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Flex } from '~/components/common/Flex'
import { TextInput } from '~/components/common/TextInput'
import { requiredRule } from '~/constants'
import { TAboutParams, postAboutData } from '~/network/fetchSourcesData'
import { useAppStore } from '~/stores/useAppStore'
import { colors } from '~/utils/colors'

type Props = {
Expand All @@ -16,10 +17,15 @@ type Props = {
export const General: FC<Props> = ({ initialValues }) => {
const form = useForm<TAboutParams>({ defaultValues: initialValues, mode: 'onSubmit' })
const { isSubmitting } = form.formState
const setAppMetaData = useAppStore((s) => s.setAppMetaData)

const onSubmit = form.handleSubmit(async (data) => {
try {
await postAboutData(data)
const res = (await postAboutData(data)) as Awaited<{ status: string }>

if (res.status === 'success') {
setAppMetaData(data)
}
} catch (error) {
console.warn(error)
}
Expand Down
71 changes: 34 additions & 37 deletions src/components/SettingsModal/SettingsView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import Tab from '@mui/material/Tab'
import Tabs from '@mui/material/Tabs'
import * as React from 'react'
import { useEffect, useState } from 'react'
import { useState } from 'react'
import styled from 'styled-components'
import { Flex } from '~/components/common/Flex'
import { Text } from '~/components/common/Text'
import { TAboutParams, getAboutData } from '~/network/fetchSourcesData'
import { useAppStore } from '~/stores/useAppStore'
import { useUserStore } from '~/stores/useUserStore'
import { colors } from '~/utils/colors'
import { UserPermissions } from '../UserPermissions'
Expand All @@ -19,12 +19,13 @@ interface TabPanelProps {
value: number
}

const defaultData = {
description: '',
mission_statement: '',
search_term: '',
title: '',
}
// const admins = [
Copy link
Collaborator

Choose a reason for hiding this comment

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

please remove this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

alright

// '02c431e64078b10925584d64824c9d1d12eca05e2c56660ffa5ac84aa6946adfe5',
// '03a9a8d953fe747d0dd94dd3c567ddc58451101e987e2d2bf7a4d1e10a2c89ff38',
// '024efa31d1e4f98bccc415b222c9d971866013ad6f95f7d1ed9e8be8e3355a36ff',
// '03bfe6723c06fb2b7546df1e8ca1a17ae5c504615da32c945425ccbe8d3ca6260d',
// '024efa31d1e4f98bccc415b222c9d971866013ad6f95f7d1ed9e8be8e3355a36ff',
// ]

const TabPanel = (props: TabPanelProps) => {
const { children, value, index, ...other } = props
Expand Down Expand Up @@ -55,39 +56,33 @@ type Props = {

export const SettingsView: React.FC<Props> = ({ onClose }) => {
const [value, setValue] = useState(0)
const [isAdmin] = useUserStore((s) => [s.isAdmin])
const [loading, setLoading] = useState(false)
const [initialValues, setInitialValues] = useState<TAboutParams>(defaultData)

useEffect(() => {
const init = async () => {
setLoading(true)

try {
const response = await getAboutData()

setInitialValues(response)
} catch (error) {
console.warn(error)
} finally {
setLoading(false)
}
}

init()
}, [])
const [isAdmin, pubKey] = useUserStore((s) => [s.isAdmin, s.setPubKey, s.pubKey])
const appMetaData = useAppStore((s) => s.appMetaData)

const getSettingsLabel = () => (isAdmin ? 'Admin Settings' : 'Settings')

const SettingsHeader = ({ children }: { children: React.ReactNode }) => (
<StyledHeader>
<Flex direction="row" pt={3}>
<StyledText>{getSettingsLabel()}</StyledText>
{resolveAdminActions()}
</Flex>
{children}
</StyledHeader>
)

const resolveAdminActions = () => {
if (!pubKey) {
return null
}

if (isAdmin) {
return null
}

return <></>
}

const handleChange = (event: React.SyntheticEvent, newValue: number) => {
setValue(newValue)
}
Expand All @@ -101,14 +96,16 @@ export const SettingsView: React.FC<Props> = ({ onClose }) => {
{isAdmin && <StyledTab disableRipple label="User Permissions" {...a11yProps(2)} />}
</StyledTabs>
</SettingsHeader>

<TabPanel index={0} value={value}>
{!loading ? <General initialValues={initialValues} /> : <></>}
</TabPanel>
<TabPanel index={2} value={value}>
{!loading ? <UserPermissions initialValues={initialValues} /> : <></>}
</TabPanel>

{isAdmin && (
<>
<TabPanel index={0} value={value}>
<General initialValues={appMetaData} />
</TabPanel>
<TabPanel index={2} value={value}>
<UserPermissions initialValues={appMetaData} />
</TabPanel>
</>
)}
<TabPanel index={isAdmin ? 1 : 0} value={value}>
<Appearance onClose={onClose} />
</TabPanel>
Expand Down
6 changes: 6 additions & 0 deletions src/stores/useAppStore/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import create from 'zustand'
import { initialAboutState } from '~/components/App/Splash/constants'
import { TAboutParams } from '~/network/fetchSourcesData'

export type SecondarySidebarActiveTab = '' | 'sentiment' | 'sources' | 'about'

Expand All @@ -11,13 +13,15 @@ type AppStore = {
transcriptIsOpen: boolean
flagErrorIsOpen: boolean
relevanceIsSelected: boolean
appMetaData: TAboutParams
clearSearch: () => void
setCurrentSearch: (_: string) => void
setRelevanceSelected: (_: boolean) => void
setSecondarySidebarActiveTab: (_: SecondarySidebarActiveTab) => void
setSidebarOpen: (_: boolean) => void
setTranscriptOpen: (_: boolean) => void
setFlagErrorOpen: (_: boolean) => void
setAppMetaData: (val: TAboutParams) => void
}

const defaultData = {
Expand All @@ -29,6 +33,7 @@ const defaultData = {
sidebarIsOpen: true,
theme: 'light' as const,
transcriptIsOpen: false,
appMetaData: initialAboutState,
}

export const useAppStore = create<AppStore>((set, get) => ({
Expand All @@ -47,4 +52,5 @@ export const useAppStore = create<AppStore>((set, get) => ({
transcriptIsOpen: !sidebarIsOpen ? false : get().transcriptIsOpen,
}),
setTranscriptOpen: (transcriptIsOpen) => set({ transcriptIsOpen }),
setAppMetaData: (appMetaData) => set({ appMetaData }),
}))
Loading