From 6d509a4f29dad8ad31b5b661dca509db56a65248 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 30 Dec 2017 13:06:59 -0500 Subject: [PATCH] Flesh out filtering --- package.json | 1 + src/index.js | 5 ++++- src/search.js | 43 ++++++++++++++++++++++++++++++++++++++ src/ui/navigation.js | 32 +--------------------------- src/ui/torrent_progress.js | 1 + 5 files changed, 50 insertions(+), 32 deletions(-) create mode 100644 src/search.js diff --git a/package.json b/package.json index 6df792c..a615192 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "isomorphic-fetch": "^2.2.1", "moment": "^2.20.1", "node-sass": "^4.7.2", + "numeral": "^2.0.6", "preact": "^8.2.7", "preact-compat": "^3.17.0", "query-string": "^5.0.1", diff --git a/src/index.js b/src/index.js index eb7da3a..8f5c995 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ import React from 'react'; import ReactDOM from 'react-dom'; import { Provider } from 'react-redux'; import { ConnectedRouter } from 'react-router-redux'; +import query from 'query-string'; import 'preact/devtools'; import './polyfills'; @@ -11,6 +12,7 @@ import scss from '../scss/main.scss'; import { ws_init } from './socket'; import { filter_subscribe } from './actions/filter_subscribe'; import { socket_uri, socket_update, SOCKET_STATE } from './actions/socket'; +import search_criteria from './search'; import Main from './ui/main'; import Connection from './ui/connection'; @@ -19,8 +21,9 @@ export function initialize(uri) { store.dispatch(socket_uri(uri)); store.dispatch(socket_update(SOCKET_STATE.CONNECTING)); ws_init(uri, () => { + const qs = query.parse(window.location.search); store.dispatch(socket_update(SOCKET_STATE.CONNECTED)); - store.dispatch(filter_subscribe()); + store.dispatch(filter_subscribe('torrent', search_criteria(qs.s))); store.dispatch(filter_subscribe('server')); }, () => { store.dispatch(socket_update(SOCKET_STATE.DISCONNECTED, diff --git a/src/search.js b/src/search.js new file mode 100644 index 0000000..a735254 --- /dev/null +++ b/src/search.js @@ -0,0 +1,43 @@ +import numeral from "numeral"; + +// via https://stackoverflow.com/a/46946490 +const ssplit = str => str.match(/\\?.|^$/g).reduce((p, c) => { + if (c === '"') { + p.quote ^= 1; + } else if (!p.quote && c === ' ') { + p.a.push(''); + } else { + p.a[p.a.length-1] += c.replace(/\\(.)/,"$1"); + } + return p; +}, {a: ['']}).a; + +export default function search_criteria(text) { + if (!text) { + return []; + } + const terms = ssplit(text); + const operators = [":", "==", "!=", ">", ">=", "<", "<="]; + return terms.map(t => operators.reduce((a, op) => { + if (t.indexOf(op) === -1) { + return a; + } + const [field, value] = t.split(op); + if (op == ":") { + return { op: "ilike", field, value: `%${t}%` }; + } + if (!isNaN(numeral(value).value())) { + return { + op, + field, + value: numeral(value).value() + }; + } + return { op, field, value }; + }, null) || { + field: "name", + op: "ilike", + value: `%${t}%` + } + ); +} diff --git a/src/ui/navigation.js b/src/ui/navigation.js index c3e1fb3..b3097be 100644 --- a/src/ui/navigation.js +++ b/src/ui/navigation.js @@ -4,6 +4,7 @@ import { Link, NavLink } from 'react-router-dom'; import { filter_subscribe } from '../actions/filter_subscribe'; import { push } from 'react-router-redux'; import query from 'query-string'; +import search_criteria from '../search'; function search_qs(text) { const qs = query.stringify({ @@ -15,37 +16,6 @@ function search_qs(text) { }${qs && "?" + qs}`; } -// via https://stackoverflow.com/a/46946490 -const ssplit = str => str.match(/\\?.|^$/g).reduce((p, c) => { - if (c === '"') { - p.quote ^= 1; - } else if (!p.quote && c === ' ') { - p.a.push(''); - } else { - p.a[p.a.length-1] += c.replace(/\\(.)/,"$1"); - } - return p; -}, {a: ['']}).a; - -function search_criteria(text) { - const terms = ssplit(text); - return terms.map(t => { - if (t.indexOf("status:") === 0) { - return { - field: "status", - op: "==", - value: t.split(":")[1] - }; - } else { - return { - field: "name", - op: "ilike", - value: `%${t}%` - }; - } - }); -} - function update_filter(text, fs, location, dispatch) { // there will always be one torrent filter const tfilter = fs.filter(fs => fs.kind === "torrent")[0]; diff --git a/src/ui/torrent_progress.js b/src/ui/torrent_progress.js index 22d3a23..0b2dd9a 100644 --- a/src/ui/torrent_progress.js +++ b/src/ui/torrent_progress.js @@ -8,6 +8,7 @@ function color(torrent) { case "seeding": return "primary"; case "hashing": + case "magnet": return "info"; case "idle": case "pending":