-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathshell.svelte
More file actions
363 lines (316 loc) · 10.8 KB
/
shell.svelte
File metadata and controls
363 lines (316 loc) · 10.8 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<script lang="ts">
import { afterNavigate, beforeNavigate } from '$app/navigation';
import { Navbar, Sidebar } from '$lib/components';
import { isNewWizardStatusOpen, wizard } from '$lib/stores/wizard';
import { activeHeaderAlert } from '$routes/(console)/store';
import { onMount, setContext } from 'svelte';
import { writable } from 'svelte/store';
import { showSubNavigation } from '$lib/stores/layout';
import { organization, organizationList } from '$lib/stores/organization';
import { sdk } from '$lib/stores/sdk';
import { user } from '$lib/stores/user';
import { tierToPlan } from '$lib/stores/billing';
import SideNavigation from '$lib/layout/navigation.svelte';
import { hasOnboardingDismissed } from '$lib/helpers/onboarding';
import { isSidebarOpen, noWidthTransition } from '$lib/stores/sidebar';
import { page } from '$app/stores';
import type { Models } from '@appwrite.io/console';
import { getSidebarState, isInDatabasesRoute, updateSidebarState } from '$lib/helpers/sidebar';
import { isTabletViewport } from '$lib/stores/viewport';
import { isBillingEnabled, ProfileMode, resolvedProfile } from '$lib/profiles/index.svelte';
import { app } from '$lib/stores/app';
import { isFreePlan } from '$lib/helpers/billing';
export let showHeader = true;
export let showFooter = true;
export let showSideNavigation = false;
export let selectedProject: Models.Project = null;
// variables
let yOnMenuOpen: number;
let showAccountMenu = false;
let showContentTransition = false;
let timeoutId: ReturnType<typeof setTimeout>;
let state: undefined | 'open' | 'closed' | 'icons' = 'closed';
const isNarrow = setContext('isNarrow', writable(false));
const bodyStyle = writable({ position: 'static', top: '' });
const hasSubNavigation = setContext('hasSubNavigation', writable(false));
onMount(() => (state = getSidebarState()));
// user defined functions
function style(node: HTMLElement, { position, top }) {
node.style.position = position;
node.style.top = top;
return {
update: ({ position, top }) => {
node.style.position = position;
node.style.top = top;
}
};
}
function getProgressCard() {
if (selectedProject && !hasOnboardingDismissed(selectedProject.$id, $user)) {
const { platforms, pingCount } = selectedProject;
let percentage = 33;
if (platforms.length > 0 && pingCount === 0) {
percentage = 66;
} else if (pingCount > 0) {
percentage = 100;
}
return {
title: 'Get started',
percentage
};
}
return undefined;
}
function handleResize() {
$isSidebarOpen = false;
showAccountMenu = false;
}
/**
* Cancel navigation when wizard is open and triggered by popstate
*/
beforeNavigate((navigation) => {
if (navigation.willUnload) return;
if (!($wizard.show || $wizard.cover)) return;
if (navigation.type === 'popstate') {
navigation.cancel();
}
if (navigation.type !== 'leave') {
wizard.hide();
}
if (!isInDatabasesRoute(navigation.from.route)) {
updateSidebarState(state);
} else if (isInDatabasesRoute(navigation.to.route)) {
$noWidthTransition = true;
}
});
/**
* Maintain the sidebar state after a navigation!
*
* This needs to be handled like this because
* the setup around the sidebar is very tightly configured with 2 states sync.
*
* The sidebar is **always closed** on mobile and tablet devices!
*/
afterNavigate((navigation) => {
if ($isTabletViewport) {
state = 'closed';
return;
}
const isEnteringDatabase = isInDatabasesRoute(navigation.to.route);
const isLeavingDatabase =
isInDatabasesRoute(navigation.from.route) && !isInDatabasesRoute(navigation.to.route);
if (isEnteringDatabase) {
state = 'icons';
} else if (isLeavingDatabase) {
state = getSidebarState();
$noWidthTransition = false;
}
});
// subscriptions
isNewWizardStatusOpen.subscribe((value) => (showHeader = !value));
page.subscribe(({ url }) => {
$showSubNavigation = url.searchParams.get('openNavbar') === 'true';
clearTimeout(timeoutId);
if (url.pathname.includes('project-')) {
timeoutId = setTimeout(() => {
showContentTransition = true;
}, 1000);
} else {
showContentTransition = false;
}
});
// reactive blocks
$: sideSize = $hasSubNavigation ? ($isNarrow ? '17rem' : '25rem') : '12.5rem';
$: navbarProps = {
logo: resolvedProfile.logo,
avatar: sdk.forConsole.avatars
.getInitials({
name: $user?.name,
width: 80,
height: 80,
background: $app.themeInUse === 'dark' ? 'E4E4E7' : '6C6C71'
})
.toString(),
organizations: $organizationList.teams.map((org) => {
const billingPlan = org['billingPlan'];
return {
$id: org.$id,
name: org.name,
isSelected: $organization?.$id === org.$id,
showUpgrade: isBillingEnabled ? isFreePlan(billingPlan) : false,
tierName: isBillingEnabled ? tierToPlan(billingPlan).name : null,
billingNextInvoiceDate: isBillingEnabled
? org['billingNextInvoiceDate']
: undefined,
billingCurrentInvoiceDate: isBillingEnabled
? org['billingCurrentInvoiceDate']
: undefined
};
}),
currentProject: selectedProject
};
$: {
if ($isSidebarOpen) {
state = 'open';
} else if ($isTabletViewport) {
state = 'closed';
} else if (resolvedProfile.id === ProfileMode.STUDIO) {
state = 'icons';
} else if (isInDatabasesRoute($page.route)) {
state = 'icons';
} else {
state = 'closed';
}
}
$: subNavigation = $page.data.subNavigation;
$: shouldRenderSidebar = !$isNewWizardStatusOpen && showSideNavigation;
$: hasSidebarSpace = shouldRenderSidebar && !$isTabletViewport && !!selectedProject;
$: {
if ($isSidebarOpen) {
yOnMenuOpen = window.scrollY;
bodyStyle.set({ position: 'fixed', top: `-${window.scrollY}px` });
} else if (!$isSidebarOpen) {
bodyStyle.set({ position: 'static', top: '' });
requestAnimationFrame(() => window.scrollTo(0, yOnMenuOpen));
}
}
</script>
<svelte:window on:resize={handleResize} />
<svelte:body use:style={$bodyStyle} />
{#if $activeHeaderAlert?.show && !$isNewWizardStatusOpen}
<svelte:component this={$activeHeaderAlert.component} />
{/if}
<main
class:has-alert={$activeHeaderAlert?.show}
class:is-open={$showSubNavigation}
class:u-hide={$wizard.show || $wizard.cover}
class:is-fixed-layout={$activeHeaderAlert?.show}
class:no-header={!showHeader}
style:--p-side-size={sideSize}>
{#if showHeader}
<Navbar {...navbarProps} bind:sideBarIsOpen={$isSidebarOpen} bind:showAccountMenu />
{/if}
{#if shouldRenderSidebar}
<Sidebar
project={selectedProject}
progressCard={getProgressCard()}
avatar={navbarProps.avatar}
bind:subNavigation
bind:sideBarIsOpen={$isSidebarOpen}
bind:showAccountMenu
bind:state />
{/if}
<SideNavigation bind:subNavigation />
<div
class="content"
class:has-transition={showContentTransition}
class:icons-content={state === 'icons' && selectedProject}
class:no-sidebar={!hasSidebarSpace}>
<section class="main-content" data-test={showSideNavigation}>
{#if $page.data?.header}
<div class="layout-header">
<svelte:component this={$page.data.header} />
</div>
{/if}
<slot />
{#if showFooter}
<slot name="footer" />
{/if}
</section>
</div>
{#if $isSidebarOpen}
<button
type="button"
class="overlay-button"
aria-label="Close sidebar"
class:overlay={$isSidebarOpen}
on:click={() => ($isSidebarOpen = false)}></button>
{/if}
</main>
<style lang="scss">
.content {
width: 100%;
margin-block-start: 48px;
border-radius: 16px;
border: 1px solid var(--border-neutral);
overflow: hidden;
height: calc(100vh - 60px);
max-width: calc(100vw - 56px);
margin-top: 48px;
margin-right: 10px;
margin-left: 48px;
overflow: auto;
@media (min-width: 1024px) {
width: 100%;
padding-left: 190px;
&.icons-content {
padding-left: 0px;
}
}
}
.has-transition {
@media (min-width: 1024px) {
transition: all 0.3s ease-in-out;
}
}
.no-sidebar {
padding-left: 0;
margin-left: auto;
margin-right: auto;
}
.main-content {
min-height: calc(100vh - 62px);
}
.no-header {
min-height: 100vh;
.content {
margin-block-start: 0;
}
}
:global(main:has(.sub-navigation)) {
.main-content {
@media (min-width: 1024px) {
padding-left: 238px;
}
}
}
:global(main:has(.databases-spreadsheet)) {
/* avoids the unnecessary sheet slide animation */
.has-transition {
transition: none !important;
}
@media (min-width: 1024px) {
.main-content {
height: auto;
padding-left: 198px;
}
}
@media (max-width: 768px) {
.main-content:not(:has(.wide-screen-wrapper)) {
width: 100%;
position: fixed;
}
}
}
.overlay {
position: fixed;
width: 100vw;
height: 100vh;
right: 0;
top: 0;
z-index: 10;
background-color: #56565c1a;
backdrop-filter: blur(5px);
transition:
backdrop-filter 0.5s ease-in-out,
background-color 0.35s ease-in-out;
}
.overlay-button {
@media (min-width: 1024px) {
display: none;
}
}
main {
min-height: calc(100vh - 48px);
}
</style>