Skip to content

Commit 4bad245

Browse files
authored
Merge pull request #198 from docker/optimistic-updates-enable-toggle
UI optimistic updates for Enable/Disable MCP Server
2 parents 8f50ec2 + 2ad371c commit 4bad245

File tree

1 file changed

+15
-2
lines changed
  • src/extension/ui/src/components/tile

1 file changed

+15
-2
lines changed

src/extension/ui/src/components/tile/Top.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@ import { Avatar, CardHeader, Switch, Tooltip, Typography } from '@mui/material';
22

33
import { CatalogItemRichened } from '../../types/catalog';
44
import { formatName } from '../../formatName';
5-
import { format } from 'path';
5+
import { useEffect, useState } from 'react';
66

77
type TopProps = {
88
onToggleRegister: (checked: boolean) => void;
99
item: CatalogItemRichened;
1010
};
1111

1212
export default function Top({ item, onToggleRegister }: TopProps) {
13+
// For some unknown reason, item.registered is not updated right away when the user toggles the switch.
14+
// This `toggled` state is used to control the switch in the UI. Its main purpose is to do optimistic UI updates.
15+
// When the user toggles the switch. The `useEffect` hook is used to synchronize the `toggled` state with the `item.registered`
16+
// prop, which is the source of truth for the registration status of the item. This way, if the `item.registered` prop changes
17+
// (e.g., due to a successful registration or unregistration), the switch will reflect the correct state.
18+
const [toggled, setToggled] = useState(item.registered);
19+
20+
useEffect(() => {
21+
setToggled(item.registered);
22+
}, [item.registered]);
23+
1324
return (
1425
<CardHeader
1526
sx={{ padding: 0 }}
@@ -45,10 +56,12 @@ export default function Top({ item, onToggleRegister }: TopProps) {
4556
}
4657
>
4758
<Switch
48-
checked={item.registered}
59+
checked={toggled}
4960
onChange={(event, checked) => {
5061
event.stopPropagation();
5162
event.preventDefault();
63+
64+
setToggled(checked);
5265
onToggleRegister(checked);
5366
}}
5467
/>

0 commit comments

Comments
 (0)