Skip to content

Refactor: URL management for project route #1933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 2 additions & 7 deletions src/lib/components/backupRestoreBox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import { goto, invalidate } from '$app/navigation';
import { page } from '$app/state';
import { addNotification } from '$lib/stores/notifications';
import { base } from '$app/paths';
import { getProjectId } from '$lib/helpers/project';
import { getProjectId, getProjectRoute } from '$lib/helpers/project';
import { toLocaleDate } from '$lib/helpers/date';
import { Typography } from '@appwrite.io/pink-svelte';

Expand All @@ -34,8 +33,6 @@

function showRestoreNotification(newDatabaseId: string, newDatabaseName: string) {
if (newDatabaseId && newDatabaseName && lastDatabaseRestorationId !== newDatabaseId) {
const region = page.params.region;
const project = page.params.project;
lastDatabaseRestorationId = newDatabaseId;

addNotification({
Expand All @@ -46,9 +43,7 @@
{
name: 'View restored data',
method: () => {
goto(
`${base}/project-${region}-${project}/databases/database-${newDatabaseId}`
);
goto(getProjectRoute(`/databases/database-${newDatabaseId}`));
}
}
]
Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/breadcrumbs.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import { newOrgModal } from '$lib/stores/organization';
import { Click, trackEvent } from '$lib/actions/analytics';
import { page } from '$app/stores';
import { getProjectRoute } from '$lib/helpers/project';

type Project = {
name: string;
Expand Down Expand Up @@ -141,7 +142,7 @@
if (index < 4) {
return {
name: project.name,
href: `${base}/project-${project.region}-${project.$id}/overview`
href: getProjectRoute(project, '/overview')
};
} else if (index === 4) {
return {
Expand Down Expand Up @@ -317,8 +318,7 @@
{#if index < 4}
<div use:melt={$itemProjects}>
<ActionMenu.Root>
<ActionMenu.Item.Anchor
href={`${base}/project-${project.region}-${project.$id}`}>
<ActionMenu.Item.Anchor href={getProjectRoute(project)}>
{project.name}
</ActionMenu.Item.Anchor>
</ActionMenu.Root>
Expand Down
5 changes: 2 additions & 3 deletions src/lib/components/csvImportBox.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<script lang="ts">
import { onMount } from 'svelte';
import { base } from '$app/paths';
import { page } from '$app/state';
import { sdk } from '$lib/stores/sdk';
import { Dependencies } from '$lib/constants';
import { goto, invalidate } from '$app/navigation';
import { getProjectId } from '$lib/helpers/project';
import { getProjectId, getProjectRoute } from '$lib/helpers/project';
import { writable, type Writable } from 'svelte/store';
import { addNotification } from '$lib/stores/notifications';
import { Layout, Typography } from '@appwrite.io/pink-svelte';
Expand All @@ -32,7 +31,7 @@
importData: Payload
) {
await invalidate(Dependencies.DOCUMENTS);
const url = `${base}/project-${page.params.region}-${page.params.project}/databases/database-${databaseId}/collection-${collectionId}`;
const url = getProjectRoute(`/databases/database-${databaseId}/collection-${collectionId}`);

// extract clean message from nested backend error.
const match = importData.errors.join('').match(/message: '(.*)' Message:/i);
Expand Down
6 changes: 2 additions & 4 deletions src/lib/components/filePicker.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script lang="ts">
import { EmptySearch, Id } from '.';
import { onMount } from 'svelte';
import { base } from '$app/paths';
import { page } from '$app/state';
import { sdk } from '$lib/stores/sdk';
import { goto } from '$app/navigation';
Expand Down Expand Up @@ -30,6 +29,7 @@
import { isSmallViewport } from '$lib/stores/viewport';
import { IconViewGrid, IconViewList } from '@appwrite.io/pink-icons-svelte';
import { showCreateBucket } from '$routes/(console)/project-[region]-[project]/storage/+page.svelte';
import { getProjectRoute } from '$lib/helpers/project';

export let show: boolean;
export let mimeTypeQuery: string = 'image/';
Expand Down Expand Up @@ -531,9 +531,7 @@
<Button
secondary
on:click={async () => {
await goto(
`${base}/project-${page.params.region}-${page.params.project}/storage`
);
await goto(getProjectRoute('/storage'));
$showCreateBucket = true;
}}>
Create bucket
Expand Down
8 changes: 3 additions & 5 deletions src/lib/components/logs/loggingAlert.svelte
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
<script lang="ts">
import { base } from '$app/paths';
import { page } from '$app/state';
import { Link } from '$lib/elements';
import { Alert } from '@appwrite.io/pink-svelte';
import { getProjectRoute } from '$lib/helpers/project';

export let product: 'site' | 'function';
</script>

{#if product === 'site'}
<Alert.Inline status="info" title="Your logs are disabled">
To view logs and errors, enable them in your
<Link
href={`${base}/project-${page.params.region}-${page.params.project}/sites/site-${page.params.site}/settings`}>
<Link href={getProjectRoute(`/sites/site-${page.params.site}/settings`)}>
site settings</Link
>.
</Alert.Inline>
{:else if product === 'function'}
<Alert.Inline status="info" title="Your execution logs are disabled">
To view execution logs and errors, enable them in your
<Link
href={`${base}/project-${page.params.region}-${page.params.project}/functions/function-${page.params.function}/settings`}>
<Link href={getProjectRoute(`/functions/function-${page.params.function}/settings`)}>
function settings</Link
>.
</Alert.Inline>
Expand Down
5 changes: 3 additions & 2 deletions src/lib/components/navbar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import { user } from '$lib/stores/user';
import { Click, trackEvent } from '$lib/actions/analytics';
import type { HTMLAttributes } from 'svelte/elements';
import { getProjectRoute } from '$lib/helpers/project';

let showSupport = false;

Expand Down Expand Up @@ -135,9 +136,9 @@
{#if selectedProject && selectedProject.pingCount === 0}
<div class="only-desktop" style:margin-inline-start="-16px">
<Button.Anchor
href={`${base}/project-${selectedProject.region}-${selectedProject.$id}/get-started`}
size="xs"
variant="secondary"
size="xs">Connect</Button.Anchor>
href={getProjectRoute(selectedProject, '/get-started')}>Connect</Button.Anchor>
</div>
{/if}
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/permissions/row.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
} from '@appwrite.io/pink-svelte';
import Avatar from '../avatar.svelte';
import { IconAnonymous, IconExternalLink, IconMinusSm } from '@appwrite.io/pink-icons-svelte';
import { base } from '$app/paths';
import { page } from '$app/state';
import { getProjectRoute } from '$lib/helpers/project';

export let role: string;

Expand Down Expand Up @@ -88,7 +88,7 @@
{/if}
<div>
<Button.Anchor
href={`${base}/project-${page.params.region}-${page.params.project}/auth/user-${data?.$id}`}
href={getProjectRoute(`/auth/user-${data?.$id}`)}
size="xs"
target="_blank"
variant="secondary">
Expand All @@ -100,7 +100,7 @@
<Typography.Text>Members: {data?.total}</Typography.Text>
<div>
<Button.Anchor
href={`${base}/project-${page.params.region}-${page.params.project}/auth/teams/team-${data?.$id}`}
href={getProjectRoute(`/auth/teams/team-${data?.$id}`)}
size="s"
target="_blank"
variant="secondary">
Expand Down
47 changes: 47 additions & 0 deletions src/lib/helpers/project.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { base } from '$app/paths';
import { page } from '$app/state';
import { get } from 'svelte/store';
import { sdk } from '$lib/stores/sdk';
import type { Models } from '@appwrite.io/console';
import { projectRegion } from '$routes/(console)/project-[region]-[project]/store';

/**
Expand Down Expand Up @@ -42,3 +44,48 @@ export function getProjectEndpoint(): string {

return currentProjectRegion ? `${protocol}//${currentProjectRegion.$id}.${hostname}/v1` : href;
}

/**
* A minimal project shape with -
* 1. `$id` and
* 2. optional `region`.
*
* Compatible with full `Models.Project`.
*/
type CompatibleProjectInstance =
| Models.Project
| {
$id: string;
region?: string;
};

/**
* Generates the project route URL, optionally using a custom project and/or path.
* Falls back to the current route's project and region if not provided.
*
* @param projectOrPath Either a project object or a path string (e.g., "/databases").
* @param maybePath Optional path string if the first argument is a project.
*
* @returns Project route URL (e.g., "/project-fra-console/databases").
*/
export function getProjectRoute(
projectOrPath?: string | CompatibleProjectInstance,
maybePath?: string
): string {
let withPath: string | undefined;
let projectInstance: CompatibleProjectInstance | undefined;

if (typeof projectOrPath === 'string') {
withPath = projectOrPath;
projectInstance = undefined;
} else {
withPath = maybePath;
projectInstance = projectOrPath;
}

const projectId = projectInstance?.$id ?? page?.params?.project;
const projectRegion = projectInstance?.region ?? page?.params?.region ?? 'default';
const suffix = withPath?.trim() ? (withPath.startsWith('/') ? withPath : `/${withPath}`) : '';

return `${base}/project-${projectRegion}-${projectId}${suffix}`;
}
6 changes: 2 additions & 4 deletions src/routes/(console)/(migration-wizard)/wizard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import { regions } from '$lib/stores/organization';
import { addNotification } from '$lib/stores/notifications';
import { goto, invalidate } from '$app/navigation';
import { base } from '$app/paths';
import { migrationFormToResources } from '$lib/stores/migration';
import { EyebrowHeading } from '$lib/components';
import {
Expand All @@ -35,6 +34,7 @@
} from '@appwrite.io/pink-icons-svelte';
import { Dependencies } from '$lib/constants';
import { filterRegions } from '$lib/helpers/regions';
import { getProjectRoute } from '$lib/helpers/project';

const onExit = () => {
formData.reset();
Expand Down Expand Up @@ -123,9 +123,7 @@
});
onExit();
await invalidate(Dependencies.PROJECTS);
await goto(
`${base}/project-${currentSelectedProject.region}-${currentSelectedProject.$id}/settings/migrations`
);
await goto(`${getProjectRoute(currentSelectedProject, '/settings/migrations')}`);
} catch (error) {
addNotification({
type: 'error',
Expand Down
15 changes: 7 additions & 8 deletions src/routes/(console)/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
} from '@appwrite.io/pink-icons-svelte';
import type { LayoutData } from './$types';
import type { NavbarProject } from '$lib/components/navbar.svelte';
import { getProjectRoute } from '$lib/helpers/project';

function kebabToSentenceCase(str: string) {
return str
Expand Down Expand Up @@ -175,9 +176,7 @@
({
label: kebabToSentenceCase(heading),
async callback() {
await goto(
`${base}/project-${$project.region}-${$project.$id}/auth/security#${heading}`
);
await goto(getProjectRoute($project, `/auth/security#${heading}`));
scrollBy({ top: -100 });
},
disabled: !$project?.$id,
Expand All @@ -191,7 +190,7 @@

keys: isOnSettingsLayout ? ['g', 'o'] : undefined,
callback: () => {
goto(`${base}/project-${$project.region}-${$project.$id}/settings`);
goto(getProjectRoute('/settings'));
},
disabled:
!$project?.$id || (isOnSettingsLayout && page.url.pathname.endsWith('settings')),
Expand All @@ -203,7 +202,7 @@

keys: isOnSettingsLayout ? ['g', 'd'] : undefined,
callback: () => {
goto(`${base}/project-${$project.region}-${$project.$id}/settings/domains`);
goto(getProjectRoute('/settings/domains'));
},
disabled:
!$project?.$id || (isOnSettingsLayout && page.url.pathname.includes('domains')),
Expand All @@ -214,7 +213,7 @@
label: 'Go to webhooks',
keys: isOnSettingsLayout ? ['g', 'w'] : undefined,
callback: () => {
goto(`${base}/project-${$project.region}-${$project.$id}/settings/webhooks`);
goto(getProjectRoute('/settings/webhooks'));
},
disabled:
!$project?.$id || (isOnSettingsLayout && page.url.pathname.includes('webhooks')),
Expand All @@ -226,7 +225,7 @@
label: 'Go to migrations',
keys: isOnSettingsLayout ? ['g', 'm'] : undefined,
callback: () => {
goto(`${base}/project-${$project.region}-${$project.$id}/settings/migrations`);
goto(getProjectRoute('/settings/migrations'));
},
disabled:
!$project?.$id || (isOnSettingsLayout && page.url.pathname.includes('migrations')),
Expand All @@ -238,7 +237,7 @@
label: 'Go to SMTP settings',
keys: isOnSettingsLayout ? ['g', 's'] : undefined,
callback: () => {
goto(`${base}/project-${$project.region}-${$project.$id}/settings/smtp`);
goto(getProjectRoute('/settings/smtp'));
},
disabled: !$project?.$id || (isOnSettingsLayout && page.url.pathname.includes('smtp')),
group: isOnSettingsLayout ? 'navigation' : 'settings',
Expand Down
4 changes: 2 additions & 2 deletions src/routes/(console)/onboarding/create-project/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import { Dependencies } from '$lib/constants';
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
import { goto, invalidate } from '$app/navigation';
import { base } from '$app/paths';
import { addNotification } from '$lib/stores/notifications';
import CreateProject from '$lib/layout/createProject.svelte';
import { loadAvailableRegions } from '$routes/(console)/regions';
import { regions as regionsStore } from '$lib/stores/organization';
import { getProjectRoute } from '$lib/helpers/project';

let isLoading = false;
let id: string;
Expand Down Expand Up @@ -40,7 +40,7 @@

setTimeout(async () => {
await invalidate(Dependencies.ACCOUNT);
goto(`${base}/project-${project.region ?? 'default'}-${project.$id}`);
goto(getProjectRoute(project));
}, 3000);
} catch (e) {
trackError(e, Submit.ProjectCreate);
Expand Down
6 changes: 3 additions & 3 deletions src/routes/(console)/organization-[organization]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script lang="ts">
import { base } from '$app/paths';
import { Button } from '$lib/elements/forms';
import { Container } from '$lib/layout';
import CreateProject from './createProject.svelte';
Expand Down Expand Up @@ -39,6 +38,7 @@
import { getPlatformInfo } from '$lib/helpers/platform';
import CreateProjectCloud from './createProjectCloud.svelte';
import { regions as regionsStore } from '$lib/stores/organization';
import { getProjectRoute } from '$lib/helpers/project';

export let data;

Expand Down Expand Up @@ -104,7 +104,7 @@
trackEvent(Submit.ProjectCreate, {
teamId: page.params.organization
});
await goto(`${base}/project-${project.region}-${project.$id}/settings/migrations`);
await goto(getProjectRoute(project, '/settings/migrations'));
openImportWizard();
loading.set(false);
} catch (e) {
Expand Down Expand Up @@ -155,7 +155,7 @@
{@const platforms = filterPlatforms(
project.platforms.map((platform) => getPlatformInfo(platform.type))
)}
<GridItem1 href={`${base}/project-${project.region}-${project.$id}`}>
<GridItem1 href={getProjectRoute(project)}>
<svelte:fragment slot="eyebrow">
{project?.platforms?.length ? project?.platforms?.length : 'No'} apps
</svelte:fragment>
Expand Down
Loading
Loading