Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/lib/components/billing/alerts/selectProjectCloud.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import { page } from '$app/state';
import { toLocaleDate, toLocaleDateTime } from '$lib/helpers/date';
import { currentPlan } from '$lib/stores/organization';
import { Query } from '@appwrite.io/console';

let {
showSelectProject = $bindable(false),
Expand All @@ -23,8 +24,21 @@
let projects = $state<Array<Models.Project>>([]);
let error = $state<string | null>(null);

onMount(() => {
projects = page.data.allProjects?.projects || [];
onMount(async () => {
if (page.data.organization?.$id) {
try {
const orgProjects = await sdk.forConsole.projects.list([
Query.equal('teamId', page.data.organization.$id),
Query.limit(1000)
]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do also not have this same thing on upper level +layout.ts? lets see which one we can avoid. this is massive data :\

projects = orgProjects.projects;
} catch (e) {
error = 'Failed to load projects';
projects = [];
}
} else {
projects = [];
}
});

let projectsToArchive = $derived(
Expand All @@ -34,7 +48,7 @@
async function updateSelected() {
try {
await sdk.forConsole.billing.updateSelectedProjects(
projects[0].teamId,
page.data.organization.$id,
selectedProjects
);
showSelectProject = false;
Expand Down Expand Up @@ -71,7 +85,8 @@

<Modal bind:show={showSelectProject} title={'Manage projects'} onSubmit={updateSelected}>
<svelte:fragment slot="description">
Choose which two projects to keep. Projects over the limit will be blocked after this date.
Choose which {$currentPlan?.projects || 2} projects to keep. Projects over the limit will be
blocked after this date.
</svelte:fragment>
{#if error}
<Alert.Inline status="error" title="Error">{error}</Alert.Inline>
Expand Down
4 changes: 3 additions & 1 deletion src/lib/layout/createProject.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
<Typography.Title size="l">Create your project</Typography.Title>
{/if}
{#if projectsLimited}
<Alert.Inline status="warning" title="You've reached your limit of 2 projects">
<Alert.Inline
status="warning"
title={`You've reached your limit of ${$currentPlan?.projects || 2} projects`}>
Extra projects are available on paid plans for an additional fee
<svelte:fragment slot="actions">
<Button
Expand Down
15 changes: 12 additions & 3 deletions src/lib/stores/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,24 @@ export function calculateTrialDay(org: Organization) {
return days;
}

export async function checkForProjectsLimit(org: Organization, projects: number) {
export async function checkForProjectsLimit(org: Organization, orgProjectCount?: number) {
if (!isCloud) return;
if (!org || !projects) return;
if (!org) return;
const plan = await sdk.forConsole.billing.getOrganizationPlan(org.$id);
if (!plan) return;
if (plan.$id !== BillingPlan.FREE) return;
if (org.projects?.length > 0) return;

if (plan.projects > 0 && projects > plan.projects) {
let projectCount = orgProjectCount;
if (projectCount === undefined) {
const orgProjects = await sdk.forConsole.projects.list([
Query.equal('teamId', org.$id),
Query.limit(1000)
]);
projectCount = orgProjects.projects.length;
}

if (plan.projects > 0 && projectCount > plan.projects) {
headerAlert.add({
id: 'projectsLimitReached',
component: ProjectsLimit,
Expand Down
6 changes: 5 additions & 1 deletion src/routes/(console)/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,11 @@
if (currentOrganizationId === org.$id) return;
if (isCloud) {
currentOrganizationId = org.$id;
checkForProjectsLimit(org, data.allProjects?.projects?.length || 0);
const orgProjectCount =
data.allProjects && data.currentOrgId === org.$id
? data.allProjects.projects.length
: undefined;
checkForProjectsLimit(org, orgProjectCount);
checkForEnterpriseTrial(org);
await checkForUsageLimit(org);
checkForMarkedForDeletion(org);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import { currentPlan, regions as regionsStore } from '$lib/stores/organization';
import SelectProjectCloud from '$lib/components/billing/alerts/selectProjectCloud.svelte';
import { toLocaleDate } from '$lib/helpers/date';
import { BillingPlan } from '$lib/constants';

export let data;

Expand Down Expand Up @@ -167,7 +168,7 @@
</DropList>
</div>

{#if isCloud && $currentPlan?.projects && $currentPlan?.projects > 0 && data.organization.projects.length > 0 && data.projects.total > 2 && $canWriteProjects}
{#if isCloud && $currentPlan?.projects && $currentPlan?.projects > 0 && data.organization.projects.length > 0 && data.projects.total > $currentPlan.projects && $canWriteProjects && data.organization.billingPlan === BillingPlan.FREE}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of the free plan check, lets see if the currentPlan.projects or whatever the variable is. We need to avoid hardcoded billing plan name/id checks.

<Alert.Inline
title={`${data.projects.total - data.organization.projects.length} projects will be archived on ${toLocaleDate(billingProjectsLimitDate)}`}>
<Typography.Text>
Expand Down