Skip to content

Commit

Permalink
chore: use explicit scroll on extra simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
desoindx committed Dec 4, 2024
1 parent a64d303 commit ddd683d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/components/faq/FAQs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const FAQs = async ({
}: {
filter: string
} & Omit<FAQSListProps, 'faqs' | 'title' | 'description' | 'link' | 'linkLabel'>) => {
const faqs = await getFAQs()
const faqs = await getFAQs(filter)
return (
<FAQsList
faqs={faqs.filter((faq) => faq.pages.includes(filter))}
faqs={faqs}
title='Questions fréquentes'
description='Explorer la FAQ pour trouver les réponses à vos questions'
{...rest}
Expand Down
8 changes: 1 addition & 7 deletions src/components/outils/CategoryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,7 @@ const CategoryPage = ({
<Category category={category} simulator={simulator} />
{category.sources && <Sources className={styles.sources} sources={category.sources} tracking={category.name} />}
</Block>
{extraSimulator && (
<Block id={extraSimulator.slug} title={extraSimulator.title} description={extraSimulator.description}>
<ExtraSimulator tracking={extraSimulator.tracking} slug={extraSimulator.slug} small={extraSimulator.small}>
{extraSimulator.simulator}
</ExtraSimulator>
</Block>
)}
{extraSimulator && <ExtraSimulator simulator={extraSimulator}>{extraSimulator.simulator}</ExtraSimulator>}
<Suspense>
<Examples
title='Exemples'
Expand Down
47 changes: 34 additions & 13 deletions src/components/outils/ExtraSimulator.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,54 @@
import React, { ReactNode, useMemo } from 'react'
'use client'

import { ReactNode, useEffect, useMemo } from 'react'
import Block from 'components/layout/Block'
import Shareable from 'components/shareable/Shareable'
import { overScreenCategoryValues, overScreenOsezChangerValues } from 'components/shareable/overScreens/Values'
import styles from './ExtraSimulator.module.css'

const ExtraSimulator = ({
children,
tracking,
slug,
small,
simulator,
}: {
children: ReactNode
tracking: string
slug: string
small?: boolean
simulator: {
slug: string
tracking: string
title: string
description: string
simulator: ReactNode
small?: boolean
}
}) => {
const overScreens = useMemo(
() =>
slug === 'osez-changer'
simulator.slug === 'osez-changer'
? overScreenOsezChangerValues()
: overScreenCategoryValues({ id: 2, unit: slug, slug, name: tracking }),
[slug, tracking]
: overScreenCategoryValues({ id: 2, unit: simulator.slug, slug: simulator.slug, name: simulator.tracking }),
[simulator]
)

const simulator = (
<Shareable slug={slug} tracking={tracking} overScreens={overScreens} small={small}>
useEffect(() => {
if (window && window.location.hash) {
console.log(window.location.hash)
const anchor = window.location.hash.replace('#', '')
const element = document.getElementById(anchor)
if (element) {
element.scrollIntoView({ behavior: 'smooth' })
}
}
}, [])

const component = (
<Shareable slug={simulator.slug} tracking={simulator.tracking} overScreens={overScreens} small={simulator.small}>
{children}
</Shareable>
)
return small ? <div className={styles.container}>{simulator}</div> : simulator
return (
<Block id={simulator.slug} title={simulator.title} description={simulator.description}>
{simulator.small ? <div className={styles.container}>{component}</div> : component}
</Block>
)
}

export default ExtraSimulator
24 changes: 13 additions & 11 deletions src/utils/faq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getAllNotionDB } from './notion'
import { getRevalidate } from './revalidate'

export const getFAQs = unstable_cache(
async (): Promise<FAQ[]> => {
async (filter?: string): Promise<FAQ[]> => {
try {
const results = await getAllNotionDB<{
Name: { title: { plain_text: string }[] }
Expand All @@ -15,17 +15,19 @@ export const getFAQs = unstable_cache(
}>('https://api.notion.com/v1/databases/f21b76594988440c98fc153d73ad5730/query')

const contents = await Promise.all(
results.map(async (result) => {
try {
const content = await getNotionContentProps(result.id)
return {
id: result.id,
content,
results
.filter((result) => result.properties['Page(s)']?.multi_select?.some((select) => select.name === filter))
.map(async (result) => {
try {
const content = await getNotionContentProps(result.id)
return {
id: result.id,
content,
}
} catch {
return { id: null, content: null }
}
} catch {
return { id: null, content: null }
}
})
})
)

return results
Expand Down
3 changes: 3 additions & 0 deletions src/utils/notion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export const NotionCommandValidation = z
export type NotionCommand = z.infer<typeof NotionCommandValidation>

export const getAllNotionDB = async <T>(url: string): Promise<{ id: string; properties: T }[]> => {
if (!process.env.NOTION_API_KEY) {
return []
}
let results: { id: string; properties: T }[] = []
let axiosResponse:
| AxiosResponse<{
Expand Down

0 comments on commit ddd683d

Please sign in to comment.