mirror of
https://github.com/appwrite/console.git
synced 2026-06-06 19:27:48 +00:00
fix: platform based org navigation via direct url hit.
This commit is contained in:
@@ -2,7 +2,7 @@ import { Dependencies } from '$lib/constants';
|
||||
import { failedInvoice } from '$lib/stores/billing';
|
||||
import { isCloud } from '$lib/system';
|
||||
import { sdk } from '$lib/stores/sdk';
|
||||
import { error } from '@sveltejs/kit';
|
||||
import { error, redirect } from '@sveltejs/kit';
|
||||
import type { LayoutLoad } from './$types';
|
||||
import Breadcrumbs from './breadcrumbs.svelte';
|
||||
import Header from './header.svelte';
|
||||
@@ -15,14 +15,17 @@ import type { Plan } from '$lib/sdk/billing';
|
||||
import { loadAvailableRegions } from '$routes/(console)/regions';
|
||||
import type { Organization } from '$lib/stores/organization';
|
||||
import { resolvedProfile } from '$lib/profiles/index.svelte';
|
||||
import { resolve } from '$app/paths';
|
||||
|
||||
export const load: LayoutLoad = async ({ params, depends, parent }) => {
|
||||
const { preferences: prefs } = await parent();
|
||||
const { organizations, preferences: prefs } = await parent();
|
||||
|
||||
depends(Dependencies.ORGANIZATION);
|
||||
depends(Dependencies.MEMBERS);
|
||||
depends(Dependencies.PAYMENT_METHODS);
|
||||
|
||||
const requestedOrg = await checkPlatformAndRedirect(params, organizations, prefs);
|
||||
|
||||
let roles = isCloud ? [] : defaultRoles;
|
||||
let scopes = isCloud ? [] : defaultScopes;
|
||||
let currentPlan: Plan = null;
|
||||
@@ -38,6 +41,7 @@ export const load: LayoutLoad = async ({ params, depends, parent }) => {
|
||||
loadFailedInvoices(params.organization);
|
||||
}
|
||||
}
|
||||
|
||||
if (prefs[resolvedProfile.organizationPrefKey] !== params.organization) {
|
||||
const newPrefs = {
|
||||
...prefs,
|
||||
@@ -46,8 +50,13 @@ export const load: LayoutLoad = async ({ params, depends, parent }) => {
|
||||
sdk.forConsole.account.updatePrefs({ prefs: newPrefs });
|
||||
}
|
||||
|
||||
const [organization, members, countryList, locale] = await Promise.all([
|
||||
sdk.forConsole.teams.get({ teamId: params.organization }) as Promise<Organization>,
|
||||
// fetch org only if we haven't already fetched it for platform check
|
||||
const orgPromise: Promise<Organization> = requestedOrg
|
||||
? Promise.resolve(requestedOrg)
|
||||
: (sdk.forConsole.teams.get({ teamId: params.organization }) as Promise<Organization>);
|
||||
|
||||
const [org, members, countryList, locale] = await Promise.all([
|
||||
orgPromise,
|
||||
sdk.forConsole.teams.listMemberships({ teamId: params.organization }),
|
||||
sdk.forConsole.locale.listCountries(),
|
||||
sdk.forConsole.locale.get(),
|
||||
@@ -58,7 +67,7 @@ export const load: LayoutLoad = async ({ params, depends, parent }) => {
|
||||
return {
|
||||
header: Header,
|
||||
breadcrumbs: Breadcrumbs,
|
||||
organization,
|
||||
organization: org,
|
||||
currentPlan,
|
||||
members,
|
||||
roles,
|
||||
@@ -91,3 +100,67 @@ function loadFailedInvoices(teamId: string) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function checkPlatformAndRedirect(
|
||||
params: { organization: string },
|
||||
organizations: { teams: Organization[] },
|
||||
prefs: Record<string, string>
|
||||
): Promise<Organization | null> {
|
||||
// check if preloaded
|
||||
let requestedOrg = organizations.teams.find((team) => team.$id === params.organization);
|
||||
|
||||
// not found, load!
|
||||
if (!requestedOrg) {
|
||||
try {
|
||||
requestedOrg = (await sdk.forConsole.teams.get({
|
||||
teamId: params.organization
|
||||
})) as Organization;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (requestedOrg && requestedOrg.platform !== resolvedProfile.organizationPlatform) {
|
||||
const orgIdInPrefs = prefs[resolvedProfile.organizationPrefKey];
|
||||
|
||||
// grab the first org with matching platform
|
||||
const samePlatformOrganization = organizations.teams.find(
|
||||
(team) => team.platform === resolvedProfile.organizationPlatform
|
||||
);
|
||||
|
||||
// exists, lets redirect!
|
||||
if (samePlatformOrganization) {
|
||||
redirect(
|
||||
303,
|
||||
resolve('/(console)/organization-[organization]', {
|
||||
organization: samePlatformOrganization.$id
|
||||
})
|
||||
);
|
||||
}
|
||||
// not in list, check prefs
|
||||
else if (orgIdInPrefs) {
|
||||
try {
|
||||
// check if exists and is valid
|
||||
const orgFromPrefs = (await sdk.forConsole.teams.get({
|
||||
teamId: orgIdInPrefs
|
||||
})) as Organization;
|
||||
|
||||
// exists and is valid, redirect
|
||||
redirect(
|
||||
303,
|
||||
resolve('/(console)/organization-[organization]', {
|
||||
organization: orgFromPrefs.$id
|
||||
})
|
||||
);
|
||||
} catch (e) {
|
||||
redirect(303, resolve('/(console)'));
|
||||
}
|
||||
} else {
|
||||
redirect(303, resolve('/(console)'));
|
||||
}
|
||||
}
|
||||
|
||||
// send the org,
|
||||
// if already in the full list so we don't have to make another API request.
|
||||
return requestedOrg;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user