diff --git a/src/components/map-projects/MapProjects.jsx b/src/components/map-projects/MapProjects.jsx index dac56dbf0..0f766f97c 100644 --- a/src/components/map-projects/MapProjects.jsx +++ b/src/components/map-projects/MapProjects.jsx @@ -111,7 +111,7 @@ const MapProjects = ({ ownerType, owner, height }) => { - {t('map_project.could_not_load_projects')} setAttempt(count => count + 1)} sx={{verticalAlign: 'baseline'}}>{t('map_project.try_again')} + {t('map_project.could_not_load_projects')} setAttempt(count => count + 1)} sx={{verticalAlign: 'baseline'}}>{t('common.try_again')} diff --git a/src/components/search/TableResults.jsx b/src/components/search/TableResults.jsx index fab882cbd..f4750d71e 100644 --- a/src/components/search/TableResults.jsx +++ b/src/components/search/TableResults.jsx @@ -13,14 +13,15 @@ import Switch from '@mui/material/Switch'; import Checkbox from '@mui/material/Checkbox'; import Skeleton from '@mui/material/Skeleton' import IconButton from '@mui/material/IconButton'; -import CircularProgress from '@mui/material/CircularProgress'; +import Button from '@mui/material/Button'; import RightIcon from '@mui/icons-material/KeyboardArrowRight'; import DownIcon from '@mui/icons-material/KeyboardArrowDown'; import { visuallyHidden } from '@mui/utils'; import { get, map, times } from 'lodash' import APIService from '../../services/APIService'; -import { SECONDARY_COLORS } from '../../common/colors'; +import { SECONDARY_COLORS, ERROR_COLORS } from '../../common/colors'; import { resolveColumns, isSameSortField } from './columns'; +import { CHILDREN_PAGE_SIZE, initialChildrenState, childrenReducer, needsFetchOnExpand, nextChildrenPage } from './childrenPaging'; // What screen readers announce for the select-all checkbox, per result type const SELECT_ALL_LABEL_KEYS = { @@ -130,11 +131,11 @@ const getChildrenURL = (row, baseURL) => { const conceptSuffix = row.url.replace(/^.*\/concepts\//, 'concepts/').replace(/\/$/, '') return baseURL.replace(/concepts\/.*$/, '') + conceptSuffix + '/children/'; } -const ResultRow = ({row, index, columns, getValue, handleClick, handleRowClick, isSelected, isItemShown, size, hierarchical, level, baseURL, onRowsLoaded}) => { +const ResultRow = ({row, index, columns, getValue, handleClick, handleRowClick, isSelected, isItemShown, size, hierarchical, level, baseURL, onRowsLoaded, hidden}) => { + const { t } = useTranslation() const [open, setOpen] = React.useState(false) - const [children, setChildren] = React.useState([]) - const [fetched, setFetched] = React.useState(false) - const [loadingChildren, setLoadingChildren] = React.useState(false) + const [childrenState, dispatchChildren] = React.useReducer(childrenReducer, initialChildrenState) + const {children, total: childrenTotal, hasMore: moreChildren, fetched, loading: loadingChildren, failed: childrenFailed} = childrenState const id = row.version_url || row.url || row.id const isItemSelected = isSelected(id) const isItemSelectedToShow = isItemShown(id) @@ -142,20 +143,34 @@ const ResultRow = ({row, index, columns, getValue, handleClick, handleRowClick, const color = row.retired ? SECONDARY_COLORS.main : 'none' let bgColor = isItemSelectedToShow ? 'primary.90' : '' const colSpan = columns.length + (handleClick ? 1 : 0) + 1 - const fetchChildren = () => { - if(fetched) + // Children load a page at a time; a failed page can be retried without losing the pages already shown + const fetchChildrenPage = page => { + if(loadingChildren) return - setLoadingChildren(true) - APIService.new().overrideURL(getChildrenURL(row, baseURL)).get().then(response => { - setLoadingChildren(false) + dispatchChildren({type: 'loading'}) + APIService.new().overrideURL(getChildrenURL(row, baseURL)).get(null, null, {limit: CHILDREN_PAGE_SIZE, page: page}).then(response => { if(Array.isArray(response?.data)) { - setChildren(response.data) - setFetched(true) + dispatchChildren({type: 'loaded', page: page, rows: response.data, total: parseInt(response?.headers?.num_found) || 0, hasMore: Boolean(response?.headers?.next)}) if(onRowsLoaded) onRowsLoaded(response.data) + } else { + dispatchChildren({type: 'failed'}) } }) } + const fetchChildren = () => { + if(needsFetchOnExpand(childrenState)) + fetchChildrenPage(1) + } + // "Show more" and "Try again" both load the page after the last one shown + const onLoadNextChildren = event => { + event.preventDefault() + event.stopPropagation() + fetchChildrenPage(nextChildrenPage(childrenState)) + } + const showChildren = hierarchical && open && !hidden + // up to 5 placeholder rows while a page loads, fewer when fewer children are left + const skeletonRows = Math.max(Math.min(5, fetched ? childrenTotal - children.length : 5), 1) const onExpandClick = event => { event.preventDefault() event.stopPropagation() @@ -166,88 +181,85 @@ const ResultRow = ({row, index, columns, getValue, handleClick, handleRowClick, } return ( - handleRowClick(event, id)} - selected={isItemSelectedToShow} - className={isItemSelectedToShow ? 'show-item' : ''} - sx={[{ - cursor: 'pointer', - backgroundColor: '#FFF', - '&.Mui-selected': { - backgroundColor: bgColor - } - }, isItemSelectedToShow ? { - '&.MuiTableRow-hover:hover': { - backgroundColor: bgColor - } - } : { - '&.MuiTableRow-hover:hover': { - backgroundColor: 'primary.95' - } - }]} - > - { - handleClick && - handleClick(event, id)} style={{color: color}}> - - - } - { - hierarchical && - - { - row.has_children ? - - { open ? : } - : - null - } - - } - { - columns.map((column, idx) => { - const value = getValue(row, column) - return idx === 0 ? - - {value} - : - - {value} - - }) - } - { - hierarchical && open && loadingChildren && - - - - + // a hidden row renders nothing of its own but stays mounted, keeping its children and expansion + !hidden && + handleRowClick(event, id)} + selected={isItemSelectedToShow} + className={isItemSelectedToShow ? 'show-item' : ''} + sx={[{ + cursor: 'pointer', + backgroundColor: '#FFF', + '&.Mui-selected': { + backgroundColor: bgColor + } + }, isItemSelectedToShow ? { + '&.MuiTableRow-hover:hover': { + backgroundColor: bgColor + } + } : { + '&.MuiTableRow-hover:hover': { + backgroundColor: 'primary.95' + } + }]} + > + { + handleClick && + handleClick(event, id)} style={{color: color}}> + + + } + { + hierarchical && + + { + row.has_children ? + + { open ? : } + : + null + } + + } + { + columns.map((column, idx) => { + const value = getValue(row, column) + return idx === 0 ? + + {value} + : + + {value} + + }) + } } { - hierarchical && open && !loadingChildren && + // fetched children stay mounted while collapsed, so they keep their own pages and expansions + hierarchical && fetched && children.map((child, i) => ( ); } diff --git a/src/components/search/__tests__/childrenPaging.test.js b/src/components/search/__tests__/childrenPaging.test.js new file mode 100644 index 000000000..fff4a91f5 --- /dev/null +++ b/src/components/search/__tests__/childrenPaging.test.js @@ -0,0 +1,66 @@ +import test from 'node:test' +import assert from 'node:assert/strict' + +const { + CHILDREN_PAGE_SIZE, initialChildrenState, childrenReducer, mergeChildren, needsFetchOnExpand, nextChildrenPage +} = await import('../childrenPaging.js') + +const rows = (from, count) => Array.from({length: count}, (_, i) => ({url: `/concepts/c${from + i}/`})) +const load = (state, page, pageRows, total, hasMore = false) => + childrenReducer(childrenReducer(state, {type: 'loading'}), {type: 'loaded', page, rows: pageRows, total, hasMore}) +const fail = state => childrenReducer(childrenReducer(state, {type: 'loading'}), {type: 'failed'}) + +test('page 2 is appended after page 1', () => { + const firstPage = load(initialChildrenState, 1, rows(0, CHILDREN_PAGE_SIZE), 30, true) + assert.equal(firstPage.children.length, CHILDREN_PAGE_SIZE) + assert.equal(firstPage.hasMore, true) + assert.equal(nextChildrenPage(firstPage), 2) + + const secondPage = load(firstPage, 2, rows(CHILDREN_PAGE_SIZE, 5), 30) + assert.deepEqual(secondPage.children.map(child => child.url), rows(0, 30).map(child => child.url)) + assert.equal(secondPage.hasMore, false) +}) + +test('a failed page keeps what is loaded and retrying asks for the same page once', () => { + const firstPage = load(initialChildrenState, 1, rows(0, CHILDREN_PAGE_SIZE), 30) + const failed = fail(firstPage) + assert.equal(failed.failed, true) + assert.equal(failed.children.length, CHILDREN_PAGE_SIZE) + assert.equal(nextChildrenPage(failed), 2) + + const retried = load(failed, nextChildrenPage(failed), rows(CHILDREN_PAGE_SIZE, 5), 30) + assert.equal(retried.failed, false) + assert.equal(retried.children.length, 30) + assert.equal(new Set(retried.children.map(child => child.url)).size, 30) +}) + +test('collapsing and expanding again does not refetch loaded children', () => { + assert.equal(needsFetchOnExpand(initialChildrenState), true) + const loaded = load(initialChildrenState, 1, rows(0, 3), 3) + assert.equal(needsFetchOnExpand(loaded), false) +}) + +test('expanding again retries a first page that failed, but not one still loading', () => { + assert.equal(needsFetchOnExpand(fail(initialChildrenState)), true) + assert.equal(needsFetchOnExpand(childrenReducer(initialChildrenState, {type: 'loading'})), false) +}) + +test('a child returned on two pages is shown once and Show more still ends', () => { + const firstPage = load(initialChildrenState, 1, rows(0, CHILDREN_PAGE_SIZE), 30) + const secondPage = load(firstPage, 2, rows(CHILDREN_PAGE_SIZE - 1, 5), 30) + assert.equal(secondPage.children.length, 29) + assert.equal(new Set(secondPage.children.map(child => child.url)).size, 29) + assert.equal(secondPage.hasMore, false) +}) + +test('Show more follows the API even when it serves fewer rows than asked', () => { + const firstPage = load(initialChildrenState, 1, rows(0, 10), 40, true) + const secondPage = load(firstPage, 2, rows(10, 10), 40, true) + assert.equal(secondPage.hasMore, true) + assert.equal(nextChildrenPage(secondPage), 3) +}) + +test('mergeChildren keeps a versioned child apart from another version of it', () => { + const merged = mergeChildren([{version_url: '/concepts/a/1/', url: '/concepts/a/'}], [{version_url: '/concepts/a/2/', url: '/concepts/a/'}]) + assert.equal(merged.length, 2) +}) diff --git a/src/components/search/childrenPaging.js b/src/components/search/childrenPaging.js new file mode 100644 index 000000000..0eba39c3d --- /dev/null +++ b/src/components/search/childrenPaging.js @@ -0,0 +1,45 @@ +// State for one hierarchy row's children, loaded a page at a time +export const CHILDREN_PAGE_SIZE = 25 + +export const initialChildrenState = {children: [], page: 0, total: 0, hasMore: false, fetched: false, loading: false, failed: false} + +const childKey = child => child.version_url || child.url || child.uuid + +// A child can come back on two pages if the list changes between requests +export const mergeChildren = (loaded, incoming) => { + const seen = new Set(loaded.map(childKey)) + const added = incoming.filter(child => { + const key = childKey(child) + if(key && seen.has(key)) + return false + seen.add(key) + return true + }) + return [...loaded, ...added] +} + +export const childrenReducer = (state, action) => { + switch(action.type) { + case 'loading': + return {...state, loading: true, failed: false} + case 'loaded': + return { + ...state, + loading: false, + failed: false, + fetched: true, + page: action.page, + total: action.total, + hasMore: action.hasMore, + children: mergeChildren(action.page === 1 ? [] : state.children, action.rows), + } + case 'failed': + return {...state, loading: false, failed: true} + default: + return state + } +} + +export const nextChildrenPage = state => state.page + 1 + +export const needsFetchOnExpand = state => !state.fetched && !state.loading diff --git a/src/i18n/locales/en/translations.json b/src/i18n/locales/en/translations.json index 31def3043..38e54031a 100644 --- a/src/i18n/locales/en/translations.json +++ b/src/i18n/locales/en/translations.json @@ -166,6 +166,9 @@ "processing": "Processing", "processed": "Processed", "load_more": "Load more", + "show_more_of": "Show more ({{shown}} of {{total}})", + "could_not_load": "This list could not be loaded.", + "try_again": "Try again", "generate_with_ai": "Generate with the OCL AI Assistant", "release": "Release", "unrelease": "Un-Release", @@ -1214,7 +1217,6 @@ "mapping_projects": "Mapping Projects", "project_name": "Project Name", "no_projects_found": "No mapping projects yet.", - "could_not_load_projects": "Could not load mapping projects.", - "try_again": "Try again" + "could_not_load_projects": "Could not load mapping projects." } } diff --git a/src/i18n/locales/es/translations.json b/src/i18n/locales/es/translations.json index e8636647e..962020382 100644 --- a/src/i18n/locales/es/translations.json +++ b/src/i18n/locales/es/translations.json @@ -73,6 +73,9 @@ "create_from_ocl_url": "Crear desde URL de OCL", "api_url": "URL de la API", "load_more": "Cargar más", + "show_more_of": "Mostrar más ({{shown}} de {{total}})", + "could_not_load": "No se pudo cargar esta lista.", + "try_again": "Intentar de nuevo", "generate_with_ai": "Generar con el asistente de IA de OCL", "logo": "Logo", "about_page": "Página Acerca de", @@ -741,7 +744,6 @@ "mapping_projects": "Proyectos de Mapeo", "project_name": "Nombre del Proyecto", "no_projects_found": "Aún no hay proyectos de mapeo.", - "could_not_load_projects": "No se pudieron cargar los proyectos de mapeo.", - "try_again": "Intentar de nuevo" + "could_not_load_projects": "No se pudieron cargar los proyectos de mapeo." } } diff --git a/src/i18n/locales/pt-BR/translations.json b/src/i18n/locales/pt-BR/translations.json index 99ec7a721..8cdffbcf0 100644 --- a/src/i18n/locales/pt-BR/translations.json +++ b/src/i18n/locales/pt-BR/translations.json @@ -166,6 +166,9 @@ "processing": "Processando", "processed": "Processado", "load_more": "Carregar mais", + "show_more_of": "Mostrar mais ({{shown}} de {{total}})", + "could_not_load": "Não foi possível carregar esta lista.", + "try_again": "Tentar novamente", "generate_with_ai": "Gerar com o Assistente de IA do OCL", "release": "Publicar", "unrelease": "Despublicar", @@ -1202,7 +1205,6 @@ "mapping_projects": "Projetos de Mapeamento", "project_name": "Nome do Projeto", "no_projects_found": "Ainda não há projetos de mapeamento.", - "could_not_load_projects": "Não foi possível carregar os projetos de mapeamento.", - "try_again": "Tentar novamente" + "could_not_load_projects": "Não foi possível carregar os projetos de mapeamento." } } diff --git a/src/i18n/locales/zh/translations.json b/src/i18n/locales/zh/translations.json index 1a0700afd..a2432dbd4 100644 --- a/src/i18n/locales/zh/translations.json +++ b/src/i18n/locales/zh/translations.json @@ -116,6 +116,9 @@ "custom": "自定义", "none": "无", "load_more": "加载更多", + "show_more_of": "显示更多({{shown}} / {{total}})", + "could_not_load": "无法加载此列表。", + "try_again": "重试", "generate_with_ai": "使用 OCL AI 助手生成", "generic_error": "出了些问题。", "something_went_wrong": "出了些问题", @@ -808,7 +811,6 @@ "mapping_projects": "映射项目", "project_name": "项目名称", "no_projects_found": "还没有映射项目。", - "could_not_load_projects": "无法加载映射项目。", - "try_again": "重试" + "could_not_load_projects": "无法加载映射项目。" } }