|
1 | 1 | <script lang="ts"> |
2 | | - import { Files, User, Plus } from 'lucide-svelte'; |
3 | | -
|
| 2 | + import * as Tabs from '$lib/components/ui/tabs'; |
| 3 | + import { Files, User, Plus, LoaderCircle } from 'lucide-svelte'; |
4 | 4 | import YizyLogo from '$lib/components/ui/YIZYLogo.svelte'; |
| 5 | + import * as yizy from '$lib/api-client/yizyClient'; |
| 6 | + import { onMount } from 'svelte'; |
| 7 | + import { Button } from '$lib/components/ui/button/index.js'; |
| 8 | + import * as Dialog from '$lib/components/ui/dialog/index.js'; |
| 9 | + import { Input } from '$lib/components/ui/input/index.js'; |
| 10 | + import { Label } from '$lib/components/ui/label/index.js'; |
| 11 | + import { currentService } from '$lib/state'; |
| 12 | + import { yizySpecToDoc } from '$lib/components/ui/editor/models/models'; |
| 13 | + import * as yizySpec from '@yizy/spec'; |
| 14 | + import CodeTab from './components/CodeTab.svelte'; |
| 15 | + import SpecTab from './components/SpecTab.svelte'; |
| 16 | + import { DEFAULT_DOCUMENT } from '$lib/components/ui/editor/models/models'; |
| 17 | +
|
| 18 | + let { data } = $props(); |
| 19 | +
|
| 20 | + interface SpecDto { |
| 21 | + id: string; |
| 22 | + name: string; |
| 23 | + } |
| 24 | +
|
| 25 | + interface SpecDetailDto { |
| 26 | + id: string; |
| 27 | + snapshotId: string; |
| 28 | + } |
| 29 | +
|
| 30 | + let doc = $state(yizySpecToDoc($currentService)); |
| 31 | + let specs: SpecDto[] = $state([]); |
| 32 | + let selectedSpec: SpecDto | null = $state(null); |
| 33 | + let selectedSpecDetails: SpecDetailDto | null = $state(null); |
| 34 | + let specContent: string = $state(''); |
| 35 | + let isContentLoading: boolean = $state(false); |
| 36 | + let createSpecDialogSpecName: string = $state(''); |
| 37 | + let isCreateSpecDialogOpen: boolean = $state(false); |
| 38 | +
|
| 39 | + function closeCreateSpecDialog() { |
| 40 | + isCreateSpecDialogOpen = false; |
| 41 | + } |
| 42 | +
|
| 43 | + function isSelectedSpec(spec: SpecDto) { |
| 44 | + if (!selectedSpec) return false; |
| 45 | + return spec.id == selectedSpec.id; |
| 46 | + } |
| 47 | +
|
| 48 | + async function selectSpecClicked(spec: SpecDto) { |
| 49 | + selectedSpec = spec; |
| 50 | + isContentLoading = true; |
| 51 | + const res = await yizy.getLatestSpecById({ id: spec.id }); |
| 52 | + if (res.error === null && res.result !== null) { |
| 53 | + selectedSpecDetails = { |
| 54 | + id: spec.id, |
| 55 | + snapshotId: res.result.snapshotId |
| 56 | + }; |
| 57 | + specContent = res.result?.content; |
| 58 | + try { |
| 59 | + const service = JSON.parse(specContent) as yizySpec.Service; |
| 60 | + doc = yizySpecToDoc(service); |
| 61 | + } catch (e) { |
| 62 | + console.log('cannot parse spec!'); |
| 63 | + doc = DEFAULT_DOCUMENT; |
| 64 | + } |
| 65 | + } |
| 66 | + isContentLoading = false; |
| 67 | + } |
| 68 | +
|
| 69 | + async function createSpecDialogSaveBtnClicked() { |
| 70 | + if (data.authState) { |
| 71 | + const res = await yizy.createSpec({ |
| 72 | + name: createSpecDialogSpecName, |
| 73 | + creatorUserId: data.authState?.user.uuid |
| 74 | + }); |
| 75 | + if (res.error === null && res.result != null) { |
| 76 | + specs = specs.concat(res.result); |
| 77 | + closeCreateSpecDialog(); |
| 78 | + } |
| 79 | + } else { |
| 80 | + console.log('please login!'); |
| 81 | + } |
| 82 | + } |
| 83 | +
|
| 84 | + async function updateSpecBtnClicked(specContent: string) { |
| 85 | + if (data.authState && selectedSpecDetails) { |
| 86 | + const res = await yizy.updateSpec({ |
| 87 | + specId: selectedSpecDetails.id, |
| 88 | + content: specContent, |
| 89 | + updatorUserId: data.authState.user.uuid, |
| 90 | + prevSpecSnapshotId: selectedSpecDetails.snapshotId |
| 91 | + }); |
| 92 | + if (res.error == null && res.result != null) { |
| 93 | + selectedSpecDetails = { |
| 94 | + snapshotId: res.result?.prevSnapshotId, |
| 95 | + id: selectedSpecDetails.id |
| 96 | + }; |
| 97 | + } |
| 98 | + return; |
| 99 | + } else { |
| 100 | + console.log('please login!'); |
| 101 | + } |
| 102 | + } |
| 103 | +
|
| 104 | + onMount(async () => { |
| 105 | + if (data.authState) { |
| 106 | + const res = await yizy.getSpecs({ |
| 107 | + userId: data.authState?.user.uuid |
| 108 | + }); |
| 109 | + if (res.error == null) { |
| 110 | + specs = res.result.resultset; |
| 111 | + } |
| 112 | + } |
| 113 | + }); |
5 | 114 | </script> |
6 | 115 |
|
7 | | -<div class="flex h-full flex-row"> |
8 | | - <div class="flex h-full w-1/5 flex-col border-r"> |
9 | | - <div class="flex h-full flex-row"> |
| 116 | +<div class="flex h-screen flex-row"> |
| 117 | + <div class="flex h-screen w-1/5 flex-col border-r"> |
| 118 | + <div class="flex h-full w-full flex-row"> |
10 | 119 | <div class="flex h-full w-12 flex-col gap-3 bg-secondary px-2 py-4"> |
11 | 120 | <button class="w-full"><Files class="h-8 w-8" /></button> |
12 | 121 | <button class="w-full"><User class="h-8 w-8 text-muted" /></button> |
|
16 | 125 | <YizyLogo /> |
17 | 126 | </div> |
18 | 127 | <div class="flex flex-row justify-between bg-border px-2 py-1"> |
19 | | - <div class="my-auto text-sm">My Specs</div> |
20 | | - <button class="my-auto h-5"><Plus class="h-full" /></button> |
| 128 | + <div class="my-auto text-sm font-bold">My Specs</div> |
| 129 | + <Dialog.Root bind:open={isCreateSpecDialogOpen}> |
| 130 | + <Dialog.Trigger class="my-auto flex"> |
| 131 | + <Plus class="h-5" /> |
| 132 | + </Dialog.Trigger> |
| 133 | + <Dialog.Content class="sm:max-w-[425px]"> |
| 134 | + <Dialog.Header> |
| 135 | + <Dialog.Title>Create New API Spec</Dialog.Title> |
| 136 | + <Dialog.Description>Create a new API spec</Dialog.Description> |
| 137 | + </Dialog.Header> |
| 138 | + <div class="grid gap-4 py-4"> |
| 139 | + <div class="grid grid-cols-4 items-center gap-4"> |
| 140 | + <Label for="name" class="text-right">Name</Label> |
| 141 | + <Input |
| 142 | + id="name" |
| 143 | + placeholder="My API Service" |
| 144 | + class="col-span-3" |
| 145 | + bind:value={createSpecDialogSpecName} /> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + <Dialog.Footer> |
| 149 | + <Button |
| 150 | + onclick={async () => { |
| 151 | + await createSpecDialogSaveBtnClicked(); |
| 152 | + }}>Create</Button> |
| 153 | + </Dialog.Footer> |
| 154 | + </Dialog.Content> |
| 155 | + </Dialog.Root> |
21 | 156 | </div> |
| 157 | + {#each specs as s} |
| 158 | + {#if isSelectedSpec(s)} |
| 159 | + <button class="bg-primary px-2 py-1 text-left text-sm text-primary-foreground" |
| 160 | + >{s.name}</button> |
| 161 | + {:else} |
| 162 | + <button |
| 163 | + class="px-2 py-1 text-left text-sm hover:bg-primary hover:text-primary-foreground" |
| 164 | + onclick={async () => { |
| 165 | + selectSpecClicked(s); |
| 166 | + }}>{s.name}</button> |
| 167 | + {/if} |
| 168 | + {/each} |
22 | 169 | </div> |
23 | 170 | </div> |
24 | 171 | </div> |
25 | | - <div class="flex w-4/5 p-2"> |
26 | | - <div class="m-auto">Create a new spec</div> |
| 172 | + |
| 173 | + <div class="flex h-full w-4/5 flex-col overflow-y-scroll"> |
| 174 | + {#if isContentLoading} |
| 175 | + <div class="m-auto"> |
| 176 | + <LoaderCircle class="animate-spin text-primary" /> |
| 177 | + </div> |
| 178 | + {:else if selectedSpec === null} |
| 179 | + <div class="m-auto">Select a spec or Create a new spec</div> |
| 180 | + {:else} |
| 181 | + <Tabs.Root value="api-spec" class="w-full p-4"> |
| 182 | + <Tabs.List class="grid w-full grid-cols-2"> |
| 183 | + <Tabs.Trigger value="api-spec">Edit Spec</Tabs.Trigger> |
| 184 | + <Tabs.Trigger value="code-gen">Generate Code</Tabs.Trigger> |
| 185 | + </Tabs.List> |
| 186 | + <Tabs.Content value="api-spec"> |
| 187 | + <SpecTab bind:doc onGenerateBtnClicked={updateSpecBtnClicked}></SpecTab> |
| 188 | + </Tabs.Content> |
| 189 | + <Tabs.Content value="code-gen"> |
| 190 | + <CodeTab bind:doc /> |
| 191 | + </Tabs.Content> |
| 192 | + </Tabs.Root> |
| 193 | + {/if} |
27 | 194 | </div> |
28 | 195 | </div> |
0 commit comments