Skip to content
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

feat(state): support filter by system, such as created_at, updated_at #194

Merged
merged 1 commit into from
Dec 17, 2024
Merged
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
6 changes: 3 additions & 3 deletions packages/state/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * from './build';
export * from './field/model';
export * from './/field/add-fields';
export * from './/field/remove-field';
export * from './/field/sort-fields';
export * from './field/add-fields';
export * from './field/remove-field';
export * from './field/sort-fields';
export * from './view';
export * from './common';
export * from './record/add-records';
Expand Down
36 changes: 23 additions & 13 deletions packages/state/src/utils/record/filter.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { AITable, AITableRecord, FieldValue } from '@ai-table/grid';
import { AITableFieldType, AITableRecord, FieldValue, isSystemField, SystemFieldTypes } from '@ai-table/grid';
import {
AITableFilterCondition,
AITableFilterConditions,
AITableFilterLogical,
AITableFilterOperation,
AITableView,
AITableViewField,
AITableViewFields,
AITableViewRecords,
AIViewTable
} from '../../types';
import { ViewOperationMap } from '../field/model';
import { isEmpty } from '../common';

export function getFilteredRecords(aiTable: AIViewTable, records: AITableViewRecords, fields: AITableViewFields, activeView: AITableView) {
const { conditions, condition_logical } = activeView.settings || {};
Expand All @@ -24,7 +22,7 @@ export function getFilteredRecords(aiTable: AIViewTable, records: AITableViewRec
}
const recordsWillHidden = aiTable.recordsWillHidden();
return records.filter((record) => {
if(recordsWillHidden && recordsWillHidden.length && recordsWillHidden.includes(record._id)){
if (recordsWillHidden && recordsWillHidden.length && recordsWillHidden.includes(record._id)) {
return true;
}
return checkConditions(fields, record, { conditions: illegalConditions, condition_logical });
Expand All @@ -49,8 +47,8 @@ function checkConditions(fields: AITableViewFields, record: AITableRecord, filte
}

function doFilterOperations(fields: AITableViewFields, record: AITableRecord, condition: AITableFilterCondition) {
const field = fields.find((item) => item._id === condition.field_id);
const cellValue = record.values[condition.field_id];
const { field, cellValue } = getFilterValue(fields, record, condition);

try {
return field && doFilter(condition, field, cellValue);
} catch (error) {
Expand All @@ -59,13 +57,6 @@ function doFilterOperations(fields: AITableViewFields, record: AITableRecord, co
}

export function doFilter(condition: AITableFilterCondition, field: AITableViewField, cellValue: FieldValue) {
if (condition.operation === AITableFilterOperation.empty) {
return isEmpty(cellValue);
}
if (condition.operation === AITableFilterOperation.exists) {
return !isEmpty(cellValue);
}

return ViewOperationMap[field.type].isMeetFilter(condition, cellValue);
}

Expand All @@ -81,3 +72,22 @@ export function getDefaultRecordDataByFilter(
}
return recordValues;
}

export function getFilterValue(fields: AITableViewFields, record: AITableRecord, condition: AITableFilterCondition) {
const field = fields.find((item) => item._id === condition.field_id);
let cellValue = null;
if (field && isSystemField(field)) {
if ([AITableFieldType.createdAt, AITableFieldType.updatedAt].includes(field.type)) {
cellValue = { timestamp: record[field.type as SystemFieldTypes] };
} else {
cellValue = record[field.type as SystemFieldTypes];
}
} else {
cellValue = record.values[condition.field_id];
}

return {
field,
cellValue
};
}
88 changes: 48 additions & 40 deletions src/app/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ export function sortDataByView(data: AITableViewRecords | AITableViewFields, act
return data;
}

export const getDefaultTrackableEntity = () => {
const member = 'member_01';
const time = new Date('2024-10-01').getTime();
return { created_by: member, created_at: time, updated_by: member, updated_at: time };
export const getDefaultTrackableEntity = (options?: {
[AITableFieldType.createdBy]?: string;
[AITableFieldType.createdAt]?: string;
[AITableFieldType.updatedBy]?: string;
[AITableFieldType.updatedAt]?: string;
}) => {
return {
created_by: options?.[AITableFieldType.createdBy] || 'member_01',
created_at: new Date(options?.[AITableFieldType.createdAt] || '2024-12-15').getTime() / 1000,
updated_by: options?.[AITableFieldType.updatedBy] || 'member_01',
updated_at: new Date(options?.[AITableFieldType.updatedAt] || '2024-12-17').getTime() / 1000
};
};

export function getDefaultValue() {
Expand All @@ -24,7 +32,7 @@ export function getDefaultValue() {
{
_id: 'row-1',
short_id: `row-short-id-${1}`,
...getDefaultTrackableEntity(),
...getDefaultTrackableEntity({ updated_at: '2024-12-19' }),
positions: {
view1: 0,
view2: 1
Expand All @@ -36,18 +44,18 @@ export function getDefaultValue() {
'column-21': ['66b31d0c8097a908f74bcd8a'],
'column-22': ['66b31d0c8097a908f74bcd8a'],
'column-3': 1,
'column-4': { timestamp: 1682235946 },
'column-4': { timestamp: 1734636127 },
'column-5': ['member_01'],
'column-6': 10,
'column-7': 3,
'column-8': {
url: 'https://www.baidu.com',
text: '百度链接'
},
'column-9': ['member_01'],
'column-10': 1682235946,
'column-11': ['member_02'],
'column-12': 1720490727
}
// 'column-9': ['member_01'],
// 'column-10': 1682235946,
// 'column-11': ['member_02'],
// 'column-12': 1720490727
}
},
{
Expand All @@ -69,11 +77,11 @@ export function getDefaultValue() {
'column-5': ['member_01', 'member_02'],
'column-6': 50,
'column-7': 1,
'column-8': {},
'column-9': ['member_01'],
'column-10': 1682235946,
'column-11': ['member_02'],
'column-12': 1720490727
'column-8': {}
// 'column-9': ['member_01'],
// 'column-10': 1682235946,
// 'column-11': ['member_02'],
// 'column-12': 1720490727
}
},
{
Expand Down Expand Up @@ -110,11 +118,11 @@ export function getDefaultValue() {
'column-5': [],
'column-6': 100,
'column-7': 1,
'column-8': {},
'column-9': [],
'column-10': 1682235946,
'column-11': ['member_02'],
'column-12': 1720490727
'column-8': {}
// 'column-9': [],
// 'column-10': 1682235946,
// 'column-11': ['member_02'],
// 'column-12': 1720490727
}
}
],
Expand Down Expand Up @@ -419,7 +427,7 @@ export function getCanvasDefaultValue() {
{
_id: 'row-1',
short_id: `row-short-id-${1}`,
...getDefaultTrackableEntity(),
...getDefaultTrackableEntity({ updated_at: '2024-12-19' }),
positions: {
view1: 0,
view2: 1
Expand All @@ -435,18 +443,18 @@ export function getCanvasDefaultValue() {
'column-112': ['tag_1'],
'column-20': ['66b31d0c8097a908f74bcd8a'],
'column-3': 1,
'column-4': { timestamp: 1682235946 },
'column-4': { timestamp: 1734636127 },
'column-5': ['member_01'],
'column-6': 10,
'column-7': 5,
'column-8': {
url: 'https://www.baidu.com',
text: '百度链接'
},
'column-9': ['member_01'],
'column-10': { timestamp: 1682235946 },
'column-11': ['member_02'],
'column-12': { timestamp: 1720490727 },
// 'column-9': ['member_01'],
// 'column-10': { timestamp: 1682235946 },
// 'column-11': ['member_02'],
// 'column-12': { timestamp: 1720490727 },
'column-21': ['66b31d0c8097a908f74bcd8a'],
'column-22': ['66b31d0c8097a908f74bcd8b', '66b31d0c8097a908f74bcd8a'],
'column-23': ['66b31d0c8097a908f74bcd8e', '66b31d0c8097a908f74bcd8a', '66b31d0c8097a908f74bcd8b']
Expand Down Expand Up @@ -474,10 +482,10 @@ export function getCanvasDefaultValue() {
'column-6': 50,
'column-7': 4,
'column-8': {},
'column-9': ['member_01'],
'column-10': { timestamp: 1682235946 },
'column-11': ['member_02'],
'column-12': { timestamp: 1720490727 },
// 'column-9': ['member_01'],
// 'column-10': { timestamp: 1682235946 },
// 'column-11': ['member_02'],
// 'column-12': { timestamp: 1720490727 },
'column-21': ['66b31d0c8097a908f74bcd8a'],
'column-22': ['66b31d0c8097a908f74bcd8b', '66b31d0c8097a908f74bcd8a'],
'column-23': ['66b31d0c8097a908f74bcd8e', '66b31d0c8097a908f74bcd8a', '66b31d0c8097a908f74bcd8b']
Expand Down Expand Up @@ -510,10 +518,10 @@ export function getCanvasDefaultValue() {
'column-6': 100,
'column-7': 3,
'column-8': {},
'column-9': [],
'column-10': { timestamp: 1727254598 },
'column-11': ['member_02'],
'column-12': { timestamp: 1720490727 },
// 'column-9': [],
// 'column-10': { timestamp: 1727254598 },
// 'column-11': ['member_02'],
// 'column-12': { timestamp: 1720490727 },
'column-21': ['66b31d0c8097a908f74bcd8a'],
'column-22': ['66b31d0c8097a908f74bcd8b', '66b31d0c8097a908f74bcd8a'],
'column-23': ['66b31d0c8097a908f74bcd8e', '66b31d0c8097a908f74bcd8a', '66b31d0c8097a908f74bcd8b']
Expand Down Expand Up @@ -990,11 +998,11 @@ export function getBigData() {
'column-5': ['member_01', 'member_02'],
'column-6': 50,
'column-7': 3,
'column-8': {},
'column-9': ['member_01'],
'column-10': { timestamp: 1682235946 },
'column-11': ['member_02'],
'column-12': { timestamp: 1720490727 }
'column-8': {}
// 'column-9': ['member_01'],
// 'column-10': { timestamp: 1682235946 },
// 'column-11': ['member_02'],
// 'column-12': { timestamp: 1720490727 }
}
});
}
Expand Down
Loading