|
|
@@ -2,53 +2,21 @@ module PureTabs.Model.GroupMapping where
|
|
|
|
|
|
|
|
|
import Browser.Tabs (WindowId)
|
|
|
-import Browser.Utils (unsafeLog)
|
|
|
-import Control.Bind ((<#>))
|
|
|
import Data.Array as A
|
|
|
import Data.Generic.Rep (class Generic)
|
|
|
import Data.Generic.Rep.Show (genericShow)
|
|
|
-import Data.Map as M
|
|
|
import Data.Maybe (fromMaybe, Maybe(..))
|
|
|
import Data.Newtype (class Newtype, unwrap)
|
|
|
-import Data.Semigroup ((<>))
|
|
|
-import Data.Show (class Show)
|
|
|
-import Data.Tuple (Tuple(..))
|
|
|
+import Data.Show (class Show, show)
|
|
|
import Effect.Aff (Aff)
|
|
|
-import Effect.Class (liftEffect)
|
|
|
-import Effect.Class.Console (error, log)
|
|
|
+import Effect.Class.Console (error)
|
|
|
import Foreign.Class (class Decode, class Encode)
|
|
|
import Foreign.Generic (defaultOptions, genericDecode, genericEncode)
|
|
|
-import Prelude (Unit, bind, flip, join, map, pure, ($), (*>), (/=), (<*), (<<<), (==), (>>=), (>>>))
|
|
|
-import PureTabs.Browser.Storage (storageLocalGet, storageLocalSet)
|
|
|
+import Prelude (Unit, bind, flip, map, pure, ($), (/=), (<*), (<>), (==), (>>=), (>>>))
|
|
|
+import PureTabs.Browser.Sessions (getWindowValue, setWindowValue)
|
|
|
import PureTabs.Model.Group (GroupId)
|
|
|
|
|
|
|
|
|
-newtype SavedGroupMapping
|
|
|
- = SavedGroupMapping { windowId :: WindowId
|
|
|
- , groupId :: GroupId
|
|
|
- , name :: String
|
|
|
- }
|
|
|
-
|
|
|
-savedGroupMapping :: WindowId -> GroupId -> String -> SavedGroupMapping
|
|
|
-savedGroupMapping winId gid name = SavedGroupMapping { windowId: winId, groupId: gid, name: name }
|
|
|
-
|
|
|
-derive instance genGroupMapping :: Generic SavedGroupMapping _
|
|
|
-derive instance newtypeGroupMapping :: Newtype SavedGroupMapping _
|
|
|
-
|
|
|
-instance showGroupMapping :: Show SavedGroupMapping where
|
|
|
- show = genericShow
|
|
|
-
|
|
|
-instance encodeGroupMapping :: Encode SavedGroupMapping where
|
|
|
- encode x = genericEncode (defaultOptions { unwrapSingleConstructors = true }) x
|
|
|
-
|
|
|
-instance decodeGroupMapping :: Decode SavedGroupMapping where
|
|
|
- decode x = genericDecode (defaultOptions { unwrapSingleConstructors = true }) x
|
|
|
-
|
|
|
-newtype SavedGroups = SavedGroups (Array SavedGroupMapping)
|
|
|
-
|
|
|
-derive instance genSavedGroups :: Generic SavedGroups _
|
|
|
-derive instance newtypeSavedGroups :: Newtype SavedGroups _
|
|
|
-
|
|
|
newtype GroupData
|
|
|
= GroupData { groupId :: GroupId
|
|
|
, name :: String
|
|
|
@@ -69,68 +37,44 @@ instance decodeGroupData :: Decode GroupData where
|
|
|
groupData :: GroupId -> String -> GroupData
|
|
|
groupData gid name = GroupData { groupId: gid, name: name }
|
|
|
|
|
|
-type GroupMapping = M.Map WindowId (Array GroupData)
|
|
|
+newtype SavedGroups = SavedGroups (Array GroupData)
|
|
|
|
|
|
-loadMap :: Array (SavedGroupMapping) -> GroupMapping
|
|
|
-loadMap = M.fromFoldableWith ((<>))
|
|
|
- <<< map (unwrap >>> \r -> Tuple r.windowId (A.singleton (groupData r.groupId r.name)))
|
|
|
-
|
|
|
-saveMap :: GroupMapping -> SavedGroups
|
|
|
-saveMap =
|
|
|
- M.toUnfoldable
|
|
|
- >>> map (\(Tuple winId groups) -> groups <#> \(GroupData g) -> savedGroupMapping winId g.groupId g.name)
|
|
|
- >>> join
|
|
|
- >>> SavedGroups
|
|
|
+derive instance genSavedGroups :: Generic SavedGroups _
|
|
|
+derive instance newtypeSavedGroups :: Newtype SavedGroups _
|
|
|
|
|
|
-retrieveGroups :: Aff (Array SavedGroupMapping)
|
|
|
-retrieveGroups = do
|
|
|
- (groups :: (Maybe SavedGroups)) <- storageLocalGet "groups"
|
|
|
+retrieveGroups :: WindowId -> Aff (Array GroupData)
|
|
|
+retrieveGroups winId = do
|
|
|
+ (groups :: (Maybe SavedGroups)) <- getWindowValue winId "groups"
|
|
|
case groups of
|
|
|
Just (SavedGroups groups') -> pure groups'
|
|
|
- Nothing -> pure [] <* error "couldn't get key `groups` from local storage"
|
|
|
-
|
|
|
-retrieveGroups' :: Aff GroupMapping
|
|
|
-retrieveGroups' = retrieveGroups <#> loadMap
|
|
|
+ Nothing -> pure [] <* error ("couldn't get key `groups` for window " <> (show winId))
|
|
|
|
|
|
-retrieveGroupsAt :: WindowId -> Aff (Array GroupData)
|
|
|
-retrieveGroupsAt winId = retrieveGroups' <#> (fromMaybe [] <<< M.lookup winId)
|
|
|
+type GroupsUpdate = (Array GroupData) -> (Array GroupData)
|
|
|
|
|
|
-type GroupsUpdate = GroupMapping -> GroupMapping
|
|
|
-
|
|
|
-updateGroupsMapping :: GroupsUpdate -> Aff Unit
|
|
|
-updateGroupsMapping updateGroups = do
|
|
|
- groups <- retrieveGroups'
|
|
|
- _ <- liftEffect $ (log "[bg] old groups:") *> (unsafeLog $ saveMap groups)
|
|
|
+updateGroupsMapping :: WindowId -> GroupsUpdate -> Aff Unit
|
|
|
+updateGroupsMapping winId updateGroups = do
|
|
|
+ groups <- retrieveGroups winId
|
|
|
let updatedGroups = updateGroups groups
|
|
|
- _ <- liftEffect $ (log "[bg] new groups:") *> (unsafeLog $ saveMap updatedGroups)
|
|
|
- storageLocalSet "groups" $ saveMap updatedGroups
|
|
|
-
|
|
|
+ setWindowValue winId "groups" updatedGroups
|
|
|
|
|
|
-updateMappingAt :: WindowId -> (Array GroupData -> Array GroupData) -> GroupsUpdate
|
|
|
-updateMappingAt winId update = M.update (update >>> Just) winId
|
|
|
|
|
|
-createGroup :: WindowId -> GroupId -> String -> GroupsUpdate
|
|
|
-createGroup winId gid name = insertIfNotExist >>> createGroup'
|
|
|
- where
|
|
|
- insertIfNotExist mapping = if M.member winId mapping then mapping else M.insert winId [] mapping
|
|
|
- createGroup' = updateMappingAt winId $
|
|
|
- A.filter (unwrap >>> _.groupId >>> (/=) gid)
|
|
|
- >>> (flip A.snoc) (groupData gid name)
|
|
|
+createGroup :: GroupId -> String -> GroupsUpdate
|
|
|
+createGroup gid name =
|
|
|
+ A.filter (unwrap >>> _.groupId >>> (/=) gid)
|
|
|
+ >>> (flip A.snoc) (groupData gid name)
|
|
|
|
|
|
-renameGroup :: WindowId -> GroupId -> String -> GroupsUpdate
|
|
|
-renameGroup winId gid newName =
|
|
|
- updateMappingAt winId $
|
|
|
+renameGroup :: GroupId -> String -> GroupsUpdate
|
|
|
+renameGroup gid newName =
|
|
|
map $ case _ of
|
|
|
GroupData { groupId: gid' } | gid == gid' -> groupData gid newName
|
|
|
other -> other
|
|
|
|
|
|
-moveGroup :: WindowId -> GroupId -> Int -> GroupsUpdate
|
|
|
-moveGroup winId gid to =
|
|
|
- updateMappingAt winId $ \arr ->
|
|
|
+moveGroup :: GroupId -> Int -> GroupsUpdate
|
|
|
+moveGroup gid to arr =
|
|
|
fromMaybe arr $ do
|
|
|
from <- A.findIndex (unwrap >>> _.groupId >>> (==) gid) arr
|
|
|
group <- arr A.!! from
|
|
|
A.deleteAt from arr >>= A.insertAt to group
|
|
|
|
|
|
-deleteGroup :: WindowId -> GroupId -> GroupsUpdate
|
|
|
-deleteGroup winId gid = updateMappingAt winId $ A.filter (unwrap >>>_.groupId >>> (/=) gid)
|
|
|
+deleteGroup :: GroupId -> GroupsUpdate
|
|
|
+deleteGroup gid = A.filter (unwrap >>>_.groupId >>> (/=) gid)
|