Skip to content

Commit

Permalink
feat: hightlight searched text (#37)
Browse files Browse the repository at this point in the history
* fix: 区分 first load and load more

* feat: 🎸 highlight searched text
  • Loading branch information
ThaddeusJiang authored Sep 22, 2023
1 parent e363be6 commit e6805a4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
22 changes: 20 additions & 2 deletions components/ui/MemoCard.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect, useRef } from "react"
import ReactMarkdown from "react-markdown"

import { IconDots } from "@tabler/icons-react"
Expand All @@ -8,8 +9,25 @@ import remarkGfm from "remark-gfm"

import { Memo } from "~/types"

export const MemoCard = ({ memo, onDelete }: { memo: Memo; onDelete: (id: string) => void }) => {
export const MemoCard = (props: { memo: Memo; onDelete: (id: string) => void; highlight: string }) => {
const { memo, onDelete, highlight } = props
const createdAt = dayjs(memo.created_at).format("YYYY-MM-DD HH:mm")

const ref = useRef<HTMLDivElement>(null)
useEffect(() => {
const words = highlight.trim().split(" ").filter(Boolean)
words.forEach((word) => {
let reg = new RegExp(word, "gi")

if (ref.current && ref.current instanceof HTMLElement) {
ref.current.innerHTML = ref.current.innerHTML.replace(
reg,
(match) => `<span class="bg-yellow-300 text-black">${match}</span>`
)
}
})
}, [highlight])

return (
<div className={classNames("w-full")}>
<div className="my-2 rounded-lg bg-base-100 px-4 py-2 hover:shadow ">
Expand All @@ -36,7 +54,7 @@ export const MemoCard = ({ memo, onDelete }: { memo: Memo; onDelete: (id: string
</div>
</div>
</div>
<article className="prose md:prose ">
<article ref={ref} className="prose md:prose ">
<ReactMarkdown children={memo?.content ?? ""} remarkPlugins={[remarkGfm]} />
</article>
</div>
Expand Down
17 changes: 8 additions & 9 deletions pages/settings/avatars/[username]/memos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,24 +186,23 @@ export default function SettingsAvatarMemoPage({ avatar }: { avatar: Avatar }) {

{memoCreateMutation.isLoading ? <p className="text-center text-gray-500">creating...</p> : null}

<div className="text-sm opacity-70">
<p>Find {count} memos</p>
</div>
{status === "loading" ? (
<p className=" text-gray-500">Loading...</p>
) : (
<p className=" text-gray-500">Find {count} memos</p>
)}
{status === "error" ? <p className="text-center text-red-500">error</p> : null}

{data?.pages?.map(({ items, nextCursor }) => (
<Fragment key={nextCursor ?? "no-more"}>
{items?.map((project: any) => (
<MemoCard key={project.id} memo={project} onDelete={onDelete} />
<MemoCard key={project.id} memo={project} onDelete={onDelete} highlight={q} />
))}
</Fragment>
))}

{status === "error" ? <p className="text-center text-red-500">error</p> : null}

<div ref={ref} className="pb-12 text-center text-gray-500">
{status === "loading"
? "loading..."
: isFetchingNextPage
{isFetchingNextPage
? "load more..."
: hasNextPage
? null
Expand Down

1 comment on commit e6805a4

@vercel
Copy link

@vercel vercel bot commented on e6805a4 Sep 22, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

aier – ./

aier.app
aier-git-dev-thaddeusjiang.vercel.app
www.aier.app
aier-thaddeusjiang.vercel.app

Please sign in to comment.