Skip to content

Commit 2a7b2e4

Browse files
authored
Command close and hide empty sites (#213)
1 parent 5527d93 commit 2a7b2e4

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

web/src/gui/command/CommandPalette.tsx

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useNavigate, useParams } from '@tanstack/react-router';
22
import { Command } from 'cmdk';
3-
import { FC, useEffect, useState } from 'react';
3+
import { createContext, FC, useContext, useEffect, useState } from 'react';
44
import { FiArrowRight, FiChevronRight } from 'react-icons/fi';
55

66
import { useMe, useSite, useTeam } from '@/api';
@@ -17,6 +17,18 @@ import {
1717
import { SiteEntries } from './entries/SiteEntry';
1818
import { TeamEntries } from './entries/TeamEntry';
1919

20+
interface CommandContextType {
21+
requestClose: () => void;
22+
}
23+
24+
const CommandContext = createContext<CommandContextType>({
25+
requestClose: () => {},
26+
});
27+
28+
export const useCommand = () => {
29+
return useContext(CommandContext);
30+
};
31+
2032
export const CommandPalette = () => {
2133
const [open, setOpen] = useState(false);
2234

@@ -34,28 +46,33 @@ export const CommandPalette = () => {
3446
return () => window.removeEventListener('keydown', down);
3547
}, []);
3648

49+
const value = {
50+
requestClose: () => setOpen(false),
51+
};
52+
3753
return (
38-
<ModalRoot open={open} onOpenChange={setOpen}>
39-
<ModalContent
40-
noPadding
41-
noCloseButton
42-
// frosted glass effect
43-
// noBg
44-
// className="dark:text-default bg-white/90 text-neutral-700 backdrop-blur-sm dark:bg-black/30"
45-
className=""
46-
>
47-
<CommandPaletteInternal requestClose={() => setOpen(false)} />
48-
</ModalContent>
49-
</ModalRoot>
54+
<CommandContext.Provider value={value}>
55+
<ModalRoot open={open} onOpenChange={setOpen}>
56+
<ModalContent
57+
noPadding
58+
noCloseButton
59+
// frosted glass effect
60+
// noBg
61+
// className="dark:text-default bg-white/90 text-neutral-700 backdrop-blur-sm dark:bg-black/30"
62+
className=""
63+
>
64+
<CommandPaletteInternal />
65+
</ModalContent>
66+
</ModalRoot>
67+
</CommandContext.Provider>
5068
);
5169
};
5270

53-
const CommandPaletteInternal: FC<{ requestClose: () => void }> = ({
54-
requestClose,
55-
}) => {
71+
const CommandPaletteInternal: FC = () => {
5672
const routeParameters = useParams({ from: undefined as any });
5773
const { data: site } = useSite(routeParameters['siteId']);
5874
const { data: team } = useTeam(routeParameters['teamId'] || site?.team_id);
75+
const { requestClose } = useCommand();
5976
const { data: user } = useMe();
6077
const navigate = useNavigate();
6178

web/src/gui/command/entries/SiteEntry.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
useTeamSites,
1313
} from '@/api';
1414

15+
import { useCommand } from '../CommandPalette';
16+
1517
export const SiteEntry: FC<{ site_id: string }> = ({ site_id }) => {
1618
const { data: site } = useSite(site_id);
1719
const { data: deployments } = useSiteDeployments(site_id);
@@ -20,6 +22,7 @@ export const SiteEntry: FC<{ site_id: string }> = ({ site_id }) => {
2022
deployments?.[0]?.deployment_id ?? ''
2123
);
2224
const navigate = useNavigate();
25+
const { requestClose } = useCommand();
2326

2427
if (!site) return <></>;
2528

@@ -34,6 +37,7 @@ export const SiteEntry: FC<{ site_id: string }> = ({ site_id }) => {
3437
siteId: site_id,
3538
},
3639
});
40+
requestClose();
3741
}}
3842
>
3943
<div className="flex items-center gap-2">
@@ -48,7 +52,7 @@ export const SiteEntry: FC<{ site_id: string }> = ({ site_id }) => {
4852
)}
4953
<div>{site.name}</div>
5054
</div>
51-
<div className="text-muted flex items-center gap-1">
55+
<div className="flex items-center gap-1 text-muted">
5256
<FiGlobe className="text-sm" />
5357
<span>site</span>
5458
</div>
@@ -60,7 +64,7 @@ export const TeamSiteEntries: FC<{ team_id: string }> = ({ team_id }) => {
6064
const { data: team } = useTeam(team_id);
6165
const { data: sites } = useTeamSites(team_id);
6266

63-
if (!team || !sites) return <></>;
67+
if (!team || !sites || sites.length === 0) return <></>;
6468

6569
return (
6670
<Command.Group heading={team.name}>

web/src/gui/command/entries/TeamEntry.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import { FiUsers } from 'react-icons/fi';
66
import { useTeam, useTeams } from '@/api';
77
import { Avatar } from '@/components';
88

9+
import { useCommand } from '../CommandPalette';
10+
911
export const TeamEntry: FC<{ team_id: string }> = ({ team_id }) => {
1012
const { data: team } = useTeam(team_id);
1113
const navigate = useNavigate();
14+
const { requestClose } = useCommand();
1215

1316
if (!team) return <></>;
1417

@@ -23,6 +26,7 @@ export const TeamEntry: FC<{ team_id: string }> = ({ team_id }) => {
2326
teamId: team_id,
2427
},
2528
});
29+
requestClose();
2630
}}
2731
>
2832
<div className="flex w-full items-center gap-2">
@@ -32,7 +36,7 @@ export const TeamEntry: FC<{ team_id: string }> = ({ team_id }) => {
3236

3337
<div>{team.name}</div>
3438
</div>
35-
<div className="text-muted flex items-center gap-1">
39+
<div className="flex items-center gap-1 text-muted">
3640
<FiUsers className="text-sm" />
3741
<span>team</span>
3842
</div>

0 commit comments

Comments
 (0)