/
header.tsx
187 lines (178 loc) · 6.59 KB
/
header.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { ReactElement, useEffect, useState } from 'react';
import NextLink from 'next/link';
import clsx from 'clsx';
import { useQuery } from 'urql';
import { GetStartedProgress } from '@/components/get-started/wizard';
import { Avatar, Button, HiveLink } from '@/components/v2';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
DropdownMenuTrigger,
} from '@/components/v2/dropdown';
import {
AlertTriangleIcon,
ArrowDownIcon,
CalendarIcon,
FileTextIcon,
GraphQLIcon,
GridIcon,
LogOutIcon,
PlusIcon,
SettingsIcon,
TrendingUpIcon,
} from '@/components/v2/icon';
import { env } from '@/env/frontend';
import { MeDocument, OrganizationsDocument } from '@/graphql';
import { getDocsUrl } from '@/lib/docs-url';
import { useRouteSelector } from '@/lib/hooks';
export function Header(): ReactElement {
const router = useRouteSelector();
const [meQuery] = useQuery({ query: MeDocument });
const [organizationsQuery] = useQuery({ query: OrganizationsDocument });
const [isOpaque, setIsOpaque] = useState(false);
const me = meQuery.data?.me;
const organizations = organizationsQuery.data?.organizations.nodes || [];
const currentOrg =
typeof router.organizationId === 'string'
? organizations.find(org => org.cleanId === router.organizationId)
: null;
// Copied from tailwindcss website
// https://github.com/tailwindlabs/tailwindcss.com/blob/94971856747c159b4896621c3308bcfa629bb736/src/components/Header.js#L149
useEffect(() => {
const offset = 30;
const onScroll = () => {
if (!isOpaque && window.scrollY > offset) {
setIsOpaque(true);
} else if (isOpaque && window.scrollY <= offset) {
setIsOpaque(false);
}
};
window.addEventListener('scroll', onScroll, { passive: true });
return () => {
window.removeEventListener('scroll', onScroll);
};
}, [isOpaque]);
const docsUrl = getDocsUrl();
return (
<header
className={clsx(
'fixed top-0 z-40 w-full border-b border-b-transparent transition',
isOpaque && 'border-b-gray-900 bg-black/80 backdrop-blur',
)}
>
<div className="container flex h-[84px] items-center justify-between">
<HiveLink />
<div className="flex flex-row gap-8">
{currentOrg?.getStarted ? <GetStartedProgress tasks={currentOrg.getStarted} /> : null}
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button>
<ArrowDownIcon className="h-5 w-5 text-gray-500" />
<Avatar shape="circle" className="border-2 border-gray-900" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent sideOffset={5} align="end">
<DropdownMenuLabel className="mb-2 w-64 px-2 truncate">
{me?.displayName}
</DropdownMenuLabel>
<DropdownMenuSub>
{me?.canSwitchOrganization ? (
<DropdownMenuSubTrigger>
<GridIcon className="h-5 w-5" />
Switch organization
<ArrowDownIcon className="ml-10 -rotate-90 text-white" />
</DropdownMenuSubTrigger>
) : null}
<DropdownMenuSubContent sideOffset={25} className="max-w-[300px]">
{organizations.length ? (
<DropdownMenuLabel className="px-2 mb-2 text-xs font-bold text-gray-500 truncate !block">
ORGANIZATIONS
</DropdownMenuLabel>
) : null}
{organizations.map(org => (
<NextLink href={`/${org.cleanId}`} key={org.cleanId}>
<DropdownMenuItem
className={`truncate !block ${
currentOrg?.name === org.name
? 'bg-gray-700 text-gray-100 pointer-events-none'
: ''
}`}
>
{org.name}
</DropdownMenuItem>
</NextLink>
))}
<DropdownMenuSeparator />
<NextLink href="/org/new">
<DropdownMenuItem>
<PlusIcon className="h-5 w-5" />
Create an organization
</DropdownMenuItem>
</NextLink>
</DropdownMenuSubContent>
</DropdownMenuSub>
<DropdownMenuItem asChild>
<a
href="https://cal.com/team/the-guild/graphql-hive-15m"
target="_blank"
rel="noreferrer"
>
<CalendarIcon className="h-5 w-5" />
Schedule a meeting
</a>
</DropdownMenuItem>
<NextLink href="/settings">
<DropdownMenuItem>
<SettingsIcon className="h-5 w-5" />
Profile settings
</DropdownMenuItem>
</NextLink>
{docsUrl ? (
<DropdownMenuItem asChild>
<a href={docsUrl} target="_blank" rel="noreferrer">
<FileTextIcon className="h-5 w-5" />
Documentation
</a>
</DropdownMenuItem>
) : null}
<DropdownMenuItem asChild>
<a href="https://status.graphql-hive.com" target="_blank" rel="noreferrer">
<AlertTriangleIcon className="h-5 w-5" />
Status page
</a>
</DropdownMenuItem>
{meQuery.data?.me?.isAdmin && (
<NextLink href="/manage">
<DropdownMenuItem>
<TrendingUpIcon className="h-5 w-5" />
Manage Instance
</DropdownMenuItem>
</NextLink>
)}
{env.nodeEnv === 'development' && (
<NextLink href="/dev">
<DropdownMenuItem>
<GraphQLIcon className="h-5 w-5" />
Dev GraphiQL
</DropdownMenuItem>
</NextLink>
)}
<DropdownMenuItem asChild>
<a href="/logout">
<LogOutIcon className="h-5 w-5" />
Log out
</a>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
</div>
</header>
);
}