-
-
Notifications
You must be signed in to change notification settings - Fork 785
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add getSelectionRecords plugin bridge method (#1358)
* feat: add getSelectionRecords plugin bridge method * chore: remove useless filter params * fix: chart plugins filter me
- Loading branch information
Showing
8 changed files
with
248 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
apps/nextjs-app/src/features/app/blocks/view/grid/hooks/useSelectionStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { IGroup } from '@teable/core'; | ||
import type { IGetRecordsRo, IQueryBaseRo } from '@teable/openapi'; | ||
import type { IFieldInstance } from '@teable/sdk/model'; | ||
import { useEffect } from 'react'; | ||
import { create } from 'zustand'; | ||
|
||
interface ISelectionStore { | ||
fields?: IFieldInstance[]; | ||
setFields: (fields: IFieldInstance[] | undefined) => void; | ||
search?: IQueryBaseRo['search']; | ||
setSearch: (search: IQueryBaseRo['search']) => void; | ||
groupBy?: IGroup; | ||
setGroupBy: (groupBy: IGroup | undefined) => void; | ||
personalViewCommonQuery?: IGetRecordsRo; | ||
setPersonalViewCommonQuery: (personalViewCommonQuery: IGetRecordsRo | undefined) => void; | ||
collapsedGroupIds?: string[]; | ||
setCollapsedGroupIds: (collapsedGroupIds: string[] | undefined) => void; | ||
} | ||
|
||
export const useSelectionStore = create<ISelectionStore>((set) => ({ | ||
setGroupBy: (groupBy) => set((state) => ({ ...state, groupBy })), | ||
setPersonalViewCommonQuery: (personalViewCommonQuery) => | ||
set((state) => ({ ...state, personalViewCommonQuery })), | ||
setCollapsedGroupIds: (collapsedGroupIds) => set((state) => ({ ...state, collapsedGroupIds })), | ||
setSearch: (search) => set((state) => ({ ...state, search })), | ||
setFields: (fields) => set((state) => ({ ...state, fields })), | ||
})); | ||
|
||
export const useSyncSelectionStore = ({ | ||
groupBy, | ||
personalViewCommonQuery, | ||
collapsedGroupIds, | ||
search, | ||
fields, | ||
}: { | ||
groupBy?: IGroup; | ||
personalViewCommonQuery?: IGetRecordsRo; | ||
collapsedGroupIds?: string[]; | ||
search?: IQueryBaseRo['search']; | ||
fields?: IFieldInstance[]; | ||
}) => { | ||
useEffect(() => { | ||
useSelectionStore.setState({ groupBy }); | ||
}, [groupBy]); | ||
useEffect(() => { | ||
useSelectionStore.setState({ personalViewCommonQuery }); | ||
}, [personalViewCommonQuery]); | ||
useEffect(() => { | ||
useSelectionStore.setState({ collapsedGroupIds }); | ||
}, [collapsedGroupIds]); | ||
useEffect(() => { | ||
useSelectionStore.setState({ search }); | ||
}, [search]); | ||
useEffect(() => { | ||
useSelectionStore.setState({ fields }); | ||
}, [fields]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
apps/nextjs-app/src/features/app/components/plugin/hooks/utils/getSelectionRecords.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import type { IRecordsVo } from '@teable/openapi'; | ||
import { getRecords } from '@teable/openapi'; | ||
import { SelectionRegionType } from '@teable/sdk/components'; | ||
import type { IGetSelectionRecordsVo, ISelection } from '@teable/sdk/plugin-bridge'; | ||
import { useSelectionStore } from '@/features/app/blocks/view/grid/hooks/useSelectionStore'; | ||
|
||
export const getSelectionRecords = async ( | ||
tableId: string, | ||
viewId: string, | ||
selection: ISelection, | ||
options?: { | ||
skip?: number; | ||
take?: number; | ||
} | ||
// eslint-disable-next-line sonarjs/cognitive-complexity | ||
): Promise<IGetSelectionRecordsVo> => { | ||
const { groupBy, search, fields, collapsedGroupIds, personalViewCommonQuery } = | ||
useSelectionStore.getState(); | ||
if (!fields) { | ||
return { | ||
records: [], | ||
fields: [], | ||
}; | ||
} | ||
|
||
const { type, range } = selection; | ||
const { skip = 0, take = 100 } = options || {}; | ||
|
||
const getRecordsByQuery = ({ | ||
skip, | ||
take, | ||
projection, | ||
}: { | ||
skip: number; | ||
take: number; | ||
projection: string[]; | ||
}) => { | ||
return getRecords(tableId, { | ||
...personalViewCommonQuery, | ||
viewId, | ||
groupBy, | ||
search, | ||
collapsedGroupIds, | ||
projection, | ||
skip, | ||
take, | ||
}).then((res) => res.data.records); | ||
}; | ||
switch (type) { | ||
case SelectionRegionType.Cells: { | ||
const [[startColIndex, startRowIndex], [endColIndex, endRowIndex]] = range; | ||
if ( | ||
startColIndex == null || | ||
startRowIndex == null || | ||
endColIndex == null || | ||
endRowIndex == null | ||
) { | ||
throw new Error('Invalid selection range'); | ||
} | ||
const projectionFields = fields.slice(startColIndex, endColIndex + 1); | ||
const projection = projectionFields.map((item) => item.name); | ||
const records = await getRecordsByQuery({ | ||
projection, | ||
skip: skip + startRowIndex, | ||
take: Math.min(take, endRowIndex - startRowIndex + 1), | ||
}); | ||
return { | ||
records, | ||
fields: projectionFields.map((item) => item['doc'].data), | ||
}; | ||
} | ||
case SelectionRegionType.Rows: { | ||
const allRecords: IRecordsVo['records'] = []; | ||
let totalSkip = skip; | ||
let totalTake = take; | ||
const projection = fields.map((item) => item.name); | ||
for (let i = 0; i < range.length; i++) { | ||
const [startRowIndex, endRowIndex] = range[i]; | ||
const currentRowCount = endRowIndex - startRowIndex + 1; | ||
if (totalSkip >= currentRowCount) { | ||
totalSkip -= currentRowCount; | ||
continue; | ||
} | ||
|
||
const records = await getRecordsByQuery({ | ||
projection, | ||
skip: totalSkip + startRowIndex, | ||
take: Math.min(totalTake, currentRowCount), | ||
}); | ||
allRecords.push(...records); | ||
if (totalTake > currentRowCount) { | ||
totalTake -= currentRowCount; | ||
totalSkip = 0; | ||
} else { | ||
break; | ||
} | ||
} | ||
return { | ||
records: allRecords, | ||
fields: fields.map((item) => item['doc'].data), | ||
}; | ||
} | ||
case SelectionRegionType.Columns: { | ||
const projections: string[] = []; | ||
for (let i = 0; i < fields.length; i++) { | ||
const [startColIndex, endColIndex] = range[i]; | ||
projections.push(...fields.slice(startColIndex, endColIndex).map((item) => item.name)); | ||
} | ||
const records = await getRecordsByQuery({ | ||
projection: projections, | ||
skip, | ||
take, | ||
}); | ||
return { | ||
records, | ||
fields: fields.map((item) => item['doc'].data), | ||
}; | ||
} | ||
default: { | ||
console.error(`Unsupported selection type: ${type}`); | ||
return { | ||
records: [], | ||
fields: [], | ||
}; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters