Skip to content
Draft
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
87 changes: 38 additions & 49 deletions react/src/components/BAIComputeSessionNodeNotificationItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@ import SessionStatusTag from './ComputeSessionNodeItems/SessionStatusTag';
import { useUpdateEffect } from 'ahooks';
import { BAIFlex, BAILink, BAINotificationItem } from 'backend.ai-ui';
import dayjs from 'dayjs';
import React, { useEffect, useState } from 'react';
import React, { useEffect, useEffectEvent } from 'react';
import { useTranslation } from 'react-i18next';
import {
fetchQuery,
graphql,
useFragment,
useRelayEnvironment,
} from 'react-relay';
import { graphql, useRefetchableFragment } from 'react-relay';
import { BAIComputeSessionNodeNotificationItemFragment$key } from 'src/__generated__/BAIComputeSessionNodeNotificationItemFragment.graphql';
import { BAIComputeSessionNodeNotificationItemRefreshQuery } from 'src/__generated__/BAIComputeSessionNodeNotificationItemRefreshQuery.graphql';
import {
NotificationState,
useSetBAINotification,
Expand All @@ -27,11 +21,15 @@ interface BAINodeNotificationItemProps {
const BAIComputeSessionNodeNotificationItem: React.FC<
BAINodeNotificationItemProps
> = ({ sessionFrgmt, showDate, notification }) => {
const { destroyNotification } = useSetBAINotification();
'use memo';
Copy link

Copilot AI Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unknown directive: 'use memo'.

Suggested change
'use memo';

Copilot uses AI. Check for mistakes.
const { destroyNotification, upsertNotification } = useSetBAINotification();
const { t } = useTranslation();
const node = useFragment(
const [node, refetch] = useRefetchableFragment(
graphql`
fragment BAIComputeSessionNodeNotificationItemFragment on ComputeSessionNode {
fragment BAIComputeSessionNodeNotificationItemFragment on ComputeSessionNode
@refetchable(
queryName: "BAIComputeSessionNodeNotificationItemFragment_RefetchQuery"
) {
row_id
id
name
Expand All @@ -43,23 +41,22 @@ const BAIComputeSessionNodeNotificationItem: React.FC<
sessionFrgmt,
);

// TODO: delete this when Status subscription is implemented
const [delay, setDelay] = useState<number | null>(null);
UNSAFE_useAutoRefreshInterval(node?.id || '', delay);
const closeNotificationWithProgress = useEffectEvent(() => {
if (notification.open) {
upsertNotification({
key: notification.key,
pauseOnHover: true,
showProgress: true,
duration: 10,
});
}
});

useEffect(() => {
if (
!node?.status ||
node?.status === 'TERMINATED' ||
node?.status === 'CANCELLED'
) {
setDelay(null);
} else if (node?.status === 'RUNNING') {
setDelay(15000);
} else {
setDelay(3000);
if (node?.status === 'RUNNING') {
closeNotificationWithProgress();
}
}, [node?.status]);
Comment on lines 55 to 59
Copy link

Copilot AI Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dependency array is incomplete. closeNotificationWithProgress should be included in the dependencies, or if using the experimental useEffectEvent, you need to ensure the React version supports it. With current stable React, this could cause stale closure issues where closeNotificationWithProgress references outdated values of notification or upsertNotification.

Copilot uses AI. Check for mistakes.
// ---

useUpdateEffect(() => {
if (node?.status === 'TERMINATED' || node?.status === 'CANCELLED') {
Expand All @@ -69,6 +66,22 @@ const BAIComputeSessionNodeNotificationItem: React.FC<
}
}, [node?.status]);

const delay = ['TERMINATED', 'CANCELLED', null, undefined].includes(
node?.status,
)
? null
: node?.status === 'RUNNING'
? 15000
: 3000;
useInterval(() => {
refetch(
{},
{
fetchPolicy: 'store-and-network',
},
);
}, delay);

return (
node && (
<BAINotificationItem
Expand Down Expand Up @@ -117,27 +130,3 @@ const BAIComputeSessionNodeNotificationItem: React.FC<
};

export default BAIComputeSessionNodeNotificationItem;

const UNSAFE_useAutoRefreshInterval = (
sessionId: string,
delay: number | null,
) => {
// const [delay, setDelay] = useState<number|null>(3000);
const relayEnv = useRelayEnvironment();

useInterval(() => {
fetchQuery<BAIComputeSessionNodeNotificationItemRefreshQuery>(
relayEnv,
graphql`
query BAIComputeSessionNodeNotificationItemRefreshQuery(
$id: GlobalIDField!
) {
compute_session_node(id: $id) {
...BAINodeNotificationItemFragment
}
}
`,
{ id: sessionId },
).toPromise();
}, delay);
};