feat: add admin activity timeline filters
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { computed, ref, reactive } from 'vue'
|
||||
import { StatsService } from '@/services'
|
||||
import type {
|
||||
DashboardActivitiesParams,
|
||||
DashboardActivityOption,
|
||||
DashboardActivity,
|
||||
DashboardActivityViewItem,
|
||||
DashboardData,
|
||||
@@ -11,8 +13,11 @@ import { formatFileSize, getErrorMessage } from '@/utils/common'
|
||||
|
||||
type UseDashboardStatsOptions = {
|
||||
loadFailedMessage?: string
|
||||
activityLoadFailedMessage?: string
|
||||
}
|
||||
|
||||
const activityTimelineLimit = 40
|
||||
|
||||
const emptyDashboardData = (): DashboardViewData => ({
|
||||
hasExtendedStats: false,
|
||||
totalFiles: 0,
|
||||
@@ -93,6 +98,17 @@ const normalizeRecentActivities = (
|
||||
meta: activity.meta && typeof activity.meta === 'object' ? activity.meta : {}
|
||||
}))
|
||||
|
||||
const normalizeActivityOptions = (
|
||||
options: DashboardActivityOption[] = []
|
||||
): DashboardActivityOption[] =>
|
||||
options
|
||||
.filter((option) => option && option.value)
|
||||
.map((option) => ({
|
||||
value: option.value,
|
||||
label: option.label || option.value,
|
||||
count: toNumber(option.count)
|
||||
}))
|
||||
|
||||
const healthSummaryKeys: (keyof DashboardHealthSummary)[] = [
|
||||
'healthAttentionCount',
|
||||
'healthDangerCount',
|
||||
@@ -126,9 +142,32 @@ export function useDashboardStats(options: UseDashboardStatsOptions = {}) {
|
||||
const isLoading = ref(false)
|
||||
const errorMessage = ref('')
|
||||
const lastUpdatedAt = ref<Date | null>(null)
|
||||
const isActivityTimelineOpen = ref(false)
|
||||
const isActivityTimelineLoading = ref(false)
|
||||
const activityTimelineError = ref('')
|
||||
const activityTimeline = ref<DashboardActivityViewItem[]>([])
|
||||
const activityTimelineTotal = ref(0)
|
||||
const activityTimelineStoredTotal = ref(0)
|
||||
const activityActionOptions = ref<DashboardActivityOption[]>([])
|
||||
const activityTargetTypeOptions = ref<DashboardActivityOption[]>([])
|
||||
const activityFilters = reactive({
|
||||
action: '',
|
||||
targetType: '',
|
||||
keyword: ''
|
||||
})
|
||||
const lastUpdatedText = computed(() =>
|
||||
lastUpdatedAt.value ? lastUpdatedAt.value.toLocaleString() : '-'
|
||||
)
|
||||
const hasActivityFilters = computed(() =>
|
||||
Boolean(activityFilters.action || activityFilters.targetType || activityFilters.keyword)
|
||||
)
|
||||
|
||||
const buildActivityRequestParams = (): DashboardActivitiesParams => ({
|
||||
limit: activityTimelineLimit,
|
||||
action: activityFilters.action || undefined,
|
||||
targetType: activityFilters.targetType || undefined,
|
||||
keyword: activityFilters.keyword.trim() || undefined
|
||||
})
|
||||
|
||||
const fetchDashboardData = async () => {
|
||||
isLoading.value = true
|
||||
@@ -206,11 +245,93 @@ export function useDashboardStats(options: UseDashboardStatsOptions = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
const fetchActivityTimeline = async () => {
|
||||
isActivityTimelineLoading.value = true
|
||||
activityTimelineError.value = ''
|
||||
|
||||
try {
|
||||
const response = await StatsService.getActivities(buildActivityRequestParams())
|
||||
if (!response.detail) {
|
||||
throw new Error('No activity data')
|
||||
}
|
||||
|
||||
const detail = response.detail
|
||||
activityTimeline.value = normalizeRecentActivities(detail.activities ?? detail.items ?? [])
|
||||
activityTimelineTotal.value = toNumber(detail.total)
|
||||
activityTimelineStoredTotal.value = toNumber(detail.storedTotal ?? detail.stored_total)
|
||||
activityActionOptions.value = normalizeActivityOptions(
|
||||
detail.actionOptions ?? detail.action_options ?? []
|
||||
)
|
||||
activityTargetTypeOptions.value = normalizeActivityOptions(
|
||||
detail.targetTypeOptions ?? detail.target_type_options ?? []
|
||||
)
|
||||
} catch (error) {
|
||||
activityTimelineError.value = getErrorMessage(
|
||||
error,
|
||||
options.activityLoadFailedMessage || 'Failed to load activity timeline'
|
||||
)
|
||||
} finally {
|
||||
isActivityTimelineLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const openActivityTimeline = async () => {
|
||||
isActivityTimelineOpen.value = true
|
||||
await fetchActivityTimeline()
|
||||
}
|
||||
|
||||
const closeActivityTimeline = () => {
|
||||
isActivityTimelineOpen.value = false
|
||||
}
|
||||
|
||||
const setActivityActionFilter = async (action: string) => {
|
||||
activityFilters.action = action
|
||||
await fetchActivityTimeline()
|
||||
}
|
||||
|
||||
const setActivityTargetTypeFilter = async (targetType: string) => {
|
||||
activityFilters.targetType = targetType
|
||||
await fetchActivityTimeline()
|
||||
}
|
||||
|
||||
const setActivityKeywordFilter = (keyword: string) => {
|
||||
activityFilters.keyword = keyword
|
||||
}
|
||||
|
||||
const applyActivityFilters = async () => {
|
||||
await fetchActivityTimeline()
|
||||
}
|
||||
|
||||
const resetActivityFilters = async () => {
|
||||
activityFilters.action = ''
|
||||
activityFilters.targetType = ''
|
||||
activityFilters.keyword = ''
|
||||
await fetchActivityTimeline()
|
||||
}
|
||||
|
||||
return {
|
||||
activityActionOptions,
|
||||
activityFilters,
|
||||
activityTargetTypeOptions,
|
||||
activityTimeline,
|
||||
activityTimelineError,
|
||||
activityTimelineStoredTotal,
|
||||
activityTimelineTotal,
|
||||
applyActivityFilters,
|
||||
closeActivityTimeline,
|
||||
dashboardData,
|
||||
errorMessage,
|
||||
fetchDashboardData,
|
||||
fetchActivityTimeline,
|
||||
hasActivityFilters,
|
||||
isActivityTimelineLoading,
|
||||
isActivityTimelineOpen,
|
||||
isLoading,
|
||||
lastUpdatedText
|
||||
lastUpdatedText,
|
||||
openActivityTimeline,
|
||||
resetActivityFilters,
|
||||
setActivityActionFilter,
|
||||
setActivityKeywordFilter,
|
||||
setActivityTargetTypeFilter
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user