1
0

refactor(status): remove null fields

This commit is contained in:
三咲智子 2023-01-04 18:21:18 +08:00
parent b0d6f310a8
commit 9677f742c8
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
6 changed files with 38 additions and 46 deletions

View File

@ -3,7 +3,7 @@ import ISO6391 from 'iso-639-1'
import Fuse from 'fuse.js' import Fuse from 'fuse.js'
let { modelValue } = $defineModel<{ let { modelValue } = $defineModel<{
modelValue: string | null | undefined modelValue: string
}>() }>()
const { t } = useI18n() const { t } = useI18n()
@ -11,17 +11,14 @@ const { t } = useI18n()
const languageKeyword = $ref('') const languageKeyword = $ref('')
const languageList: { const languageList: {
code: string | null code: string
nativeName: string nativeName: string
name?: string name: string
}[] = [{ }[] = ISO6391.getAllCodes().map(code => ({
code: null,
nativeName: t('language.none'),
}, ...ISO6391.getAllCodes().map(code => ({
code, code,
nativeName: ISO6391.getNativeName(code), nativeName: ISO6391.getNativeName(code),
name: ISO6391.getName(code), name: ISO6391.getName(code),
}))] }))
const fuse = new Fuse(languageList, { const fuse = new Fuse(languageList, {
keys: ['code', 'nativeName', 'name'], keys: ['code', 'nativeName', 'name'],
@ -32,15 +29,11 @@ const languages = $computed(() =>
languageKeyword.trim() languageKeyword.trim()
? fuse.search(languageKeyword).map(r => r.item) ? fuse.search(languageKeyword).map(r => r.item)
: [...languageList].sort(({ code: a }, { code: b }) => { : [...languageList].sort(({ code: a }, { code: b }) => {
return a === modelValue return a === modelValue ? -1 : b === modelValue ? 1 : a.localeCompare(b)
? -1
: b === modelValue
? 1
: (a === null ? -1 : b === null ? 1 : a.localeCompare(b))
}), }),
) )
function chooseLanguage(language: string | null) { function chooseLanguage(language: string) {
modelValue = language modelValue = language
} }
</script> </script>
@ -57,16 +50,11 @@ function chooseLanguage(language: string | null) {
<CommonDropdownItem <CommonDropdownItem
v-for="{ code, nativeName, name } in languages" v-for="{ code, nativeName, name } in languages"
:key="code" :key="code"
:checked="code === (modelValue || null)" :text="nativeName"
:description="name"
:checked="code === modelValue"
@click="chooseLanguage(code)" @click="chooseLanguage(code)"
> />
{{ nativeName }}
<template #description>
<template v-if="name">
{{ name }}
</template>
</template>
</CommonDropdownItem>
</div> </div>
</div> </div>
</template> </template>

View File

@ -333,9 +333,7 @@ defineExpose({
</button> </button>
<template #popper> <template #popper>
<div min-w-80 p3> <PublishLanguagePicker v-model="draft.params.language" min-w-80 p3 />
<PublishLanguagePicker v-model="draft.params.language" />
</div>
</template> </template>
</CommonDropdown> </CommonDropdown>
</CommonTooltip> </CommonTooltip>

View File

@ -1,32 +1,35 @@
import type { Account, Status } from 'masto' import type { Account, CreateStatusParams, Status } from 'masto'
import { STORAGE_KEY_DRAFTS } from '~/constants' import { STORAGE_KEY_DRAFTS } from '~/constants'
import type { Draft, DraftMap } from '~/types' import type { Draft, DraftMap } from '~/types'
import type { Mutable } from '~/types/utils'
export const currentUserDrafts = process.server ? computed<DraftMap>(() => ({})) : useUserLocalStorage<DraftMap>(STORAGE_KEY_DRAFTS, () => ({})) export const currentUserDrafts = process.server ? computed<DraftMap>(() => ({})) : useUserLocalStorage<DraftMap>(STORAGE_KEY_DRAFTS, () => ({}))
export function getDefaultDraft(options: Partial<Draft['params'] & Omit<Draft, 'params'>> = {}): Draft { export function getDefaultDraft(options: Partial<Mutable<CreateStatusParams> & Omit<Draft, 'params'>> = {}): Draft {
const { const {
status = '',
inReplyToId,
visibility = 'public',
attachments = [], attachments = [],
initialText = '', initialText = '',
sensitive = false,
spoilerText = '', status,
inReplyToId,
visibility,
sensitive,
spoilerText,
language, language,
} = options } = options
return { return {
params: {
status,
inReplyToId,
visibility,
sensitive,
spoilerText,
language,
},
attachments, attachments,
initialText, initialText,
params: {
status: status || '',
inReplyToId,
visibility: visibility || 'public',
sensitive: sensitive ?? false,
spoilerText: spoilerText || '',
language: language || 'en',
},
} }
} }

View File

@ -18,7 +18,7 @@ import { CustomEmoji } from './tiptap/custom-emoji'
import { Emoji } from './tiptap/emoji' import { Emoji } from './tiptap/emoji'
export interface UseTiptapOptions { export interface UseTiptapOptions {
content: Ref<string | undefined> content: Ref<string>
placeholder: Ref<string | undefined> placeholder: Ref<string | undefined>
onSubmit: () => void onSubmit: () => void
onFocus: () => void onFocus: () => void

View File

@ -1,6 +1,6 @@
import type { Account, AccountCredentials, Attachment, CreateStatusParams, Emoji, Instance, MastoClient, Notification, PushSubscription, Status } from 'masto' import type { Account, AccountCredentials, Attachment, CreateStatusParams, Emoji, Instance, MastoClient, Notification, PushSubscription, Status } from 'masto'
import type { Ref } from 'vue' import type { Ref } from 'vue'
import type { Mutable } from './utils' import type { MarkNonNullable, Mutable } from './utils'
export interface AppInfo { export interface AppInfo {
id: string id: string
@ -59,9 +59,7 @@ export type TranslateFn = ReturnType<typeof useI18n>['t']
export interface Draft { export interface Draft {
editingStatus?: Status editingStatus?: Status
initialText?: string initialText?: string
params: Omit<Mutable<CreateStatusParams>, 'status'> & { params: MarkNonNullable<Mutable<CreateStatusParams>, 'status' | 'language' | 'sensitive' | 'spoilerText' | 'visibility'>
status?: Exclude<CreateStatusParams['status'], null>
}
attachments: Attachment[] attachments: Attachment[]
} }
export type DraftMap = Record<string, Draft> export type DraftMap = Record<string, Draft>

View File

@ -1,3 +1,8 @@
export type Mutable<T> = { export type Mutable<T> = {
-readonly[P in keyof T]: T[P] -readonly[P in keyof T]: T[P]
} }
export type Overwrite<T, O> = Omit<T, keyof O> & O
export type MarkNonNullable<T, K extends keyof T> = Overwrite<T, {
[P in K]-?: NonNullable<T[P]>
}>