Add infinite scroll support to app list and dataset list.tags/0.2.2
| 'use client' | 'use client' | ||||
| import type { FC } from 'react' | |||||
| import { FC, useRef } from 'react' | |||||
| import React, { useEffect, useState } from 'react' | import React, { useEffect, useState } from 'react' | ||||
| import { usePathname, useRouter, useSelectedLayoutSegments } from 'next/navigation' | import { usePathname, useRouter, useSelectedLayoutSegments } from 'next/navigation' | ||||
| import useSWR, { SWRConfig } from 'swr' | import useSWR, { SWRConfig } from 'swr' | ||||
| import { fetchDatasets } from '@/service/datasets' | import { fetchDatasets } from '@/service/datasets' | ||||
| import { fetchLanggeniusVersion, fetchUserProfile, logout } from '@/service/common' | import { fetchLanggeniusVersion, fetchUserProfile, logout } from '@/service/common' | ||||
| import Loading from '@/app/components/base/loading' | import Loading from '@/app/components/base/loading' | ||||
| import AppContext from '@/context/app-context' | |||||
| import { AppContextProvider } from '@/context/app-context' | |||||
| import DatasetsContext from '@/context/datasets-context' | import DatasetsContext from '@/context/datasets-context' | ||||
| import type { LangGeniusVersionResponse, UserProfileResponse } from '@/models/common' | import type { LangGeniusVersionResponse, UserProfileResponse } from '@/models/common' | ||||
| const pattern = pathname.replace(/.*\/app\//, '') | const pattern = pathname.replace(/.*\/app\//, '') | ||||
| const [idOrMethod] = pattern.split('/') | const [idOrMethod] = pattern.split('/') | ||||
| const isNotDetailPage = idOrMethod === 'list' | const isNotDetailPage = idOrMethod === 'list' | ||||
| const pageContainerRef = useRef<HTMLDivElement>(null) | |||||
| const appId = isNotDetailPage ? '' : idOrMethod | const appId = isNotDetailPage ? '' : idOrMethod | ||||
| <SWRConfig value={{ | <SWRConfig value={{ | ||||
| shouldRetryOnError: false | shouldRetryOnError: false | ||||
| }}> | }}> | ||||
| <AppContext.Provider value={{ apps: appList.data, mutateApps, userProfile, mutateUserProfile }}> | |||||
| <DatasetsContext.Provider value={{ datasets: datasetList?.data || [], mutateDatasets, currentDataset }}> | |||||
| <div className='relative flex flex-col h-full overflow-scroll bg-gray-100'> | |||||
| <AppContextProvider value={{ apps: appList.data, mutateApps, userProfile, mutateUserProfile, pageContainerRef }}> | |||||
| <DatasetsContext.Provider value={{ datasets: datasetList?.data || [], mutateDatasets, currentDataset }}> | |||||
| <div ref={pageContainerRef} className='relative flex flex-col h-full overflow-auto bg-gray-100'> | |||||
| <Header isBordered={['/apps', '/datasets'].includes(pathname)} curApp={curApp as any} appItems={appList.data} userProfile={userProfile} onLogout={onLogout} langeniusVersionInfo={langeniusVersionInfo} /> | <Header isBordered={['/apps', '/datasets'].includes(pathname)} curApp={curApp as any} appItems={appList.data} userProfile={userProfile} onLogout={onLogout} langeniusVersionInfo={langeniusVersionInfo} /> | ||||
| {children} | {children} | ||||
| </div> | </div> | ||||
| </DatasetsContext.Provider> | </DatasetsContext.Provider> | ||||
| </AppContext.Provider> | |||||
| </AppContextProvider> | |||||
| </SWRConfig> | </SWRConfig> | ||||
| ) | ) | ||||
| } | } |
| 'use client' | 'use client' | ||||
| import { useEffect } from 'react' | |||||
| import { useEffect, useRef } from 'react' | |||||
| import useSWRInfinite from 'swr/infinite' | |||||
| import { debounce } from 'lodash-es' | |||||
| import AppCard from './AppCard' | import AppCard from './AppCard' | ||||
| import NewAppCard from './NewAppCard' | import NewAppCard from './NewAppCard' | ||||
| import { useAppContext } from '@/context/app-context' | |||||
| import { AppListResponse } from '@/models/app' | |||||
| import { fetchAppList } from '@/service/apps' | |||||
| import { useSelector } from '@/context/app-context' | |||||
| const getKey = (pageIndex: number, previousPageData: AppListResponse) => { | |||||
| if (!pageIndex || previousPageData.has_more) | |||||
| return { url: 'apps', params: { page: pageIndex + 1, limit: 30 } } | |||||
| return null | |||||
| } | |||||
| const Apps = () => { | const Apps = () => { | ||||
| const { apps, mutateApps } = useAppContext() | |||||
| const { data, isLoading, setSize, mutate } = useSWRInfinite(getKey, fetchAppList, { revalidateFirstPage: false }) | |||||
| const loadingStateRef = useRef(false) | |||||
| const pageContainerRef = useSelector(state => state.pageContainerRef) | |||||
| const anchorRef = useRef<HTMLAnchorElement>(null) | |||||
| useEffect(() => { | |||||
| loadingStateRef.current = isLoading | |||||
| }, [isLoading]) | |||||
| useEffect(() => { | useEffect(() => { | ||||
| mutateApps() | |||||
| const onScroll = debounce(() => { | |||||
| if (!loadingStateRef.current) { | |||||
| const { scrollTop, clientHeight } = pageContainerRef.current! | |||||
| const anchorOffset = anchorRef.current!.offsetTop | |||||
| if (anchorOffset - scrollTop - clientHeight < 100) { | |||||
| setSize(size => size + 1) | |||||
| } | |||||
| } | |||||
| }, 50) | |||||
| pageContainerRef.current?.addEventListener('scroll', onScroll) | |||||
| return () => pageContainerRef.current?.removeEventListener('scroll', onScroll) | |||||
| }, []) | }, []) | ||||
| return ( | return ( | ||||
| <nav className='grid content-start grid-cols-1 gap-4 px-12 pt-8 sm:grid-cols-2 lg:grid-cols-4 grow shrink-0'> | <nav className='grid content-start grid-cols-1 gap-4 px-12 pt-8 sm:grid-cols-2 lg:grid-cols-4 grow shrink-0'> | ||||
| {apps.map(app => (<AppCard key={app.id} app={app} />))} | |||||
| <NewAppCard /> | |||||
| {data?.map(({ data: apps }) => apps.map(app => (<AppCard key={app.id} app={app} />)))} | |||||
| <NewAppCard ref={anchorRef} onSuccess={mutate} /> | |||||
| </nav> | </nav> | ||||
| ) | ) | ||||
| } | } | ||||
| 'use client' | 'use client' | ||||
| import { useState } from 'react' | |||||
| import { forwardRef, useState } from 'react' | |||||
| import classNames from 'classnames' | import classNames from 'classnames' | ||||
| import { useTranslation } from 'react-i18next' | import { useTranslation } from 'react-i18next' | ||||
| import style from '../list.module.css' | import style from '../list.module.css' | ||||
| import NewAppDialog from './NewAppDialog' | import NewAppDialog from './NewAppDialog' | ||||
| const CreateAppCard = () => { | |||||
| export type CreateAppCardProps = { | |||||
| onSuccess?: () => void | |||||
| } | |||||
| const CreateAppCard = forwardRef<HTMLAnchorElement, CreateAppCardProps>(({ onSuccess }, ref) => { | |||||
| const { t } = useTranslation() | const { t } = useTranslation() | ||||
| const [showNewAppDialog, setShowNewAppDialog] = useState(false) | const [showNewAppDialog, setShowNewAppDialog] = useState(false) | ||||
| return ( | return ( | ||||
| <a className={classNames(style.listItem, style.newItemCard)} onClick={() => setShowNewAppDialog(true)}> | |||||
| <a ref={ref} className={classNames(style.listItem, style.newItemCard)} onClick={() => setShowNewAppDialog(true)}> | |||||
| <div className={style.listItemTitle}> | <div className={style.listItemTitle}> | ||||
| <span className={style.newItemIcon}> | <span className={style.newItemIcon}> | ||||
| <span className={classNames(style.newItemIconImage, style.newItemIconAdd)} /> | <span className={classNames(style.newItemIconImage, style.newItemIconAdd)} /> | ||||
| </div> | </div> | ||||
| </div> | </div> | ||||
| {/* <div className='text-xs text-gray-500'>{t('app.createFromConfigFile')}</div> */} | {/* <div className='text-xs text-gray-500'>{t('app.createFromConfigFile')}</div> */} | ||||
| <NewAppDialog show={showNewAppDialog} onClose={() => setShowNewAppDialog(false)} /> | |||||
| <NewAppDialog show={showNewAppDialog} onSuccess={onSuccess} onClose={() => setShowNewAppDialog(false)} /> | |||||
| </a> | </a> | ||||
| ) | ) | ||||
| } | |||||
| }) | |||||
| export default CreateAppCard | export default CreateAppCard |
| type NewAppDialogProps = { | type NewAppDialogProps = { | ||||
| show: boolean | show: boolean | ||||
| onSuccess?: () => void | |||||
| onClose?: () => void | onClose?: () => void | ||||
| } | } | ||||
| const NewAppDialog = ({ show, onClose }: NewAppDialogProps) => { | |||||
| const NewAppDialog = ({ show, onSuccess, onClose }: NewAppDialogProps) => { | |||||
| const router = useRouter() | const router = useRouter() | ||||
| const { notify } = useContext(ToastContext) | const { notify } = useContext(ToastContext) | ||||
| const { t } = useTranslation() | const { t } = useTranslation() | ||||
| mode: isWithTemplate ? templates.data[selectedTemplateIndex].mode : newAppMode!, | mode: isWithTemplate ? templates.data[selectedTemplateIndex].mode : newAppMode!, | ||||
| config: isWithTemplate ? templates.data[selectedTemplateIndex].model_config : undefined, | config: isWithTemplate ? templates.data[selectedTemplateIndex].model_config : undefined, | ||||
| }) | }) | ||||
| if (onSuccess) | |||||
| onSuccess() | |||||
| if (onClose) | if (onClose) | ||||
| onClose() | onClose() | ||||
| notify({ type: 'success', message: t('app.newApp.appCreated') }) | notify({ type: 'success', message: t('app.newApp.appCreated') }) |
| 'use client' | 'use client' | ||||
| import { useEffect } from 'react' | |||||
| import useSWR from 'swr' | |||||
| import { DataSet } from '@/models/datasets'; | |||||
| import { useEffect, useRef } from 'react' | |||||
| import useSWRInfinite from 'swr/infinite' | |||||
| import { debounce } from 'lodash-es'; | |||||
| import { DataSetListResponse } from '@/models/datasets'; | |||||
| import NewDatasetCard from './NewDatasetCard' | import NewDatasetCard from './NewDatasetCard' | ||||
| import DatasetCard from './DatasetCard'; | import DatasetCard from './DatasetCard'; | ||||
| import { fetchDatasets } from '@/service/datasets'; | import { fetchDatasets } from '@/service/datasets'; | ||||
| import { useSelector } from '@/context/app-context'; | |||||
| const getKey = (pageIndex: number, previousPageData: DataSetListResponse) => { | |||||
| if (!pageIndex || previousPageData.has_more) | |||||
| return { url: 'datasets', params: { page: pageIndex + 1, limit: 30 } } | |||||
| return null | |||||
| } | |||||
| const Datasets = () => { | const Datasets = () => { | ||||
| // const { datasets, mutateDatasets } = useAppContext() | |||||
| const { data: datasetList, mutate: mutateDatasets } = useSWR({ url: '/datasets', params: { page: 1 } }, fetchDatasets) | |||||
| const { data, isLoading, setSize, mutate } = useSWRInfinite(getKey, fetchDatasets, { revalidateFirstPage: false }) | |||||
| const loadingStateRef = useRef(false) | |||||
| const pageContainerRef = useSelector(state => state.pageContainerRef) | |||||
| const anchorRef = useRef<HTMLAnchorElement>(null) | |||||
| useEffect(() => { | useEffect(() => { | ||||
| mutateDatasets() | |||||
| loadingStateRef.current = isLoading | |||||
| }, [isLoading]) | |||||
| useEffect(() => { | |||||
| const onScroll = debounce(() => { | |||||
| if (!loadingStateRef.current) { | |||||
| const { scrollTop, clientHeight } = pageContainerRef.current! | |||||
| const anchorOffset = anchorRef.current!.offsetTop | |||||
| if (anchorOffset - scrollTop - clientHeight < 100) { | |||||
| setSize(size => size + 1) | |||||
| } | |||||
| } | |||||
| }, 50) | |||||
| pageContainerRef.current?.addEventListener('scroll', onScroll) | |||||
| return () => pageContainerRef.current?.removeEventListener('scroll', onScroll) | |||||
| }, []) | }, []) | ||||
| return ( | return ( | ||||
| <nav className='grid content-start grid-cols-1 gap-4 px-12 pt-8 sm:grid-cols-2 lg:grid-cols-4 grow shrink-0'> | <nav className='grid content-start grid-cols-1 gap-4 px-12 pt-8 sm:grid-cols-2 lg:grid-cols-4 grow shrink-0'> | ||||
| {datasetList?.data.map(dataset => (<DatasetCard key={dataset.id} dataset={dataset} />))} | |||||
| <NewDatasetCard /> | |||||
| {data?.map(({ data: datasets }) => datasets.map(dataset => (<DatasetCard key={dataset.id} dataset={dataset} />)))} | |||||
| <NewDatasetCard ref={anchorRef} /> | |||||
| </nav> | </nav> | ||||
| ) | ) | ||||
| } | } |
| 'use client' | 'use client' | ||||
| import { useState } from 'react' | |||||
| import { forwardRef, useState } from 'react' | |||||
| import classNames from 'classnames' | import classNames from 'classnames' | ||||
| import { useTranslation } from 'react-i18next' | import { useTranslation } from 'react-i18next' | ||||
| import style from '../list.module.css' | import style from '../list.module.css' | ||||
| const CreateAppCard = () => { | |||||
| const CreateAppCard = forwardRef<HTMLAnchorElement>((_, ref) => { | |||||
| const { t } = useTranslation() | const { t } = useTranslation() | ||||
| const [showNewAppDialog, setShowNewAppDialog] = useState(false) | const [showNewAppDialog, setShowNewAppDialog] = useState(false) | ||||
| return ( | return ( | ||||
| <a className={classNames(style.listItem, style.newItemCard)} href='/datasets/create'> | |||||
| <a ref={ref} className={classNames(style.listItem, style.newItemCard)} href='/datasets/create'> | |||||
| <div className={style.listItemTitle}> | <div className={style.listItemTitle}> | ||||
| <span className={style.newItemIcon}> | <span className={style.newItemIcon}> | ||||
| <span className={classNames(style.newItemIconImage, style.newItemIconAdd)} /> | <span className={classNames(style.newItemIconImage, style.newItemIconAdd)} /> | ||||
| {/* <div className='text-xs text-gray-500'>{t('app.createFromConfigFile')}</div> */} | {/* <div className='text-xs text-gray-500'>{t('app.createFromConfigFile')}</div> */} | ||||
| </a> | </a> | ||||
| ) | ) | ||||
| } | |||||
| }) | |||||
| export default CreateAppCard | export default CreateAppCard |
| const close = useCallback(() => onClose?.(), [onClose]) | const close = useCallback(() => onClose?.(), [onClose]) | ||||
| return ( | return ( | ||||
| <Transition appear show={show} as={Fragment}> | <Transition appear show={show} as={Fragment}> | ||||
| <Dialog as="div" className="relative z-10" onClose={close}> | |||||
| <Dialog as="div" className="relative z-40" onClose={close}> | |||||
| <Transition.Child | <Transition.Child | ||||
| as={Fragment} | as={Fragment} | ||||
| enter="ease-out duration-300" | enter="ease-out duration-300" |
| 'use client' | |||||
| import { createContext, useContext } from 'use-context-selector' | |||||
| import type { App } from '@/types/app' | |||||
| import type { UserProfileResponse } from '@/models/common' | |||||
| export type AppContextValue = { | |||||
| apps: App[] | |||||
| mutateApps: () => void | |||||
| userProfile: UserProfileResponse | |||||
| mutateUserProfile: () => void | |||||
| } | |||||
| const AppContext = createContext<AppContextValue>({ | |||||
| apps: [], | |||||
| mutateApps: () => { }, | |||||
| userProfile: { | |||||
| id: '', | |||||
| name: '', | |||||
| email: '', | |||||
| }, | |||||
| mutateUserProfile: () => { }, | |||||
| }) | |||||
| export const useAppContext = () => useContext(AppContext) | |||||
| export default AppContext |
| 'use client' | |||||
| import { createContext, useContext, useContextSelector } from 'use-context-selector' | |||||
| import type { App } from '@/types/app' | |||||
| import type { UserProfileResponse } from '@/models/common' | |||||
| import { createRef, FC, PropsWithChildren } from 'react' | |||||
| export const useSelector = <T extends any>(selector: (value: AppContextValue) => T): T => | |||||
| useContextSelector(AppContext, selector); | |||||
| export type AppContextValue = { | |||||
| apps: App[] | |||||
| mutateApps: () => void | |||||
| userProfile: UserProfileResponse | |||||
| mutateUserProfile: () => void | |||||
| pageContainerRef: React.RefObject<HTMLDivElement>, | |||||
| useSelector: typeof useSelector, | |||||
| } | |||||
| const AppContext = createContext<AppContextValue>({ | |||||
| apps: [], | |||||
| mutateApps: () => { }, | |||||
| userProfile: { | |||||
| id: '', | |||||
| name: '', | |||||
| email: '', | |||||
| }, | |||||
| mutateUserProfile: () => { }, | |||||
| pageContainerRef: createRef(), | |||||
| useSelector, | |||||
| }) | |||||
| export type AppContextProviderProps = PropsWithChildren<{ | |||||
| value: Omit<AppContextValue, 'useSelector'> | |||||
| }> | |||||
| export const AppContextProvider: FC<AppContextProviderProps> = ({ value, children }) => ( | |||||
| <AppContext.Provider value={{ ...value, useSelector }}> | |||||
| {children} | |||||
| </AppContext.Provider> | |||||
| ) | |||||
| export const useAppContext = () => useContext(AppContext) | |||||
| export default AppContext |
| export type AppListResponse = { | export type AppListResponse = { | ||||
| data: App[] | data: App[] | ||||
| has_more: boolean | |||||
| limit: number | |||||
| page: number | |||||
| total: number | |||||
| } | } | ||||
| export type AppDetailResponse = App | export type AppDetailResponse = App |
| export type DataSetListResponse = { | export type DataSetListResponse = { | ||||
| data: DataSet[] | data: DataSet[] | ||||
| has_more: boolean | |||||
| limit: number | |||||
| page: number | |||||
| total: number | |||||
| } | } | ||||
| export type IndexingEstimateResponse = { | export type IndexingEstimateResponse = { |
| import type { CommonResponse } from '@/models/common' | import type { CommonResponse } from '@/models/common' | ||||
| import type { AppMode, ModelConfig } from '@/types/app' | import type { AppMode, ModelConfig } from '@/types/app' | ||||
| export const fetchAppList: Fetcher<AppListResponse, { params?: Record<string, any> }> = ({ params }) => { | |||||
| return get('apps', params) as Promise<AppListResponse> | |||||
| export const fetchAppList: Fetcher<AppListResponse, { url: string; params?: Record<string, any> }> = ({ url, params }) => { | |||||
| return get(url, { params }) as Promise<AppListResponse> | |||||
| } | } | ||||
| export const fetchAppDetail: Fetcher<AppDetailResponse, { url: string; id: string }> = ({ url, id }) => { | export const fetchAppDetail: Fetcher<AppDetailResponse, { url: string; id: string }> = ({ url, id }) => { |