mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-05 14:19:56 -05:00
133 lines
3.5 KiB
JavaScript
133 lines
3.5 KiB
JavaScript
|
|
import {useEffect, useState} from "react";
|
||
|
|
import {fetchDomains, getVersion, searchArchives, searchVideos} from "../api";
|
||
|
|
import {useHistory} from "react-router-dom";
|
||
|
|
|
||
|
|
export const useSearchParam = (key, defaultValue = null) => {
|
||
|
|
// Get a window.location.search param.
|
||
|
|
const startingValue = new URLSearchParams(window.location.search).get(key);
|
||
|
|
|
||
|
|
const [value, setValue] = useState(startingValue || defaultValue);
|
||
|
|
const history = useHistory();
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
const params = new URLSearchParams(window.location.search);
|
||
|
|
params.delete(key);
|
||
|
|
if (value) {
|
||
|
|
params.append(key, value);
|
||
|
|
}
|
||
|
|
history.push({search: params.toString()});
|
||
|
|
}, [value])
|
||
|
|
|
||
|
|
return [value, setValue];
|
||
|
|
}
|
||
|
|
|
||
|
|
export const useSearch = () => {
|
||
|
|
let [searchStr, setSearchStr] = useSearchParam('q');
|
||
|
|
|
||
|
|
const [archives, setArchives] = useState();
|
||
|
|
const [videos, setVideos] = useState();
|
||
|
|
|
||
|
|
const localSearchArchives = async (term) => {
|
||
|
|
setArchives(null);
|
||
|
|
const [archives, total] = await searchArchives(0, 6, null, term);
|
||
|
|
setArchives(archives);
|
||
|
|
}
|
||
|
|
|
||
|
|
const localSearchVideos = async (term) => {
|
||
|
|
setVideos(null);
|
||
|
|
const [videos, total] = await searchVideos(0, 6, null, term);
|
||
|
|
setVideos(videos);
|
||
|
|
}
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
if (searchStr) {
|
||
|
|
localSearchArchives(searchStr);
|
||
|
|
localSearchVideos(searchStr)
|
||
|
|
}
|
||
|
|
}, [searchStr]);
|
||
|
|
|
||
|
|
return {searchStr, setSearchStr, archives, videos}
|
||
|
|
}
|
||
|
|
|
||
|
|
export const useDomains = () => {
|
||
|
|
const [domains, setDomains] = useState(null);
|
||
|
|
const [total, setTotal] = useState(null);
|
||
|
|
|
||
|
|
const _fetchDomains = async () => {
|
||
|
|
setDomains(null);
|
||
|
|
setTotal(0);
|
||
|
|
let [domains, total] = await fetchDomains();
|
||
|
|
setDomains(domains);
|
||
|
|
setTotal(total);
|
||
|
|
}
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
_fetchDomains();
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
return [domains, total];
|
||
|
|
}
|
||
|
|
|
||
|
|
export const useArchives = (defaultLimit = 20) => {
|
||
|
|
const [archivesData, setArchives] = useState({archives: null, total: 0});
|
||
|
|
const [totalPages, setTotalPages] = useState(0);
|
||
|
|
const [activePage, setActivePage] = useState(1);
|
||
|
|
|
||
|
|
let [offset, setOffset] = useSearchParam('o');
|
||
|
|
let [limit, setLimit] = useSearchParam('l', defaultLimit);
|
||
|
|
let [domain, setDomain] = useSearchParam('domain');
|
||
|
|
let [searchStr, setSearchStr] = useSearchParam('q');
|
||
|
|
|
||
|
|
const setPage = (i) => {
|
||
|
|
i = parseInt(i);
|
||
|
|
let l = parseInt(limit);
|
||
|
|
setOffset((l * i) - l);
|
||
|
|
setActivePage(i);
|
||
|
|
}
|
||
|
|
|
||
|
|
const search = async (term) => {
|
||
|
|
setArchives({archives: null, total: 0});
|
||
|
|
setTotalPages(0);
|
||
|
|
const [archives, total] = await searchArchives(offset, limit, domain, term);
|
||
|
|
setTotalPages(Math.floor(total / limit) + 1);
|
||
|
|
setArchives({archives, total});
|
||
|
|
}
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
search(searchStr);
|
||
|
|
}, [offset, limit, domain, searchStr]);
|
||
|
|
|
||
|
|
return {
|
||
|
|
archivesData,
|
||
|
|
totalPages,
|
||
|
|
setTotalPages,
|
||
|
|
offset,
|
||
|
|
setOffset,
|
||
|
|
setPage,
|
||
|
|
limit,
|
||
|
|
setLimit,
|
||
|
|
domain,
|
||
|
|
setDomain,
|
||
|
|
searchStr,
|
||
|
|
setSearchStr,
|
||
|
|
activePage,
|
||
|
|
setActivePage,
|
||
|
|
search,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
export const useVersion = () => {
|
||
|
|
const [version, setVersion] = useState('');
|
||
|
|
|
||
|
|
const fetchVersion = async () => {
|
||
|
|
let version = await getVersion();
|
||
|
|
setVersion(version);
|
||
|
|
}
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
fetchVersion();
|
||
|
|
})
|
||
|
|
|
||
|
|
return version;
|
||
|
|
}
|