-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from kudos-ink/feat/infinite-scroll
feat: add infinite scroll + small type refactor
- Loading branch information
Showing
7 changed files
with
175 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
"use client"; | ||
|
||
import { isValidNotionPage, daysSince, getImagePath } from "@/utils/helpers"; | ||
import { Spinner } from "@nextui-org/spinner"; | ||
import { useEffect, useState } from "react"; | ||
import { QueryDatabaseResponse } from "@notionhq/client/build/src/api-endpoints"; | ||
import projectLogosJson from "@/public/images/imageMap.json"; | ||
import { useInView } from "react-intersection-observer"; | ||
import Row from "./row"; | ||
import { getGoodFirstIssues } from "@/actions/notion"; | ||
import { LoadMoreState } from "@/types"; | ||
|
||
export function LoadMore({ cursor }: { cursor: string }) { | ||
const { ref, inView } = useInView(); | ||
const [awaitingResponse, setAwaitingResponse] = useState(false); | ||
const [queryData, setQueryData] = useState<LoadMoreState>({ | ||
data: [], | ||
nextCursor: cursor, | ||
}); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
if (inView && !awaitingResponse) { | ||
setAwaitingResponse(true); | ||
try { | ||
const res = await getGoodFirstIssues({ | ||
page_size: 10, | ||
start_cursor: queryData.nextCursor, | ||
}); | ||
setQueryData((prevData) => ({ | ||
data: prevData.data.concat(res), | ||
nextCursor: res.next_cursor || undefined, | ||
})); | ||
} catch (error) { | ||
console.log("There was an error fetching the data", error); | ||
} | ||
} | ||
}; | ||
fetchData(); | ||
}, [inView, queryData, awaitingResponse]); | ||
|
||
useEffect(() => { | ||
if (!inView && awaitingResponse) { | ||
setAwaitingResponse(false); | ||
} | ||
}, [inView, awaitingResponse]); | ||
|
||
return ( | ||
<> | ||
{queryData.data && | ||
queryData.data.map((query) => { | ||
return query.results.map((row, index) => { | ||
if (isValidNotionPage(row)) { | ||
return ( | ||
<Row | ||
key={index} | ||
projectName={ | ||
row.properties["Project Name"].rollup.array[0].rich_text[0] | ||
.plain_text | ||
} | ||
repo={row.properties["Issue Link"].url.split("/issues")[0]} | ||
issueTitle={row.properties["Issue Title"].title[0].plain_text} | ||
issueLink={row.properties["Issue Link"].url} | ||
openedDate={ | ||
daysSince( | ||
row.properties["Opened Date"].date.start, | ||
).toString() + "d" | ||
} | ||
labels={row.properties["Issue Labels"].multi_select.map( | ||
(label) => label.name, | ||
)} | ||
image={getImagePath( | ||
row.properties["Issue Link"].url.split("/issues")[0], | ||
projectLogosJson, | ||
)} | ||
/> | ||
); | ||
} | ||
}); | ||
})} | ||
{queryData.nextCursor && ( | ||
<section className="flex justify-center"> | ||
<div ref={ref}> | ||
<Spinner /> | ||
</div> | ||
</section> | ||
)} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.