diff --git a/app/src/api/handlers.ts b/app/src/api/handlers.ts index dfed5419..efb82103 100644 --- a/app/src/api/handlers.ts +++ b/app/src/api/handlers.ts @@ -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 { 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 = JSON.parse(data) toEntries(messages).forEach(([status, text]) => { text = text.replaceAll('\n', '
') const progressBar = text.match(/^\[#*\+*\.*\] > /)?.[0] @@ -63,9 +59,7 @@ export function openWebSocket(request: APIRequestAction): Promise { 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) => {} diff --git a/app/src/components/QueryHeader.vue b/app/src/components/QueryHeader.vue index d76cd571..e94c04c0 100644 --- a/app/src/components/QueryHeader.vue +++ b/app/src/components/QueryHeader.vue @@ -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 diff --git a/app/src/components/globals/YAlert.vue b/app/src/components/globals/YAlert.vue index 113203d5..885c7f44 100644 --- a/app/src/components/globals/YAlert.vue +++ b/app/src/components/globals/YAlert.vue @@ -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] }) diff --git a/app/src/components/globals/YListGroupItem.vue b/app/src/components/globals/YListGroupItem.vue index b0d5e809..96fcae18 100644 --- a/app/src/components/globals/YListGroupItem.vue +++ b/app/src/components/globals/YListGroupItem.vue @@ -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-' diff --git a/app/src/composables/useRequests.ts b/app/src/composables/useRequests.ts index 52cafd37..e01e3014 100644 --- a/app/src/composables/useRequests.ts +++ b/app/src/composables/useRequests.ts @@ -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() diff --git a/app/src/helpers/yunohostArguments.ts b/app/src/helpers/yunohostArguments.ts index dae9ad90..07d0a790 100644 --- a/app/src/helpers/yunohostArguments.ts +++ b/app/src/helpers/yunohostArguments.ts @@ -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 diff --git a/app/src/types/commons.ts b/app/src/types/commons.ts index 2d7f3762..fe1af5d6 100644 --- a/app/src/types/commons.ts +++ b/app/src/types/commons.ts @@ -27,6 +27,7 @@ export type CustomRoute = { export type RouteFromTo = Record<'to' | 'from', RouteLocationNormalized> export type Translation = string | Record export type StateVariant = 'success' | 'info' | 'warning' | 'danger' +export type StateStatus = 'success' | 'info' | 'warning' | 'error' // HELPERS