|
| 1 | +import { Runtime, useGetRuntimeRoflApps } from 'oasis-nexus/api' |
| 2 | +import { TableLayout } from '../../components/TableLayoutButton' |
| 3 | +import { useCallback, useEffect, useState } from 'react' |
| 4 | +import { useScreenSize } from '../../hooks/useScreensize' |
| 5 | +import { useSearchParamsPagination } from '../../components/Table/useSearchParamsPagination' |
| 6 | +import { NUMBER_OF_ITEMS_ON_SEPARATE_PAGE } from '../../../config' |
| 7 | +import { TablePaginationProps } from '../../components/Table/TablePagination' |
| 8 | +import { Network } from '../../../types/network' |
| 9 | +import { useTranslation } from 'react-i18next' |
| 10 | +import { useTypedSearchParam } from '../../hooks/useTypedSearchParam' |
| 11 | +import { textSearch } from '../../components/Search/search-utils' |
| 12 | +import { getHighlightPattern } from '../../components/Search/search-utils' |
| 13 | +import { useNavigate, useSearchParams } from 'react-router-dom' |
| 14 | +import { encodeURIComponentPretty, RouteUtils } from '../../utils/route-utils' |
| 15 | + |
| 16 | +const limit = NUMBER_OF_ITEMS_ON_SEPARATE_PAGE |
| 17 | + |
| 18 | +export const useTableViewMode = () => { |
| 19 | + const { isMobile } = useScreenSize() |
| 20 | + const [tableView, setTableView] = useState<TableLayout>(TableLayout.Horizontal) |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + if (!isMobile) { |
| 24 | + setTableView(TableLayout.Horizontal) |
| 25 | + } |
| 26 | + }, [isMobile, setTableView]) |
| 27 | + |
| 28 | + return { tableView, setTableView } |
| 29 | +} |
| 30 | + |
| 31 | +const ROFL_SEARCH_ARG_NAME = 'name' |
| 32 | + |
| 33 | +export const useROFLAppFiltering = () => { |
| 34 | + const setSearchParams = useSearchParams()[1] |
| 35 | + const { t } = useTranslation() |
| 36 | + const [wantedNameInput, setWantedNameInput] = useTypedSearchParam(ROFL_SEARCH_ARG_NAME, '', { |
| 37 | + deleteParams: ['page'], |
| 38 | + }) |
| 39 | + const search = textSearch.roflAppName(wantedNameInput, t) |
| 40 | + const { result: wantedNamePattern, warning: nameError } = search |
| 41 | + const highlightPattern = getHighlightPattern(search) |
| 42 | + const hasFilters = !!wantedNamePattern.length |
| 43 | + const clearFilters = () => { |
| 44 | + setSearchParams(searchParams => { |
| 45 | + searchParams.delete('name') |
| 46 | + searchParams.delete('page') |
| 47 | + return searchParams |
| 48 | + }) |
| 49 | + } |
| 50 | + return { |
| 51 | + wantedNameInput, |
| 52 | + setWantedNameInput, |
| 53 | + nameError, |
| 54 | + wantedNamePattern, |
| 55 | + highlightPattern, |
| 56 | + hasFilters, |
| 57 | + clearFilters, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +export const useRoflApps = (network: Network, layer: Runtime) => { |
| 62 | + const { tableView } = useTableViewMode() |
| 63 | + const navigate = useNavigate() |
| 64 | + const pagination = useSearchParamsPagination('page') |
| 65 | + const { wantedNameInput, wantedNamePattern, hasFilters } = useROFLAppFiltering() |
| 66 | + const offset = (pagination.selectedPage - 1) * limit |
| 67 | + const roflAppsQuery = useGetRuntimeRoflApps(network, layer, { |
| 68 | + name: wantedNamePattern, |
| 69 | + limit: tableView === TableLayout.Vertical ? offset + limit : limit, |
| 70 | + offset: tableView === TableLayout.Vertical ? 0 : offset, |
| 71 | + }) |
| 72 | + const { isLoading, isFetched, data } = roflAppsQuery |
| 73 | + const roflApps = data?.data.rofl_apps |
| 74 | + |
| 75 | + const tablePagination: TablePaginationProps = { |
| 76 | + selectedPage: pagination.selectedPage, |
| 77 | + linkToPage: pagination.linkToPage, |
| 78 | + totalCount: data?.data.total_count, |
| 79 | + isTotalCountClipped: data?.data.is_total_count_clipped, |
| 80 | + rowsPerPage: limit, |
| 81 | + } |
| 82 | + |
| 83 | + const hasData = !!roflApps?.length |
| 84 | + const isOnFirstPage = pagination.selectedPage === 1 |
| 85 | + |
| 86 | + const hasNoResultsOnSelectedPage = isFetched && !isOnFirstPage && !hasData |
| 87 | + const hasNoResultsBecauseOfFilters = !isLoading && !hasData && isOnFirstPage && hasFilters |
| 88 | + |
| 89 | + const jumpToSingleResult = useCallback(() => { |
| 90 | + // If we only have a single result |
| 91 | + if (hasData && isOnFirstPage && roflApps?.length === 1) { |
| 92 | + // Then let's jump to it |
| 93 | + const path = `${RouteUtils.getRoflAppRoute(network, roflApps![0].id)}?q=${encodeURIComponentPretty(wantedNameInput)}` |
| 94 | + navigate(path) |
| 95 | + } |
| 96 | + }, [hasData, isOnFirstPage, roflApps, navigate, network, wantedNameInput]) |
| 97 | + |
| 98 | + return { |
| 99 | + isLoading, |
| 100 | + limit, |
| 101 | + roflApps, |
| 102 | + pagination, |
| 103 | + tablePagination, |
| 104 | + hasNoResultsOnSelectedPage, |
| 105 | + hasNoResultsBecauseOfFilters, |
| 106 | + jumpToSingleResult, |
| 107 | + } |
| 108 | +} |
0 commit comments