|
31 | 31 | import { Button } from '$lib/elements/forms'; |
32 | 32 | import { symmetricDifference } from '$lib/helpers/array'; |
33 | 33 | import { scopes as allScopes, cloudOnlyBackupScopes } from '$lib/constants'; |
34 | | - import { Accordion, Divider, Layout, Selector } from '@appwrite.io/pink-svelte'; |
| 34 | + import { Accordion, Badge, Divider, Layout, Selector } from '@appwrite.io/pink-svelte'; |
35 | 35 | import type { Scopes } from '@appwrite.io/console'; |
36 | 36 |
|
37 | 37 | export let scopes: Scopes[]; |
38 | 38 |
|
39 | | - const baseFilteredScopes = allScopes.filter((scope) => { |
40 | | - const val = scope.scope; |
41 | | - if (!val) return false; |
42 | | -
|
43 | | - const legacyPrefixes = ['collections.', 'attributes.', 'documents.']; |
44 | | - return !legacyPrefixes.some((prefix) => val.startsWith(prefix)); |
45 | | - }); |
46 | | -
|
47 | 39 | // insert cloud-only scopes right after databases.write |
48 | | - const databasesWriteIndex = baseFilteredScopes.findIndex((s) => s.scope === 'databases.write'); |
| 40 | + const databasesWriteIndex = allScopes.findIndex((s) => s.scope === 'databases.write'); |
49 | 41 | const filteredScopes = |
50 | 42 | isCloud && databasesWriteIndex !== -1 |
51 | 43 | ? [ |
52 | | - ...baseFilteredScopes.slice(0, databasesWriteIndex + 1), |
| 44 | + ...allScopes.slice(0, databasesWriteIndex + 1), |
53 | 45 | ...cloudOnlyBackupScopes, |
54 | | - ...baseFilteredScopes.slice(databasesWriteIndex + 1) |
| 46 | + ...allScopes.slice(databasesWriteIndex + 1) |
55 | 47 | ] |
56 | | - : baseFilteredScopes; |
| 48 | + : allScopes; |
57 | 49 |
|
58 | 50 | // include all scopes |
59 | 51 | const scopeCatalog = new Set([ |
|
90 | 82 |
|
91 | 83 | onMount(() => { |
92 | 84 | scopes.forEach((scope) => { |
93 | | - const newerScope = toNewerScope(scope); |
94 | | - if (newerScope in activeScopes) { |
95 | | - activeScopes[newerScope] = true; |
| 85 | + if (scope in activeScopes) { |
| 86 | + activeScopes[scope] = true; |
96 | 87 | } |
97 | 88 | }); |
98 | 89 |
|
|
111 | 102 | } |
112 | 103 | } |
113 | 104 |
|
114 | | - function toNewerScope(scope: string): string { |
115 | | - for (const pair of compatPairs) { |
116 | | - if (scope.startsWith(pair.legacy)) { |
117 | | - return scope.replace(pair.legacy, pair.newer); |
118 | | - } |
119 | | - } |
120 | | - return scope; |
121 | | - } |
122 | | -
|
123 | | - function getAllScopeVariants(scope: string): string[] { |
124 | | - const variants = new Set([scope]); |
125 | | -
|
126 | | - for (const pair of compatPairs) { |
127 | | - if (scope.startsWith(pair.newer)) { |
128 | | - variants.add(scope.replace(pair.newer, pair.legacy)); |
129 | | - } else if (scope.startsWith(pair.legacy)) { |
130 | | - variants.add(scope.replace(pair.legacy, pair.newer)); |
131 | | - } |
132 | | - } |
133 | | -
|
134 | | - return Array.from(variants); |
135 | | - } |
136 | | -
|
137 | 105 | function categoryState(category: string, s: string[]): boolean | 'indeterminate' { |
138 | 106 | const scopesByCategory = filteredScopes.filter((n) => n.category === category); |
139 | | -
|
140 | | - const activeInCategory = scopesByCategory.filter((scopeItem) => { |
141 | | - const newerScope = scopeItem.scope; |
142 | | - return s.some((scope) => toNewerScope(scope) === newerScope); |
143 | | - }); |
| 107 | + const activeInCategory = scopesByCategory.filter((scopeItem) => |
| 108 | + s.includes(scopeItem.scope as Scopes) |
| 109 | + ); |
144 | 110 |
|
145 | 111 | if (activeInCategory.length === 0) { |
146 | 112 | return false; |
|
154 | 120 | function onCategoryChange(event: CustomEvent<boolean | 'indeterminate'>, category: Category) { |
155 | 121 | if (event.detail === 'indeterminate') return; |
156 | 122 | filteredScopes.forEach((s) => { |
157 | | - if (s.category === category) { |
| 123 | + if (s.category === category && !s.deprecated) { |
158 | 124 | activeScopes[s.scope] = event.detail; |
159 | 125 | } |
160 | 126 | }); |
161 | 127 | } |
162 | 128 |
|
163 | 129 | function generateSyncedScopes(activeScopesObj: Record<string, boolean>): Scopes[] { |
164 | | - const result = new Set<string>(); |
165 | | -
|
166 | | - Object.entries(activeScopesObj).forEach(([scope, isActive]) => { |
167 | | - if (isActive) { |
168 | | - const variants = getAllScopeVariants(scope); |
169 | | - variants.forEach((variant) => { |
170 | | - if (scopeCatalog.has(variant)) { |
171 | | - result.add(variant); |
172 | | - } |
173 | | - }); |
174 | | - } |
175 | | - }); |
176 | | -
|
177 | | - return Array.from(result) as Scopes[]; |
| 130 | + return Object.entries(activeScopesObj) |
| 131 | + .filter(([scope, isActive]) => isActive && scopeCatalog.has(scope)) |
| 132 | + .map(([scope]) => scope as Scopes); |
178 | 133 | } |
179 | 134 |
|
180 | 135 | $: { |
|
203 | 158 | {@const checked = categoryState(category, scopes)} |
204 | 159 | {@const isLastItem = index === categories.length - 1} |
205 | 160 | {@const scopesLength = filteredScopes.filter( |
206 | | - (n) => |
207 | | - n.category === category && |
208 | | - scopes.some((scope) => toNewerScope(scope) === n.scope) |
| 161 | + (n) => n.category === category && scopes.includes(n.scope as Scopes) |
209 | 162 | ).length} |
210 | 163 | <Accordion |
211 | 164 | selectable |
|
216 | 169 | on:change={(event) => onCategoryChange(event, category)}> |
217 | 170 | <Layout.Stack> |
218 | 171 | {#each filteredScopes.filter((s) => s.category === category) as scope} |
219 | | - <Selector.Checkbox |
220 | | - size="s" |
221 | | - id={scope.scope} |
222 | | - label={scope.scope} |
223 | | - description={scope.description} |
224 | | - bind:checked={activeScopes[scope.scope]} /> |
| 172 | + <Layout.Stack direction="row" alignItems="center" gap="s"> |
| 173 | + <Selector.Checkbox |
| 174 | + size="s" |
| 175 | + id={scope.scope} |
| 176 | + label={`${scope.scope}${scope.deprecated ? ' (Deprecated)' : ''}`} |
| 177 | + description={scope.description} |
| 178 | + bind:checked={activeScopes[scope.scope]} /> |
| 179 | + {#if scope.deprecated} |
| 180 | + <Badge size="xs" variant="secondary" content="Deprecated" /> |
| 181 | + {/if} |
| 182 | + </Layout.Stack> |
225 | 183 | {/each} |
226 | 184 | </Layout.Stack> |
227 | 185 | </Accordion> |
|
0 commit comments