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

fix: reset Model Registry page number on pageload [ET-640] #9876

Merged
merged 3 commits into from
Aug 30, 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
7 changes: 1 addition & 6 deletions webui/react/src/components/ModelRegistry.settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const isOfSortKey = (sortKey: React.Key): sortKey is V1GetModelsRequestSo
return sortKeys.includes(String(sortKey));
};

export interface Settings extends InteractiveTableSettings {
export interface Settings extends Omit<InteractiveTableSettings, 'tableOffset'> {
archived?: boolean;
columns: ModelColumnName[];
description?: string;
Expand Down Expand Up @@ -122,11 +122,6 @@ const config = (id: string): SettingsConfig<Settings> => {
storageKey: 'tableLimit',
type: number,
},
tableOffset: {
defaultValue: 0,
storageKey: 'tableOffset',
type: number,
},
tags: {
defaultValue: undefined,
storageKey: 'tags',
Expand Down
44 changes: 34 additions & 10 deletions webui/react/src/components/ModelRegistry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Toggle from 'hew/Toggle';
import Tooltip from 'hew/Tooltip';
import { Label } from 'hew/Typography';
import { Loadable } from 'hew/utils/loadable';
import * as io from 'io-ts';
import _ from 'lodash';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';

Expand All @@ -43,7 +44,8 @@ import TableFilterSearch from 'components/Table/TableFilterSearch';
import WorkspaceFilter from 'components/WorkspaceFilter';
import usePermissions from 'hooks/usePermissions';
import usePolling from 'hooks/usePolling';
import { useSettings } from 'hooks/useSettings';
import { UpdateSettings, useSettings } from 'hooks/useSettings';
import useTypedParams from 'hooks/useTypedParams';
import { paths } from 'routes/utils';
import { archiveModel, getModelLabels, getModels, patchModel, unarchiveModel } from 'services/api';
import { V1GetModelsRequestSortBy } from 'services/api-ts-sdk';
Expand All @@ -61,7 +63,7 @@ import settingsConfig, {
DEFAULT_COLUMN_WIDTHS,
isOfSortKey,
ModelColumnName,
Settings,
Settings as ReducedSettings,
} from './ModelRegistry.settings';

const filterKeys: Array<keyof Settings> = ['tags', 'name', 'users', 'description', 'workspace'];
Expand All @@ -70,6 +72,11 @@ interface Props {
workspace?: Workspace;
}

const tableOffsetType = io.type({ tableOffset: io.number });
type Settings = ReducedSettings & io.TypeOf<typeof tableOffsetType>;

const DEFAULT_PARAMS = { tableOffset: 0 };

const ModelRegistry: React.FC<Props> = ({ workspace }: Props) => {
const canceler = useRef(new AbortController());
const users = Loadable.getOrElse([], useObservable(userStore.getUsers()));
Expand All @@ -93,9 +100,20 @@ const ModelRegistry: React.FC<Props> = ({ workspace }: Props) => {
activeSettings,
isLoading: isLoadingSettings,
settings,
updateSettings,
updateSettings: updateUseSettings,
resetSettings,
} = useSettings<Settings>(settingConfig);
} = useSettings<ReducedSettings>(settingConfig);

const { params, updateParams } = useTypedParams(tableOffsetType, DEFAULT_PARAMS);

const updateSettings: UpdateSettings<Settings> = useCallback(
(newSettings) => {
const { tableOffset, ...restSettings } = newSettings;
if (tableOffset) updateParams({ tableOffset });
updateUseSettings(restSettings);
},
[updateParams, updateUseSettings],
);

const [permissionsByModel, setPermissionsByModel] = useState<
Record<number, { canDelete: boolean; canModify: boolean }>
Expand Down Expand Up @@ -128,7 +146,7 @@ const ModelRegistry: React.FC<Props> = ({ workspace }: Props) => {
labels: settings.tags,
limit: settings.tableLimit,
name: settings.name,
offset: settings.tableOffset,
offset: params.tableOffset,
orderBy: settings.sortDesc ? 'ORDER_BY_DESC' : 'ORDER_BY_ASC',
sortBy: validateDetApiEnum(V1GetModelsRequestSortBy, settings.sortKey),
users: settings.users,
Expand All @@ -151,7 +169,7 @@ const ModelRegistry: React.FC<Props> = ({ workspace }: Props) => {
} finally {
setIsLoading(false);
}
}, [settings, workspace?.id]);
}, [params.tableOffset, settings, workspace?.id]);

const fetchTags = useCallback(async () => {
try {
Expand Down Expand Up @@ -391,8 +409,9 @@ const ModelRegistry: React.FC<Props> = ({ workspace }: Props) => {
}, []);

const resetFilters = useCallback(() => {
resetSettings([...filterKeys, 'tableOffset']);
}, [resetSettings]);
resetSettings(filterKeys);
updateParams(DEFAULT_PARAMS);
}, [resetSettings, updateParams]);

const ModelActionMenu = useCallback(
(record: ModelItem) => {
Expand Down Expand Up @@ -671,6 +690,11 @@ const ModelRegistry: React.FC<Props> = ({ workspace }: Props) => {
[settings, updateSettings],
);

const settingsWithOffset: Settings = useMemo(
() => ({ ...settings, tableOffset: params.tableOffset }),
[params.tableOffset, settings],
);

return (
<>
<div className={css.options}>
Expand Down Expand Up @@ -715,13 +739,13 @@ const ModelRegistry: React.FC<Props> = ({ workspace }: Props) => {
pagination={getFullPaginationConfig(
{
limit: settings.tableLimit,
offset: settings.tableOffset,
offset: params.tableOffset,
},
total,
)}
rowClassName={defaultRowClassName({ clickable: false })}
rowKey="name"
settings={settings}
settings={settingsWithOffset}
showSorterTooltip={false}
size="small"
updateSettings={updateSettings}
Expand Down
Loading