Some checks failed
CodeQL / Analyze (javascript) (push) Failing after 16s
CodeQL / Analyze (python) (push) Failing after 3s
i18n sync check / check:sync (push) Failing after 3s
React Doctor / react-doctor (push) Failing after 16s
Branch Build CE / Build-Push Admin Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Admin Manifest (push) Has been cancelled
Branch Build CE / Build-Push Web Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Web Manifest (push) Has been cancelled
Branch Build CE / Build-Push Space Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Proxy Manifest (push) Has been cancelled
Branch Build CE / Merge Space Manifest (push) Has been cancelled
Branch Build CE / Prepare AIO Assets (push) Has been cancelled
Branch Build CE / Build Setup (push) Has been cancelled
Branch Build CE / Build-Push Live Collaboration Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Live Collaboration Manifest (push) Has been cancelled
Branch Build CE / Build-Push API Server Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge API Server Manifest (push) Has been cancelled
Branch Build CE / Build-Push Proxy Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Build-Push AIO Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge AIO Manifest (push) Has been cancelled
Branch Build CE / Upload Build Assets (push) Has been cancelled
Branch Build CE / Build Release (push) Has been cancelled
133 lines
4.3 KiB
TypeScript
133 lines
4.3 KiB
TypeScript
/**
|
|
* Copyright (c) 2023-present Plane Software, Inc. and contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
* See the LICENSE file for details.
|
|
*/
|
|
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
import { observer } from "mobx-react";
|
|
import { CloseOutline, FilterOutline, SearchOutline } from "@makeplane/propel/icons";
|
|
// plane imports
|
|
import { useOutsideClickDetector } from "@plane/hooks";
|
|
import { IconButton } from "@plane/propel/icon-button";
|
|
import { useTranslation } from "@plane/i18n";
|
|
import type { TCycleFilters } from "@plane/types";
|
|
import { cn, calculateTotalFilters } from "@plane/utils";
|
|
// components
|
|
import { FiltersDropdown } from "@/components/issues/issue-layouts/filters";
|
|
// hooks
|
|
import { useCycleFilter } from "@/hooks/store/use-cycle-filter";
|
|
// local imports
|
|
import { CycleFiltersSelection } from "./dropdowns";
|
|
|
|
type Props = {
|
|
projectId: string;
|
|
};
|
|
|
|
export const CyclesViewHeader = observer(function CyclesViewHeader(props: Props) {
|
|
const { projectId } = props;
|
|
// refs
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
// hooks
|
|
const { currentProjectFilters, searchQuery, updateFilters, updateSearchQuery } = useCycleFilter();
|
|
const { t } = useTranslation();
|
|
// states
|
|
const [isSearchOpen, setIsSearchOpen] = useState(searchQuery !== "" ? true : false);
|
|
// outside click detector hook
|
|
useOutsideClickDetector(inputRef, () => {
|
|
if (isSearchOpen && searchQuery.trim() === "") setIsSearchOpen(false);
|
|
});
|
|
|
|
const handleFilters = useCallback(
|
|
(key: keyof TCycleFilters, value: string | string[]) => {
|
|
if (!projectId) return;
|
|
const newValues = currentProjectFilters?.[key] ?? [];
|
|
|
|
if (Array.isArray(value))
|
|
value.forEach((val) => {
|
|
if (!newValues.includes(val)) newValues.push(val);
|
|
else newValues.splice(newValues.indexOf(val), 1);
|
|
});
|
|
else {
|
|
if (currentProjectFilters?.[key]?.includes(value)) newValues.splice(newValues.indexOf(value), 1);
|
|
else newValues.push(value);
|
|
}
|
|
|
|
updateFilters(projectId, { [key]: newValues });
|
|
},
|
|
[currentProjectFilters, projectId, updateFilters]
|
|
);
|
|
|
|
const handleInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
if (e.key === "Escape") {
|
|
if (searchQuery && searchQuery.trim() !== "") updateSearchQuery("");
|
|
else {
|
|
setIsSearchOpen(false);
|
|
inputRef.current?.blur();
|
|
}
|
|
}
|
|
};
|
|
|
|
const isFiltersApplied = calculateTotalFilters(currentProjectFilters ?? {}) !== 0;
|
|
|
|
useEffect(() => {
|
|
if (searchQuery.trim() !== "") setIsSearchOpen(true);
|
|
}, [searchQuery]);
|
|
|
|
return (
|
|
<div className="flex items-center gap-2">
|
|
{!isSearchOpen ? (
|
|
<IconButton
|
|
variant="ghost"
|
|
size="lg"
|
|
onClick={() => {
|
|
setIsSearchOpen(true);
|
|
inputRef.current?.focus();
|
|
}}
|
|
icon={SearchOutline}
|
|
/>
|
|
) : (
|
|
<div
|
|
className={cn(
|
|
"ml-auto flex w-0 items-center justify-start gap-1 overflow-hidden rounded-md border border-transparent bg-surface-1 text-placeholder opacity-0 transition-[width] ease-linear",
|
|
{
|
|
"w-64 border-subtle px-2.5 py-1.5 opacity-100": isSearchOpen,
|
|
}
|
|
)}
|
|
>
|
|
<SearchOutline className="h-3.5 w-3.5" />
|
|
<input
|
|
ref={inputRef}
|
|
className="w-full max-w-[234px] border-none bg-transparent text-13 text-primary placeholder:text-placeholder focus:outline-none"
|
|
placeholder="Search"
|
|
value={searchQuery}
|
|
onChange={(e) => updateSearchQuery(e.target.value)}
|
|
onKeyDown={handleInputKeyDown}
|
|
/>
|
|
{isSearchOpen && (
|
|
<button
|
|
type="button"
|
|
className="grid place-items-center"
|
|
onClick={() => {
|
|
updateSearchQuery("");
|
|
setIsSearchOpen(false);
|
|
}}
|
|
>
|
|
<CloseOutline className="h-3 w-3" />
|
|
</button>
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
<FiltersDropdown
|
|
icon={<FilterOutline className="h-3 w-3" />}
|
|
title={t("common.filters")}
|
|
placement="bottom-end"
|
|
isFiltersApplied={isFiltersApplied}
|
|
>
|
|
<CycleFiltersSelection filters={currentProjectFilters ?? {}} handleFiltersUpdate={handleFilters} />
|
|
</FiltersDropdown>
|
|
</div>
|
|
);
|
|
});
|