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: implement orama search for active members and staff members #799

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"@next/font": "^13.1.1",
"@orama/orama": "^2.0.0-beta.10",
"@tailwindcss/line-clamp": "^0.4.2",
"@tailwindcss/typography": "^0.5.8",
"autoprefixer": "^10.4.13",
Expand Down
54 changes: 36 additions & 18 deletions src/components/ActiveMembers.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
import React, { useState } from "react"
import React, { useContext, useState } from "react"

import { OramaContext } from "context/OramaProvider"
import GetActivemembers from "components/GetActivemembers"
import Users from "components/Users"

// import contributors data
import activeMembers from "data/activemembers"
import Title from "components/Title"
import { FilterUsers } from "utils/filterUsers"

const ActiveMembers = () => {
const { searchDatabase } = useContext(OramaContext)

// state for currentUsers
const [currentUsers, setCurrentUsers] = useState(activeMembers)

// filter handler
const searchHandler = (event) => {
// Search handler
const searchHandler = async(event) => {
event.preventDefault()
const filterdUsers = FilterUsers(
activeMembers,
event.target.value,
)
setCurrentUsers(filterdUsers)

const value = event.target.value

try {
const results = await searchDatabase(
value
? {
term: value,
}
: null,
)

setCurrentUsers(results)
} catch (error) {
setCurrentUsers([])
}
}

const [tab] = useState("Active Members")
Expand Down Expand Up @@ -50,17 +64,21 @@ const ActiveMembers = () => {
</svg>
</div>
</div>
{currentUsers.length
? <div className="m-2 flex flex-wrap items-center justify-center overflow-y-auto pb-4">
{tab === "Active Members"
? (
{currentUsers.length
? (
<div className="m-2 flex flex-wrap items-center justify-center overflow-y-auto pb-4">
{tab === "Active Members"
? (
<GetActivemembers users={currentUsers} />
)
: (
)
: (
<Users users={currentUsers} />
)}
</div>
: <p className="mt-3">User does not exist!</p>}
)}
</div>
)
: (
<p className="mt-3">User does not exist!</p>
)}
</section>
</div>
)
Expand Down
30 changes: 24 additions & 6 deletions src/components/Staff.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
import React, { useState } from "react"
import React, { useContext, useState } from "react"
import { OramaContext } from "context/OramaProvider"

import GetStaff from "components/GetStaff"
import Users from "components/Users"

// import staff data
import staff from "data/staff"
import Title from "components/Title"
import { FilterUsersByNameAndDesc } from "utils/filterUsers"

const Staff = () => {
const { searchDatabase } = useContext(OramaContext)

const [tab] = useState("Staff")
// state for currentUsers
const [currentUsers, setCurrentUsers] = useState(staff)

// filter handler
const searchHandler = (event) => {
// Search handler
const searchHandler = async(event) => {
event.preventDefault()
const filterdUsers = FilterUsersByNameAndDesc(staff, event.target.value)
setCurrentUsers(filterdUsers)

const value = event.target.value

try {
const results = await searchDatabase(
value
? {
term: value,
}
: null,
)

setCurrentUsers(results)
} catch (error) {
setCurrentUsers([])
}
}

return (
<div className="flex items-center justify-center">
<div className="mt-[3em] flex max-w-bodyContainer items-start justify-start">
Expand Down
51 changes: 51 additions & 0 deletions src/context/OramaProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React, { createContext, useCallback, useEffect, useMemo, useState } from "react"
import { create, insertMultiple, search } from "@orama/orama"

export const OramaContext = createContext(null)

export const OramaProvider = ({ children, schema, initialData }) => {
const [db, setDb] = useState(null)

useEffect(() => {
const init = async() => {
const database = await create({
schema,
})

setDb(database)

await insertMultiple(database, initialData)
}

init()
}, [schema, initialData])

const searchDatabase = useCallback(
async(term) => {
if (!db) return null

if (term) {
const searchResult = await search(db, term)

const documents = searchResult.hits.map(
(result) => result.document,
)

return documents
} else {
const allDocuments = Object.values(db?.data?.docs?.docs)

return allDocuments
}
},
[db],
)

const value = useMemo(() => ({ searchDatabase }), [searchDatabase])

return (
<OramaContext.Provider value={value}>
{children}
</OramaContext.Provider>
)
}
17 changes: 17 additions & 0 deletions src/context/schemas/activemembersSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const activemembersSchema = {
name: "string",
avatar: {
blurDataURL: "string",
blurHeight: "number",
blurWidth: "number",
height: "number",
src: "string",
width: "number",
},
github: "string",
twitter: "string",
blogUrl: "string",
linkedin: "string",
}

export default activemembersSchema
18 changes: 18 additions & 0 deletions src/context/schemas/staffmembersSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const staffmembersSchema = {
name: "string",
avatar: {
blurDataURL: "string",
blurHeight: "number",
blurWidth: "number",
height: "number",
src: "string",
width: "number",
},
github: "string",
description: "string",
twitter: "string",
blogUrl: "string",
linkedin: "string",
}

export default staffmembersSchema
8 changes: 4 additions & 4 deletions src/data/activemembers.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const base = [
name: "Posandu Mapa",
avatar: PosanduAvatar,
github: "Posandu",
twitter: null,
twitter: "",
blogUrl: "https://www.tronic247.com",
contributions: 3,
},
Expand Down Expand Up @@ -198,7 +198,7 @@ const base = [
twitter: "devvsakib",
blogUrl: "https://devvsakib.me",
linkedin: "devvsakib",
contributions: null,
contributions: 0,
},
{
name: "Ctofanin",
Expand All @@ -207,7 +207,7 @@ const base = [
twitter: "ctoffaninDev",
blogUrl: "",
linkedin: "",
contributions: null,
contributions: 0,
},
{
name: "Haider Ali Punjabi",
Expand All @@ -225,7 +225,7 @@ const base = [
twitter: "krshkun",
blogUrl: "https://links.krsh.eu.org",
linkedin: "krshkun",
contributions: null,
contributions: 0,
},
].sort((a, b) => (a.name > b.name ? 1 : -1))

Expand Down
9 changes: 3 additions & 6 deletions src/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ export default function App({ Component, pageProps }) {
<meta name="viewport" content="width=device-width, initial-scale=1" />
</Head>

<AnimatePresence
mode="wait"
onExitComplete={() => window.scrollTo(0, 0)}
>
<Component {...pageProps} />
<AnimatePresence mode="wait" onExitComplete={() => window.scrollTo(0, 0)}>
<Component {...pageProps} />
</AnimatePresence>
<GoToTop/>
<GoToTop />
</div>
)
}
11 changes: 8 additions & 3 deletions src/pages/active-members.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import Layout from "components/Layout"
import ActiveMembers from "components/ActiveMembers"
import { OramaProvider } from "context/OramaProvider"
import activeMembers from "data/activemembers"
import activemembersSchema from "context/schemas/activemembersSchema"

export default function Home() {
return (
<Layout className="flex flex-col justify-start mt-[7rem]">
<ActiveMembers />
</Layout>
<OramaProvider schema={activemembersSchema} initialData={activeMembers}>
<Layout className="flex flex-col justify-start mt-[7rem]">
<ActiveMembers />
</Layout>
</OramaProvider>
)
}
11 changes: 8 additions & 3 deletions src/pages/staff.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import Layout from "components/Layout"
import Staff from "components/Staff"
import { OramaProvider } from "context/OramaProvider"
import staffmembersSchema from "context/schemas/staffmembersSchema"
import staff from "data/staff"

export default function Home() {
return (
<Layout className="flex flex-col justify-start mt-[7rem]">
<Staff />
</Layout>
<OramaProvider schema={staffmembersSchema} initialData={staff}>
<Layout className="flex flex-col justify-start mt-[7rem]">
<Staff />
</Layout>
</OramaProvider>
)
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,11 @@
resolved "https://registry.npmjs.org/@one-ini/wasm/-/wasm-0.1.1.tgz"
integrity sha512-XuySG1E38YScSJoMlqovLru4KTUNSjgVTIjyh7qMX6aNN5HY5Ct5LhRJdxO79JtTzKfzV/bnWpz+zquYrISsvw==

"@orama/orama@^2.0.0-beta.10":
version "2.0.0-beta.12"
resolved "https://registry.npmjs.org/@orama/orama/-/orama-2.0.0-beta.12.tgz"
integrity sha512-svPT72gxR1zyF7bMz+itmZBdZB1ndI97YdzArCqo5esIfWy1u5Ht/K8EEYJ/EuC35Di3j9czjCzhL1e/zVEARQ==

"@pnpm/config.env-replace@^1.1.0":
version "1.1.0"
resolved "https://registry.npmjs.org/@pnpm/config.env-replace/-/config.env-replace-1.1.0.tgz"
Expand Down