diff --git a/app/[leaflet_id]/publish/PublishPost.tsx b/app/[leaflet_id]/publish/PublishPost.tsx index 6c45065e..f0378271 100644 --- a/app/[leaflet_id]/publish/PublishPost.tsx +++ b/app/[leaflet_id]/publish/PublishPost.tsx @@ -33,6 +33,7 @@ type Props = { record?: PubLeafletPublication.Record; posts_in_pub?: number; entitiesToDelete?: string[]; + hasDraft: boolean; }; export function PublishPost(props: Props) { @@ -67,17 +68,29 @@ const PublishPostForm = ( let params = useParams(); let { rep } = useReplicache(); - // Get tags from Replicache state (same as in the draft editor) - let tags = useSubscribe(rep, (tx) => tx.get("publication_tags")); + // For publications with drafts, use Replicache; otherwise use local state + let replicacheTags = useSubscribe(rep, (tx) => + tx.get("publication_tags"), + ); + let [localTags, setLocalTags] = useState([]); - // Default to empty array if undefined - const currentTags = Array.isArray(tags) ? tags : []; + // Use Replicache tags only when we have a draft + const hasDraft = props.hasDraft; + const currentTags = hasDraft + ? Array.isArray(replicacheTags) + ? replicacheTags + : [] + : localTags; - // Update tags via the same mutation used in the editor + // Update tags via Replicache mutation or local state depending on context const handleTagsChange = async (newTags: string[]) => { - await rep?.mutate.updatePublicationDraft({ - tags: newTags, - }); + if (hasDraft) { + await rep?.mutate.updatePublicationDraft({ + tags: newTags, + }); + } else { + setLocalTags(newTags); + } }; async function submit() { diff --git a/app/[leaflet_id]/publish/page.tsx b/app/[leaflet_id]/publish/page.tsx index 9ee5917c..42690741 100644 --- a/app/[leaflet_id]/publish/page.tsx +++ b/app/[leaflet_id]/publish/page.tsx @@ -99,6 +99,11 @@ export default async function PublishLeafletPage(props: Props) { // If parsing fails, just use empty array } + // Check if a draft record exists (either in a publication or standalone) + let hasDraft = + data.leaflets_in_publications.length > 0 || + data.leaflets_to_documents.length > 0; + return ( ); diff --git a/components/Tags.tsx b/components/Tags.tsx index 10b33ed3..5232d732 100644 --- a/components/Tags.tsx +++ b/components/Tags.tsx @@ -95,9 +95,13 @@ export const TagSearchInput = (props: { [tagInputValue], ); - const filteredTags = searchResults.filter( - (tag) => !props.selectedTags.includes(tag.name), - ); + const filteredTags = searchResults + .filter((tag) => !props.selectedTags.includes(tag.name)) + .filter((tag) => + tag.name.toLowerCase().includes(tagInputValue.toLowerCase()), + ); + + const showResults = tagInputValue.length >= 3; function clearTagInput() { setHighlightedIndex(0); @@ -139,6 +143,7 @@ export const TagSearchInput = (props: { }; const userInputResult = + showResults && tagInputValue !== "" && !filteredTags.some((tag) => tag.name === tagInputValue); @@ -219,34 +224,42 @@ export const TagSearchInput = (props: { )}
- {userInputResult && ( + {showResults ? ( <> - { - selectTag(tagInputValue); - }} - /> + {userInputResult && ( + { + selectTag(tagInputValue); + }} + /> + )} + {filteredTags.map((tag, i) => ( + { + selectTag(tag.name); + }} + /> + ))} + ) : ( +
+ type at least 3 characters to search +
)} - {filteredTags.map((tag, i) => ( - { - selectTag(tag.name); - }} - /> - ))}