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

refactor: move useRecentSessionHistory to a separate file #2763

Merged
merged 1 commit into from
Oct 21, 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
2 changes: 1 addition & 1 deletion react/src/components/SessionTemplateModal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useBackendAIImageMetaData } from '../hooks';
import { useRecentSessionHistory } from '../hooks/backendai';
import { useRecentSessionHistory } from '../hooks/useRecentSessionHistory';
import {
ResourceNumbersOfSession,
SessionLauncherFormValue,
Expand Down
48 changes: 1 addition & 47 deletions react/src/hooks/backendai.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { useSuspendedBackendaiClient, useUpdatableState } from '.';
import {
generateRandomString,
maskString,
useBaiSignedRequestWithPromise,
} from '../helper';
import { maskString, useBaiSignedRequestWithPromise } from '../helper';
import {
useSuspenseTanQuery,
useTanMutation,
useTanQuery,
} from './reactQueryAlias';
import { SessionHistory, useBAISettingUserState } from './useBAISetting';
import { useEventNotStable } from './useEventNotStable';
import _ from 'lodash';
import { useCallback, useEffect, useMemo, useState } from 'react';

Expand Down Expand Up @@ -310,43 +304,3 @@ export const useAllowedHostNames = () => {
});
return allowedHosts?.allowed;
};

export const useRecentSessionHistory = () => {
const [recentSessionHistory, setRecentSessionHistory] =
useBAISettingUserState('recentSessionHistory');

const push = useEventNotStable(
({
id,
params,
createdAt,
}: SelectivePartial<SessionHistory, 'id' | 'createdAt'>) => {
const newHistory: SessionHistory = {
id: id ?? generateRandomString(8),
params,
createdAt: createdAt ?? new Date().toISOString(),
};
// push new history to the top of recentSessionHistory and keep it up to 5
const newRecentSessionHistory = [
newHistory,
...(recentSessionHistory || []),
].slice(0, 5);
setRecentSessionHistory(newRecentSessionHistory);
},
);
const clear = useEventNotStable(() => setRecentSessionHistory([]));
const remove = useEventNotStable((id: string) => {
const newRecentSessionHistory = (recentSessionHistory || []).filter(
(item) => item.id !== id,
);
setRecentSessionHistory(newRecentSessionHistory);
});
return [
recentSessionHistory,
{
push,
clear,
remove,
},
] as const;
};
43 changes: 43 additions & 0 deletions react/src/hooks/useRecentSessionHistory.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { generateRandomString } from '../helper';
import { SessionHistory, useBAISettingUserState } from './useBAISetting';
import { useEventNotStable } from './useEventNotStable';

export const useRecentSessionHistory = () => {
const [recentSessionHistory, setRecentSessionHistory] =
useBAISettingUserState('recentSessionHistory');

const push = useEventNotStable(
({
id,
params,
createdAt,
}: SelectivePartial<SessionHistory, 'id' | 'createdAt'>) => {
const newHistory: SessionHistory = {
id: id ?? generateRandomString(8),
params,
createdAt: createdAt ?? new Date().toISOString(),
};
// push new history to the top of recentSessionHistory and keep it up to 5
const newRecentSessionHistory = [
newHistory,
...(recentSessionHistory || []),
].slice(0, 5);
setRecentSessionHistory(newRecentSessionHistory);
},
);
const clear = useEventNotStable(() => setRecentSessionHistory([]));
const remove = useEventNotStable((id: string) => {
const newRecentSessionHistory = (recentSessionHistory || []).filter(
(item) => item.id !== id,
);
setRecentSessionHistory(newRecentSessionHistory);
});
return [
recentSessionHistory,
{
push,
clear,
remove,
},
] as const;
};
14 changes: 4 additions & 10 deletions react/src/pages/SessionLauncherPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ import {
useUpdatableState,
useWebUINavigate,
} from '../hooks';
import {
useCurrentUserRole,
useRecentSessionHistory,
} from '../hooks/backendai';
import { useCurrentUserRole } from '../hooks/backendai';
import { useSetBAINotification } from '../hooks/useBAINotification';
import { useCurrentProjectValue } from '../hooks/useCurrentProject';
import { useRecentSessionHistory } from '../hooks/useRecentSessionHistory';
import { useThemeMode } from '../hooks/useThemeMode';
// @ts-ignore
import customCSS from './SessionLauncherPage.css?raw';
Expand Down Expand Up @@ -296,11 +294,7 @@ const SessionLauncherPage = () => {
}, []);

const mergedInitialValues = useMemo(() => {
return _.merge(
{},
INITIAL_FORM_VALUES,
formValuesFromQueryParams,
);
return _.merge({}, INITIAL_FORM_VALUES, formValuesFromQueryParams);
}, [INITIAL_FORM_VALUES, formValuesFromQueryParams]);

// ScrollTo top when step is changed
Expand Down Expand Up @@ -685,7 +679,7 @@ const SessionLauncherPage = () => {
style={{ paddingRight: 0, paddingLeft: 0 }}
onClick={() => toggleIsOpenTemplateModal()}
>
{t('session.launcher.TemplateAndHistory')}
{t('session.launcher.RecentHistory')}
</Button>
</Flex>
</Flex>
Expand Down
Loading