Skip to content

Commit

Permalink
fix: support old drafts
Browse files Browse the repository at this point in the history
  • Loading branch information
maybeanerd committed Apr 7, 2024
1 parent a9bdf59 commit e0f14c9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
15 changes: 11 additions & 4 deletions components/publish/PublishWidgetFull.client.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
import { formatTimeAgo } from '@vueuse/core'
import type { DraftItem } from '~/types'
const route = useRoute()
const { formatNumber } = useHumanReadableNumber()
Expand All @@ -20,6 +21,12 @@ watchEffect(() => {
onDeactivated(() => {
clearEmptyDrafts()
})
function firstDraftItemOf(drafts: DraftItem | Array<DraftItem>): DraftItem {
if (Array.isArray(drafts))
return drafts[0]
return drafts
}
</script>

<template>
Expand All @@ -33,20 +40,20 @@ onDeactivated(() => {
<template #popper="{ hide }">
<div flex="~ col">
<NuxtLink
v-for="[key, draft] of nonEmptyDrafts" :key="key" border="b base" text-left py2 px4
v-for="[key, drafts] of nonEmptyDrafts" :key="key" border="b base" text-left py2 px4
hover:bg-active :replace="true" :to="`/compose?draft=${encodeURIComponent(key)}`" @click="hide()"
>
<div>
<div flex="~ gap-1" items-center>
<i18n-t keypath="compose.draft_title">
<code>{{ key }}</code>
</i18n-t>
<span v-if="draft[0].lastUpdated" text-secondary text-sm>
&middot; {{ formatTimeAgo(new Date(draft[0].lastUpdated), timeAgoOptions) }}
<span v-if="firstDraftItemOf(drafts).lastUpdated" text-secondary text-sm>
&middot; {{ formatTimeAgo(new Date(firstDraftItemOf(drafts).lastUpdated), timeAgoOptions) }}
</span>
</div>
<div text-secondary>
{{ htmlToText(draft[0].params.status).slice(0, 50) }}
{{ htmlToText(firstDraftItemOf(drafts).params.status).slice(0, 50) }}
</div>
</div>
</NuxtLink>
Expand Down
13 changes: 9 additions & 4 deletions composables/masto/statusDrafts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,16 @@ export function getReplyDraft(status: mastodon.v1.Status) {
}
}

export function isEmptyDraft(drafts: Array<DraftItem> | null | undefined) {
export function isEmptyDraft(drafts: Array<DraftItem> | DraftItem | null | undefined) {
if (!drafts)
return true

if (drafts.length === 0)
const draftsArray: Array<DraftItem> = Array.isArray(drafts) ? drafts : [drafts]

if (draftsArray.length === 0)
return true

const anyDraftHasContent = drafts.some((draft) => {
const anyDraftHasContent = draftsArray.some((draft) => {
const { params, attachments } = draft
const status = params.status ?? ''
const text = htmlToText(status).trim().replace(/^(@\S+\s?)+/, '').replaceAll(/```/g, '').trim()
Expand All @@ -144,7 +146,10 @@ export function useDraft(
get() {
if (!currentUserDrafts.value[draftKey])
currentUserDrafts.value[draftKey] = [initial()]
return currentUserDrafts.value[draftKey]
const drafts = currentUserDrafts.value[draftKey]
if (Array.isArray(drafts))
return drafts
return [drafts]
},
set(val) {
currentUserDrafts.value[draftKey] = val
Expand Down
4 changes: 3 additions & 1 deletion types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export interface DraftItem {
mentions?: string[]
}

export type DraftMap = Record<string, Array<DraftItem>>
export type DraftMap = Record<string, Array<DraftItem>
// For backward compatibility we need to support single draft items
| DraftItem>

export interface ConfirmDialogOptions {
title: string
Expand Down

0 comments on commit e0f14c9

Please sign in to comment.