@@ -16,7 +16,7 @@ import {
1616import { LocalStorageWithSchema } from "../../utils/local-storage-with-schema" ;
1717import defaultResultFilters from "../../constants/default-result-filters" ;
1818import { getAllFunboxes } from "@monkeytype/funbox" ;
19- import { SnapshotUserTag } from "../../constants/default-snapshot" ;
19+ import { Snapshot , SnapshotUserTag } from "../../constants/default-snapshot" ;
2020import { LanguageList } from "../../constants/languages" ;
2121
2222export function mergeWithDefaultFilters (
@@ -742,9 +742,38 @@ let selectChangeCallbackFn: () => void = () => {
742742 //
743743} ;
744744
745+ export function updateTagsDropdownOptions ( ) : void {
746+ const el = document . querySelector < HTMLElement > (
747+ ".pageAccount .content .filterButtons .buttonsAndTitle.tags .select select"
748+ ) ;
749+
750+ if ( ! ( el instanceof HTMLElement ) ) return ;
751+
752+ const snapshot = DB . getSnapshot ( ) ;
753+
754+ if ( snapshot === undefined ) {
755+ return ;
756+ }
757+
758+ let html = "" ;
759+
760+ html += "<option value='all'>all</option>" ;
761+ html += "<option value='none'>no tag</option>" ;
762+
763+ for ( const tag of snapshot . tags ) {
764+ html += `<option value="${ tag . _id } " filter="${ tag . name } ">${ tag . display } </option>` ;
765+ }
766+
767+ el . innerHTML = html ;
768+ }
769+
770+ let buttonsAppended = false ;
771+
745772export async function appendButtons (
746773 selectChangeCallback : ( ) => void
747774) : Promise < void > {
775+ if ( buttonsAppended ) return ;
776+
748777 selectChangeCallbackFn = selectChangeCallback ;
749778
750779 groupSelects [ "language" ] = new SlimSelect ( {
@@ -810,34 +839,24 @@ export async function appendButtons(
810839 } ,
811840 } ) ;
812841
813- const snapshot = DB . getSnapshot ( ) ;
814-
815- if ( snapshot !== undefined && ( snapshot . tags ?. length ?? 0 ) > 0 ) {
816- $ ( ".pageAccount .content .filterButtons .buttonsAndTitle.tags" ) . removeClass (
817- "hidden"
818- ) ;
819-
820- let html = "" ;
821-
822- html +=
823- "<select class='tagsSelect' group='tags' placeholder='select a tag' multiple>" ;
842+ //snapshot at this point is guaranteed to exist
843+ const snapshot = DB . getSnapshot ( ) as Snapshot ;
824844
825- html += "<option value='all'>all</option>" ;
826- html += "<option value='none'>no tag</option>" ;
827-
828- for ( const tag of snapshot . tags ) {
829- html += `<option value="${ tag . _id } " filter="${ tag . name } ">${ tag . display } </option>` ;
830- }
831-
832- html += "</select>" ;
845+ const tagsSection = $ (
846+ ".pageAccount .content .filterButtons .buttonsAndTitle.tags"
847+ ) ;
833848
834- const el = document . querySelector (
835- ".pageAccount .content .filterButtons .buttonsAndTitle.tags .select"
849+ if ( snapshot . tags . length === 0 ) {
850+ tagsSection . addClass ( "hidden" ) ;
851+ } else {
852+ tagsSection . removeClass ( "hidden" ) ;
853+ updateTagsDropdownOptions ( ) ;
854+ const selectEl = document . querySelector (
855+ ".pageAccount .content .filterButtons .buttonsAndTitle.tags .select .tagsSelect"
836856 ) ;
837- if ( el ) {
838- el . innerHTML = html ;
857+ if ( selectEl ) {
839858 groupSelects [ "tags" ] = new SlimSelect ( {
840- select : el . querySelector ( ".tagsSelect" ) as HTMLSelectElement ,
859+ select : selectEl ,
841860 settings : {
842861 showSearch : true ,
843862 placeholderText : "select a tag" ,
@@ -861,13 +880,10 @@ export async function appendButtons(
861880 } ,
862881 } ) ;
863882 }
864- } else {
865- $ ( ".pageAccount .content .filterButtons .buttonsAndTitle.tags" ) . addClass (
866- "hidden"
867- ) ;
868883 }
869884
870885 void updateFilterPresets ( ) ;
886+ buttonsAppended = true ;
871887}
872888
873889export function removeButtons ( ) : void {
0 commit comments