From 979b308838c9ab628020568bc344909cf534810d Mon Sep 17 00:00:00 2001 From: Changkun Ou Date: Sun, 29 Aug 2021 17:54:37 +0200 Subject: [PATCH] dashboard: add dev mode for better dev experience --- dashboard/public/index.html | 4 ++-- dashboard/src/App.js | 2 ++ dashboard/src/components/Home.js | 2 +- dashboard/src/components/RedirTable.js | 18 ++++++++++++----- dashboard/src/components/Stats.js | 27 +++++++++++++++++--------- short.go | 6 +++--- 6 files changed, 39 insertions(+), 20 deletions(-) diff --git a/dashboard/public/index.html b/dashboard/public/index.html index c4c9c6b..014d6f0 100644 --- a/dashboard/public/index.html +++ b/dashboard/public/index.html @@ -12,7 +12,7 @@ - -
+ +
diff --git a/dashboard/src/App.js b/dashboard/src/App.js index 9ffb666..92f864d 100644 --- a/dashboard/src/App.js +++ b/dashboard/src/App.js @@ -11,9 +11,11 @@ const App = () => { const root = document.getElementById('root') const isAdmin = root.getAttribute('is-admin') const statsMode = root.getAttribute('stats-mode') + const devMode = root.getAttribute('dev-mode') return } diff --git a/dashboard/src/components/Home.js b/dashboard/src/components/Home.js index 1580bcf..cdeea23 100644 --- a/dashboard/src/components/Home.js +++ b/dashboard/src/components/Home.js @@ -36,7 +36,7 @@ const Home = (props) => { {props.isAdmin ? :
} - + diff --git a/dashboard/src/components/RedirTable.js b/dashboard/src/components/RedirTable.js index bb978d1..be200a0 100644 --- a/dashboard/src/components/RedirTable.js +++ b/dashboard/src/components/RedirTable.js @@ -113,7 +113,7 @@ const RedirTable = (props) => { let pageSize = 10 const expandedRowRender = (params) => { - return + return } return ( @@ -126,10 +126,18 @@ const RedirTable = (props) => { expandable={props.isAdmin && props.statsMode ? { expandedRowRender } : false} request={async (params) => { const mode = props.isAdmin ? 'index-pro' : 'index' - const host = window.location.origin - const path = window.location.pathname.endsWith('/') ? - window.location.pathname.slice(0, -1) : - window.location.pathname; + + let host = '' + let path = '' + if (props.devMode) { + host = 'http://localhost:9123' + path = '/s' + } else { + host = window.location.origin + path = window.location.pathname.endsWith('/') ? + window.location.pathname.slice(0, -1) : + window.location.pathname; + } const url = `${host}${path}/?mode=${mode}&pn=${params.current}&ps=${params.pageSize}` const resp = await fetch(url, { diff --git a/dashboard/src/components/Stats.js b/dashboard/src/components/Stats.js index 9e6f013..7ba3d12 100644 --- a/dashboard/src/components/Stats.js +++ b/dashboard/src/components/Stats.js @@ -29,10 +29,17 @@ const Stats = (props) => { setT1(t1) } + let endpoint = '/s/?' + if (props.devMode) { + endpoint = 'http://localhost:9123/s/?' + } + + console.log(begin, end) + const [pvuvData, setPVUVData] = useState([]) useEffect(() => asyncFetchTime(t0, t1), []) const asyncFetchTime = (t0, t1) => { - fetch('/s/?'+ new URLSearchParams({ + fetch(endpoint+ new URLSearchParams({ mode: 'stats', a: props.alias, stat: 'time', @@ -51,7 +58,7 @@ const Stats = (props) => { const [refData, setRefData] = useState([]) useEffect(() => asyncFetchRef(t0, t1), []) const asyncFetchRef = (t0, t1) => { - fetch('/s/?'+ new URLSearchParams({ + fetch(endpoint+ new URLSearchParams({ mode: 'stats', a: props.alias, stat: 'referer', @@ -70,7 +77,7 @@ const Stats = (props) => { const [uaData, setUAData] = useState([]) useEffect(() => {asyncFetchUA(t0, t1)}, []) const asyncFetchUA = (t0, t1) => { - fetch('/s/?'+ new URLSearchParams({ + fetch(endpoint+ new URLSearchParams({ mode: 'stats', a: props.alias, stat: 'ua', @@ -139,11 +146,13 @@ const Stats = (props) => { return 0 }) const dateRangeOnChange = (_, dateString) => { - setT0(dateString[0]) - setT1(dateString[1]) - asyncFetchTime(t0, t1) - asyncFetchRef(t0, t1) - asyncFetchUA(t0, t1) + const d0 = dateString[0] + const d1 = dateString[1] + setT0(d0) + setT1(d1) + asyncFetchTime(d0, d1) + asyncFetchRef(d0, d1) + asyncFetchUA(d0, d1) } return (
@@ -152,7 +161,7 @@ const Stats = (props) => { onBack={false} title="Visitors" /> - + diff --git a/short.go b/short.go index 6f1da45..52f1c71 100644 --- a/short.go +++ b/short.go @@ -385,9 +385,7 @@ func (s *server) checkvcs(ctx context.Context, alias string) (*models.Redir, err // construct the try path and make the request to vcs repoPath := config.Conf.X.RepoPath - if strings.HasSuffix(repoPath, "/*") { - repoPath = strings.TrimSuffix(repoPath, "/*") - } + repoPath = strings.TrimSuffix(repoPath, "/*") tryPath := fmt.Sprintf("%s/%s", repoPath, alias) resp, err := http.Get(tryPath) if err != nil { @@ -445,9 +443,11 @@ func (s *server) sIndex( e := struct { AdminView bool StatsMode bool + DevMode bool }{ AdminView: false, StatsMode: config.Conf.Stats.Enable, + DevMode: false, } mode := r.URL.Query().Get("mode")