From e8d4720162b3d48e994ef498e72a8f12548110d2 Mon Sep 17 00:00:00 2001 From: Darshan Date: Thu, 28 Aug 2025 18:33:11 +0530 Subject: [PATCH] fix: unlinking of single items. --- .../rows/columns/types/relationship.svelte | 140 ++++++++++++------ 1 file changed, 94 insertions(+), 46 deletions(-) diff --git a/src/routes/(console)/project-[region]-[project]/databases/database-[database]/table-[table]/rows/columns/types/relationship.svelte b/src/routes/(console)/project-[region]-[project]/databases/database-[database]/table-[table]/rows/columns/types/relationship.svelte index 910e37e68..10da4abbf 100644 --- a/src/routes/(console)/project-[region]-[project]/databases/database-[database]/table-[table]/rows/columns/types/relationship.svelte +++ b/src/routes/(console)/project-[region]-[project]/databases/database-[database]/table-[table]/rows/columns/types/relationship.svelte @@ -4,11 +4,20 @@ import { preferences } from '$lib/stores/preferences'; import { sdk } from '$lib/stores/sdk'; import { Query, type Models } from '@appwrite.io/console'; - import { onMount } from 'svelte'; + import { type ComponentType, onMount } from 'svelte'; import { isRelationshipToMany } from '../../store'; import { IconPlus, IconX } from '@appwrite.io/pink-icons-svelte'; import { Icon, Layout, Typography } from '@appwrite.io/pink-svelte'; + type SelectOption = { + label: string; + value: string | null; + data?: string[]; + disabled?: boolean; + badge?: string; + leadingIcon?: ComponentType; + }; + export let id: string; export let label: string; export let limited: boolean = false; @@ -110,6 +119,58 @@ showInput = false; } + function generateOptions( + loading: boolean, + rows: Models.Row[] | undefined, + column: Models.ColumnRelationship, + editing: boolean + ): SelectOption[] { + if (loading) { + return [{ label: 'Loading...', value: null, disabled: true }]; + } + + let baseOptions: SelectOption[] = + rows?.map((row) => { + const names = preferences + .getDisplayNames(column?.relatedTable) + .filter((name) => name !== '$id'); + + const values = names + .map((name) => row?.[name]) + .filter((value) => value != null && typeof value === 'string' && value !== ''); + + const displayValues = !editing + ? values + : values.map((value) => (value.length > 5 ? value.slice(0, 5) + '...' : value)); + + let label: string; + if (!values.length) { + label = row.$id; + } else { + label = `${row.$id} (${displayValues.join(' | ')})`; + } + + return { + label, + value: row.$id, + data: names.map((name) => row?.[name]) + }; + }) ?? []; + + if (!isRelationshipToMany(column) && baseOptions.length && value && limited) { + baseOptions = [ + { + value: null, + badge: 'Unlink', + label: 'Remove relationship' + }, + ...baseOptions + ]; + } + + return baseOptions; + } + // Reactive statements $: getRows(search).then((res) => (rowList = res)); @@ -122,42 +183,7 @@ $: totalCount = relatedList?.length ?? 0; - $: options = loadingRelationships - ? [ - { - label: 'Loading...', - value: null, - disabled: true - } - ] - : (rowList?.rows?.map((row) => { - const names = preferences - .getDisplayNames(column?.relatedTable) - .filter((name) => name !== '$id'); - - const values = names - .map((name) => row?.[name]) - // always supposed to be a string but just being a bit safe here - .filter((value) => value != null && typeof value === 'string' && value !== ''); - - const displayValues = !editing - ? values - : // on non edit routes like create, there's enough space! - values.map((value) => (value.length > 5 ? value.slice(0, 5) + '...' : value)); - - let label: string; - if (!values.length) { - label = row.$id; - } else { - label = `${row.$id} (${displayValues.join(' | ')})`; - } - - return { - label, - value: row.$id, - data: names.map((name) => row?.[name]) - }; - }) ?? []); + $: options = generateOptions(loadingRelationships, rowList?.rows, column, editing); $: hasItems = totalCount > 0; @@ -297,13 +323,35 @@ {:else} - (value = rowList.rows.find((row) => row.$id === singleRel))} /> + + { + if (singleRel === null) { + value = null; + } else { + value = rowList.rows.find((row) => row.$id === singleRel); + } + }} /> + + {#if !limited && singleRel} +
+ +
+ {/if} +
{/if}