-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarouselUtils.ts
66 lines (57 loc) · 2.52 KB
/
carouselUtils.ts
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
import { CarouselConfig } from "./configs";
import { FrameButtonMetadata } from "@coinbase/onchainkit/frame";
type FrameButtonInfoWithAction = FrameButtonMetadata & { carouselAction?: "prev" | "next" | "compose" };
export function getButtonsWithActionForCarouselItem(
config: CarouselConfig,
itemNumber: number
): [FrameButtonInfoWithAction, ...FrameButtonInfoWithAction[]] | undefined {
const frameButtons: FrameButtonInfoWithAction[] = [];
const configItem = config.itemConfigs[itemNumber];
if (!configItem) {
console.error("getButtonInfoForCarouselItem - index out of bounds, ", itemNumber);
return undefined;
}
// Not the first page, and didn't disable back nav
if (
!config.navButtonConfig?.disablePrevNavigation &&
!configItem.navButtonConfigOverrides?.disablePrevNavigation &&
itemNumber != 0
) {
// Prev
frameButtons.push({
action: "post",
label: configItem.navButtonConfigOverrides?.prevButtonLabel ?? config.navButtonConfig?.prevButtonLabel ?? "⬅️",
carouselAction: "prev",
} as FrameButtonInfoWithAction);
}
// Not the last page
if (itemNumber != config.itemConfigs.length - 1) {
// Next
frameButtons.push({
action: "post",
label: configItem.navButtonConfigOverrides?.nextButtonLabel ?? config.navButtonConfig?.nextButtonLabel ?? "➡️",
carouselAction: "next",
} as FrameButtonInfoWithAction);
}
if (configItem.buttonThreeConfig != undefined) {
const isCompose = configItem.buttonThreeConfig.action == "compose";
frameButtons.push({
action: isCompose ? "post" : configItem.buttonThreeConfig.action,
label: configItem.buttonThreeConfig.label,
target: configItem.buttonThreeConfig.target + (isCompose ? "?composing=1" : ""),
carouselAction: configItem.buttonThreeConfig.action == "compose" ? "compose" : undefined,
} as FrameButtonInfoWithAction);
}
if (configItem.buttonFourConfig != undefined) {
const isCompose = configItem.buttonFourConfig.action == "compose";
frameButtons.push({
action: configItem.buttonFourConfig.action == "compose" ? "post" : configItem.buttonFourConfig.action,
label: configItem.buttonFourConfig.label,
target: configItem.buttonFourConfig.target + (isCompose ? "?composing=1" : ""),
carouselAction: configItem.buttonFourConfig.action == "compose" ? "compose" : undefined,
} as FrameButtonInfoWithAction);
}
return frameButtons.length == 0
? undefined
: (frameButtons as [FrameButtonInfoWithAction, ...FrameButtonInfoWithAction[]]);
}