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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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