<commit_before>module GUI.TagEditor where

import Bucket.EditItem
import Bucket.Import
import Bucket.Types
import Control.Monad
import Data.IORef
import Data.List
import Graphics.UI.Gtk
import GUI.ItemsTreeModel
import Meta
import Open
import SearchFilter
import System.Directory

handleEditButtonClicked tagEditor treeView model tagEditorText currentBucketRef updateItemList = do
    -- TODO: extract tags -> comma sep string -> tags somehere else
    (treePath, _) <- treeViewGetCursor treeView
    item <- getItem treeView model treePath
    entrySetText tagEditorText (intercalate "," (tags item))
    response <- dialogRun tagEditor
    when (response == ResponseOk) $ do
        currentBucket <- readIORef currentBucketRef
        tagsText <- entryGetText tagEditorText
        let newItem = setTags item (getTags tagsText)
        -- TODO: show error dialog if we can't save item
        newBucket <- editItem currentBucket item newItem
        writeIORef currentBucketRef newBucket
        updateItemList
    widgetHide tagEditor
    where
        getTags "" = []
        getTags s = (takeWhile notComma s):getTags (drop 1 (dropWhile notComma s))
        notComma ',' = False
        notComma _ = True<commit_msg>refactor: extract tag conversion and remove unnecessary imports<commit_after>module GUI.TagEditor where

import Bucket.EditItem
import Bucket.Types
import Control.Monad
import Data.IORef
import Data.List
import Graphics.UI.Gtk
import GUI.ItemsTreeModel

handleEditButtonClicked tagEditor treeView model tagEditorText currentBucketRef updateItemList = do
    (treePath, _) <- treeViewGetCursor treeView
    item <- getItem treeView model treePath
    entrySetText tagEditorText (tagsToString (tags item))
    response <- dialogRun tagEditor
    when (response == ResponseOk) $ do
        currentBucket <- readIORef currentBucketRef
        tagsText <- entryGetText tagEditorText
        let newItem = setTags item (stringToTags tagsText)
        -- TODO: show error dialog if we can't save item
        newBucket <- editItem currentBucket item newItem
        writeIORef currentBucketRef newBucket
        updateItemList
    widgetHide tagEditor

tagsToString :: [String] -> String
tagsToString = intercalate ","

stringToTags :: String -> [String]
stringToTags = getTags
    where
        getTags "" = []
        getTags s = (takeWhile notComma s):getTags (drop 1 (dropWhile notComma s))
        notComma ',' = False
        notComma _ = True
{'fraction_non_alphanumeric': 0.06252537555826228, 'fraction_numerical': 0.0008120178643930166, 'mean_word_length': 3.431654676258993, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '618387', 'n_tokens_mistral': 677, 'n_tokens_neox': 647, 'n_words': 221}