refactor: normalize status to variants + icons

This commit is contained in:
axolotle 2024-08-11 17:53:03 +02:00
parent 5b5cac5478
commit da02692d93
7 changed files with 31 additions and 27 deletions

View file

@ -5,13 +5,10 @@
import errors from '@/api/errors'
import { useInfos } from '@/composables/useInfos'
import {
STATUS_VARIANT,
type APIRequest,
type APIRequestAction,
} from '@/composables/useRequests'
import type { APIRequest, APIRequestAction } from '@/composables/useRequests'
import { toEntries } from '@/helpers/commons'
import type { Obj } from '@/types/commons'
import { STATUS_VARIANT, isOkStatus } from '@/helpers/yunohostArguments'
import type { StateStatus, Obj } from '@/types/commons'
import type { APIErrorData } from './api'
/**
@ -46,8 +43,7 @@ export function openWebSocket(request: APIRequestAction): Promise<Event> {
return new Promise((resolve) => {
const ws = new WebSocket(`wss://${host.value}/yunohost/api/messages`)
ws.onmessage = ({ data }) => {
const messages: Record<'info' | 'success' | 'warning' | 'error', string> =
JSON.parse(data)
const messages: Record<StateStatus, string> = JSON.parse(data)
toEntries(messages).forEach(([status, text]) => {
text = text.replaceAll('\n', '<br>')
const progressBar = text.match(/^\[#*\+*\.*\] > /)?.[0]
@ -63,9 +59,7 @@ export function openWebSocket(request: APIRequestAction): Promise<Event> {
text,
variant: STATUS_VARIANT[status],
})
if (['error', 'warning'].includes(status)) {
request.action[`${status as 'error' | 'warning'}s`]++
}
if (!isOkStatus(status)) request.action[`${status}s`]++
})
}
// ws.onclose = (e) => {}

View file

@ -2,7 +2,7 @@
import { computed, toRefs } from 'vue'
import type { APIRequest } from '@/composables/useRequests'
import { STATUS_VARIANT } from '@/composables/useRequests'
import { STATUS_VARIANT } from '@/helpers/yunohostArguments'
const props = defineProps<{
request: APIRequest

View file

@ -2,7 +2,7 @@
import type { ColorVariant } from 'bootstrap-vue-next'
import { computed } from 'vue'
import { DEFAULT_STATUS_ICON } from '@/helpers/yunohostArguments'
import { DEFAULT_VARIANT_ICON } from '@/helpers/yunohostArguments'
const props = withDefaults(
defineProps<{
@ -18,7 +18,7 @@ const props = withDefaults(
)
const icon = computed(() => {
return props.icon || DEFAULT_STATUS_ICON[props.variant]
return props.icon || DEFAULT_VARIANT_ICON[props.variant]
})
</script>

View file

@ -2,7 +2,7 @@
import type { Breakpoint, ColorVariant } from 'bootstrap-vue-next'
import { computed } from 'vue'
import { DEFAULT_STATUS_ICON } from '@/helpers/yunohostArguments'
import { DEFAULT_VARIANT_ICON } from '@/helpers/yunohostArguments'
const props = withDefaults(
defineProps<{
@ -25,7 +25,7 @@ const props = withDefaults(
const icon = computed(() => {
if (props.noIcon) return
return props.icon || DEFAULT_STATUS_ICON[props.variant]
return props.icon || DEFAULT_VARIANT_ICON[props.variant]
})
const class_ = computed(() => {
const baseClass = 'yuno-list-group-item-'

View file

@ -47,14 +47,6 @@ export type ReconnectingArgs = {
delay?: number
}
export const STATUS_VARIANT = {
pending: 'primary',
success: 'success',
warning: 'warning',
error: 'danger',
info: 'info',
} as const
export const useRequests = createGlobalState(() => {
const router = useRouter()

View file

@ -1,17 +1,30 @@
import { toValue, type MaybeRef } from 'vue'
import { useSettings } from '@/composables/useSettings'
import {
getFileContent,
isEmptyValue,
isObjectLiteral,
toEntries,
} from '@/helpers/commons'
import type { ArrInnerType, Obj, Translation } from '@/types/commons'
import type {
ArrInnerType,
Obj,
StateStatus,
Translation,
} from '@/types/commons'
import type { AdressModelValue, FileModelValue } from '@/types/form'
import { isAdressModelValue, isFileModelValue } from '@/types/form'
import { useSettings } from '@/composables/useSettings'
export const DEFAULT_STATUS_ICON = {
export const STATUS_VARIANT = {
pending: 'primary',
success: 'success',
warning: 'warning',
error: 'danger',
info: 'info',
} as const
export const DEFAULT_VARIANT_ICON = {
primary: null,
secondary: null,
success: 'check',
@ -21,6 +34,10 @@ export const DEFAULT_STATUS_ICON = {
light: null,
dark: null,
best: null,
} as const
export function isOkStatus(status: StateStatus): status is 'info' | 'success' {
return ['info', 'success'].includes(status)
}
// FORMAT FROM CORE

View file

@ -27,6 +27,7 @@ export type CustomRoute = {
export type RouteFromTo = Record<'to' | 'from', RouteLocationNormalized>
export type Translation = string | Record<string, string>
export type StateVariant = 'success' | 'info' | 'warning' | 'danger'
export type StateStatus = 'success' | 'info' | 'warning' | 'error'
// HELPERS