@@ -3,28 +3,54 @@ import { Article } from "@/lib/types";
3
3
import { postsPerPage } from "@/site" ;
4
4
import Feed from "@/components/Feed" ;
5
5
import HeroSection from "../components/HeroSection" ;
6
+ import axios , { AxiosResponse } from 'axios' ;
6
7
8
+ interface SlackMessage {
9
+ text : string ;
10
+ }
7
11
12
+ const sendSlackMessage = async ( message : SlackMessage ) : Promise < AxiosResponse > => {
13
+ const slackWebhookUrl = process . env . SLACK_WEBHOOK ;
14
+
15
+ if ( ! slackWebhookUrl ) {
16
+ throw new Error ( 'Slack webhook URL is not defined.' ) ;
17
+ }
18
+
19
+ return await axios . post ( slackWebhookUrl , message ) ;
20
+ } ;
8
21
9
22
const HomePage = async ( {
10
23
searchParams,
11
24
} : {
12
- searchParams : { [ key : string ] : string } ;
25
+ searchParams : { [ key : string ] : string | undefined } ;
13
26
} ) => {
14
27
const page = Number ( searchParams . page ) || 1 ;
15
28
const publishedPosts : Article [ ] = await getAllPosts ( ) ;
16
- // const startIndex = (page - 1) * postsPerPage;
17
- // const endIndex = startIndex + postsPerPage;
18
- // const postsToShow = publishedPosts.slice(startIndex, endIndex);
19
29
30
+ // Send a message to Slack when the page is accessed
31
+ const message : SlackMessage = {
32
+ text : `Someone visited the homepage! Page: ${ page } ` ,
33
+ } ;
34
+
35
+ try {
36
+ await sendSlackMessage ( message ) ;
37
+ } catch ( error : unknown ) {
38
+ if ( error instanceof Error ) {
39
+ console . error ( 'Error sending Slack message:' , error . message ) ;
40
+ // Handle the error as needed
41
+ } else {
42
+ console . error ( 'An unexpected error occurred:' , error ) ;
43
+ // Handle other types of errors or log them
44
+ }
45
+ }
20
46
21
47
return (
22
48
< >
23
49
< div className = "" >
24
- < HeroSection />
25
- < div className = "mt-4 max-w-5xl m-auto p-4 min-h-screen" >
26
- < Feed articles = { publishedPosts } />
27
- </ div >
50
+ < HeroSection />
51
+ < div className = "mt-4 max-w-5xl m-auto p-4 min-h-screen" >
52
+ < Feed articles = { publishedPosts } />
53
+ </ div >
28
54
</ div >
29
55
</ >
30
56
) ;
0 commit comments