feat(chat): add history unlock api
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
"use client";
|
||||
|
||||
import type { LoginStatus } from "@/data/dto/auth";
|
||||
import { AppStorage } from "@/data/storage/app/app_storage";
|
||||
import { AuthStorage } from "@/data/storage/auth/auth_storage";
|
||||
import { UserStorage } from "@/data/storage/user/user_storage";
|
||||
import { ROUTE_BUILDERS, ROUTES } from "@/router/routes";
|
||||
import { AppEnvUtil, BrowserDetector, todayString, UrlLauncherUtil } from "@/utils";
|
||||
|
||||
export interface ExternalBrowserPromptState {
|
||||
hasInitialized: boolean;
|
||||
isLoading: boolean;
|
||||
loginStatus: LoginStatus;
|
||||
}
|
||||
|
||||
export function deriveIsGuest(loginStatus: LoginStatus): boolean {
|
||||
return loginStatus === "guest";
|
||||
}
|
||||
|
||||
export function isChatDevelopmentEnvironment(): boolean {
|
||||
return AppEnvUtil.isDevelopment();
|
||||
}
|
||||
|
||||
export function shouldStartExternalBrowserPrompt({
|
||||
hasInitialized,
|
||||
isLoading,
|
||||
loginStatus,
|
||||
}: ExternalBrowserPromptState): boolean {
|
||||
if (!hasInitialized || isLoading) return false;
|
||||
return (
|
||||
isChatDevelopmentEnvironment() ||
|
||||
(loginStatus === "facebook" && BrowserDetector.isInAppBrowser())
|
||||
);
|
||||
}
|
||||
|
||||
export async function resolveExternalBrowserPromptEligibility(): Promise<{
|
||||
canShow: boolean;
|
||||
today: string;
|
||||
}> {
|
||||
const today = todayString();
|
||||
const result = await AppStorage.canShowExternalBrowserDialog(today);
|
||||
return {
|
||||
canShow: result.success && result.data,
|
||||
today,
|
||||
};
|
||||
}
|
||||
|
||||
export function recordExternalBrowserPromptShown(today: string): Promise<unknown> {
|
||||
return AppStorage.recordExternalBrowserDialogShown(today);
|
||||
}
|
||||
|
||||
export async function openChatInExternalBrowser(): Promise<void> {
|
||||
const authStorage = AuthStorage.getInstance();
|
||||
const userStorage = UserStorage.getInstance();
|
||||
const [deviceIdR, facebookIdR, avatarUrlR] = await Promise.all([
|
||||
authStorage.getDeviceId(),
|
||||
authStorage.getFacebookId(),
|
||||
userStorage.getAvatarUrl(),
|
||||
]);
|
||||
|
||||
const deviceId = deviceIdR.success ? deviceIdR.data : null;
|
||||
const facebookId = facebookIdR.success ? facebookIdR.data : null;
|
||||
const avatarUrl = avatarUrlR.success ? avatarUrlR.data : null;
|
||||
|
||||
if (deviceId && facebookId) {
|
||||
UrlLauncherUtil.openUrlWithExternalBrowser(
|
||||
ROUTE_BUILDERS.chatDeviceId(deviceId),
|
||||
{
|
||||
queryParams: {
|
||||
fbid: facebookId,
|
||||
...(avatarUrl ? { avatarUrl } : {}),
|
||||
},
|
||||
},
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
UrlLauncherUtil.openUrlWithExternalBrowser(ROUTES.splash);
|
||||
}
|
||||
|
||||
export function getChatPaywallSubscriptionUrl(): string {
|
||||
return ROUTE_BUILDERS.subscription("vip", {
|
||||
returnTo: "chat",
|
||||
});
|
||||
}
|
||||
|
||||
export function getChatPaywallNavigationUrl(loginStatus: LoginStatus): string {
|
||||
const subscriptionUrl = getChatPaywallSubscriptionUrl();
|
||||
if (deriveIsGuest(loginStatus)) {
|
||||
return ROUTE_BUILDERS.authWithRedirect(subscriptionUrl);
|
||||
}
|
||||
return subscriptionUrl;
|
||||
}
|
||||
Reference in New Issue
Block a user