update: persist column ordering and widths.

This commit is contained in:
Darshan committed 2025-07-25 19:53:37 +05:30
1 parent 1308776bed
commit eb436d2359
15 files changed
+270 -82

No files matched your search

+1 -1
View File
@@ -26,7 +26,7 @@
"@appwrite.io/pink-icons": "0.25.0",
"@appwrite.io/pink-icons-svelte": "^2.0.0-RC.1",
"@appwrite.io/pink-legacy": "^1.0.3",
"@appwrite.io/pink-svelte": "https://pkg.vc/-/@appwrite/@appwrite.io/pink-svelte@c88c338",
"@appwrite.io/pink-svelte": "https://pkg.vc/-/@appwrite/@appwrite.io/pink-svelte@e0df9cb",
"@popperjs/core": "^2.11.8",
"@sentry/sveltekit": "^8.38.0",
"@stripe/stripe-js": "^3.5.0",
+5 -5
View File
@@ -24,8 +24,8 @@ importers:
specifier: ^1.0.3
version: 1.0.3
'@appwrite.io/pink-svelte':
specifier: https://pkg.vc/-/@appwrite/@appwrite.io/pink-svelte@c88c338
version: https://pkg.vc/-/@appwrite/@appwrite.io/pink-svelte@c88c338(svelte@5.25.3)
specifier: https://pkg.vc/-/@appwrite/@appwrite.io/pink-svelte@e0df9cb
version: https://pkg.vc/-/@appwrite/@appwrite.io/pink-svelte@e0df9cb(svelte@5.25.3)
'@popperjs/core':
specifier: ^2.11.8
version: 2.11.8
@@ -281,8 +281,8 @@ packages:
'@appwrite.io/pink-legacy@1.0.3':
resolution: {integrity: sha512-GGde5fmPhs+s6/3aFeMPc/kKADG/gTFkYQSy6oBN8pK0y0XNCLrZZgBv+EBbdhwdtqVEWXa0X85Mv9w7jcIlwQ==}
'@appwrite.io/pink-svelte@https://pkg.vc/-/@appwrite/@appwrite.io/pink-svelte@c88c338':
resolution: {tarball: https://pkg.vc/-/@appwrite/@appwrite.io/pink-svelte@c88c338}
'@appwrite.io/pink-svelte@https://pkg.vc/-/@appwrite/@appwrite.io/pink-svelte@e0df9cb':
resolution: {tarball: https://pkg.vc/-/@appwrite/@appwrite.io/pink-svelte@e0df9cb}
version: 2.0.0-RC.2
peerDependencies:
svelte: ^4.0.0
@@ -3679,7 +3679,7 @@ snapshots:
'@appwrite.io/pink-icons': 1.0.0
the-new-css-reset: 1.11.3
'@appwrite.io/pink-svelte@https://pkg.vc/-/@appwrite/@appwrite.io/pink-svelte@c88c338(svelte@5.25.3)':
'@appwrite.io/pink-svelte@https://pkg.vc/-/@appwrite/@appwrite.io/pink-svelte@e0df9cb(svelte@5.25.3)':
dependencies:
'@appwrite.io/pink-icons-svelte': 2.0.0-RC.1(svelte@5.25.3)
'@floating-ui/dom': 1.6.13
+1
View File
@@ -12,6 +12,7 @@ export type PinkColumn = {
min: number;
}
| number;
minimumWidth?: number;
hide?: boolean;
};
+44 -21
View File
@@ -17,6 +17,7 @@ type Preferences = {
type TeamPreferences = {
names?: string[];
order?: string[];
widths?: { [columnId: string]: { fixed: number | { min: number }; resized: number } };
};
type PreferencesStore = {
@@ -30,6 +31,9 @@ type PreferencesStore = {
columnOrder?: {
[key: string]: TeamPreferences['order'];
};
columnWidths?: {
[key: string]: TeamPreferences['widths'];
};
} & { hideAiDisclaimer?: boolean };
async function updateConsolePreferences(store: PreferencesStore): Promise<void> {
@@ -49,6 +53,7 @@ async function updateConsolePreferences(store: PreferencesStore): Promise<void>
function createPreferences() {
const { subscribe, set, update } = writable<PreferencesStore>({});
let preferences: PreferencesStore = {};
let teamPreferences: PreferencesStore = {};
if (browser) {
// fresh fetch.
@@ -74,6 +79,17 @@ function createPreferences() {
}
});
async function loadTeamPreferences(orgId: string) {
try {
const teamPrefs = await sdk.forConsole.teams.getPrefs(orgId);
teamPreferences = teamPrefs ?? {};
return teamPreferences;
} catch (error) {
teamPreferences = {};
return teamPreferences;
}
}
/**
* Update the local store and then synchronizes them on user prefs.
*/
@@ -162,15 +178,7 @@ function createPreferences() {
n.collections[collectionId] = Array.from(new Set(columns));
return n;
}),
loadTeamPrefs: async (id: string) => {
const teamPrefs = await sdk.forConsole.teams.getPrefs(id);
update((n) => {
n[id] = teamPrefs;
return n;
});
return teamPrefs;
},
loadTeamPrefs: loadTeamPreferences,
getDisplayNames: () => {
return preferences?.displayNames ?? {};
},
@@ -196,7 +204,7 @@ function createPreferences() {
},
getColumnOrder(collectionId: string): TeamPreferences['order'] {
return preferences?.columnOrder[collectionId] ?? [];
return teamPreferences?.columnOrder?.[collectionId] ?? [];
},
async saveColumnOrder(
@@ -204,23 +212,38 @@ function createPreferences() {
collectionId: string,
columnIds: TeamPreferences['order']
) {
let teamPrefs: Models.Preferences;
if (!teamPreferences.columnOrder) {
teamPreferences.columnOrder = {};
}
await updateAndSync((n) => {
if (!n?.columnOrder) {
n ??= {};
n.columnOrder ??= {};
}
teamPreferences.columnOrder[collectionId] = columnIds;
teamPrefs = n;
n.columnOrder[collectionId] = columnIds;
await sdk.forConsole.teams.updatePrefs(orgId, teamPreferences);
},
return n;
});
getColumnWidths(collectionId: string): TeamPreferences['widths'] {
return teamPreferences?.columnWidths?.[collectionId] ?? {};
},
await sdk.forConsole.teams.updatePrefs(orgId, teamPrefs);
async saveColumnWidths(
orgId: string,
collectionId: string,
width: TeamPreferences['widths']
) {
if (!teamPreferences.columnWidths) {
teamPreferences.columnWidths = {};
}
teamPreferences.columnWidths[collectionId] = {
...teamPreferences.columnWidths[collectionId],
...width
};
await sdk.forConsole.teams.updatePrefs(orgId, teamPreferences);
}
};
}
export const preferences = createPreferences();
export type PreferencesType = ReturnType<typeof createPreferences>;
@@ -21,8 +21,11 @@
import { onMount } from 'svelte';
import {
collection,
columns,
columnsOrder,
databaseColumnSheetOptions,
databaseRowSheetOptions,
reorderItems,
showCreateAttributeSheet
} from './store';
import { addSubPanel, registerCommands, updateCommandGroupRanks } from '$lib/commandCenter';
@@ -214,17 +217,26 @@
<slot />
<SideSheet
title="Insert column"
bind:show={$showCreateAttributeSheet}
title={$showCreateAttributeSheet.title}
bind:show={$showCreateAttributeSheet.show}
submit={{
text: 'Insert',
onClick: async () => await createAttribute?.submit(),
onClick: async () => {
await createAttribute?.submit();
},
disabled: !selectedOption
}}>
<CreateAttribute
showCreate={$showCreateAttributeSheet}
bind:selectedOption
bind:this={createAttribute} />
bind:this={createAttribute}
showCreate={$showCreateAttributeSheet.show}
direction={$showCreateAttributeSheet.direction}
columns={$showCreateAttributeSheet.columns}
columnsOrder={$showCreateAttributeSheet.columnsOrder}
onColumnsReorder={(newOrder) => {
columnsOrder.set(newOrder);
columns.set(reorderItems($columns, $columnsOrder));
}} />
</SideSheet>
<SideSheet
@@ -83,7 +83,7 @@
}
}
onDestroy(() => ($showCreateAttributeSheet = false));
onDestroy(() => ($showCreateAttributeSheet.show = false));
</script>
{#key page.params.collection}
@@ -174,6 +174,7 @@
{:else}
<EmptySheet
mode="records"
customColumns={$columns}
showActions={$canWriteDocuments}
actions={{
primary: {
@@ -192,7 +193,7 @@
primary: {
text: 'Create column',
onClick: async () => {
$showCreateAttributeSheet = true;
$showCreateAttributeSheet.show = true;
}
}
}} />
@@ -139,7 +139,7 @@
columnsOrder = preferences.getColumnOrder(collectionId);
});
onDestroy(() => ($showCreateAttributeSheet = false));
onDestroy(() => ($showCreateAttributeSheet.show = false));
</script>
<Container expanded style="background: var(--bgcolor-neutral-primary)">
@@ -150,7 +150,7 @@
secondary
disabled={$isCsvImportInProgress}
on:click={() => {
$showCreateAttributeSheet = true;
$showCreateAttributeSheet.show = true;
}}
event="create_attribute">
<Icon icon={IconPlus} slot="start" size="s" />
@@ -352,7 +352,7 @@
showActions={$canWriteCollections}
actions={{
primary: {
onClick: () => ($showCreateAttributeSheet = true)
onClick: () => ($showCreateAttributeSheet.show = true)
}
}} />
{/if}
@@ -1,6 +1,6 @@
<script lang="ts">
import { page } from '$app/state';
import type { Attributes } from './store';
import { type Attributes, type ColumnDirection } from './store';
import { invalidate } from '$app/navigation';
import { Dependencies } from '$lib/constants';
import { Layout } from '@appwrite.io/pink-svelte';
@@ -8,13 +8,25 @@
import { addNotification } from '$lib/stores/notifications';
import { Submit, trackEvent, trackError } from '$lib/actions/analytics';
import { option, attributeOptions, type Option } from './attributes/store';
import type { Column } from '$lib/helpers/types';
import { preferences } from '$lib/stores/preferences';
let {
direction = null,
showCreate = false,
selectedOption = $bindable('String')
columns = $bindable(null),
columnId = $bindable(null),
columnsOrder = $bindable(null),
selectedOption = $bindable('String'),
onColumnsReorder = null
}: {
showCreate: boolean;
columnId?: string;
columns?: Column[];
columnsOrder?: string[];
direction: ColumnDirection;
selectedOption: Option['name'];
onColumnsReorder?: (newOrder: string[]) => void;
} = $props();
const databaseId = page.params.database;
@@ -31,9 +43,45 @@
attributeOptions.find((option) => option.name === selectedOption).component
);
function insertColumnInOrder() {
if (!direction || !direction.neighbour || !key) return;
const currentOrder = columnsOrder?.length
? columnsOrder
: columns?.map((col) => col.id) || [];
const neighbourIndex = currentOrder.indexOf(direction.neighbour);
let newOrder: string[];
if (neighbourIndex === -1) {
newOrder = [...currentOrder, key];
} else {
const insertIndex = direction.to === 'left' ? neighbourIndex : neighbourIndex + 1;
newOrder = [
...currentOrder.slice(0, insertIndex),
key,
...currentOrder.slice(insertIndex)
];
}
preferences.saveColumnOrder(
page.data.organization.$id ?? page.data.project.teamId,
collectionId,
newOrder
);
onColumnsReorder?.(newOrder);
return newOrder;
}
export async function submit() {
try {
await $option.create(databaseId, collectionId, key, data);
columnId = key;
if (direction) {
insertColumnInOrder();
}
await invalidate(Dependencies.COLLECTION);
addNotification({
@@ -81,7 +81,7 @@
}
});
onDestroy(() => ($showCreateAttributeSheet = false));
onDestroy(() => ($showCreateAttributeSheet.show = false));
</script>
<Container expanded style="background: var(--bgcolor-neutral-primary)">
@@ -202,7 +202,7 @@
primary: {
text: 'Create columns',
onClick: async () => {
$showCreateAttributeSheet = true;
$showCreateAttributeSheet.show = true;
}
}
}} />
@@ -1,5 +1,6 @@
<script lang="ts">
import {
Badge,
Button,
Icon,
Layout,
@@ -24,6 +25,7 @@
export let mode: Mode;
export let showActions: boolean = true;
export let customColumns: Column[] = [];
export let title: string | undefined = undefined;
export let actions:
| {
@@ -41,13 +43,14 @@
width: 180,
draggable: false,
resizable: false,
isPrimary: true,
icon: IconFingerPrint
},
...middle,
{
id: 'actions',
title: '',
width: 555 * 1.5,
width: customColumns.length ? 555 : 555 * 1.5,
draggable: false,
resizable: false,
type: 'string',
@@ -65,6 +68,13 @@
const columnsMap: ColumnsMap = {
records: makeColumns(
// TODO: improve.
...customColumns.map((col) => {
col.width = { min: 180 };
col.draggable = false;
col.resizable = false;
return col;
}),
{
id: '$createdAt',
title: 'Created',
@@ -84,6 +94,7 @@
icon: IconCalendar
}
),
// TODO: fixed, 3 columns
columns: makeColumns(
{
id: 'indexed',
@@ -104,6 +115,7 @@
isAction: false
}
),
// TODO: fixed, 3 columns
indexes: makeColumns(
{
id: 'type',
@@ -151,6 +163,11 @@
alignItems="center"
alignContent="center">
{column.title}
{#if column.isPrimary}
<Badge content="Primary key" size="xs" variant="secondary" />
{/if}
<SortButton disabled column={column.id} />
</Layout.Stack>
{/if}
@@ -260,7 +277,7 @@
position: fixed;
@media (min-width: 1024px) {
left: 52.5%;
left: 50%;
bottom: 40%;
}
}
@@ -86,8 +86,21 @@
function handleSelect(action: HeaderCellAction | RowCellAction, hide: () => void) {
hide();
onSelect(action);
$databaseColumnSheetOptions.column = column;
if (action === 'column-left') {
$databaseColumnSheetOptions.direction = {
neighbour: column?.key,
to: 'left'
};
} else if (action === 'column-right') {
$databaseColumnSheetOptions.direction = {
neighbour: column?.key,
to: 'right'
};
}
onSelect(action);
}
function shouldShow(item: MenuItem) {
@@ -25,7 +25,9 @@
databaseRowSheetOptions,
sortState,
showCreateAttributeSheet,
reorderItems
reorderItems,
columnsOrder,
columnsWidth
} from './store';
import RelationshipsModal from './relationshipsModal.svelte';
import type { Column, ColumnType } from '$lib/helpers/types';
@@ -63,6 +65,7 @@
import SpreadsheetContainer from './layout/spreadsheet.svelte';
import type { HeaderCellAction, RowCellAction } from './sheetOptions.svelte';
import { copy } from '$lib/helpers/copy';
import { debounce } from '$lib/helpers/debounce';
export let data: PageData;
export let showRecordsCreateSheet: {
@@ -73,8 +76,6 @@
const databaseId = page.params.database;
const collectionId = page.params.collection;
let columnsOrder = [];
let displayNames = {};
let showRelationships = false;
let relationshipData: Partial<Models.Document>[];
@@ -84,18 +85,22 @@
onMount(async () => {
displayNames = preferences.getDisplayNames();
columnsOrder = preferences.getColumnOrder(collectionId);
columnsOrder.set(preferences.getColumnOrder(collectionId));
columnsWidth.set(preferences.getColumnWidths(collectionId));
});
onDestroy(() => ($showCreateAttributeSheet = false));
onDestroy(() => ($showCreateAttributeSheet.show = false));
function saveColumnsOrder(newOrder: string[]) {
preferences
.saveColumnOrder(data.organization.$id ?? data.project.teamId, collectionId, newOrder)
.then(() => {
columnsOrder = newOrder;
columns.set(reorderItems($columns, columnsOrder));
});
function saveColumnsOrder(newOrder: string[], update: boolean = true) {
if (update) {
columnsOrder.set(newOrder);
}
preferences.saveColumnOrder(
data.organization.$id ?? data.project.teamId,
collectionId,
newOrder
);
}
function formatArray(array: unknown[]) {
@@ -160,16 +165,28 @@
}
}
function getColumnWidth(
columnId: string,
defaultWidth: number | { min: number }
): number | { min: number; max?: number } {
const savedWidth = $columnsWidth[columnId];
if (!savedWidth) return defaultWidth;
return savedWidth.resized;
}
$: selected = preferences.getCustomCollectionColumns(page.params.collection);
$: {
const minimumWidth = 168;
$: if ($collection.attributes && $columnsOrder && $columnsWidth) {
const baseColumns = $collection.attributes.map((attribute) => ({
id: attribute.key,
title: attribute.key,
type: attribute.type as ColumnType,
hide: !!selected?.includes(attribute.key),
array: attribute?.array,
width: { min: 168 },
width: getColumnWidth(attribute.key, { min: minimumWidth }),
minimumWidth: minimumWidth,
draggable: true,
icon: getAppropriateIcon(attribute.type),
format: 'format' in attribute && attribute?.format === 'enum' ? attribute.format : null,
@@ -180,7 +197,8 @@
{
id: '$id',
title: 'ID',
width: 200,
width: getColumnWidth('$id', 200),
minimumWidth: 200,
draggable: false,
type: 'string',
icon: IconFingerPrint,
@@ -190,7 +208,8 @@
{
id: '$createdAt',
title: 'createdAt',
width: { min: 200 },
width: getColumnWidth('$createdAt', { min: 200 }),
minimumWidth: 200,
draggable: true,
type: 'datetime',
icon: IconCalendar,
@@ -199,7 +218,8 @@
{
id: '$updatedAt',
title: 'updatedAt',
width: { min: 200 },
width: getColumnWidth('$updatedAt', { min: 200 }),
minimumWidth: 200,
draggable: true,
type: 'datetime',
icon: IconCalendar,
@@ -208,21 +228,27 @@
{
id: 'actions',
title: '',
width: 40,
width: getColumnWidth('actions', 40),
isAction: true,
draggable: false,
type: 'string',
resizable: false,
isEditable: false
}
];
columns.set([
staticColumns[0],
...baseColumns,
staticColumns[1],
staticColumns[2],
staticColumns[3]
]);
columns.set(
reorderItems(
[
staticColumns[0],
...baseColumns,
staticColumns[1],
staticColumns[2],
staticColumns[3]
],
$columnsOrder
)
);
}
let loading = false;
@@ -274,6 +300,7 @@
async function handleColumnDelete() {
showColumnDelete = false;
try {
await sdk
.forProject(page.params.region, page.params.project)
@@ -283,12 +310,11 @@
$databaseColumnSheetOptions.column.key
);
if (columnsOrder.includes($databaseColumnSheetOptions.column.key)) {
const updatedOrder = columnsOrder.filter(
if ($columnsOrder.includes($databaseColumnSheetOptions.column.key)) {
const updatedOrder = $columnsOrder.filter(
(id) => id !== $databaseColumnSheetOptions.column.key
);
saveColumnsOrder(updatedOrder);
columnsOrder = updatedOrder;
saveColumnsOrder(updatedOrder, false);
}
trackEvent(Submit.AttributeDelete);
@@ -343,15 +369,18 @@
}
if (action === 'column-left' || action === 'column-right') {
$databaseColumnSheetOptions.show = true;
$databaseColumnSheetOptions.title = 'New column';
const { to, neighbour } = $databaseColumnSheetOptions.direction;
$showCreateAttributeSheet.show = true;
$showCreateAttributeSheet.title = `Insert column to the ${to} of ${neighbour}`;
$showCreateAttributeSheet.direction = $databaseColumnSheetOptions.direction;
$showCreateAttributeSheet.columns = $columns;
$showCreateAttributeSheet.columnsOrder = $columnsOrder;
}
if (action === 'delete') {
showColumnDelete = true;
}
$databaseColumnSheetOptions.column = null;
} else if (type === 'row') {
if (action === 'update') {
$databaseRowSheetOptions.show = true;
@@ -393,6 +422,18 @@
data.documents.documents.length >= emptyCellsLimit
? 0
: emptyCellsLimit - data.documents.documents.length;
const saveColumnsWidth = debounce((column: { columnId: string; newWidth: number }) => {
const fixedWidth =
$columns.find((col) => col.id === column.columnId)?.width ?? column.newWidth;
preferences.saveColumnWidths(data.organization.$id ?? data.project.teamId, collectionId, {
[column.columnId]: {
fixed: fixedWidth,
resized: Math.ceil(column.newWidth)
}
});
}, 1000);
</script>
<SpreadsheetContainer>
@@ -407,6 +448,9 @@
bottomActionClick={() => (showRecordsCreateSheet.show = true)}
on:columnsSwap={(order) => {
saveColumnsOrder(order.detail);
}}
on:columnsResize={(resize) => {
saveColumnsWidth(resize.detail);
}}>
<svelte:fragment slot="header" let:root>
{#each $columns as column (column.id)}
@@ -415,7 +459,7 @@
<Button.Button
icon
variant="extra-compact"
on:click={() => ($showCreateAttributeSheet = true)}>
on:click={() => ($showCreateAttributeSheet.show = true)}>
<Icon icon={IconPlus} color="--fgcolor-neutral-primary" />
</Button.Button>
</Spreadsheet.Header.Cell>
@@ -27,6 +27,10 @@ export const attributes = derived(
export const indexes = derived(page, ($page) => $page.data.collection.indexes as Models.Index[]);
export const columns = writable<Column[]>([]);
export const columnsOrder = writable<string[]>([]);
export const columnsWidth = writable<{
[columnId: string]: { fixed: number | { min: number }; resized: number };
}>();
export const isCsvImportInProgress = writable(false);
@@ -37,6 +41,7 @@ type DatabaseSheetOptions = {
isEdit?: boolean;
disableSubmit?: boolean;
submitAction?: () => Promise<void>;
direction?: ColumnDirection;
};
export const databaseColumnSheetOptions = writable<DatabaseSheetOptions>({
@@ -45,7 +50,8 @@ export const databaseColumnSheetOptions = writable<DatabaseSheetOptions>({
column: null,
isEdit: false,
disableSubmit: false,
submitAction: null
submitAction: null,
direction: null
});
export const databaseRowSheetOptions = writable<
@@ -68,7 +74,30 @@ export const sortState = writable({
direction: 'default' as SortDirection
});
export const showCreateAttributeSheet = writable(false);
export type ColumnDirection = {
neighbour: string;
to: 'left' | 'right';
};
export type CreateAttribute = {
show: boolean;
column?: string;
title: string;
direction?: ColumnDirection;
onDone?: () => void;
columns?: Column[];
columnsOrder?: string[];
};
export const showCreateAttributeSheet = writable<CreateAttribute>({
show: false,
column: null,
title: 'Insert column',
direction: null,
onDone: null,
columns: null,
columnsOrder: null
});
export function reorderItems<T extends { id: string } | { key: string }>(
items: T[],
@@ -92,7 +92,6 @@
on:input={() => {
if (!touchedId) {
touchedId = true;
console.log(touchedId);
}
}} />
@@ -86,7 +86,8 @@
</Layout.Stack>
{/if}
<Layout.Stack gap="m" alignItems="center" direction="row">
<!-- TODO: refer designs for spacing and stuff -->
<Layout.Stack gap="xs" alignItems="center" direction="row">
<Icon icon={IconPlus} size="s" />
<Button