From 0c75c04b4cd681038a56bda6b42c860d6a874d2a Mon Sep 17 00:00:00 2001 From: hole-thu Date: Fri, 24 Dec 2021 22:33:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=BA=E5=8C=96=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Flows.js | 54 ++++++++++++++++++++++-------------------------- src/flows_api.js | 14 +++++-------- 2 files changed, 30 insertions(+), 38 deletions(-) diff --git a/src/Flows.js b/src/Flows.js index ea15271..e0c5aba 100644 --- a/src/Flows.js +++ b/src/Flows.js @@ -21,7 +21,7 @@ import { import './Flows.css'; import LazyLoad, { forceCheck } from './react-lazyload/src'; import { TokenCtx, ReplyForm } from './UserAction'; -import { API } from './flows_api'; +import { API, parse_replies } from './flows_api'; import { cache } from './cache'; import { save_attentions } from './Attention' import Poll from 'react-polls'; @@ -886,8 +886,9 @@ class FlowItemRow extends PureComponent { !props.search_param && (window.config.whitelist_cw.indexOf('*')==-1 && window.config.whitelist_cw.indexOf(props.info.cw)==-1) && props.mode !== 'attention' && props.mode !== 'attention_finished'; + this.color_picker = new ColorPicker(); this.state = { - replies: [], + replies: props.info.comments ? parse_replies(props.info.comments, this.color_picker) : [], reply_status: 'done', reply_error: null, info: Object.assign({}, props.info, { variant: {} }), @@ -898,12 +899,12 @@ class FlowItemRow extends PureComponent { props.attention_override === null ? false : props.attention_override, cached: true, // default no display anything }; - this.color_picker = new ColorPicker(); } componentDidMount() { // cache from getlist, so always to this to update attention - if (true || parseInt(this.state.info.reply, 10)) { + if (!this.props.info.comments) { + //if (true || parseInt(this.state.info.reply, 10)) { this.load_replies(null, /*update_count=*/ false); } } @@ -1234,7 +1235,7 @@ function FlowChunk(props) { {!!props.title && } {props.list.map((info, ind) => !info.blocked && ( this.state.loaded_pages + 1) throw new Error('bad page'); if (page === this.state.loaded_pages + 1) { + const { mode, search_param } = this.state; + const { token, submode } = this.props; console.log('fetching page', page); cache(); - if (this.state.mode === 'list') { - API.get_list(page, this.props.token, this.props.submode) + if (mode === 'list') { + API.get_list(page, token, submode) .then((json) => { if (page === 1 && json.data.length) { // update latest_post_id @@ -1407,29 +1411,21 @@ class SubFlow extends PureComponent { })); }) .catch(failed); - } else if (this.state.mode === 'search' && this.state.search_param) { - API.get_search(page, this.state.search_param, this.props.token) + } else if (mode === 'search' && search_param) { + API.get_search(page, search_param, token, submode) .then((json) => { const finished = json.data.length === 0; this.setState((prev, props) => ({ chunks: { - title: 'Result for "' + this.state.search_param + '"', - data: prev.chunks.data.concat( - json.data.filter( - (x) => - prev.chunks.data.length === 0 || - !prev.chunks.data - .slice(-100) - .some((p) => p.pid === x.pid), - ), - ), + title: 'Result for "' + search_param + '"', + data: prev.chunks.data.concat(json.data), }, mode: finished ? 'search_finished' : 'search', loading_status: 'done', })); }) .catch(failed); - } else if (this.state.mode === 'single') { + } else if (mode === 'single') { const pid = parseInt(this.state.search_param.substr(1), 10); API.get_single(pid, this.props.token) .then((json) => { @@ -1454,7 +1450,7 @@ class SubFlow extends PureComponent { }); }) .catch(failed); - } else if (this.state.mode === 'attention') { + } else if (mode === 'attention') { let use_search = !!this.state.search_param; let use_regex = use_search && !!this.state.search_param.match(/\/.+\//); let regex_search = /.+/; diff --git a/src/flows_api.js b/src/flows_api.js index 1fe3a48..95b8a86 100644 --- a/src/flows_api.js +++ b/src/flows_api.js @@ -17,7 +17,7 @@ const handle_response = async (response, notify = false) => { return json; }; -const parse_replies = (replies, color_picker) => +export const parse_replies = (replies, color_picker) => replies .sort((a, b) => parseInt(a.cid, 10) - parseInt(b.cid, 10)) .map((info) => { @@ -185,15 +185,11 @@ export const API = { return handle_response(response); }, - get_search: async (page, keyword, token) => { + get_search: async (page, keyword, token, submode) => { let response = await fetch( - API_BASE + - '/search?pagesize=' + - SEARCH_PAGESIZE + - '&page=' + - page + - '&keywords=' + - encodeURIComponent(keyword), + `${API_BASE}/search?search_mode=${submode}&page=${page}&keywords=${ + encodeURIComponent(keyword) + }&pagesize=${SEARCH_PAGESIZE}`, { headers: {'User-Token': token}, }