2024-05-14 20:55:38 +00:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
import { useEffect, FC } from "react";
|
2024-05-15 22:33:43 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
2024-05-17 11:24:58 +00:00
|
|
|
import { useRouter, useSearchParams } from "next/navigation";
|
2024-05-14 20:55:38 +00:00
|
|
|
// components
|
2024-06-10 06:46:23 +00:00
|
|
|
import { IssuesLayoutSelection, NavbarTheme, UserAvatar } from "@/components/issues";
|
2024-05-14 20:55:38 +00:00
|
|
|
import { IssueFiltersDropdown } from "@/components/issues/filters";
|
2024-05-16 11:47:04 +00:00
|
|
|
// helpers
|
|
|
|
import { queryParamGenerator } from "@/helpers/query-param-generator";
|
2024-05-14 20:55:38 +00:00
|
|
|
// hooks
|
2024-06-10 06:46:23 +00:00
|
|
|
import { useIssueFilter, useIssueDetails } from "@/hooks/store";
|
2024-05-22 07:09:34 +00:00
|
|
|
import useIsInIframe from "@/hooks/use-is-in-iframe";
|
2024-06-10 06:46:23 +00:00
|
|
|
// store
|
|
|
|
import { PublishStore } from "@/store/publish/publish.store";
|
2024-05-14 20:55:38 +00:00
|
|
|
// types
|
2024-05-16 07:37:47 +00:00
|
|
|
import { TIssueLayout } from "@/types/issue";
|
2024-05-14 20:55:38 +00:00
|
|
|
|
|
|
|
export type NavbarControlsProps = {
|
2024-06-10 06:46:23 +00:00
|
|
|
publishSettings: PublishStore;
|
2024-05-14 20:55:38 +00:00
|
|
|
};
|
|
|
|
|
2024-05-15 22:33:43 +00:00
|
|
|
export const NavbarControls: FC<NavbarControlsProps> = observer((props) => {
|
2024-05-16 07:37:47 +00:00
|
|
|
// props
|
2024-06-10 06:46:23 +00:00
|
|
|
const { publishSettings } = props;
|
2024-05-14 20:55:38 +00:00
|
|
|
// router
|
|
|
|
const router = useRouter();
|
2024-05-16 07:37:47 +00:00
|
|
|
const searchParams = useSearchParams();
|
|
|
|
// query params
|
|
|
|
const board = searchParams.get("board") || undefined;
|
|
|
|
const labels = searchParams.get("labels") || undefined;
|
|
|
|
const state = searchParams.get("state") || undefined;
|
|
|
|
const priority = searchParams.get("priority") || undefined;
|
|
|
|
const peekId = searchParams.get("peekId") || undefined;
|
|
|
|
// hooks
|
2024-06-10 06:46:23 +00:00
|
|
|
const { getIssueFilters, isIssueFiltersUpdated, initIssueFilters } = useIssueFilter();
|
2024-05-16 07:37:47 +00:00
|
|
|
const { setPeekId } = useIssueDetails();
|
|
|
|
// derived values
|
2024-06-10 06:46:23 +00:00
|
|
|
const { anchor, view_props, workspace_detail } = publishSettings;
|
|
|
|
const issueFilters = anchor ? getIssueFilters(anchor) : undefined;
|
2024-05-16 07:37:47 +00:00
|
|
|
const activeLayout = issueFilters?.display_filters?.layout || undefined;
|
2024-05-14 20:55:38 +00:00
|
|
|
|
2024-05-22 07:09:34 +00:00
|
|
|
const isInIframe = useIsInIframe();
|
|
|
|
|
2024-05-14 20:55:38 +00:00
|
|
|
useEffect(() => {
|
2024-06-10 06:46:23 +00:00
|
|
|
if (anchor && workspace_detail) {
|
2024-05-14 20:55:38 +00:00
|
|
|
const viewsAcceptable: string[] = [];
|
2024-05-16 07:37:47 +00:00
|
|
|
let currentBoard: TIssueLayout | null = null;
|
2024-05-14 20:55:38 +00:00
|
|
|
|
2024-06-10 06:46:23 +00:00
|
|
|
if (view_props?.list) viewsAcceptable.push("list");
|
|
|
|
if (view_props?.kanban) viewsAcceptable.push("kanban");
|
|
|
|
if (view_props?.calendar) viewsAcceptable.push("calendar");
|
|
|
|
if (view_props?.gantt) viewsAcceptable.push("gantt");
|
|
|
|
if (view_props?.spreadsheet) viewsAcceptable.push("spreadsheet");
|
2024-05-14 20:55:38 +00:00
|
|
|
|
2024-05-16 07:37:47 +00:00
|
|
|
if (board) {
|
|
|
|
if (viewsAcceptable.includes(board.toString())) currentBoard = board.toString() as TIssueLayout;
|
|
|
|
else {
|
|
|
|
if (viewsAcceptable && viewsAcceptable.length > 0) currentBoard = viewsAcceptable[0] as TIssueLayout;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (viewsAcceptable && viewsAcceptable.length > 0) currentBoard = viewsAcceptable[0] as TIssueLayout;
|
|
|
|
}
|
2024-05-14 20:55:38 +00:00
|
|
|
|
|
|
|
if (currentBoard) {
|
2024-05-16 07:37:47 +00:00
|
|
|
if (activeLayout === undefined || activeLayout !== currentBoard) {
|
2024-05-16 11:47:04 +00:00
|
|
|
const { query, queryParam } = queryParamGenerator({ board: currentBoard, peekId, priority, state, labels });
|
|
|
|
const params: any = {
|
|
|
|
display_filters: { layout: (query?.board as string[])[0] },
|
|
|
|
filters: {
|
|
|
|
priority: query?.priority ?? undefined,
|
|
|
|
state: query?.state ?? undefined,
|
|
|
|
labels: query?.labels ?? undefined,
|
|
|
|
},
|
|
|
|
};
|
2024-05-14 20:55:38 +00:00
|
|
|
|
2024-06-10 06:46:23 +00:00
|
|
|
if (!isIssueFiltersUpdated(anchor, params)) {
|
|
|
|
initIssueFilters(anchor, params);
|
|
|
|
router.push(`/issues/${anchor}?${queryParam}`);
|
2024-05-16 07:37:47 +00:00
|
|
|
}
|
2024-05-14 20:55:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [
|
2024-06-10 06:46:23 +00:00
|
|
|
anchor,
|
2024-05-16 07:37:47 +00:00
|
|
|
board,
|
2024-05-14 20:55:38 +00:00
|
|
|
labels,
|
2024-05-16 07:37:47 +00:00
|
|
|
state,
|
|
|
|
priority,
|
2024-05-14 20:55:38 +00:00
|
|
|
peekId,
|
|
|
|
activeLayout,
|
2024-05-16 07:37:47 +00:00
|
|
|
router,
|
|
|
|
initIssueFilters,
|
|
|
|
setPeekId,
|
|
|
|
isIssueFiltersUpdated,
|
2024-06-10 06:46:23 +00:00
|
|
|
view_props,
|
|
|
|
workspace_detail,
|
2024-05-14 20:55:38 +00:00
|
|
|
]);
|
2024-05-16 07:37:47 +00:00
|
|
|
|
2024-06-10 06:46:23 +00:00
|
|
|
if (!anchor) return null;
|
|
|
|
|
2024-05-14 20:55:38 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{/* issue views */}
|
|
|
|
<div className="relative flex flex-shrink-0 items-center gap-1 transition-all delay-150 ease-in-out">
|
2024-06-10 06:46:23 +00:00
|
|
|
<IssuesLayoutSelection anchor={anchor} />
|
2024-05-14 20:55:38 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
{/* issue filters */}
|
|
|
|
<div className="relative flex flex-shrink-0 items-center gap-1 transition-all delay-150 ease-in-out">
|
2024-06-10 06:46:23 +00:00
|
|
|
<IssueFiltersDropdown anchor={anchor} />
|
2024-05-14 20:55:38 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
{/* theming */}
|
|
|
|
<div className="relative flex-shrink-0">
|
|
|
|
<NavbarTheme />
|
|
|
|
</div>
|
|
|
|
|
2024-05-22 07:09:34 +00:00
|
|
|
{!isInIframe && <UserAvatar />}
|
2024-05-14 20:55:38 +00:00
|
|
|
</>
|
|
|
|
);
|
2024-05-15 22:33:43 +00:00
|
|
|
});
|