From 069cd3a402ee08bdb6624a781f7e545160abfaa1 Mon Sep 17 00:00:00 2001 From: Roland Date: Mon, 31 Jan 2022 09:08:02 -0700 Subject: [PATCH] Moving custom hooks into one file. --- app/src/App.js | 34 +------- app/src/components/Archive.js | 3 +- app/src/hooks/customHooks.js | 132 ++++++++++++++++++++++++++++++++ app/src/hooks/useArchives.js | 52 ------------- app/src/hooks/useDomains.js | 21 ----- app/src/hooks/useSearchParam.js | 21 ----- app/src/hooks/useVersion.js | 17 ---- 7 files changed, 135 insertions(+), 145 deletions(-) create mode 100644 app/src/hooks/customHooks.js delete mode 100644 app/src/hooks/useArchives.js delete mode 100644 app/src/hooks/useDomains.js delete mode 100644 app/src/hooks/useSearchParam.js delete mode 100644 app/src/hooks/useVersion.js diff --git a/app/src/App.js b/app/src/App.js index bf7fbbef..57df089c 100644 --- a/app/src/App.js +++ b/app/src/App.js @@ -1,4 +1,4 @@ -import React, {useEffect, useState} from 'react'; +import React from 'react'; import './App.css'; import {NavBar} from "./components/Nav"; import {Route, Switch, useHistory} from "react-router-dom"; @@ -12,39 +12,9 @@ import {AppsRoute} from "./components/Apps"; import {InventoryRoute} from "./components/Inventory"; import {ArchiveRoute, ArchivesList} from "./components/Archive"; import {Saver} from "./components/Upload"; -import {useSearchParam} from "./hooks/useSearchParam"; -import {searchArchives, searchVideos} from "./api"; import {MoreButton, SearchInput} from "./components/Common"; import {FilesRoute} from "./components/Files"; -import {useVersion} from "./hooks/useVersion"; - -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} -} +import {useSearch, useVersion} from "./hooks/customHooks"; function Welcome() { const {searchStr, setSearchStr, archives, videos} = useSearch(); diff --git a/app/src/components/Archive.js b/app/src/components/Archive.js index e7b311c4..62859f90 100644 --- a/app/src/components/Archive.js +++ b/app/src/components/Archive.js @@ -14,9 +14,8 @@ import Button from "semantic-ui-react/dist/commonjs/elements/Button"; import {Link, Route} from "react-router-dom"; import {ArchivePlaceholder} from "./Placeholder"; import Table from "semantic-ui-react/dist/commonjs/collections/Table"; -import {useArchives} from "../hooks/useArchives"; -import {useDomains} from "../hooks/useDomains"; import Message from "semantic-ui-react/dist/commonjs/collections/Message"; +import {useArchives, useDomains} from "../hooks/customHooks"; function FailedArchiveCard({archive, syncArchive, deleteArchive}) { diff --git a/app/src/hooks/customHooks.js b/app/src/hooks/customHooks.js new file mode 100644 index 00000000..0d92950f --- /dev/null +++ b/app/src/hooks/customHooks.js @@ -0,0 +1,132 @@ +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; +} diff --git a/app/src/hooks/useArchives.js b/app/src/hooks/useArchives.js deleted file mode 100644 index a1b00ad5..00000000 --- a/app/src/hooks/useArchives.js +++ /dev/null @@ -1,52 +0,0 @@ -import {useEffect, useState} from "react"; -import {searchArchives} from "../api"; -import {useSearchParam} from "./useSearchParam"; - - -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, - }; -} diff --git a/app/src/hooks/useDomains.js b/app/src/hooks/useDomains.js deleted file mode 100644 index 19766522..00000000 --- a/app/src/hooks/useDomains.js +++ /dev/null @@ -1,21 +0,0 @@ -import {useEffect, useState} from "react"; -import {fetchDomains} from "../api"; - -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]; -} diff --git a/app/src/hooks/useSearchParam.js b/app/src/hooks/useSearchParam.js deleted file mode 100644 index 7d20264d..00000000 --- a/app/src/hooks/useSearchParam.js +++ /dev/null @@ -1,21 +0,0 @@ -import {useEffect, useState} from "react"; -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]; -} diff --git a/app/src/hooks/useVersion.js b/app/src/hooks/useVersion.js deleted file mode 100644 index c6ac9dfc..00000000 --- a/app/src/hooks/useVersion.js +++ /dev/null @@ -1,17 +0,0 @@ -import {useEffect, useState} from "react"; -import {getVersion} from "../api"; - -export const useVersion = () => { - const [version, setVersion] = useState(''); - - const fetchVersion = async () => { - let version = await getVersion(); - setVersion(version); - } - - useEffect(() => { - fetchVersion(); - }) - - return version; -}