-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.tsx
169 lines (150 loc) · 4.01 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { Button, Frog, TextInput } from 'frog'
import { devtools } from 'frog/dev'
import { handle } from 'frog/next'
import { serveStatic } from 'frog/serve-static'
import { isValidDuration, parseDurationToTimestamp } from '../lib/utils.js'
import { addReminder } from '../lib/db.js';
import { neynar as neynarHub } from 'frog/hubs'
import { neynar } from 'frog/middlewares'
import { SHARE_URL } from '../lib/constants.js';
const neynarMiddleware = neynar({
apiKey: process.env.NEYNAR_API_KEY!,
features: ['interactor', 'cast'],
})
export const app = new Frog({
assetsPath: '/',
basePath: '/api',
hub: neynarHub({ apiKey: process.env.NEYNAR_API_KEY! }),
title: "Reminder Bot",
verify: "silent",
initialState: {
timestamp: null
},
imageOptions: {
fonts: [{ name: "Inter", weight: 500, source: "google" }],
},
})
app.castAction(
'/',
(c) => {
return c.frame({
path: "/input"
})
},
{
aboutUrl: 'https://warpcast.com/reminderbot',
name: 'Reminder Bot',
description: 'Set up reminders for any cast as direct messages from @reminderbot.',
icon: 'bell'
}
)
const baseContainerStyle = {
display: "flex",
alignItems: "center",
justifyContent: "center",
width: "100%",
height: "100%",
backgroundColor: "black",
color: "white",
}
app.frame('/input', (c) => {
return c.res({
image: (
<div style={{
...baseContainerStyle,
fontSize: 60,
}}>
Set a reminder for this cast in
</div>
),
intents: [
<TextInput placeholder="e.g. 1d 12h 25m" />,
<Button.Link href={SHARE_URL}>Share</Button.Link>,
<Button action="/confirm">Submit</Button>,
],
})
})
app.frame("/confirm", async (c) => {
const { inputText, deriveState } = c
if (!inputText) return c.error({
message: "Please input a time"
})
if (!isValidDuration(inputText)) return c.error({
message: "Invalid duration format. Use format like '1d 12h 25m', '3h 30m', or '45m'"
})
const timestamp = parseDurationToTimestamp(inputText!)
deriveState((previousState: any) => {
previousState.timestamp = timestamp
})
const dateString = new Date(timestamp * 1000).toLocaleString('en-US', {
weekday: 'long',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
hour12: false
});
return c.res({
image: (
<div style={{
...baseContainerStyle,
flexDirection: "column",
padding: "10rem",
textAlign: "center",
}}>
<div style={{ display: "flex", fontSize: 60 }}>
Confirm time: {dateString} UTC
</div>
<div style={{ display: "flex", fontSize: 32, marginTop: "5rem" }}>
@reminderbot will send you this cast as a direct message.
</div>
</div>
),
intents: [
<Button action="/input">Go back</Button>,
<Button action="/submit">Confirm</Button>,
],
})
})
// @ts-ignore
app.frame("/submit", neynarMiddleware, async (c) => {
const { frameData, previousState }: any = c
await addReminder(
frameData!.castId.hash,
frameData!.fid.toString(),
c.var.cast?.author.username!,
previousState?.timestamp
)
return c.res({
image: (
<div style={{
...baseContainerStyle,
flexDirection: "column",
flexWrap: "nowrap",
position: "relative"
}}>
<div style={{
fontSize: 60,
fontStyle: "normal",
letterSpacing: "-0.025em",
lineHeight: 1.4,
marginTop: 30,
padding: "0 120px",
whiteSpace: "pre-wrap",
}}>
Success!
</div>
</div>
),
intents: [
<Button.Link href={SHARE_URL}>Share</Button.Link>,
<Button action="/input">Go back</Button>,
],
})
})
// @ts-ignore
const isEdgeFunction = typeof EdgeFunction !== 'undefined'
const isProduction = isEdgeFunction || import.meta.env?.MODE !== 'development'
devtools(app, isProduction ? { assetsPath: '/.frog' } : { serveStatic })
export const GET = handle(app)
export const POST = handle(app)