Skip to content

Commit 94de9d9

Browse files
committed
fix: display names on relationship component.
1 parent e22527f commit 94de9d9

File tree

4 files changed

+76
-50
lines changed

4 files changed

+76
-50
lines changed

src/lib/components/columnSelector.svelte

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
if (isCustomCollection) {
4747
const shownColumns = preferences.getCustomCollectionColumns(collectionId);
4848
49-
columns.set(
50-
$columns.map((column) => {
49+
columns.update((n) =>
50+
n.map((column) => {
5151
column.hide = shownColumns?.includes(column.id) ?? false;
5252
return column;
5353
})
@@ -56,24 +56,15 @@
5656
const prefs = preferences.get(page.route);
5757
5858
if (prefs?.columns) {
59-
columns.set(
60-
$columns.map((column) => {
59+
columns.update((n) =>
60+
n.map((column) => {
6161
column.hide = prefs.columns?.includes(column.id) ?? false;
6262
return column;
6363
})
6464
);
6565
}
6666
}
6767
68-
columns.subscribe((ctx) => {
69-
const columns = ctx.filter((n) => n.hide === true).map((n) => n.id);
70-
if (isCustomCollection) {
71-
preferences.setCustomCollectionColumns(collectionId, columns);
72-
} else {
73-
preferences.setColumns(columns);
74-
}
75-
});
76-
7768
calcMaxHeight();
7869
});
7970

src/routes/(console)/project-[region]-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/relationship.svelte

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,24 @@
116116
$: totalCount = relatedList?.length ?? 0;
117117
118118
$: options =
119-
documentList?.documents?.map((n) => {
120-
const data = displayNames.filter((name) => name !== '$id').map((name) => n?.[name]);
121-
return { value: n.$id, label: n.$id, data };
119+
documentList?.documents?.map((document) => {
120+
const names = displayNames.filter((name) => name !== '$id');
121+
const values = names
122+
.map((name) => document?.[name])
123+
// always supposed to be a string but just being a bit safe here
124+
.filter((value) => value != null && typeof value === 'string' && value !== '');
125+
126+
const label = !values.length
127+
? document.$id
128+
: // values are in `$id (a | b)` format
129+
// previously used to have a `$id a | b`.
130+
`${document.$id} (${values.join(' | ')})`;
131+
132+
return {
133+
label,
134+
value: document.$id,
135+
data: names.map((name) => document?.[name])
136+
};
122137
}) ?? [];
123138
124139
$: hasItems = totalCount > 0;

src/routes/(console)/project-[region]-[project]/databases/database-[database]/collection-[collection]/relationshipsModal.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
data = null;
2323
selectedRelationship = null;
2424
}
25+
26+
$: tableColumns = [{ id: '$id', width: 200 }, ...args.map((id) => ({ id }))];
2527
</script>
2628

2729
<Modal bind:show title={selectedRelationship?.key} hideFooter>
2830
{#if data?.length}
2931
<Paginator items={data} {limit}>
3032
{#snippet children(paginatedItems: typeof data)}
31-
<Table.Root
32-
let:root
33-
columns={[{ id: '$id', width: 150 }, ...args.map((id) => ({ id }))]}>
33+
<Table.Root let:root columns={tableColumns}>
3434
<svelte:fragment slot="header" let:root>
3535
<Table.Header.Cell column="$id" {root}>Document ID</Table.Header.Cell>
3636
{#if args?.length}

src/routes/(console)/project-[region]-[project]/databases/database-[database]/collection-[collection]/settings/displayName.svelte

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
import { organization } from '$lib/stores/organization';
1616
1717
const collectionId = page.params.collection;
18-
let names: string[] = [...(preferences.getDisplayNames()?.[collectionId] ?? [])];
18+
19+
let names: string[] = $state(
20+
// edge case with `$id`? probably got saved during local tests...
21+
[...(preferences.getDisplayNames()?.[collectionId] ?? [])].filter((name) => name !== '$id')
22+
);
1923
2024
async function updateDisplayName() {
2125
try {
@@ -36,23 +40,32 @@
3640
}
3741
}
3842
39-
$: options = ($attributes as Models.AttributeString[])
40-
.filter(
41-
(attr) =>
42-
attr.type === 'string' && !attr?.array && !names?.some((name) => name === attr.key)
43-
)
44-
.map((attr) => {
45-
return {
43+
function getValidAttributes() {
44+
return ($attributes as Models.AttributeString[]).filter(
45+
(attr) => attr.type === 'string' && !attr?.array
46+
);
47+
}
48+
49+
function getOptions(index: number) {
50+
const current = names?.[index];
51+
return getValidAttributes()
52+
.filter((attr) => !names?.includes(attr.key) || attr.key === current)
53+
.map((attr) => ({
4654
value: attr.key,
4755
label: attr.key
48-
};
49-
});
56+
}));
57+
}
5058
51-
$: addAttributeDisabled = names?.length >= 5 || (names?.length && !names[names?.length - 1]);
59+
const addAttributeDisabled = $derived(
60+
names?.length >= 5 || (names?.length && !names[names?.length - 1])
61+
);
5262
53-
$: updateBtnDisabled =
63+
const updateBtnDisabled = $derived(
5464
!symmetricDifference(names, preferences.getDisplayNames()?.[collectionId] ?? [])?.length ||
55-
(names?.length && !last(names));
65+
(names?.length && !last(names))
66+
);
67+
68+
const hasExhaustedOptions = $derived(getValidAttributes().length === names?.length);
5669
</script>
5770

5871
<Form onSubmit={updateDisplayName}>
@@ -72,38 +85,45 @@
7285
</span>
7386
</Layout.Stack>
7487
{#if names?.length}
75-
{#each names as name, i}
88+
{#each names as name, index}
7689
<Layout.Stack direction="row" gap="xxs">
90+
{@const options = getOptions(index)}
91+
{@const disabled =
92+
(!!names[index] && names.length > index + 1) || hasExhaustedOptions}
7793
<InputSelect
7894
id={name}
79-
placeholder="Select attribute"
80-
bind:value={names[i]}
81-
disabled={!!names[i] && names.length > i + 1}
82-
{options} />
95+
{options}
96+
{disabled}
97+
bind:value={names[index]}
98+
placeholder="Select attribute" />
8399
<Button
84100
icon
85101
extraCompact
86102
on:click={() => {
87-
names.splice(i, 1);
103+
names.splice(index, 1);
88104
names = names;
89105
}}>
90106
<Icon icon={IconX} />
91107
</Button>
92108
</Layout.Stack>
93109
{/each}
94110
{/if}
95-
<div>
96-
<Button
97-
compact
98-
disabled={addAttributeDisabled}
99-
on:click={() => {
100-
names[names.length] = null;
101-
names = names;
102-
}}>
103-
<Icon icon={IconPlus} slot="start" size="s" />
104-
Add attribute
105-
</Button>
106-
</div>
111+
112+
<!-- show only when options don't have all the attributes -->
113+
{#if !hasExhaustedOptions}
114+
<div>
115+
<Button
116+
compact
117+
disabled={addAttributeDisabled}
118+
on:click={() => {
119+
names[names.length] = null;
120+
names = names;
121+
}}>
122+
<Icon icon={IconPlus} slot="start" size="s" />
123+
Add attribute
124+
</Button>
125+
</div>
126+
{/if}
107127
</Layout.Stack>
108128
</svelte:fragment>
109129

0 commit comments

Comments
 (0)