add intro

This commit is contained in:
Joey Hess 2012-07-29 21:54:23 -04:00
parent 0186f06744
commit 326617ad2f
5 changed files with 97 additions and 10 deletions

View file

@ -26,6 +26,7 @@ import Utility.Percentage
import Utility.DataUnits import Utility.DataUnits
import Types.Key import Types.Key
import qualified Remote import qualified Remote
import Logs.Web (webUUID)
import Yesod import Yesod
import Yesod.Static import Yesod.Static
@ -34,6 +35,7 @@ import Network.Socket (PortNumber)
import Text.Blaze.Renderer.String import Text.Blaze.Renderer.String
import Data.Text (Text, pack, unpack) import Data.Text (Text, pack, unpack)
import qualified Data.Map as M import qualified Data.Map as M
import Control.Concurrent.STM
thisThread :: String thisThread :: String
thisThread = "WebApp" thisThread = "WebApp"
@ -43,10 +45,29 @@ data WebApp = WebApp
, daemonStatus :: DaemonStatusHandle , daemonStatus :: DaemonStatusHandle
, transferQueue :: TransferQueue , transferQueue :: TransferQueue
, secretToken :: Text , secretToken :: Text
, baseTitle :: String , relDir :: FilePath
, getStatic :: Static , getStatic :: Static
, webAppState :: TMVar WebAppState
} }
data WebAppState = WebAppState
{ showIntro :: Bool
}
newWebAppState :: IO (TMVar WebAppState)
newWebAppState = liftIO $ atomically $
newTMVar $ WebAppState { showIntro = True }
getWebAppState :: forall sub. GHandler sub WebApp WebAppState
getWebAppState = liftIO . atomically . readTMVar =<< webAppState <$> getYesod
modifyWebAppState :: forall sub. (WebAppState -> WebAppState) -> GHandler sub WebApp ()
modifyWebAppState a = go =<< webAppState <$> getYesod
where
go s = liftIO $ atomically $ do
v <- takeTMVar s
putTMVar s $ a v
waitNotifier :: forall sub. (DaemonStatus -> NotificationBroadcaster) -> NotificationId -> GHandler sub WebApp () waitNotifier :: forall sub. (DaemonStatus -> NotificationBroadcaster) -> NotificationId -> GHandler sub WebApp ()
waitNotifier selector nid = do waitNotifier selector nid = do
notifier <- getNotifier selector notifier <- getNotifier selector
@ -71,6 +92,7 @@ mkYesod "WebApp" [parseRoutes|
/transfers/#NotificationId TransfersR GET /transfers/#NotificationId TransfersR GET
/sidebar/#NotificationId SideBarR GET /sidebar/#NotificationId SideBarR GET
/config ConfigR GET /config ConfigR GET
/addrepository AddRepositoryR GET
/static StaticR Static getStatic /static StaticR Static getStatic
|] |]
@ -119,7 +141,9 @@ autoUpdate ident gethtml ms_delay ms_startdelay = do
let startdelay = show ms_startdelay let startdelay = show ms_startdelay
$(widgetFile "longpolling") $(widgetFile "longpolling")
{- A display of currently running and queued transfers. -} {- A display of currently running and queued transfers.
-
- Or, if there have never been any this run, an intro display. -}
transfersDisplay :: Bool -> Widget transfersDisplay :: Bool -> Widget
transfersDisplay warnNoScript = do transfersDisplay warnNoScript = do
webapp <- lift getYesod webapp <- lift getYesod
@ -127,13 +151,35 @@ transfersDisplay warnNoScript = do
M.toList . currentTransfers M.toList . currentTransfers
<$> liftIO (getDaemonStatus $ daemonStatus webapp) <$> liftIO (getDaemonStatus $ daemonStatus webapp)
queued <- liftIO $ getTransferQueue $ transferQueue webapp queued <- liftIO $ getTransferQueue $ transferQueue webapp
let transfers = current ++ queued
let ident = transfersDisplayIdent let ident = transfersDisplayIdent
$(widgetFile "transfers") let transfers = current ++ queued
if null transfers
then ifM (lift $ showIntro <$> getWebAppState)
( introDisplay ident
, noop
)
else do
lift $ modifyWebAppState $ \s -> s { showIntro = False }
$(widgetFile "transfers")
transfersDisplayIdent :: Text transfersDisplayIdent :: Text
transfersDisplayIdent = "transfers" transfersDisplayIdent = "transfers"
introDisplay :: Text -> Widget
introDisplay ident = do
webapp <- lift getYesod
let reldir = relDir webapp
remotelist <- liftIO $ runThreadState (threadState webapp) $
Remote.prettyListUUIDs
=<< filter (/= webUUID) . nub . map Remote.uuid
<$> Remote.remoteList
let n = (length remotelist) + 1 -- plus this one
let numrepos = show n
let notenough = n < 2
let barelyenough = n == 2
let morethanenough = n > 2
$(widgetFile "intro")
{- Called by client to get a display of currently in process transfers. {- Called by client to get a display of currently in process transfers.
- -
- Returns a div, which will be inserted into the calling page. - Returns a div, which will be inserted into the calling page.
@ -237,7 +283,13 @@ getNoScriptR = defaultLayout $
getConfigR :: Handler RepHtml getConfigR :: Handler RepHtml
getConfigR = defaultLayout $ do getConfigR = defaultLayout $ do
sideBarDisplay False sideBarDisplay False
setTitle "configuration" setTitle "Configuration"
[whamlet|<a href="@{HomeR}">main|]
getAddRepositoryR :: Handler RepHtml
getAddRepositoryR = defaultLayout $ do
sideBarDisplay False
setTitle "Add repository"
[whamlet|<a href="@{HomeR}">main|] [whamlet|<a href="@{HomeR}">main|]
webAppThread :: ThreadState -> DaemonStatusHandle -> TransferQueue -> Maybe (IO ()) -> IO () webAppThread :: ThreadState -> DaemonStatusHandle -> TransferQueue -> Maybe (IO ()) -> IO ()
@ -259,13 +311,15 @@ webAppThread st dstatus transferqueue onstartup = do
then relPathDirToFile home dir then relPathDirToFile home dir
else dir else dir
token <- genRandomToken token <- genRandomToken
s <- newWebAppState
return $ WebApp return $ WebApp
{ threadState = st { threadState = st
, daemonStatus = dstatus , daemonStatus = dstatus
, transferQueue = transferqueue , transferQueue = transferqueue
, secretToken = pack token , secretToken = pack token
, baseTitle = reldir , relDir = reldir
, getStatic = $(embed "static") , getStatic = $(embed "static")
, webAppState = s
} }
{- Creates a html shim file that's used to redirect into the webapp, {- Creates a html shim file that's used to redirect into the webapp,

View file

@ -24,6 +24,7 @@ module Remote (
uuidDescriptions, uuidDescriptions,
byName, byName,
prettyPrintUUIDs, prettyPrintUUIDs,
prettyListUUIDs,
remotesWithUUID, remotesWithUUID,
remotesWithoutUUID, remotesWithoutUUID,
keyLocations, keyLocations,
@ -128,6 +129,15 @@ prettyPrintUUIDs desc uuids = do
, ("here", toJSON $ hereu == u) , ("here", toJSON $ hereu == u)
] ]
{- List of remote names and/or descriptions, for human display.
- Omits the current repisitory. -}
prettyListUUIDs :: [UUID] -> Annex [String]
prettyListUUIDs uuids = do
hereu <- getUUID
m <- uuidDescriptions
return $ map (\u -> M.findWithDefault "" u m) $
filter (/= hereu) uuids
{- Filters a list of remotes to ones that have the listed uuids. -} {- Filters a list of remotes to ones that have the listed uuids. -}
remotesWithUUID :: [Remote] -> [UUID] -> [Remote] remotesWithUUID :: [Remote] -> [UUID] -> [Remote]
remotesWithUUID rs us = filter (\r -> uuid r `elem` us) rs remotesWithUUID rs us = filter (\r -> uuid r `elem` us) rs

View file

@ -1,7 +1,7 @@
$doctype 5 $doctype 5
<html> <html>
<head> <head>
<title>#{baseTitle webapp} #{pageTitle page} <title>#{relDir webapp} #{pageTitle page}
<link rel="icon" href=@{StaticR favicon_ico} type="image/x-icon"> <link rel="icon" href=@{StaticR favicon_ico} type="image/x-icon">
<meta name="viewport" content="width=device-width,initial-scale=1.0"> <meta name="viewport" content="width=device-width,initial-scale=1.0">
^{pageHead page} ^{pageHead page}

23
templates/intro.hamlet Normal file
View file

@ -0,0 +1,23 @@
<div .span9 ##{ident} .hero-unit>
<h2>
git-annex is watching over your files in <small><tt>#{reldir}</tt></small>
<p>
It will automatically notice changes, and keep files in sync between #
$if notenough
repositories on your devices ...
<h2>
But no other repositories are set up yet.
<a .btn .btn-primary .btn-large href="@{AddRepositoryR}">Add another repository</a>
$else
these #
$if barelyenough
<span .badge .badge-warning>#{numrepos}</span>
$else
<span .badge .badge-success>#{numrepos}</span>
\ repositories and devices:
<ul>
$forall name <- remotelist
<li>#{name}
<a .btn .btn-primary .btn-large href="@{AddRepositoryR}">Add another repository</a>
<div>
Or just sit back, watch the magic, and get on with using your files.

View file

@ -11,12 +11,12 @@
<ul .nav .pull-right> <ul .nav .pull-right>
<li .dropdown #menu1> <li .dropdown #menu1>
<a .dropdown-toggle data-toggle="dropdown" href="#menu1"> <a .dropdown-toggle data-toggle="dropdown" href="#menu1">
Current Repository: #{baseTitle webapp} Current Repository: #{relDir webapp}
<b .caret></b> <b .caret></b>
<ul .dropdown-menu> <ul .dropdown-menu>
<li><a href="#">#{baseTitle webapp}</a></li> <li><a href="#">#{relDir webapp}</a></li>
<li .divider></li> <li .divider></li>
<li><a href="#">Add new repository</a></li> <li><a href="@{AddRepositoryR}">Add new repository</a></li>
<div .container-fluid> <div .container-fluid>
<div .row-fluid> <div .row-fluid>