import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {useEnableSquareButtons} from '#/state/preferences/enable-square-buttons'
import {atoms as a} from '#/alf'
import {Button, ButtonText} from '#/components/Button'
import * as Dialog from '#/components/Dialog'
import * as Prompt from '#/components/Prompt'
import {useAnalytics} from '#/analytics'
import {DraftsListDialog} from './DraftsListDialog'
import {useSaveDraftMutation} from './state/queries'
import {type DraftSummary} from './state/schema'
export function DraftsButton({
onSelectDraft,
onSaveDraft,
onDiscard,
isEmpty,
isDirty,
isEditingDraft,
canSaveDraft,
textLength,
}: {
onSelectDraft: (draft: DraftSummary) => void
onSaveDraft: () => Promise<{success: boolean}>
onDiscard: () => void
isEmpty: boolean
isDirty: boolean
isEditingDraft: boolean
canSaveDraft: boolean
textLength: number
}) {
const {_} = useLingui()
const enableSquareButtons = useEnableSquareButtons()
const ax = useAnalytics()
const draftsDialogControl = Dialog.useDialogControl()
const savePromptControl = Prompt.usePromptControl()
const {isPending: isSaving} = useSaveDraftMutation()
const handlePress = () => {
if (isEmpty || !isDirty) {
// Composer is empty or has no unsaved changes, go directly to drafts list
draftsDialogControl.open()
} else {
// Composer has unsaved changes, ask what to do
savePromptControl.open()
}
}
const handleSaveAndOpen = async () => {
const {success} = await onSaveDraft()
if (success) {
draftsDialogControl.open()
}
}
const handleDiscardAndOpen = () => {
// Fire draft:discard metric before discarding
ax.metric('draft:discard', {
logContext: 'BeforeDraftsList',
hadContent: !isEmpty,
textLength,
})
onDiscard()
draftsDialogControl.open()
}
return (
<>
{canSaveDraft ? (
isEditingDraft ? (
Save changes?
) : (
Save draft?
)
) : (
Discard draft?
)}
{canSaveDraft ? (
isEditingDraft ? (
You have unsaved changes. Would you like to save them before
viewing your drafts?
) : (
Would you like to save this as a draft before viewing your
drafts?
)
) : (
You can only save drafts up to 1000 characters. Would you like to
discard this post before viewing your drafts?
)}
{canSaveDraft && (
)}
>
)
}