StreamList.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <script setup lang="tsx">
  2. import type { EnvGroup } from '@/api/env_group'
  3. import { StdCurd } from '@uozi-admin/curd'
  4. import { message } from 'ant-design-vue'
  5. import env_group from '@/api/env_group'
  6. import stream from '@/api/stream'
  7. import EnvGroupTabs from '@/components/EnvGroupTabs'
  8. import InspectConfig from '@/views/config/InspectConfig.vue'
  9. import columns from '@/views/stream/columns'
  10. import StreamDuplicate from '@/views/stream/components/StreamDuplicate.vue'
  11. const route = useRoute()
  12. const router = useRouter()
  13. const curd = ref()
  14. const inspect_config = ref()
  15. const envGroupId = ref(Number.parseInt(route.query.env_group_id as string) || 0)
  16. const envGroups = ref<EnvGroup[]>([])
  17. onMounted(async () => {
  18. let page = 1
  19. while (true) {
  20. try {
  21. const { data, pagination } = await env_group.getList({ page })
  22. if (!data || !pagination)
  23. return
  24. envGroups.value.push(...data)
  25. if (data.length < pagination?.per_page) {
  26. return
  27. }
  28. page++
  29. }
  30. catch {
  31. return
  32. }
  33. }
  34. })
  35. watch(route, () => {
  36. inspect_config.value?.test()
  37. })
  38. function destroy(stream_name: string) {
  39. stream.deleteItem(stream_name).then(() => {
  40. curd.value.refresh()
  41. message.success($gettext('Delete stream: %{stream_name}', { stream_name }))
  42. inspect_config.value?.test()
  43. })
  44. }
  45. const showDuplicator = ref(false)
  46. const target = ref('')
  47. function handle_click_duplicate(name: string) {
  48. showDuplicator.value = true
  49. target.value = name
  50. }
  51. const showAddStream = ref(false)
  52. const name = ref('')
  53. function add() {
  54. showAddStream.value = true
  55. name.value = ''
  56. }
  57. function handleAddStream() {
  58. stream.updateItem(name.value, { name: name.value, content: 'server\t{\n\n}' }).then(() => {
  59. showAddStream.value = false
  60. curd.value?.refresh()
  61. message.success($gettext('Added successfully'))
  62. })
  63. }
  64. </script>
  65. <template>
  66. <div>
  67. <StdCurd
  68. ref="curd"
  69. :title="$gettext('Manage Streams')"
  70. :api="stream"
  71. :columns="columns"
  72. :table-props="{
  73. rowKey: 'name',
  74. }"
  75. disable-delete
  76. disable-view
  77. disable-export
  78. row-selection-type="checkbox"
  79. :custom-query-params="{
  80. env_group_id: envGroupId,
  81. }"
  82. :scroll-x="800"
  83. @edit-item="record => router.push({
  84. path: `/streams/${encodeURIComponent(record.name)}`,
  85. })"
  86. >
  87. <template #extra>
  88. <div class="flex items-center cursor-default">
  89. <a class="mr-4" @click="add">{{ $gettext('Add') }}</a>
  90. </div>
  91. </template>
  92. <template #beforeCardBody>
  93. <InspectConfig ref="inspect_config" />
  94. <EnvGroupTabs v-model:active-key="envGroupId" :env-groups="envGroups" />
  95. </template>
  96. <template #afterActions="{ record }">
  97. <AButton
  98. type="link"
  99. size="small"
  100. @click="handle_click_duplicate(record.name)"
  101. >
  102. {{ $gettext('Duplicate') }}
  103. </AButton>
  104. <APopconfirm
  105. :cancel-text="$gettext('No')"
  106. :ok-text="$gettext('OK')"
  107. :title="$gettext('Are you sure you want to delete?')"
  108. :disabled="record.enabled"
  109. @confirm="destroy(record.name)"
  110. >
  111. <AButton
  112. type="link"
  113. size="small"
  114. :disabled="record.enabled"
  115. >
  116. {{ $gettext('Delete') }}
  117. </AButton>
  118. </APopconfirm>
  119. </template>
  120. </StdCurd>
  121. <AModal
  122. v-model:open="showAddStream"
  123. :title="$gettext('Add Stream')"
  124. :mask="false"
  125. @ok="handleAddStream"
  126. >
  127. <AForm layout="vertical">
  128. <AFormItem :label="$gettext('Name')">
  129. <AInput v-model:value="name" />
  130. </AFormItem>
  131. </AForm>
  132. </AModal>
  133. <StreamDuplicate
  134. v-model:visible="showDuplicator"
  135. :name="target"
  136. @duplicated="() => curd.refresh()"
  137. />
  138. </div>
  139. </template>
  140. <style scoped>
  141. </style>