-
-
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.
- Loading branch information
Showing
9 changed files
with
523 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Octokit } from 'octokit'; | ||
|
||
export const runtime = 'edge'; | ||
export const revalidate = 60 * 10; | ||
|
||
const token = process.env.DISCUSSION_TOKEN; | ||
const repo = process.env.DISCUSSION_REPOSITORY; | ||
const octokit = new Octokit({ | ||
auth: token | ||
}); | ||
|
||
export async function GET() { | ||
if (!repo || !token) { | ||
return Response.json( | ||
{ error: 'DISCUSSION_REPOSITORY and DISCUSSION_TOKEN must be set' }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
try { | ||
const discussions = await octokit.graphql<{ | ||
search: { | ||
nodes: { __typename: string; url: string; title: string; id: string }[]; | ||
}; | ||
}>( | ||
` | ||
query ($searchQuery: String!) { | ||
search(type: DISCUSSION, query: $searchQuery, first: 5) { | ||
nodes { | ||
__typename | ||
... on Discussion { | ||
url | ||
title | ||
id | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
{ | ||
searchQuery: `repo:${repo} is:open label:pinned category:announcements` | ||
} | ||
); | ||
|
||
return Response.json(discussions.search.nodes); | ||
} catch (error) { | ||
console.error(error); | ||
return Response.json( | ||
{ error: 'Failed to fetch discussions' }, | ||
{ status: 500 } | ||
); | ||
} | ||
} |
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,11 @@ | ||
interface Props { | ||
children: React.ReactNode; | ||
} | ||
|
||
export default function RootLayout({ children }: Props) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/image-types/global" /> | ||
/// <reference types="next/navigation-types/compat/navigation" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/basic-features/typescript for more information. |
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,63 @@ | ||
import { Alert, CloseButton, Link } from '@chakra-ui/react'; | ||
import { Fragment, MouseEvent, useCallback, useMemo } from 'react'; | ||
import { useLocalStorage } from 'react-use'; | ||
import { useAnnouncements } from '~/utils/hooks/api/use-announcements'; | ||
|
||
export const Announcements = () => { | ||
const [reads, setReads] = useLocalStorage<string[]>( | ||
'knzklive-announcement-reads', | ||
[] | ||
); | ||
const announcements = useAnnouncements(); | ||
const unreadAnnouncements = useMemo( | ||
() => | ||
announcements?.filter( | ||
announcement => !reads || !reads.includes(announcement.id) | ||
), | ||
[announcements, reads] | ||
); | ||
|
||
const handleRead = useCallback( | ||
(e: MouseEvent<HTMLButtonElement>) => { | ||
e.preventDefault(); | ||
const id = e.currentTarget.dataset.id; | ||
if (!id) { | ||
return; | ||
} | ||
|
||
setReads(() => { | ||
if (!reads) { | ||
return [id]; | ||
} | ||
|
||
return [...reads, id]; | ||
}); | ||
}, | ||
[setReads, reads] | ||
); | ||
|
||
return ( | ||
<Fragment> | ||
{unreadAnnouncements?.map(announcement => ( | ||
<Alert status="info" py={2} px={3} key={announcement.id}> | ||
<Link | ||
href={announcement.url} | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
_hover={{ textDecoration: 'underline' }} | ||
isTruncated | ||
> | ||
{announcement.title} | ||
</Link> | ||
|
||
<CloseButton | ||
size="sm" | ||
ml="auto" | ||
onClick={handleRead} | ||
data-id={announcement.id} | ||
/> | ||
</Alert> | ||
))} | ||
</Fragment> | ||
); | ||
}; |
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
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,20 @@ | ||
import useSWR from 'swr'; | ||
|
||
const fetcher = (...args: Parameters<typeof fetch>) => | ||
fetch(...args).then(res => res.json()); | ||
|
||
export const useAnnouncements = () => { | ||
const { data } = useSWR< | ||
{ | ||
url: string; | ||
title: string; | ||
id: string; | ||
}[] | ||
>('/api/announcements', fetcher, { | ||
revalidateOnFocus: false, | ||
revalidateOnReconnect: false, | ||
revalidateIfStale: false | ||
}); | ||
|
||
return data; | ||
}; |
Oops, something went wrong.