Merge pull request #1833 from appwrite/add-init-banner

Update: promo modal logic
This commit is contained in:
Arman authored and GitHub committed 2025-05-12 17:19:00 +02:00
commit 6ec8c32534
6 files changed
+175 -44

No files matched your search

+96 -18
View File
@@ -4,7 +4,7 @@
import { app } from '$lib/stores/app';
import {
type BottomModalAlertItem,
bottomModalAlerts,
bottomModalAlertsConfig,
dismissBottomModalAlert,
hideAllModalAlerts
} from '$lib/stores/bottom-alerts';
@@ -16,6 +16,7 @@
import { project } from '$routes/(console)/project-[region]-[project]/store';
import { page } from '$app/stores';
import { trackEvent } from '$lib/actions/analytics';
import { goto } from '$app/navigation';
let currentIndex = 0;
let openModalOnMobile = false;
@@ -33,23 +34,27 @@
return alerts
.sort((a, b) => b.importance - a.importance)
.filter((alert) => {
return (
alert.show &&
shouldShowNotification(alert.id) &&
// if no scope > show in projects & org pages.
((!alert.scope && (isProjectPage || isOrganizationPage)) ||
// project scope, show only in project pages
(isProjectPage && alert.scope === 'project') ||
// organization scope, show only in organization pages
(isOrganizationPage && alert.scope === 'organization'))
);
if (!alert.show || !shouldShowNotification(alert.id)) return false;
switch (alert.scope) {
case 'everywhere':
return true;
case 'project':
return isProjectPage;
case 'organization':
return isOrganizationPage;
default:
return false;
}
});
}
$: filteredModalAlerts = filterModalAlerts($bottomModalAlerts, $page.route.id);
$: filteredModalAlerts = filterModalAlerts($bottomModalAlertsConfig.alerts, $page.route.id);
$: currentModalAlert = filteredModalAlerts[currentIndex] as BottomModalAlertItem;
$: hasOnlyPrimaryCta = typeof currentModalAlert?.learnMore === 'undefined';
function handleClose() {
filteredModalAlerts.forEach((alert) => {
const modalAlert = alert;
@@ -67,6 +72,45 @@
currentIndex = (currentIndex - 1 + filteredModalAlerts.length) % filteredModalAlerts.length;
}
function getMobileWindowConfig(): {
html: boolean;
cta: boolean;
title: string;
message: string;
} {
const config = $bottomModalAlertsConfig?.mobileSingleLayout;
const visibleAlerts = $bottomModalAlertsConfig.alerts.filter((a) => a.show);
const fallback = {
title: 'New features available',
message: 'Explore new features to enhance your projects and improve security.'
};
const shouldApplyConfig = config?.enabled === true && visibleAlerts.length === 1;
return {
cta: !!(shouldApplyConfig && config.cta),
html: !!(shouldApplyConfig && config.isHtml),
title: shouldApplyConfig && config.title ? config.title : fallback.title,
message: shouldApplyConfig && config.message ? config.message : fallback.message
};
}
function triggerMobileWindowLink() {
handleClose();
const url = $bottomModalAlertsConfig.mobileSingleLayout.cta.link({
organization: $organization,
project: $project
});
if ($bottomModalAlertsConfig.mobileSingleLayout.cta.external) {
window.open(url, '_blank');
} else {
goto(url);
}
}
function showUpgrade() {
const plan = currentModalAlert.plan;
const organizationPlan = $organization.billingPlan;
@@ -159,8 +203,8 @@
<div
class="buttons u-flex u-flex-vertical-mobile u-gap-4 u-padding-inline-8 u-padding-block-8">
<Button
secondary
class="button"
secondary={!hasOnlyPrimaryCta}
class={`${hasOnlyPrimaryCta ? 'only-primary-cta' : ''}`}
href={shouldShowUpgrade
? $upgradeURL
: currentModalAlert.cta.link({
@@ -170,6 +214,12 @@
external={!!currentModalAlert.cta.external}
fullWidthMobile
on:click={() => {
if (currentModalAlert.cta?.hideOnClick === true) {
// be careful of this one.
// once clicked, its gone!
handleClose();
}
trackEvent('click_promo', {
promo: currentModalAlert.id,
type: shouldShowUpgrade ? 'upgrade' : 'try_now'
@@ -270,7 +320,7 @@
<div
class="buttons u-flex u-flex-vertical-mobile u-gap-4 u-padding-inline-8 u-padding-block-8">
<Button
secondary
secondary={!hasOnlyPrimaryCta}
class="button"
href={shouldShowUpgrade
? $upgradeURL
@@ -312,20 +362,32 @@
</article>
</div>
{:else}
{@const mobileConfig = getMobileWindowConfig()}
<button
class:showing={!openModalOnMobile}
class="card notification-card u-width-full-line"
on:click={() => (openModalOnMobile = true)}>
on:click={() => {
if (mobileConfig.cta) {
// navigate manually!
triggerMobileWindowLink();
} else {
openModalOnMobile = true;
}
}}>
<div class="u-flex-vertical u-gap-4">
<div class="u-flex u-cross-center u-main-space-between">
<h3 class="body-text-2 u-bold">New features available</h3>
<h3 class="body-text-2 u-bold">{mobileConfig.title}</h3>
<button on:click={hideAllModalAlerts}>
<span class="icon-x" />
</button>
</div>
<span class="u-width-fit-content">
Explore new features to enhance your projects and improve security.
{#if mobileConfig.html}
{@html mobileConfig.message}
{:else}
{mobileConfig.message}
{/if}
</span>
</div>
</button>
@@ -370,6 +432,22 @@
border: hsl(var(--color-neutral-80)) solid 1px;
}
:global(.main-alert-wrapper .only-primary-cta) {
width: 100%;
text-align: center;
justify-content: center;
}
.showcase-image.u-only-light {
border-radius: 8px;
border: 0.795px solid var(--border-neutral-strong, #d8d8db);
}
.showcase-image.u-only-dark {
border-radius: 8px;
border: 0.795px solid var(--border-neutral-strong, #414146);
}
.u-gap-10 {
gap: 0.625rem;
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 279 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 155 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 368 KiB

+46 -7
View File
@@ -5,10 +5,28 @@ import type { Models } from '@appwrite.io/console';
type BottomModalAlertAction = {
text: string;
hideOnClick?: boolean;
link: (ctx: { organization: Organization; project: Models.Project }) => string;
external?: boolean;
};
/**
* Special layout settings for mobile when exactly one alert is shown.
*
* Applied only if `enabled` is `true` and the number of visible alerts is exactly 1.
*
* Useful when you want to display the alert directly in the floating window,
* without opening a separate modal.
*/
export type MobileSingleAlertLayoutConfig = {
title: string;
message: string;
enabled: boolean;
isHtml?: boolean;
// because message is the text!
cta: Omit<BottomModalAlertAction, 'text'>;
};
export type BottomModalAlertItem = {
id: string;
title: string;
@@ -23,18 +41,36 @@ export type BottomModalAlertItem = {
importance?: number;
closed?: () => void;
scope?: 'organization' | 'project';
scope: 'organization' | 'project' | 'everywhere';
notificationHideOptions?: NotificationCoolOffOptions;
};
export const bottomModalAlerts = writable<BottomModalAlertItem[]>([]);
type BottomModalAlertState = {
alerts: BottomModalAlertItem[];
mobileSingleLayout?: MobileSingleAlertLayoutConfig;
};
export const bottomModalAlertsConfig = writable<BottomModalAlertState>({ alerts: [] });
export const hideAllModalAlerts = () => {
bottomModalAlerts.update((all) => all.map((t) => ({ ...t, show: false })));
bottomModalAlertsConfig.update((state) => ({
...state,
alerts: state.alerts.map((t) => ({ ...t, show: false }))
}));
};
export const setMobileSingleAlertLayout = (config: MobileSingleAlertLayoutConfig) => {
bottomModalAlertsConfig.update((state) => ({
...state,
mobileSingleLayout: config
}));
};
export const dismissBottomModalAlert = (id: string) => {
bottomModalAlerts.update((all) => all.filter((t) => t.id !== id));
bottomModalAlertsConfig.update((state) => ({
...state,
alerts: state.alerts.filter((t) => t.id !== id)
}));
};
export const showBottomModalAlert = (notification: BottomModalAlertItem) => {
@@ -45,8 +81,11 @@ export const showBottomModalAlert = (notification: BottomModalAlertItem) => {
...notification
};
bottomModalAlerts.update((all) => {
if (all.some((t) => t.id === notification.id)) return all;
return [...all, defaults as BottomModalAlertItem];
bottomModalAlertsConfig.update((state) => {
if (state.alerts.some((t) => t.id === notification.id)) return state;
return {
...state,
alerts: [...state.alerts, defaults as BottomModalAlertItem]
};
});
};
+33 -19
View File
@@ -1,32 +1,46 @@
import { base } from '$app/paths';
import BackupsDark from '$lib/images/backups/promo/backups-dark.png';
import BackupsLight from '$lib/images/backups/promo/backups-light.png';
import Init3Promo from '$lib/images/promo-init3.png';
import { type BottomModalAlertItem, showBottomModalAlert } from '$lib/stores/bottom-alerts';
import {
type BottomModalAlertItem,
setMobileSingleAlertLayout,
showBottomModalAlert
} from '$lib/stores/bottom-alerts';
import { isCloud } from '$lib/system';
const listOfPromotions: BottomModalAlertItem[] = [];
if (isCloud) {
const title = 'Join Init 19-23 May';
const message =
'Change the way you build with Appwrite forever. Claim your ticket and register for the event.';
const callToAction = {
external: true,
hideOnClick: true,
text: 'Register and join the give away',
link: () => 'https://apwr.dev/clcon'
};
listOfPromotions.push({
id: 'modal:databaseBackups',
id: 'modal:init3',
src: {
dark: BackupsDark,
light: BackupsLight
dark: Init3Promo,
light: Init3Promo
},
title: 'Database Backups are available now',
message: 'Protect your data and ensure quick recovery with our new backups',
plan: 'pro',
scope: 'project',
title,
message,
plan: 'free',
importance: 8,
cta: {
text: 'Try now',
link: ({ project }) => `${base}/project-${project.region}-${project.$id}/databases`
},
learnMore: {
text: 'Learn more',
link: () => 'http://appwrite.io/docs/products/databases/backups'
}
scope: 'everywhere',
cta: callToAction
});
// there's only one item.
setMobileSingleAlertLayout({
title,
message,
enabled: true,
cta: callToAction
});
}