|
| 1 | +<script lang="ts"> |
| 2 | + import { onMount } from 'svelte'; |
| 3 | + import { Link } from '$lib/elements'; |
| 4 | + import { Button } from '$lib/elements/forms'; |
| 5 | + import { sdk, RuleType, DeploymentResourceType, RuleTrigger } from '$lib/stores/sdk'; |
| 6 | + import { Query, type Models } from '@appwrite.io/console'; |
| 7 | + import { |
| 8 | + IconDotsHorizontal, |
| 9 | + IconExternalLink, |
| 10 | + IconPlus, |
| 11 | + IconRefresh, |
| 12 | + IconTrash |
| 13 | + } from '@appwrite.io/pink-icons-svelte'; |
| 14 | + import { |
| 15 | + ActionMenu, |
| 16 | + Badge, |
| 17 | + Icon, |
| 18 | + Layout, |
| 19 | + Popover, |
| 20 | + Table, |
| 21 | + Typography, |
| 22 | + Skeleton, |
| 23 | + Divider |
| 24 | + } from '@appwrite.io/pink-svelte'; |
| 25 | + import { resolve } from '$app/paths'; |
| 26 | + import { Click, trackEvent } from '$lib/actions/analytics'; |
| 27 | + import { regionalProtocol } from '$routes/(console)/project-[region]-[project]/store'; |
| 28 | + import { goto } from '$app/navigation'; |
| 29 | + import DeleteDomainModal from '$routes/(console)/project-[region]-[project]/sites/site-[site]/domains/deleteDomainModal.svelte'; |
| 30 | + import RetryDomainModal from '$routes/(console)/project-[region]-[project]/sites/site-[site]/domains/retryDomainModal.svelte'; |
| 31 | +
|
| 32 | + let { |
| 33 | + siteId, |
| 34 | + region, |
| 35 | + projectId |
| 36 | + }: { |
| 37 | + siteId: string; |
| 38 | + region: string; |
| 39 | + projectId: string; |
| 40 | + } = $props(); |
| 41 | +
|
| 42 | + let loading = $state(true); |
| 43 | + let showRetry = $state(false); |
| 44 | + let showDelete = $state(false); |
| 45 | +
|
| 46 | + let selectedProxyRule: Models.ProxyRule = $state(null); |
| 47 | + let proxyRules = $state<Models.ProxyRuleList | null>(null); |
| 48 | +
|
| 49 | + async function loadDomains() { |
| 50 | + loading = true; |
| 51 | + try { |
| 52 | + proxyRules = await sdk.forProject(region, projectId).proxy.listRules({ |
| 53 | + queries: [ |
| 54 | + Query.equal('type', [RuleType.DEPLOYMENT, RuleType.REDIRECT]), |
| 55 | + Query.equal('deploymentResourceType', DeploymentResourceType.SITE), |
| 56 | + Query.equal('deploymentResourceId', siteId), |
| 57 | + Query.equal('trigger', RuleTrigger.MANUAL), |
| 58 | + Query.limit(100) |
| 59 | + ] |
| 60 | + }); |
| 61 | + } catch (error) { |
| 62 | + console.error('Failed to load domains:', error); |
| 63 | + } finally { |
| 64 | + loading = false; |
| 65 | + } |
| 66 | + } |
| 67 | +
|
| 68 | + let previousDeleteState = $state(false); |
| 69 | + let previousRetryState = $state(false); |
| 70 | +
|
| 71 | + onMount(loadDomains); |
| 72 | +
|
| 73 | + $effect(() => { |
| 74 | + const wasDeleteOpen = previousDeleteState && !showDelete; |
| 75 | + const wasRetryOpen = previousRetryState && !showRetry; |
| 76 | +
|
| 77 | + if (wasDeleteOpen || wasRetryOpen) { |
| 78 | + loadDomains(); |
| 79 | + } |
| 80 | +
|
| 81 | + previousDeleteState = showDelete; |
| 82 | + previousDeleteState = showDelete; |
| 83 | + previousRetryState = showRetry; |
| 84 | + }); |
| 85 | +
|
| 86 | + const addDomainUrl = $derived.by(() => { |
| 87 | + const baseUrl = resolve( |
| 88 | + '/(console)/project-[region]-[project]/sites/site-[site]/domains/add-domain', |
| 89 | + { |
| 90 | + region, |
| 91 | + project: projectId, |
| 92 | + site: siteId |
| 93 | + } |
| 94 | + ); |
| 95 | + return `${baseUrl}?types=false`; |
| 96 | + }); |
| 97 | +</script> |
| 98 | + |
| 99 | +<Table.Root columns={[{ id: 'domain' }, { id: 'actions', width: 40 }]} let:root> |
| 100 | + <svelte:fragment slot="header" let:root> |
| 101 | + <Table.Header.Cell column="domain" {root}>Domain</Table.Header.Cell> |
| 102 | + <Table.Header.Cell column="actions" {root} /> |
| 103 | + </svelte:fragment> |
| 104 | + {#if loading} |
| 105 | + {#each Array(2) as _} |
| 106 | + <Table.Row.Base {root}> |
| 107 | + <Table.Cell column="domain" {root}> |
| 108 | + <Layout.Stack direction="row" gap="xs" alignItems="center"> |
| 109 | + <Skeleton variant="line" width={200} height={20} /> |
| 110 | + </Layout.Stack> |
| 111 | + </Table.Cell> |
| 112 | + <Table.Cell column="actions" {root}> |
| 113 | + <Layout.Stack direction="row" justifyContent="flex-end"> |
| 114 | + <Skeleton variant="line" width={24} height={12} /> |
| 115 | + </Layout.Stack> |
| 116 | + </Table.Cell> |
| 117 | + </Table.Row.Base> |
| 118 | + {/each} |
| 119 | + {:else if proxyRules && proxyRules.total > 0} |
| 120 | + {#each proxyRules.rules as rule} |
| 121 | + <Table.Row.Base {root}> |
| 122 | + <Table.Cell column="domain" {root}> |
| 123 | + <Layout.Stack direction="row" gap="xs" alignItems="center"> |
| 124 | + <Link external variant="quiet" href={`${$regionalProtocol}${rule.domain}`}> |
| 125 | + <Layout.Stack direction="row" gap="xxs" alignItems="center"> |
| 126 | + <Typography.Text truncate> |
| 127 | + {rule.domain} |
| 128 | + </Typography.Text> |
| 129 | + <Icon size="xs" icon={IconExternalLink} /> |
| 130 | + </Layout.Stack> |
| 131 | + </Link> |
| 132 | + |
| 133 | + {#if rule.status === 'verifying'} |
| 134 | + <Badge variant="secondary" content="Verifying" size="s" /> |
| 135 | + {:else if rule.status !== 'verified'} |
| 136 | + <Badge |
| 137 | + size="s" |
| 138 | + type="warning" |
| 139 | + variant="secondary" |
| 140 | + content="Verification failed" /> |
| 141 | + {/if} |
| 142 | + </Layout.Stack> |
| 143 | + </Table.Cell> |
| 144 | + <Table.Cell column="actions" {root}> |
| 145 | + <Popover let:toggle padding="none"> |
| 146 | + <Button |
| 147 | + text |
| 148 | + icon |
| 149 | + on:click={(e) => { |
| 150 | + e.preventDefault(); |
| 151 | + toggle(e); |
| 152 | + }}> |
| 153 | + <Icon icon={IconDotsHorizontal} size="s" /> |
| 154 | + </Button> |
| 155 | + |
| 156 | + <svelte:fragment slot="tooltip" let:toggle> |
| 157 | + {@render domainActions(rule, toggle)} |
| 158 | + </svelte:fragment> |
| 159 | + </Popover> |
| 160 | + </Table.Cell> |
| 161 | + </Table.Row.Base> |
| 162 | + {/each} |
| 163 | + {/if} |
| 164 | +</Table.Root> |
| 165 | + |
| 166 | +<Layout.Stack style="width: min-content;"> |
| 167 | + <Button compact on:onclick={async () => await goto(addDomainUrl)}> |
| 168 | + <Icon icon={IconPlus} size="s" /> |
| 169 | + Add domain |
| 170 | + </Button> |
| 171 | +</Layout.Stack> |
| 172 | + |
| 173 | +{#if showDelete} |
| 174 | + <DeleteDomainModal bind:show={showDelete} {selectedProxyRule} /> |
| 175 | +{/if} |
| 176 | + |
| 177 | +{#if showRetry} |
| 178 | + <RetryDomainModal bind:show={showRetry} {selectedProxyRule} /> |
| 179 | +{/if} |
| 180 | + |
| 181 | +{#snippet domainActions(rule: Models.ProxyRule, toggle: () => void)} |
| 182 | + <ActionMenu.Root> |
| 183 | + <ActionMenu.Item.Anchor href={`${$regionalProtocol}${rule.domain}`} external> |
| 184 | + Open domain |
| 185 | + </ActionMenu.Item.Anchor> |
| 186 | + {#if rule.status !== 'verified' && rule.status !== 'verifying'} |
| 187 | + <ActionMenu.Item.Button |
| 188 | + leadingIcon={IconRefresh} |
| 189 | + on:click={() => { |
| 190 | + selectedProxyRule = rule; |
| 191 | + showRetry = true; |
| 192 | + toggle(); |
| 193 | + }}> |
| 194 | + Retry |
| 195 | + </ActionMenu.Item.Button> |
| 196 | + |
| 197 | + <div class="action-menu-divider"> |
| 198 | + <Divider /> |
| 199 | + </div> |
| 200 | + {/if} |
| 201 | + |
| 202 | + <ActionMenu.Item.Button |
| 203 | + status="danger" |
| 204 | + leadingIcon={IconTrash} |
| 205 | + on:click={() => { |
| 206 | + selectedProxyRule = rule; |
| 207 | + showDelete = true; |
| 208 | + toggle(); |
| 209 | + trackEvent(Click.DomainDeleteClick, { |
| 210 | + source: 'studio_manage_domains' |
| 211 | + }); |
| 212 | + }}> |
| 213 | + Delete |
| 214 | + </ActionMenu.Item.Button> |
| 215 | + </ActionMenu.Root> |
| 216 | +{/snippet} |
| 217 | + |
| 218 | +<style> |
| 219 | + .action-menu-divider { |
| 220 | + margin-inline: -1rem; |
| 221 | + padding-block-end: 0.25rem; |
| 222 | + padding-block-start: 0.25rem; |
| 223 | + } |
| 224 | +</style> |
0 commit comments