-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy path+page.svelte
More file actions
185 lines (170 loc) · 6.69 KB
/
+page.svelte
File metadata and controls
185 lines (170 loc) · 6.69 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
<script lang="ts">
import { page } from '$app/state';
import { registerCommands, updateCommandGroupRanks } from '$lib/commandCenter';
import {
CardContainer,
Empty,
EmptySearch,
GridItem1,
Id,
PaginationWithLimit,
SearchQuery,
SvgIcon
} from '$lib/components';
import { toLocaleDateTime } from '$lib/helpers/date';
import { Container } from '$lib/layout';
import { isServiceLimited } from '$lib/stores/billing';
import { organization } from '$lib/stores/organization';
import { wizard } from '$lib/stores/wizard';
import { parseExpression } from 'cron-parser';
import { onMount } from 'svelte';
import { canWriteFunctions } from '$lib/stores/roles';
import type { Models } from '@appwrite.io/console';
import { Icon, Layout, Tooltip } from '@appwrite.io/pink-svelte';
import { IconClock, IconPlus } from '@appwrite.io/pink-icons-svelte';
import { goto } from '$app/navigation';
import { Button } from '$lib/elements/forms';
import Avatar from '$lib/components/avatar.svelte';
import { resolveRoute, withPath } from '$lib/stores/navigation';
import type { PageProps } from './$types';
const { data }: PageProps = $props();
let offset = $state(0);
const createFunctionsUrl = $derived.by(() => {
return resolveRoute('/(console)/project-[region]-[project]/functions/create-function', {
...page.params
});
});
const isLimited = $derived(isServiceLimited('functions', $organization, data.functions.total));
onMount(async () => {
const from = page.url.searchParams.get('from');
if (from === 'github') {
const to = page.url.searchParams.get('to');
switch (to) {
case 'template': {
const template = page.url.searchParams.get('template');
const templateConfig = page.url.searchParams.get('templateConfig');
const templateUrl = resolveRoute(
'/(console)/project-[region]-[project]/functions/create-function/template-[template]',
{
...page.params,
template
}
);
if (!templateConfig) {
await goto(templateUrl);
} else {
await goto(withPath(templateUrl, `?templateConfig=${templateConfig}`));
}
break;
}
case 'cover':
await goto(createFunctionsUrl);
break;
}
}
});
function getNextScheduledExecution(func: Models.Function) {
return toLocaleDateTime(parseExpression(func.schedule, { utc: true }).next().toString());
}
$effect(() => {
$registerCommands([
{
label: 'Create function',
callback: () => goto(createFunctionsUrl),
keys: ['c'],
disabled:
$wizard.show ||
isServiceLimited('functions', $organization, data.functions?.total) ||
!$canWriteFunctions,
icon: IconPlus,
group: 'functions'
}
]);
});
$effect(() => {
$updateCommandGroupRanks({ functions: 1000 });
});
</script>
<Container>
<Layout.Stack direction="row" justifyContent="space-between">
<SearchQuery placeholder="Search by name or ID" />
<Tooltip disabled={!isLimited}>
<div>
<Button disabled={isLimited} href={createFunctionsUrl}>
<Icon icon={IconPlus} slot="start" />
Create function
</Button>
</div>
<svelte:fragment slot="tooltip">
<div style="white-space: pre-line;">
You have reached the maximum number of functions for your plan.
</div>
</svelte:fragment>
</Tooltip>
</Layout.Stack>
{#if data.functions.total}
<CardContainer
{offset}
disableEmpty={!$canWriteFunctions}
event="functions"
total={data.functions.total}
service="functions"
on:click={() => goto(createFunctionsUrl)}>
{#each data.functions.functions as func (func.$id)}
<GridItem1
href={resolveRoute(
'/(console)/project-[region]-[project]/functions/function-[function]',
{
...page.params,
function: func.$id
}
)}>
<svelte:fragment slot="title">
<Layout.Stack gap="l" alignItems="center" direction="row" inline>
<Avatar alt={func.name} size="m">
<SvgIcon name={func.runtime.split('-')[0]} />
</Avatar>
{func.name}
</Layout.Stack>
</svelte:fragment>
<svelte:fragment slot="icons">
{#if func.schedule}
<Tooltip>
<Icon icon={IconClock} size="s" />
<span slot="tooltip"
>{`Next execution:
${getNextScheduledExecution(func)}`}</span>
</Tooltip>
{/if}
</svelte:fragment>
<Id value={func.$id} event="function">{func.$id}</Id>
</GridItem1>
{/each}
<svelte:fragment slot="empty">
<p>Create a new function</p>
</svelte:fragment>
</CardContainer>
<PaginationWithLimit
name="Functions"
limit={data.limit}
offset={data.offset}
total={data.functions.total} />
{:else if data?.search}
<EmptySearch hidePages bind:search={data.search} target="functions">
<Button
secondary
href={resolveRoute('/(console)/project-[region]-[project]/functions', {
...page.params
})}>
Clear search
</Button>
</EmptySearch>
{:else}
<Empty
single
allowCreate={$canWriteFunctions}
href="https://appwrite.io/docs/products/functions"
target="function"
on:click={() => goto(createFunctionsUrl)} />
{/if}
</Container>