Files
alamin-br e53694bfec fix: cap the page size of view row listings (#6489)
* fix(backend): cap page size on view row listings and the agents list

Grid, gallery and timeline row listings (including public views) and the agents
list accepted any page size, so one request could serialize a whole table.
Kanban limit/offset now reject negative values instead of erroring.

* fix(frontend): keep view row requests within the row page size limit

Grouped grid ranges can span collapsed groups and exceed the limit the backend
now enforces, so grid requests are split into chunks and merged. Gallery and
timeline request sizes are capped to the configured limit.

* fix(backend): cap kanban limits and keep default page sizes within the limit

Kanban limits were unbounded, and a default page size above a lowered
BASEROW_ROW_PAGE_SIZE_LIMIT made requests without a size fail.

* chore: add changelog entry

* fix(frontend): fetch grouped grid rows per visible section

One range from the first to the last visible section also fetched every row
of the collapsed groups in between, split into thousands of capped requests.

* fix(frontend): fill the visible gallery and timeline range under a small page size limit

Capping the request size left visible rows as placeholders when the limit was
smaller than the visible range.

* fix(frontend): keep kanban row requests within the page size limit

The kanban store always asked for 24 rows, so it stopped loading when the limit
was set lower.

* fix(premium): return no rows when kanban or calendar grouping has no group to fetch

With no matching select option or an empty date range, the empty filter selected
every row of the queryset without the view filters applied.

* fix(frontend): refresh grouped grid rows per visible section

The refresh fetched one range across all visible sections, so a large
collapsed group in between turned into thousands of capped requests.

* chore: mark the page size limit changelog entry as a breaking change

Requests above the limit now return a 400 instead of rows.
2026-10-28 15:00:55 +00:00

939 lines
28 KiB
JavaScript

import thenBy from 'thenby'
import BigNumber from 'bignumber.js'
import { getCookieName } from '@baserow/modules/core/utils/cookie'
import { escapeRegExp, isSecureURL } from '@baserow/modules/core/utils/string'
import { SearchMode } from '@baserow/modules/database/utils/search'
import { convertStringToMatchBackendTsvectorData } from '@baserow/modules/database/search/regexes'
import { DEFAULT_SORT_TYPE_KEY } from '@baserow/modules/database/constants'
export const maxPossibleOrderValue = 32767
export const DEFAULT_VIEW_ID_COOKIE_NAME = 'defaultViewId'
/**
* Wraps a field sort function so a row without the cell, e.g. one created
* through a realtime event in a public grid view, or with a null cell left by a
* field conversion, sorts like an empty cell instead of making the field type's
* sort function throw.
*/
function sortMissingCellAsEmpty(sortFunction, fieldName, fieldType, field) {
const emptyValue = fieldType.getEmptyValue(field)
const withCell = (row) =>
row[fieldName] == null ? { [fieldName]: emptyValue } : row
return (a, b) => sortFunction(withCell(a), withCell(b))
}
/**
* Generates a sort function based on the provided sortings.
*/
export function getRowSortFunction($registry, sortings, fields, groupBys = []) {
const { firstBy } = thenBy
let sortFunction = firstBy()
const groupByCount = groupBys.length
const combined = [...groupBys, ...sortings]
combined.forEach((sort, index) => {
const field = fields.find((f) => f.id === sort.field)
if (field !== undefined) {
const fieldName = `field_${field.id}`
const fieldType = $registry.get('field', field.type)
let fieldSortFunction
if (index < groupByCount) {
fieldSortFunction = fieldType.getGroupBySort(
fieldName,
sort.order,
field,
sort.type
)
} else {
const sortTypes = fieldType.getSortTypes(field)
fieldSortFunction = sortTypes[sort.type].function(
fieldName,
sort.order,
field
)
}
sortFunction = sortFunction.thenBy(
sortMissingCellAsEmpty(fieldSortFunction, fieldName, fieldType, field)
)
}
})
sortFunction = sortFunction.thenBy((a, b) =>
new BigNumber(a.order).minus(new BigNumber(b.order)).toNumber()
)
sortFunction = sortFunction.thenBy((a, b) => a.id - b.id)
return sortFunction
}
/**
* Like getRowSortFunction but uses getGroupBySort instead of getSort, so
* group-by node ordering can differ from row ordering (e.g. set-based
* ordering for multi-select fields).
*/
export function getGroupByRowSortFunction($registry, fields, groupBys) {
const { firstBy } = thenBy
let sortFunction = firstBy()
groupBys.forEach((sort) => {
const field = fields.find((f) => f.id === sort.field)
if (field !== undefined) {
const fieldName = `field_${field.id}`
const fieldType = $registry.get('field', field.type)
const fieldSortFunction = fieldType.getGroupBySort(
fieldName,
sort.order,
field,
sort.type
)
sortFunction = sortFunction.thenBy(
sortMissingCellAsEmpty(fieldSortFunction, fieldName, fieldType, field)
)
}
})
sortFunction = sortFunction.thenBy((a, b) =>
new BigNumber(a.order).minus(new BigNumber(b.order)).toNumber()
)
sortFunction = sortFunction.thenBy((a, b) => a.id - b.id)
return sortFunction
}
/**
* Generates a sort function for fields based on order and id.
*/
export function sortFieldsByOrderAndIdFunction(
fieldOptions,
primaryAlwaysFirst = false
) {
return (a, b) => {
if (primaryAlwaysFirst) {
// If primary must always be first, then first by primary.
if (a.primary > b.primary) {
return -1
} else if (a.primary < b.primary) {
return 1
}
}
const orderA = fieldOptions[a.id]
? fieldOptions[a.id].order
: maxPossibleOrderValue
const orderB = fieldOptions[b.id]
? fieldOptions[b.id].order
: maxPossibleOrderValue
// First by order.
if (orderA > orderB) {
return 1
} else if (orderA < orderB) {
return -1
}
// Then by id.
return a.id - b.id
}
}
/**
* Returns only fields that are visible (not hidden).
*/
export function filterVisibleFieldsFunction(fieldOptions) {
return (field) => {
const options = fieldOptions[field.id]
return options && !options.hidden
}
}
/**
* Returns only fields that are hidden (not visible).
*/
export function filterHiddenFieldsFunction(fieldOptions) {
const isFieldVisible = filterVisibleFieldsFunction(fieldOptions)
return (field) => !isFieldVisible(field)
}
/**
* Represents a node in a tree structure used for grouped filters.
* A group node is made of a filterType (AND or OR), a list of filters and a parent.
* If the parent is null it means that it is the root node. If the parent is not
* null it means that it is a child of the parent node, and the constructor will take care to
* add itself to the children of the parent node, so that we can later traverse the tree
* from the root node and check if a row matches the filters.
*/
export const TreeGroupNode = class {
/**
* Constructs a new TreeGroupNode.
*
* @param {string} filterType - The type of filter (e.g., 'AND' or 'OR').
* @param {TreeGroupNode} [parent=null] - The parent node of this node. Null for the root node.
* @param {number} [groupId=null] - The ID of the group this node represents. Null for the root node.
*/
constructor(filterType, parent = null, groupId = null) {
this.filterType = filterType
this.groupId = groupId
this.parent = parent
this.filters = []
this.children = []
if (parent) {
parent.children.push(this)
}
}
/**
* Finds the group node with the provided ID in the tree rooted at this node.
*
* @param {number} groupId - The ID of the group to find.
* @returns {TreeGroupNode|null} - The group node with the provided ID or null if it is not found.
*/
findNodeByGroupId(groupId) {
if (this.groupId === groupId) {
return this
}
for (const groupNode of this.children) {
const found = groupNode.findNodeByGroupId(groupId)
if (found) {
return found
}
}
return null
}
/**
* Checks if this node or any of its descendants has filters.
*
* @returns {boolean} - True if there are filters, false otherwise.
*/
hasFilters() {
return this.filters.length > 0 || this.children.some((c) => c.hasFilters())
}
/**
* Adds a filter object to this node list of filters.
*
* @param {object} filter - The filter to add.
*/
addFilter(filter) {
this.filters.push(filter)
}
/**
* Serializes the filter tree rooted at this node. The serialized version will be in the form:
* {
* filter_type: 'AND' | 'OR',
* filters: [
* {
* type: 'contains' | 'does_not_contain' | 'is' | 'is_not' | 'is_empty' | 'is_not_empty' | etc.,
* field: 1,
* value: 'some value'
* },
* ...
* ],
* groups: [
* {
* filter_type: 'AND' | 'OR',
* filters: [
* type: 'contains' | 'does_not_contain' | 'is' | 'is_not' | 'is_empty' | 'is_not_empty' | etc.,
* field: 2,
* value: 'some other value'
* },
* ...
* ],
* groups: [...]
* },
* ...,
* ],
* }
*
* @returns {object} - The serialized tree.
*/
getFiltersTreeSerialized() {
const serialized = {
filter_type: this.filterType,
filters: [],
groups: [],
}
for (const filter of this.filters) {
serialized.filters.push({
type: filter.type,
field: filter.field,
value: filter.value,
})
}
for (const groupNode of this.children) {
serialized.groups.push(groupNode.getFiltersTreeSerialized())
}
return serialized
}
/**
* Determines if a given row matches the conditions of this node and its descendants.
* This function will recursively check if the row matches the filters of this node
* and its descendants. If the filter type of this node is 'AND' then it will return
* true if the row matches all the filters. If the filter type of this node is 'OR'
* then it will return true if the row matches at least one of the filters.
*
* @param {object} $registry - The registry containing field and filter type information.
* @param {Array} fields - The list of fields.
* @param {object} rowValues - The values of the row being checked.
* @returns {boolean} - True if the row matches, false otherwise.
*/
matches($registry, fields, rowValues) {
this.hasValidFilter = false
for (const child of this.children) {
const matches = child.matches($registry, fields, rowValues)
if (child.hasValidFilter) {
this.hasValidFilter = true
}
if (this.filterType === 'AND' && !matches) {
return false
} else if (this.filterType === 'OR' && matches) {
return true
}
}
const filterType = this.filterType
for (const filter of this.filters) {
const filterValue = String(filter.value ?? '')
const field = fields.find((f) => f.id === filter.field)
const fieldType = $registry.get('field', field.type)
const viewFilterType = $registry.get('viewFilter', filter.type)
const rowValue = rowValues[`field_${field.id}`]
const matches = viewFilterType.matches(
rowValue,
filterValue,
field,
fieldType
)
if (matches === null) {
continue
}
this.hasValidFilter = true
if (filterType === 'AND' && !matches) {
// With an `AND` filter type, the row must match all the filters, so if
// one of the filters doesn't match we can mark it as invalid.
return false
} else if (filterType === 'OR' && matches) {
// With an 'OR' filter type, the row only has to match one of the filters,
// that is the case here so we can mark it as valid.
return true
}
}
if (filterType === 'AND') {
// At this point with an `AND` condition the filter type matched all the
// filters and therefore we can mark it as valid.
return true
} else if (filterType === 'OR') {
// If no valid filters were found, return true (no filtering should be applied).
// If there were valid filters but none matched, return false.
return !this.hasValidFilter
}
}
}
/**
* Creates a tree structure from given filters and filter groups. Groups are
* first sorted by ID because parent groups have smaller IDs since they were
* created before their children. In this way, we ensure that when a child node
* is added to the tree, its parent will already be present.
* Once the tree has been created, it adds all the filters to the respective
* groups.
*
* @param {string} filterType - The root filter type.
* @param {Array} filters - The list of filters.
* @param {Array} filterGroups - The list of filter groups.
* @returns {TreeGroupNode} - The root of the filter tree.
*/
export const createFiltersTree = (filterType, filters, filterGroups) => {
const rootGroup = new TreeGroupNode(filterType)
const filterGroupsById = { '': rootGroup }
const filterGroupsOrderedById = filterGroups
? [...filterGroups].sort((a, b) => a.id - b.id)
: []
for (const filterGroup of filterGroupsOrderedById) {
const parent = filterGroupsById[filterGroup.parent_group || '']
filterGroupsById[filterGroup.id] = new TreeGroupNode(
filterGroup.filter_type,
parent,
filterGroup.id
)
}
for (const filter of filters) {
const filterGroupId = filter.group || ''
const filterGroup = filterGroupsById[filterGroupId]
filterGroup.addFilter(filter)
}
return rootGroup
}
/**
* A helper function that checks if the provided row values match the provided view
* filters. Returning false indicates that the row should not be visible for that
* view.
*/
export const matchSearchFilters = (
$registry,
filterType,
filters,
filterGroups,
fields,
values
) => {
// If there aren't any filters then it is not possible to check if the row
// matches any of the filters, so we can mark it as valid.
if (filters.length === 0) {
return true
}
const filterTree = createFiltersTree(filterType, filters, filterGroups)
return filterTree.matches($registry, fields, values)
}
function _fullTextSearch(registry, field, value, activeSearchTerm) {
const searchableString = registry
.get('field', field.type)
.toSearchableString(field, value)
const fixedValue = convertStringToMatchBackendTsvectorData(searchableString)
const fixedTerm = convertStringToMatchBackendTsvectorData(activeSearchTerm)
if (fixedTerm.length === 0) {
return false
} else {
const regexMatchingWordsThatStartWithTerm =
'(^|\\s+)' + escapeRegExp(fixedTerm)
return !!fixedValue.match(
new RegExp(regexMatchingWordsThatStartWithTerm, 'gu')
)
}
}
function _compatSearchMode(registry, field, value, activeSearchTerm) {
return registry
.get('field', field.type)
.containsFilter(value, activeSearchTerm, field)
}
export function valueMatchesActiveSearchTerm(
searchMode,
registry,
field,
value,
activeSearchTerm
) {
if (searchMode === SearchMode.FT_WITH_COUNT) {
return _fullTextSearch(registry, field, value, activeSearchTerm)
} else {
return _compatSearchMode(registry, field, value, activeSearchTerm)
}
}
function _findFieldsInRowMatchingSearch(
row,
activeSearchTerm,
fields,
registry,
overrides,
searchMode
) {
const fieldSearchMatches = new Set()
// If the row is loading then a temporary UUID is put in its id. We don't want to
// accidentally match against that UUID as it will be shortly replaced with its
// real id.
if (
!row._.loading &&
row.id?.toString() === (activeSearchTerm || '').trim()
) {
fieldSearchMatches.add('row_id')
}
for (const field of fields) {
const fieldName = `field_${field.id}`
const rowValue =
fieldName in overrides ? overrides[fieldName] : row[fieldName]
if (rowValue !== undefined && rowValue !== null) {
const doesMatch = valueMatchesActiveSearchTerm(
searchMode,
registry,
field,
rowValue,
activeSearchTerm
)
if (doesMatch) {
fieldSearchMatches.add(field.id.toString())
}
}
}
return fieldSearchMatches
}
/**
* Helper function which calculates if a given row and which of it's fields matches a
* given search term. The rows values can be overridden by providing an overrides
* object containing a mapping of the field name to override to a value that will be
* used to check for matches instead of the rows real one. The rows values will not be
* changed.
*/
export function calculateSingleRowSearchMatches(
row,
activeSearchTerm,
hideRowsNotMatchingSearch,
fields,
registry,
searchMode,
overrides = {}
) {
const searchIsBlank = activeSearchTerm === ''
const fieldSearchMatches = searchIsBlank
? new Set()
: _findFieldsInRowMatchingSearch(
row,
activeSearchTerm,
fields,
registry,
overrides,
searchMode
)
const matchSearch =
!hideRowsNotMatchingSearch || searchIsBlank || fieldSearchMatches.size > 0
return { row, matchSearch, fieldSearchMatches }
}
/**
* Returns true is the empty value of the provided field matches the active search term.
*/
export function newFieldMatchesActiveSearchTerm(
searchMode,
registry,
newField,
activeSearchTerm
) {
if (newField && activeSearchTerm !== '') {
const fieldType = registry.get('field', newField.type)
const defaultValue = fieldType.getDefaultValue(newField)
return valueMatchesActiveSearchTerm(
searchMode,
registry,
newField,
defaultValue,
activeSearchTerm
)
}
return false
}
// Safe to call unconditionally: only GridViewType has can_group_by=True,
// so view.group_bys is always empty for gallery/kanban/calendar views.
export function serializeGroupBys(view) {
if (!view || !view.group_bys || view.group_bys.length === 0) {
return ''
}
return view.group_bys
.map((groupBy) => {
let serialized = `${groupBy.order === 'DESC' ? '-' : ''}field_${
groupBy.field
}`
if (groupBy.type !== DEFAULT_SORT_TYPE_KEY) {
serialized += `[${groupBy.type}]`
}
return serialized
})
.join(',')
}
export function getGroupBy(rootGetters, viewId, adhocGroupBy = false) {
if (rootGetters['page/view/public/getIsPublic'] || adhocGroupBy) {
const view = rootGetters['view/get'](viewId)
return serializeGroupBys(view)
} else {
return null
}
}
export function isAdhocSorting(app, workspace, view, publicView) {
return (
publicView ||
(app.$hasPermission('database.table.view.list_sort', view, workspace.id) &&
!app.$hasPermission(
'database.table.view.create_sort',
view,
workspace.id
))
)
}
export function isAdhocGroupBy(app, workspace, view, publicView) {
return (
publicView ||
(app.$hasPermission(
'database.table.view.list_group_bys',
view,
workspace.id
) &&
!app.$hasPermission(
'database.table.view.create_group_by',
view,
workspace.id
))
)
}
/**
* If filters, sorts or group bys are read only (i.e. formula fields) or there is
* an activeSearchTerm and a searchable read-only field, then the rows cannot be
* optimistically because the backend calculates the read-only values based on other
* fields and the UI cannot predict the outcome reliably.
*/
export function canRowsBeOptimisticallyUpdatedInView(
$registry,
view,
fields,
activeSearchTerm
) {
const readOnlyFields = fields.filter((f) =>
$registry.get('field', f.type).isReadOnlyField(f)
)
const readOnlyFieldIds = new Set(
readOnlyFields.map((field) => String(field.id))
)
// A read-only field the search can't match, like one holding no value at
// all, never makes the search result depend on the backend.
const searchableReadOnlyFields = readOnlyFields.filter((f) =>
$registry.get('field', f.type).isSearchable(f)
)
const hasReadOnlyField = (sort) => readOnlyFieldIds.has(String(sort.field))
const readOnlyGroupBys = view.group_bys
? view.group_bys.some(hasReadOnlyField)
: false
const readOnlySorts = view.sortings.some(hasReadOnlyField)
const readOnlyFilters = view.filters.some((filter) =>
readOnlyFieldIds.has(String(filter.field))
)
// if any of the above conditions are true, then we cannot optimistically update rows
// in this view
const needsServerSideCalculation =
readOnlyGroupBys ||
readOnlySorts ||
readOnlyFilters ||
(activeSearchTerm && searchableReadOnlyFields.length > 0)
return !needsServerSideCalculation
}
/**
* Returns true when changing a row can affect its visible position, visibility,
* or mismatch warning in the current view.
*/
export function viewHasRulesThatCanMoveOrHideRows(view, activeSearchTerm) {
const hasSortings = (view.sortings?.length ?? 0) > 0
const hasGroupBys = (view.group_bys?.length ?? 0) > 0
const hasFilters = !view.filters_disabled && (view.filters?.length ?? 0) > 0
return hasSortings || hasGroupBys || hasFilters || Boolean(activeSearchTerm)
}
export function getOrderBy(view, adhocSorting) {
if (adhocSorting) {
const serializeSort = (sort) => {
let serialized = `${sort.order === 'DESC' ? '-' : ''}field_${sort.field}`
if (sort.type !== DEFAULT_SORT_TYPE_KEY) {
serialized += `[${sort.type}]`
}
return serialized
}
const sorts = view.sortings.map(serializeSort)
return sorts.length > 0 ? sorts.join(',') : ''
} else {
return null
}
}
export function isAdhocFiltering(app, workspace, view, publicView) {
return (
publicView ||
(app.$hasPermission(
'database.table.view.list_filter',
view,
workspace.id
) &&
!app.$hasPermission(
'database.table.view.create_filter',
view,
workspace.id
))
)
}
export function getFilters(view, adhocFiltering) {
const payload = {}
if (adhocFiltering && !view.filters_disabled) {
const {
filter_type: filterType,
filter_groups: filterGroups,
filters,
} = view
const filterTree = createFiltersTree(filterType, filters, filterGroups)
const serializedTree = filterTree.getFiltersTreeSerialized()
payload.filters = [JSON.stringify(serializedTree)]
}
return payload
}
/**
* Calculates the size of a UTF-8 encoded string in bytes - computes the size
* of a string in UTF-8 encoding and utilizes the TextEncoder API if available.
*
* Using TextEncoder is preferred in Modern Browsers and Node.js Supported
* environments because it provides a more efficient and accurate way to encode
* strings into UTF-8 bytes and directly calculate the byte size of the encoded
* string.
*
* In some older web browsers or environments where TextEncoder may not be available
* (such as SSR where certain browser APIs are absent), it falls back to a less
* accurate method and simply returns the length of the string.
*/
export function utf8ByteSize(str) {
// Use TextEncoder if available (modern browsers and Node.js)
if (typeof TextEncoder !== 'undefined') {
const encoder = new TextEncoder()
const data = encoder.encode(str)
return data.length
} else {
// Fallback for older browsers (may not be as accurate)
return str.length
}
}
/**
* Return the view that has been visited most recently or the first
* available one that is capable of displaying the provided row data if required.
* If no view is available that can display the row data, return undefined.
*/
export function getDefaultView(app, store, workspaceId, showRowModal) {
// Put the most recently visited one first in the list.
const defaultView = store.getters['view/default']
const allViews = store.getters['view/getAllOrdered']
const views = defaultView ? [defaultView, ...allViews] : allViews
return views.find((view) => {
const viewType = app.$registry.get('view', view.type)
if (viewType.isDeactivated(workspaceId)) {
return false
}
// Ensure that the view can display the row data if required.
return showRowModal ? viewType.canShowRowModal() : true
})
}
/** Toasts and closes the modals of rows the backend hid; returns their ids. */
export function reportHiddenRows(data, { dispatch, i18n, tableId, created }) {
const rowIds = new Set(data.metadata?.hidden_row_ids || [])
const count = rowIds.size
if (count === 0) {
return rowIds
}
const messageKey = created
? 'hiddenRows.createdMessage'
: 'hiddenRows.updatedMessage'
dispatch(
'toast/info',
{
title: i18n.t('hiddenRows.title', { count }),
message: i18n.t(messageKey, { count }),
},
{ root: true }
)
dispatch(
'rowModal/rowsHiddenByBackend',
{ tableId, rowIds: [...rowIds] },
{ root: true }
)
return rowIds
}
/*
* Extracts the metadata from the provided data to populate the row.
*/
export function extractRowMetadata(data, rowId) {
const metadata = data.row_metadata || {}
return metadata[rowId] || {}
}
/**
* Calls `fetchRows` once per `chunkSize` rows when `limit` is above it, because the
* backend rejects page sizes above the row page size limit. The responses are merged
* into one, as if a single request was made. Only the first chunk asks for the field
* options and the count.
*/
export async function fetchRowsInChunks(fetchRows, { chunkSize, ...params }) {
const { offset = 0, limit = 100 } = params
if (!chunkSize || limit <= chunkSize) {
return await fetchRows(params)
}
const requests = []
for (let start = 0; start < limit; start += chunkSize) {
const isFirst = start === 0
requests.push(
fetchRows({
...params,
offset: (offset ?? 0) + start,
limit: Math.min(chunkSize, limit - start),
includeFieldOptions: isFirst && params.includeFieldOptions,
excludeCount: !isFirst || params.excludeCount,
})
)
}
const responses = await Promise.all(requests)
const data = { ...responses[0].data }
data.results = responses.flatMap((response) => response.data.results)
if (data.row_metadata) {
data.row_metadata = Object.assign(
{},
...responses.map((response) => response.data.row_metadata)
)
}
return { ...responses[0], data }
}
/**
* Limit the size of a cookie's value by removing elements from an array
* until it fits within the maximum allowed cookie size. The array is
* assumed to be ordered by least important to most important, so the first
* elements are removed first.
*
* @param {Array} arrayOfValues - The array of values to encode.
* @param {Function} encodingFunc - The function to use to encode the array.
* @param {Number} maxLength - The maximum allowed length of the encoded value string.
* @returns {String} - The serialized value to save in the cookie with the
* max number of elements that fit in, or an empty string if none fit.
*/
export function fitInCookieEncoded(
arrayOfValues,
encodingFunc,
maxLength = 2048
) {
for (let i = 0, l = arrayOfValues.length; i < l; i++) {
const encoded = encodingFunc(arrayOfValues.slice(i))
// The encoded URI will be serialized when saved in the cookie, so we
// need to encode it first to get the correct byte size.
const serialized = encodeURIComponent(encoded)
if (utf8ByteSize(serialized) < maxLength) {
return encoded
}
}
return ''
}
export function decodeDefaultViewIdPerTable(value) {
// backward compatibility, we used to store the array of default views
// with a slightly different format
if (Array.isArray(value)) {
return value.map((item) => ({
tableId: item.table_id,
viewId: item.id,
}))
}
const data = []
for (const item of value.split(',')) {
const [tableId, viewId] = item.split(':')
if (tableId !== undefined && viewId !== undefined) {
data.push({ tableId: parseInt(tableId), viewId: parseInt(viewId) })
}
}
return data
}
export function encodeDefaultViewIdPerTable(data) {
return data.map(({ tableId, viewId }) => `${tableId}:${viewId}`).join(',')
}
/**
* Reads the default view for table from cookies.
*
* @param {Object} cookies - The cookies object.
* @param {Number} tableId - The id of the table.
* @param {String} cookieName - The name of the cookie.
* @returns {Number|null} - The id of the default view for the table, or null if there
* is no default view for the table.
*/
export function readDefaultViewIdFromCookie(
tableId,
cookieName = DEFAULT_VIEW_ID_COOKIE_NAME
) {
try {
const config = useRuntimeConfig()
const cookie = useCookie(getCookieName(config, cookieName))
const cookieValue = cookie.value || ''
const defaultViews = decodeDefaultViewIdPerTable(cookieValue)
const defaultView = defaultViews.find((view) => view.tableId === tableId)
return defaultView ? defaultView.viewId : null
} catch (error) {
return null
}
}
/**
* Updates the default view for table in cookies (if it exists) or creates a new one if
* it doesn't. The entry will be placed at the end of the list as the most recently
* visited view. If the entire list does not fit in the cookie, the oldest entries (the
* first ones) will be removed.
*
* @param {Object} cookies - The cookies object.
* @param {Object} view - The view object.
* @param {Object} config - The config object.
* @param {String} cookieName - The name of the cookie.
*/
export function saveDefaultViewIdInCookie(
view,
config,
cookieName = DEFAULT_VIEW_ID_COOKIE_NAME
) {
const secure = isSecureURL(config.public.publicWebFrontendUrl)
const cookieValue = useCookie(getCookieName(config, cookieName), {
path: '/',
maxAge: 60 * 60 * 24 * 365, // 1 year
sameSite: config.public.baserowFrontendSameSiteCookie,
secure,
default: () => {
return ''
},
})
let defaultViews = decodeDefaultViewIdPerTable(cookieValue.value)
function createEntry(view) {
return { tableId: view.table_id, viewId: view.id }
}
try {
const index = defaultViews.findIndex((obj) => obj.tableId === view.table_id)
if (index !== -1) {
const existingView = defaultViews.splice(index, 1)[0]
existingView.viewId = view.id
defaultViews.push(existingView)
} else if (view.id !== view.slug) {
defaultViews.push(createEntry(view))
}
} catch (error) {
defaultViews = [createEntry(view)]
} finally {
const fittedListEncoded = fitInCookieEncoded(
defaultViews,
encodeDefaultViewIdPerTable
)
cookieValue.value = fittedListEncoded
}
}