also looking at this
Edit queued and scheduled posts (#21)
#22
6 files
+359
-7
| @@ -18,9 +18,13 @@ | ||
| 18 | 18 | </header> |
| 19 | 19 | <div class="grid"> |
| 20 | 20 | <section class="grid content-start grid-rows-[auto_auto_auto] self-start bg-white border border-zinc-200 rounded-2xl shadow-sm" aria-label="Post composer"> |
| 21 | <div class="hidden flex-wrap gap-3 items-center justify-between px-3 py-2 text-xs bg-blue-50 border-b border-blue-200" data-edit-banner> | |
| 22 | <span class="font-bold text-blue-800">Editing a queued post</span> | |
| 23 | <a class="inline-flex items-center px-2.5 min-h-6 font-bold text-blue-800 bg-white hover:bg-blue-100 border border-blue-200 rounded-lg transition-colors" href="/" data-stx-link>Cancel</a> | |
| 24 | </div> | |
| 21 | 25 | <header class="flex flex-wrap gap-3 items-center justify-between p-3 border-b border-zinc-200"> |
| 22 | 26 | <div class="flex gap-2 items-center"> |
| 23 | <strong class="text-[13px]">New post</strong> | |
| 27 | <strong class="text-[13px]" data-composer-heading>New post</strong> | |
| 24 | 28 | <a class="inline-flex gap-1.5 items-center px-2.5 min-h-7 font-bold text-xs text-zinc-600 bg-zinc-50 hover:bg-white border border-zinc-200 rounded-lg transition-colors" href="/accounts" data-stx-link data-bluesky-connection data-no-window-drag> |
| 25 | 29 | <span class="h-2 w-2 bg-zinc-300 rounded-full" data-bluesky-connection-dot aria-hidden="true"></span> |
| 26 | 30 | <span data-bluesky-connection-label>Checking accounts…</span> |
| @@ -150,6 +154,13 @@ | ||
| 150 | 154 | let providers = [] |
| 151 | 155 | const selected = new Set() |
| 152 | 156 | |
| 157 | // Edit mode: /?edit=<id> loads a queued post into the composer. | |
| 158 | const editBanner = root.querySelector('[data-edit-banner]') | |
| 159 | const composerHeading = root.querySelector('[data-composer-heading]') | |
| 160 | let editingId = null | |
| 161 | let editHadImage = false | |
| 162 | let editRemoveImage = false | |
| 163 | ||
| 153 | 164 | const setStatus = (message, tone = 'neutral', options = {}) => { |
| 154 | 165 | if (toast) { |
| 155 | 166 | toast({ |
| @@ -500,11 +511,122 @@ | ||
| 500 | 511 | |
| 501 | 512 | const updatePublishLabel = () => { |
| 502 | 513 | if (!publishLabel) return |
| 503 | if (activeMode === 'draft') publishLabel.textContent = 'Save draft' | |
| 514 | if (editingId) publishLabel.textContent = scheduleInput?.value ? 'Update schedule' : 'Update post' | |
| 515 | else if (activeMode === 'draft') publishLabel.textContent = 'Save draft' | |
| 504 | 516 | else if (scheduleInput?.value) publishLabel.textContent = 'Schedule' |
| 505 | 517 | else publishLabel.textContent = 'Publish' |
| 506 | 518 | } |
| 507 | 519 | |
| 520 | // Convert a UTC "YYYY-MM-DD HH:MM:SS" to the datetime-local input value. | |
| 521 | const utcToLocalInput = (utc) => { | |
| 522 | const date = new Date(`${utc.replace(' ', 'T')}Z`) | |
| 523 | if (Number.isNaN(date.getTime())) return '' | |
| 524 | return new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString().slice(0, 16) | |
| 525 | } | |
| 526 | ||
| 527 | const enterEditMode = (item) => { | |
| 528 | editingId = item.id | |
| 529 | editBanner?.classList.remove('hidden') | |
| 530 | editBanner?.classList.add('flex') | |
| 531 | if (composerHeading) composerHeading.textContent = `Editing post #${item.id}` | |
| 532 | ||
| 533 | // Text (single-post edit — threads edit one segment). | |
| 534 | const textarea = activeTextarea() | |
| 535 | if (textarea) textarea.value = item.text || '' | |
| 536 | if (composeTitle) composeTitle.value = item.title || '' | |
| 537 | ||
| 538 | // Providers: select exactly the post's set (intersected with connected). | |
| 539 | selected.clear() | |
| 540 | for (const provider of item.providers || []) selected.add(provider) | |
| 541 | renderProviderToggles() | |
| 542 | updatePublishability() | |
| 543 | ||
| 544 | // Schedule. | |
| 545 | if (item.scheduledAt && scheduleInput) { | |
| 546 | scheduleInput.value = utcToLocalInput(item.scheduledAt) | |
| 547 | scheduleInput.classList.remove('hidden') | |
| 548 | } | |
| 549 | ||
| 550 | // Link card. | |
| 551 | if (item.external?.uri) { | |
| 552 | activePost.dataset.externalUri = item.external.uri | |
| 553 | activePost.dataset.externalTitle = item.external.title || '' | |
| 554 | activePost.dataset.externalDescription = item.external.description || '' | |
| 555 | } | |
| 556 | ||
| 557 | // Existing image → show a "kept" chip; attaching a new file replaces, | |
| 558 | // clearing it marks removal. | |
| 559 | editHadImage = Boolean(item.image) | |
| 560 | editRemoveImage = false | |
| 561 | if (item.image) { | |
| 562 | if (attachName) attachName.textContent = item.image.url ? 'Current image (URL)' : 'Current image' | |
| 563 | attachChip?.classList.remove('hidden') | |
| 564 | attachChip?.classList.add('inline-flex') | |
| 565 | } | |
| 566 | ||
| 567 | updateCount() | |
| 568 | void updateLinkPreview(activePost) | |
| 569 | updatePublishLabel() | |
| 570 | } | |
| 571 | ||
| 572 | const loadEditPost = async (id) => { | |
| 573 | try { | |
| 574 | const response = await fetch(`/api/postline/queue/item?id=${encodeURIComponent(id)}`) | |
| 575 | const payload = await response.json() | |
| 576 | if (!response.ok || !payload.ok) throw new Error(payload.error || 'Could not load the post.') | |
| 577 | enterEditMode(payload.data) | |
| 578 | } | |
| 579 | catch (error) { | |
| 580 | setStatus(error instanceof Error ? error.message : String(error), 'error', { title: 'Edit' }) | |
| 581 | } | |
| 582 | } | |
| 583 | ||
| 584 | const updateQueued = async () => { | |
| 585 | const text = activeTextarea()?.value.trim() | |
| 586 | if (!text) { | |
| 587 | setStatus('Write something before updating.', 'error') | |
| 588 | activeTextarea()?.focus() | |
| 589 | return | |
| 590 | } | |
| 591 | const targetProviders = selectedConnected().map(provider => provider.provider) | |
| 592 | if (targetProviders.length === 0) { | |
| 593 | setStatus('Select a connected account.', 'error', { title: 'Not connected' }) | |
| 594 | return | |
| 595 | } | |
| 596 | ||
| 597 | const form = new FormData() | |
| 598 | form.set('id', String(editingId)) | |
| 599 | form.set('text', text) | |
| 600 | form.set('providers', targetProviders.join(',')) | |
| 601 | const editTitle = composedTitle() | |
| 602 | if (editTitle) form.set('title', editTitle) | |
| 603 | if (scheduleInput?.value) form.set('scheduled_at', toUtcTimestamp(scheduleInput.value)) | |
| 604 | if (activePost?.dataset.externalUri && activePost.dataset.externalTitle) { | |
| 605 | form.set('external_uri', activePost.dataset.externalUri) | |
| 606 | form.set('external_title', activePost.dataset.externalTitle) | |
| 607 | form.set('external_description', activePost.dataset.externalDescription || '') | |
| 608 | } | |
| 609 | const editImageUrl = composeImage?.value.trim() | |
| 610 | if (editImageUrl) form.set('image_url', editImageUrl) | |
| 611 | const editFile = attachedFile() | |
| 612 | if (editFile) form.set('image', editFile, editFile.name) | |
| 613 | else if (editRemoveImage) form.set('remove_image', '1') | |
| 614 | ||
| 615 | buttons.forEach(button => button.setAttribute('disabled', 'disabled')) | |
| 616 | try { | |
| 617 | const response = await fetch('/api/postline/queue/update', { method: 'POST', body: form }) | |
| 618 | const payload = await response.json() | |
| 619 | if (!response.ok || !payload.ok) throw new Error(payload.error || 'Could not update the post.') | |
| 620 | setStatus('Post updated.', 'success', { title: 'Updated' }) | |
| 621 | // Back to the queue to see the change. | |
| 622 | window.location.assign('/queue') | |
| 623 | } | |
| 624 | catch (error) { | |
| 625 | setStatus(error instanceof Error ? error.message : String(error), 'error', { title: 'Update failed' }) | |
| 626 | buttons.forEach((button) => { if (canPublish) button.removeAttribute('disabled') }) | |
| 627 | } | |
| 628 | } | |
| 629 | ||
| 508 | 630 | const toUtcTimestamp = (localValue) => { |
| 509 | 631 | const date = new Date(localValue) |
| 510 | 632 | if (Number.isNaN(date.getTime())) throw new Error('Pick a valid schedule time.') |
| @@ -618,6 +740,11 @@ | ||
| 618 | 740 | } |
| 619 | 741 | |
| 620 | 742 | const publish = async () => { |
| 743 | // Editing a queued post always updates it in place (never publishes). | |
| 744 | if (editingId) { | |
| 745 | await updateQueued() | |
| 746 | return | |
| 747 | } | |
| 621 | 748 | if (activeMode === 'draft' || scheduleInput?.value) { |
| 622 | 749 | await saveToQueue(scheduleInput?.value || null) |
| 623 | 750 | return |
| @@ -789,7 +916,11 @@ | ||
| 789 | 916 | attachChip?.classList.remove('hidden') |
| 790 | 917 | attachChip?.classList.add('inline-flex') |
| 791 | 918 | }) |
| 792 | attachClear?.addEventListener('click', clearAttachment) | |
| 919 | attachClear?.addEventListener('click', () => { | |
| 920 | // In edit mode, clearing an existing image marks it for removal. | |
| 921 | if (editingId && editHadImage && !attachedFile()) editRemoveImage = true | |
| 922 | clearAttachment() | |
| 923 | }) | |
| 793 | 924 | addPostButton?.addEventListener('click', addPost) |
| 794 | 925 | previewButton?.addEventListener('click', () => { |
| 795 | 926 | const text = activeTextarea()?.value.trim() |
| @@ -827,8 +958,13 @@ | ||
| 827 | 958 | }) |
| 828 | 959 | modeButtons.forEach(button => button.addEventListener('click', () => setMode(button.dataset.composerMode || 'single'))) |
| 829 | 960 | buttons.forEach(button => button.addEventListener('click', publish)) |
| 830 | window.addEventListener('beforeunload', saveDraft) | |
| 831 | restoreDraft() | |
| 961 | // Editing a queued post — don't restore a local draft over it, and don't | |
| 962 | // save the edited content back into the draft. | |
| 963 | const editParam = new URLSearchParams(window.location.search).get('edit') | |
| 964 | if (!editParam) { | |
| 965 | window.addEventListener('beforeunload', saveDraft) | |
| 966 | restoreDraft() | |
| 967 | } | |
| 832 | 968 | setMode(activeMode, { silent: true, skipSave: true, skipThreadPost: true }) |
| 833 | 969 | renamePosts() |
| 834 | 970 | selectPost(allPosts()[savedActiveIndex] || activePost, { skipSave: true }) |
| @@ -836,11 +972,16 @@ | ||
| 836 | 972 | void updateLinkPreview(activePost) |
| 837 | 973 | // Wait for the social module (a deferred `type=module` script) before the |
| 838 | 974 | // first provider fetch so window.postlineSocial is guaranteed to exist. |
| 975 | // Load the edit post only after connection so provider toggles resolve. | |
| 976 | const start = async () => { | |
| 977 | await refreshConnection() | |
| 978 | if (editParam) await loadEditPost(editParam) | |
| 979 | } | |
| 839 | 980 | if (document.readyState === 'loading') { |
| 840 | document.addEventListener('DOMContentLoaded', () => void refreshConnection(), { once: true }) | |
| 981 | document.addEventListener('DOMContentLoaded', () => void start(), { once: true }) | |
| 841 | 982 | } |
| 842 | 983 | else { |
| 843 | void refreshConnection() | |
| 984 | void start() | |
| 844 | 985 | } |
| 845 | 986 | })() |
| 846 | 987 | </script> |