StreamList.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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-add
  76. disable-delete
  77. disable-view
  78. disable-export
  79. row-selection-type="checkbox"
  80. :custom-query-params="{
  81. env_group_id: envGroupId,
  82. }"
  83. :scroll-x="800"
  84. @edit-item="record => router.push({
  85. path: `/streams/${encodeURIComponent(record.name)}`,
  86. })"
  87. >
  88. <template #beforeListActions>
  89. <div class="flex items-center cursor-default">
  90. <a class="mr-4" @click="add">{{ $gettext('Add') }}</a>
  91. </div>
  92. </template>
  93. <template #beforeCardBody>
  94. <InspectConfig ref="inspect_config" />
  95. <EnvGroupTabs v-model:active-key="envGroupId" :env-groups="envGroups" />
  96. </template>
  97. <template #afterActions="{ record }">
  98. <AButton
  99. type="link"
  100. size="small"
  101. @click="handle_click_duplicate(record.name)"
  102. >
  103. {{ $gettext('Duplicate') }}
  104. </AButton>
  105. <APopconfirm
  106. :cancel-text="$gettext('No')"
  107. :ok-text="$gettext('OK')"
  108. :title="$gettext('Are you sure you want to delete?')"
  109. :disabled="record.enabled"
  110. @confirm="destroy(record.name)"
  111. >
  112. <AButton
  113. type="link"
  114. size="small"
  115. :disabled="record.enabled"
  116. >
  117. {{ $gettext('Delete') }}
  118. </AButton>
  119. </APopconfirm>
  120. </template>
  121. </StdCurd>
  122. <AModal
  123. v-model:open="showAddStream"
  124. :title="$gettext('Add Stream')"
  125. :mask="false"
  126. @ok="handleAddStream"
  127. >
  128. <AForm layout="vertical">
  129. <AFormItem :label="$gettext('Name')">
  130. <AInput v-model:value="name" />
  131. </AFormItem>
  132. </AForm>
  133. </AModal>
  134. <StreamDuplicate
  135. v-model:visible="showDuplicator"
  136. :name="target"
  137. @duplicated="() => curd.refresh()"
  138. />
  139. </div>
  140. </template>
  141. <style scoped>
  142. </style>