tiny-rdm/frontend/src/components/content_value/ContentValueStream.vue

271 lines
8.0 KiB
Vue
Raw Normal View History

2023-07-25 19:12:50 +08:00
<script setup>
import { computed, h, ref } from 'vue'
2023-07-25 19:12:50 +08:00
import { useI18n } from 'vue-i18n'
import ContentToolbar from './ContentToolbar.vue'
2023-08-02 17:57:39 +08:00
import AddLink from '@/components/icons/AddLink.vue'
import { NButton, NCode, NIcon, NInput, useThemeVars } from 'naive-ui'
2023-08-02 17:57:39 +08:00
import { types, types as redisTypes } from '@/consts/support_redis_type.js'
import EditableTableColumn from '@/components/common/EditableTableColumn.vue'
import useDialogStore from 'stores/dialog.js'
import { includes, isEmpty, keys, size, some, values } from 'lodash'
import bytes from 'bytes'
import { decodeTypes, formatTypes } from '@/consts/value_view_type.js'
import useBrowserStore from 'stores/browser.js'
import LoadList from '@/components/icons/LoadList.vue'
import LoadAll from '@/components/icons/LoadAll.vue'
import IconButton from '@/components/common/IconButton.vue'
2023-07-25 19:12:50 +08:00
const i18n = useI18n()
const themeVars = useThemeVars()
2023-07-25 19:12:50 +08:00
const props = defineProps({
name: String,
db: Number,
keyPath: String,
keyCode: {
type: Array,
default: null,
},
2023-07-25 19:12:50 +08:00
ttl: {
type: Number,
default: -1,
},
value: {
type: Array,
default: () => [],
},
size: Number,
length: Number,
viewAs: {
type: String,
default: formatTypes.RAW,
},
decode: {
type: String,
default: decodeTypes.NONE,
},
end: Boolean,
loading: Boolean,
2023-07-25 19:12:50 +08:00
})
const emit = defineEmits(['loadmore', 'loadall', 'reload', 'rename', 'delete'])
/**
*
* @type {ComputedRef<string|number[]>}
*/
const keyName = computed(() => {
return !isEmpty(props.keyCode) ? props.keyCode : props.keyPath
})
const filterOption = [
2023-07-25 19:12:50 +08:00
{
value: 1,
label: i18n.t('common.field'),
2023-07-25 19:12:50 +08:00
},
{
value: 2,
label: i18n.t('common.value'),
2023-07-25 19:12:50 +08:00
},
]
2023-07-25 19:12:50 +08:00
const filterType = ref(1)
const browserStore = useBrowserStore()
2023-07-25 19:12:50 +08:00
const dialogStore = useDialogStore()
const keyType = redisTypes.STREAM
const idFilterOption = ref(null)
const idColumn = computed(() => ({
2023-07-25 19:12:50 +08:00
key: 'id',
title: 'ID',
align: 'center',
titleAlign: 'center',
resizable: true,
filterOptionValue: idFilterOption.value,
}))
const valueFilterOption = ref(null)
const valueColumn = computed(() => ({
2023-07-25 19:12:50 +08:00
key: 'value',
title: i18n.t('common.value'),
align: 'left',
2023-07-25 19:12:50 +08:00
titleAlign: 'center',
resizable: true,
filterOptionValue: valueFilterOption.value,
filter: (value, row) => {
2023-07-25 19:12:50 +08:00
const v = value.toString()
if (filterType.value === 1) {
// filter key
return some(keys(row.v), (key) => includes(key, v))
2023-07-25 19:12:50 +08:00
} else {
// filter value
return some(values(row.v), (val) => includes(val, v))
2023-07-25 19:12:50 +08:00
}
},
// sorter: (row1, row2) => row1.value - row2.value,
render: (row) => {
return h(NCode, { language: 'json', wordWrap: true, code: row.dv })
2023-07-25 19:12:50 +08:00
},
}))
2023-07-25 19:12:50 +08:00
const actionColumn = {
key: 'action',
title: i18n.t('interface.action'),
2023-07-25 19:12:50 +08:00
width: 60,
align: 'center',
titleAlign: 'center',
fixed: 'right',
render: (row) => {
return h(EditableTableColumn, {
bindKey: row.id,
readonly: true,
onDelete: async () => {
try {
const { success, msg } = await browserStore.removeStreamValues(
2023-07-25 19:12:50 +08:00
props.name,
props.db,
keyName.value,
2023-07-25 19:12:50 +08:00
row.id,
)
if (success) {
$message.success(i18n.t('dialogue.delete_key_succ', { key: row.id }))
2023-07-25 19:12:50 +08:00
} else {
$message.error(msg)
2023-07-25 19:12:50 +08:00
}
} catch (e) {
$message.error(e.message)
2023-07-25 19:12:50 +08:00
}
},
})
},
}
const columns = computed(() => [idColumn.value, valueColumn.value, actionColumn])
2023-08-20 14:42:36 +08:00
const entries = computed(() => {
const len = size(props.value)
return `${len} / ${Math.max(len, props.length)}`
})
2023-07-25 19:12:50 +08:00
const onAddRow = () => {
dialogStore.openAddFieldsDialog(props.name, props.db, props.keyPath, props.keyCode, types.STREAM)
2023-07-25 19:12:50 +08:00
}
const filterValue = ref('')
const onFilterInput = (val) => {
valueFilterOption.value = val
2023-07-25 19:12:50 +08:00
}
const onChangeFilterType = (type) => {
onFilterInput(filterValue.value)
}
const clearFilter = () => {
idFilterOption.value = null
valueFilterOption.value = null
2023-07-25 19:12:50 +08:00
}
const onUpdateFilter = (filters, sourceColumn) => {
switch (filterType.value) {
case filterOption[0].value:
idFilterOption.value = filters[sourceColumn.key]
2023-07-25 19:12:50 +08:00
break
case filterOption[1].value:
valueFilterOption.value = filters[sourceColumn.key]
2023-07-25 19:12:50 +08:00
break
}
}
defineExpose({
reset: () => {
clearFilter()
},
})
2023-07-25 19:12:50 +08:00
</script>
<template>
<div class="content-wrapper flex-box-v">
<content-toolbar
:db="props.db"
:key-code="props.keyCode"
:key-path="props.keyPath"
:key-type="keyType"
:loading="props.loading"
:server="props.name"
:ttl="ttl"
class="value-item-part"
@delete="emit('delete')"
@reload="emit('reload')"
@rename="emit('rename')" />
<div class="tb2 value-item-part flex-box-h">
2023-07-25 19:12:50 +08:00
<div class="flex-box-h">
<n-input-group>
<n-select
v-model:value="filterType"
:consistent-menu-width="false"
:options="filterOption"
style="width: 120px"
2023-08-24 15:23:25 +08:00
@update:value="onChangeFilterType" />
2023-07-25 19:12:50 +08:00
<n-input
v-model:value="filterValue"
:placeholder="$t('interface.search')"
2023-07-25 19:12:50 +08:00
clearable
@clear="clearFilter"
2023-08-24 15:23:25 +08:00
@update:value="onFilterInput" />
2023-07-25 19:12:50 +08:00
</n-input-group>
</div>
<div class="flex-item-expand"></div>
<n-button-group>
<icon-button
:disabled="props.end || props.loading"
:icon="LoadList"
border
size="18"
t-tooltip="interface.load_more_entries"
@click="emit('loadmore')" />
<icon-button
:disabled="props.end || props.loading"
:icon="LoadAll"
border
size="18"
t-tooltip="interface.load_all_entries"
@click="emit('loadall')" />
</n-button-group>
<n-button :focusable="false" plain @click="onAddRow">
2023-07-25 19:12:50 +08:00
<template #icon>
<n-icon :component="AddLink" size="18" />
</template>
{{ $t('interface.add_row') }}
2023-07-25 19:12:50 +08:00
</n-button>
</div>
<div class="value-wrapper value-item-part flex-box-v flex-item-expand">
2023-07-25 19:12:50 +08:00
<n-data-table
:bordered="false"
:bottom-bordered="false"
2023-07-25 19:12:50 +08:00
:columns="columns"
:data="props.value"
:loading="props.loading"
:row-key="(row) => row.id"
2023-07-25 19:12:50 +08:00
:single-column="true"
:single-line="false"
class="flex-item-expand"
2023-07-25 19:12:50 +08:00
flex-height
size="small"
striped
virtual-scroll
2023-08-24 15:23:25 +08:00
@update:filters="onUpdateFilter" />
2023-07-25 19:12:50 +08:00
</div>
<div class="value-footer flex-box-h">
<n-text v-if="!isNaN(props.length)">{{ $t('interface.entries') }}: {{ entries }}</n-text>
<n-divider v-if="!isNaN(props.length)" vertical />
<n-text v-if="!isNaN(props.size)">{{ $t('interface.memory_usage') }}: {{ bytes(props.size) }}</n-text>
<div class="flex-item-expand"></div>
</div>
2023-07-25 19:12:50 +08:00
</div>
</template>
<style lang="scss" scoped>
.value-footer {
border-top: v-bind('themeVars.borderColor') 1px solid;
background-color: v-bind('themeVars.tableHeaderColor');
}
</style>