fix: avoid reference context in `getDefaultDraft`

zio/stable
Anthony Fu 2023-01-30 12:20:22 +01:00
parent 2a4862fb42
commit e277832b61
4 changed files with 27 additions and 29 deletions

View File

@ -1,5 +1,4 @@
<script setup lang="ts"> <script setup lang="ts">
import ISO6391 from 'iso-639-1'
import Fuse from 'fuse.js' import Fuse from 'fuse.js'
let { modelValue } = $defineModel<{ let { modelValue } = $defineModel<{
@ -29,13 +28,15 @@ function chooseLanguage(language: string) {
</script> </script>
<template> <template>
<div> <div relative of-x-hidden>
<div p2>
<input <input
v-model="languageKeyword" v-model="languageKeyword"
:placeholder="t('language.search')" :placeholder="t('language.search')"
p2 mb2 border-rounded w-full bg-transparent p2 border-rounded w-full bg-transparent
outline-none border="~ base" outline-none border="~ base"
> >
</div>
<div max-h-40vh overflow-auto> <div max-h-40vh overflow-auto>
<CommonDropdownItem <CommonDropdownItem
v-for="{ code, nativeName, name } in languages" v-for="{ code, nativeName, name } in languages"

View File

@ -35,7 +35,7 @@ const {
dropZoneRef, dropZoneRef,
} = $(useUploadMediaAttachment($$(draft))) } = $(useUploadMediaAttachment($$(draft)))
let { shouldExpanded, isExpanded, isSending, isPublishDisabled, publishDraft, failedMessages } = $(usePublish( let { shouldExpanded, isExpanded, isSending, isPublishDisabled, publishDraft, failedMessages, preferredLanguage } = $(usePublish(
{ {
draftState, draftState,
...$$({ expanded, isUploading, initialDraft: initial }), ...$$({ expanded, isUploading, initialDraft: initial }),
@ -77,7 +77,7 @@ const characterCount = $computed(() => {
return length return length
}) })
const postLanguageDisplay = $computed(() => languagesNameList.find(i => i.code === draft.params.language)?.nativeName) const postLanguageDisplay = $computed(() => languagesNameList.find(i => i.code === (draft.params.language || preferredLanguage))?.nativeName)
async function handlePaste(evt: ClipboardEvent) { async function handlePaste(evt: ClipboardEvent) {
const files = evt.clipboardData?.files const files = evt.clipboardData?.files
@ -290,7 +290,7 @@ defineExpose({
</button> </button>
<template #popper> <template #popper>
<PublishLanguagePicker v-model="draft.params.language" min-w-80 p3 /> <PublishLanguagePicker v-model="draft.params.language" min-w-80 />
</template> </template>
</CommonDropdown> </CommonDropdown>
</CommonTooltip> </CommonTooltip>

View File

@ -4,15 +4,18 @@ import type { mastodon } from 'masto'
import type { UseDraft } from './statusDrafts' import type { UseDraft } from './statusDrafts'
import type { Draft } from '~~/types' import type { Draft } from '~~/types'
export const usePublish = (options: { export function usePublish(options: {
draftState: UseDraft draftState: UseDraft
expanded: Ref<boolean> expanded: Ref<boolean>
isUploading: Ref<boolean> isUploading: Ref<boolean>
initialDraft: Ref<() => Draft> initialDraft: Ref<() => Draft>
}) => { }) {
const { expanded, isUploading, initialDraft } = $(options) const { expanded, isUploading, initialDraft } = $(options)
let { draft, isEmpty } = $(options.draftState) let { draft, isEmpty } = $(options.draftState)
const { client } = $(useMasto()) const { client } = $(useMasto())
const settings = useUserSettings()
const preferredLanguage = $computed(() => (settings.value?.language || 'en').split('-')[0])
let isSending = $ref(false) let isSending = $ref(false)
const isExpanded = $ref(false) const isExpanded = $ref(false)
@ -31,6 +34,7 @@ export const usePublish = (options: {
async function publishDraft() { async function publishDraft() {
if (isPublishDisabled) if (isPublishDisabled)
return return
let content = htmlToText(draft.params.status || '') let content = htmlToText(draft.params.status || '')
if (draft.mentions?.length) if (draft.mentions?.length)
content = `${draft.mentions.map(i => `@${i}`).join(' ')} ${content}` content = `${draft.mentions.map(i => `@${i}`).join(' ')} ${content}`
@ -39,6 +43,7 @@ export const usePublish = (options: {
...draft.params, ...draft.params,
status: content, status: content,
mediaIds: draft.attachments.map(a => a.id), mediaIds: draft.attachments.map(a => a.id),
language: draft.params.language || preferredLanguage,
...(isGlitchEdition.value ? { 'content-type': 'text/markdown' } : {}), ...(isGlitchEdition.value ? { 'content-type': 'text/markdown' } : {}),
} as mastodon.v1.CreateStatusParams } as mastodon.v1.CreateStatusParams
@ -60,6 +65,7 @@ export const usePublish = (options: {
let status: mastodon.v1.Status let status: mastodon.v1.Status
if (!draft.editingStatus) if (!draft.editingStatus)
status = await client.v1.statuses.create(payload) status = await client.v1.statuses.create(payload)
else else
status = await client.v1.statuses.update(draft.editingStatus.id, payload) status = await client.v1.statuses.update(draft.editingStatus.id, payload)
if (draft.params.inReplyToId) if (draft.params.inReplyToId)
@ -84,14 +90,14 @@ export const usePublish = (options: {
shouldExpanded, shouldExpanded,
isPublishDisabled, isPublishDisabled,
failedMessages, failedMessages,
preferredLanguage,
publishDraft, publishDraft,
}) })
} }
export type MediaAttachmentUploadError = [filename: string, message: string] export type MediaAttachmentUploadError = [filename: string, message: string]
export const useUploadMediaAttachment = (draftRef: Ref<Draft>) => { export function useUploadMediaAttachment(draftRef: Ref<Draft>) {
const draft = $(draftRef) const draft = $(draftRef)
const { client } = $(useMasto()) const { client } = $(useMasto())
const { t } = useI18n() const { t } = useI18n()
@ -159,9 +165,10 @@ export const useUploadMediaAttachment = (draftRef: Ref<Draft>) => {
return $$({ return $$({
isUploading, isUploading,
isExceedingAttachmentLimit, isExceedingAttachmentLimit,
isOverDropZone,
failedAttachments, failedAttachments,
dropZoneRef, dropZoneRef,
isOverDropZone,
uploadAttachments, uploadAttachments,
pickAttachments, pickAttachments,

View File

@ -33,7 +33,7 @@ export function getDefaultDraft(options: Partial<Mutable<mastodon.v1.CreateStatu
visibility: visibility || 'public', visibility: visibility || 'public',
sensitive: sensitive ?? false, sensitive: sensitive ?? false,
spoilerText: spoilerText || '', spoilerText: spoilerText || '',
language: language || getDefaultLanguage(), language: language || '', // auto inferred from current language on posting
}, },
mentions, mentions,
lastUpdated: Date.now(), lastUpdated: Date.now(),
@ -52,16 +52,6 @@ export async function getDraftFromStatus(status: mastodon.v1.Status): Promise<Dr
}) })
} }
function getDefaultLanguage() {
const userSettings = useUserSettings()
const defaultLanguage = userSettings.value.language
if (defaultLanguage)
return defaultLanguage.split('-')[0]
return 'en'
}
function getAccountsToMention(status: mastodon.v1.Status) { function getAccountsToMention(status: mastodon.v1.Status) {
const userId = currentUser.value?.account.id const userId = currentUser.value?.account.id
const accountsToMention = new Set<string>() const accountsToMention = new Set<string>()