-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPagesLayout.tsx
176 lines (162 loc) · 4.71 KB
/
PagesLayout.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
169
170
171
172
173
174
175
176
import { assertOk } from "@/lib/assert";
import { type RouteSectionProps } from "@solidjs/router";
import { compareVersions, postEvent, type UnionKeys } from "@telegram-apps/sdk";
import {
Show,
createEffect,
createSignal,
on,
onCleanup,
type Component,
} from "solid-js";
import { Portal } from "solid-js/web";
import { BottomDialog } from "./BottomDialog";
import { getSelfUserId } from "./idUtils";
import { useNavigationReady } from "./navigation";
import { OnboardingContent, createOnboarding } from "./onboarding";
import {
usePageTransitionFinished,
usePageTransitionsCount,
} from "./pageTransitions";
import {
createProfileShareUrl,
isApple,
launchParams,
mainButton,
platform,
userHasPremium,
} from "./telegramIntegration";
interface CreateParams<
Params = never,
VersionedParam extends UnionKeys<Params> = never,
> {
params: Params;
versionedParams: VersionedParam;
}
declare module "@telegram-apps/sdk" {
interface MiniAppsMethods {
web_app_share_to_story: CreateParams<WepAppShareStory>;
}
}
type WepAppShareStory = {
media_url: string;
text?: string;
widget_link?: {
url: string;
name?: string;
};
};
const randomInt = (start: number, endExclusive: number) => {
assertOk((start | 0) === start);
assertOk((endExclusive | 0) === endExclusive);
assertOk(endExclusive >= start);
const diff = endExclusive - start;
return (start + diff * Math.random()) | 0;
};
const useStories = (shouldShowMainButton: () => boolean) => {
const pageTransitionFinished = usePageTransitionFinished();
onCleanup(
mainButton.on("click", () => {
const mediaUrl =
import.meta.env.VITE_SELF_WEBAPP_URL +
`/assets/stories/${randomInt(1, 6)}${userHasPremium ? "-premium" : ""}.webp`;
const postLink = createProfileShareUrl(getSelfUserId());
postEvent(
"web_app_share_to_story",
userHasPremium
? {
media_url: mediaUrl,
widget_link: {
url: postLink,
name: "OPEN APP",
},
}
: {
media_url: mediaUrl,
text: postLink,
},
);
}),
);
createEffect(
on(shouldShowMainButton, (shouldShow) => {
if (!shouldShow) return;
const showButton = () => {
mainButton.setParams({
isVisible: true,
text: "Share to Stories",
isEnabled: true,
});
};
const hideButton = () => {
mainButton.hide();
};
if (!pageTransitionFinished) {
showButton();
onCleanup(hideButton);
return;
}
// we need to have some timeout to do not break view transition
pageTransitionFinished().finally(() => {
if (mainButton.isVisible) return;
showButton();
});
onCleanup(() => {
if (!mainButton.isVisible) return;
pageTransitionFinished().finally(() => {
if (!mainButton.isVisible) return;
// don't need to hide
if (shouldShowMainButton()) return;
hideButton();
});
});
}),
);
};
export const PageLayout: Component<RouteSectionProps> = (props) => {
const canPostStories = platform === "ios" || platform === "android";
const [shouldShowOnboarding, onOnboardingClose] = createOnboarding();
// sharing stories supported from 7.8 version
if (canPostStories && compareVersions(launchParams.version, "7.8") >= 0) {
useStories(
() =>
!shouldShowOnboarding() &&
props.location.pathname.includes("/board") &&
props.params.idWithoutPrefix === getSelfUserId(),
);
}
const transitionsCount = usePageTransitionsCount();
// skipping page load transition
const [finishedTransition, setFinishedTransition] = createSignal<number>(0);
return (
<>
{props.children}
<BottomDialog
when={useNavigationReady()() && shouldShowOnboarding()}
onClose={onOnboardingClose}
>
{() => <OnboardingContent onClose={onOnboardingClose} />}
</BottomDialog>
<Show when={isApple()}>
<Portal>
<Show keyed when={transitionsCount()}>
{(count) => (
<div
onAnimationEnd={() => {
setFinishedTransition(count);
}}
class="pointer-events-none fixed inset-x-0 bottom-0 origin-left animate-transition-indicator bg-accent will-change-[transform,opacity]"
style={{
height: mainButton.isVisible ? "1.5px" : "3.2px",
}}
classList={{
hidden: finishedTransition() === count,
}}
/>
)}
</Show>
</Portal>
</Show>
</>
);
};