feat: init
Some checks failed
CodeQL / Analyze (javascript) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled

This commit is contained in:
chuan
2025-11-11 01:56:44 +08:00
commit bba4bb40c8
4638 changed files with 447437 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
import { useRef, useState } from "react";
import { observer } from "mobx-react";
import { useParams } from "next/navigation";
import useSWR from "swr";
import { STICKIES_PER_PAGE } from "@plane/constants";
import { ContentWrapper, Loader } from "@plane/ui";
import { cn } from "@plane/utils";
import { useIntersectionObserver } from "@/hooks/use-intersection-observer";
import { useSticky } from "@/hooks/use-stickies";
import { StickiesLayout } from "./stickies-list";
export const StickiesInfinite = observer(() => {
const { workspaceSlug } = useParams();
// hooks
const { fetchWorkspaceStickies, fetchNextWorkspaceStickies, getWorkspaceStickyIds, loader, paginationInfo } =
useSticky();
//state
const [elementRef, setElementRef] = useState<HTMLDivElement | null>(null);
// ref
const containerRef = useRef<HTMLDivElement>(null);
useSWR(
workspaceSlug ? `WORKSPACE_STICKIES_${workspaceSlug}` : null,
workspaceSlug ? () => fetchWorkspaceStickies(workspaceSlug.toString()) : null,
{ revalidateIfStale: false, revalidateOnFocus: false }
);
const handleLoadMore = () => {
if (loader === "pagination") return;
fetchNextWorkspaceStickies(workspaceSlug?.toString());
};
const hasNextPage = paginationInfo?.next_page_results && paginationInfo?.next_cursor !== undefined;
const shouldObserve = hasNextPage && loader !== "pagination";
const workspaceStickies = getWorkspaceStickyIds(workspaceSlug?.toString());
useIntersectionObserver(containerRef, shouldObserve ? elementRef : null, handleLoadMore);
return (
<ContentWrapper ref={containerRef} className="space-y-4">
<StickiesLayout
workspaceSlug={workspaceSlug.toString()}
intersectionElement={
hasNextPage &&
workspaceStickies?.length >= STICKIES_PER_PAGE && (
<div
className={cn("flex min-h-[300px] box-border p-2 w-full")}
ref={setElementRef}
id="intersection-element"
>
<div className="flex w-full rounded min-h-[300px]">
<Loader className="w-full h-full">
<Loader.Item height="100%" width="100%" />
</Loader>
</div>
</div>
)
}
/>
</ContentWrapper>
);
});

View File

@@ -0,0 +1,201 @@
import { useEffect, useRef, useState } from "react";
import type {
DropTargetRecord,
DragLocationHistory,
} from "@atlaskit/pragmatic-drag-and-drop/dist/types/internal-types";
import type { ElementDragPayload } from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
import { observer } from "mobx-react";
import { usePathname } from "next/navigation";
import Masonry from "react-masonry-component";
import { Plus } from "lucide-react";
// plane imports
import { EUserPermissionsLevel } from "@plane/constants";
import { useTranslation } from "@plane/i18n";
import { EUserWorkspaceRoles } from "@plane/types";
// components
import { DetailedEmptyState } from "@/components/empty-state/detailed-empty-state-root";
import { SimpleEmptyState } from "@/components/empty-state/simple-empty-state-root";
import { StickiesEmptyState } from "@/components/home/widgets/empty-states/stickies";
// hooks
import { useUserPermissions } from "@/hooks/store/user";
import { useResolvedAssetPath } from "@/hooks/use-resolved-asset-path";
import { useSticky } from "@/hooks/use-stickies";
// local imports
import { useStickyOperations } from "../sticky/use-operations";
import { StickiesLoader } from "./stickies-loader";
import { StickyDNDWrapper } from "./sticky-dnd-wrapper";
import { getInstructionFromPayload } from "./sticky.helpers";
type TStickiesLayout = {
workspaceSlug: string;
intersectionElement?: React.ReactNode | null;
};
type TProps = TStickiesLayout & {
columnCount: number;
};
export const StickiesList = observer((props: TProps) => {
const { workspaceSlug, intersectionElement, columnCount } = props;
// navigation
const pathname = usePathname();
// plane hooks
const { t } = useTranslation();
// store hooks
const { getWorkspaceStickyIds, toggleShowNewSticky, searchQuery, loader } = useSticky();
const { allowPermissions } = useUserPermissions();
// sticky operations
const { stickyOperations } = useStickyOperations({ workspaceSlug: workspaceSlug?.toString() });
// derived values
const workspaceStickyIds = getWorkspaceStickyIds(workspaceSlug?.toString());
const itemWidth = `${100 / columnCount}%`;
const totalRows = Math.ceil(workspaceStickyIds.length / columnCount);
const isStickiesPage = pathname?.includes("stickies");
const hasGuestLevelPermissions = allowPermissions(
[EUserWorkspaceRoles.ADMIN, EUserWorkspaceRoles.MEMBER, EUserWorkspaceRoles.GUEST],
EUserPermissionsLevel.WORKSPACE
);
const stickiesResolvedPath = useResolvedAssetPath({ basePath: "/empty-state/stickies/stickies" });
const stickiesSearchResolvedPath = useResolvedAssetPath({
basePath: "/empty-state/stickies/stickies-search",
});
const masonryRef = useRef<any>(null);
const handleLayout = () => {
if (masonryRef.current) {
// Force reflow
masonryRef.current.performLayout();
}
};
// Function to determine if an item is in first or last row
const getRowPositions = (index: number) => {
const currentRow = Math.floor(index / columnCount);
return {
isInFirstRow: currentRow === 0,
isInLastRow: currentRow === totalRows - 1 || index >= workspaceStickyIds.length - columnCount,
};
};
const handleDrop = (self: DropTargetRecord, source: ElementDragPayload, location: DragLocationHistory) => {
const dropTargets = location?.current?.dropTargets ?? [];
if (!dropTargets || dropTargets.length <= 0) return;
const dropTarget = dropTargets[0];
if (!dropTarget?.data?.id || !source.data?.id) return;
const instruction = getInstructionFromPayload(dropTarget, source, location);
const droppedId = dropTarget.data.id;
const sourceId = source.data.id;
try {
if (!instruction || !droppedId || !sourceId) return;
stickyOperations.updatePosition(workspaceSlug, sourceId as string, droppedId as string, instruction);
} catch (error) {
console.error("Error reordering sticky:", error);
}
};
if (loader === "init-loader") {
return <StickiesLoader />;
}
if (loader === "loaded" && workspaceStickyIds.length === 0) {
return (
<div className="size-full grid place-items-center">
{isStickiesPage ? (
<>
{searchQuery ? (
<SimpleEmptyState
title={t("stickies.empty_state.search.title")}
description={t("stickies.empty_state.search.description")}
assetPath={stickiesSearchResolvedPath}
/>
) : (
<DetailedEmptyState
title={t("stickies.empty_state.general.title")}
description={t("stickies.empty_state.general.description")}
assetPath={stickiesResolvedPath}
primaryButton={{
prependIcon: <Plus className="size-4" />,
text: t("stickies.empty_state.general.primary_button.text"),
onClick: () => {
toggleShowNewSticky(true);
stickyOperations.create();
},
disabled: !hasGuestLevelPermissions,
}}
/>
)}
</>
) : (
<StickiesEmptyState />
)}
</div>
);
}
return (
<div className="transition-opacity duration-300 ease-in-out">
{/* @ts-expect-error type mismatch here */}
<Masonry elementType="div" ref={masonryRef}>
{workspaceStickyIds.map((stickyId, index) => {
const { isInFirstRow, isInLastRow } = getRowPositions(index);
return (
<StickyDNDWrapper
key={stickyId}
stickyId={stickyId}
workspaceSlug={workspaceSlug.toString()}
itemWidth={itemWidth}
handleDrop={handleDrop}
isLastChild={index === workspaceStickyIds.length - 1}
isInFirstRow={isInFirstRow}
isInLastRow={isInLastRow}
handleLayout={handleLayout}
/>
);
})}
{intersectionElement && <div style={{ width: itemWidth }}>{intersectionElement}</div>}
</Masonry>
</div>
);
});
export const StickiesLayout = (props: TStickiesLayout) => {
// states
const [containerWidth, setContainerWidth] = useState<number | null>(null);
// refs
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!ref?.current) return;
setContainerWidth(ref?.current.offsetWidth);
const resizeObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
setContainerWidth(entry.contentRect.width);
}
});
resizeObserver.observe(ref?.current);
return () => resizeObserver.disconnect();
}, []);
const getColumnCount = (width: number | null): number => {
if (width === null) return 4;
if (width < 640) return 2; // sm
if (width < 850) return 3; // md
if (width < 1024) return 4; // lg
if (width < 1280) return 5; // xl
return 6; // 2xl and above
};
const columnCount = getColumnCount(containerWidth);
return (
<div ref={ref} className="size-full">
<StickiesList {...props} columnCount={columnCount} />
</div>
);
};

View File

@@ -0,0 +1,45 @@
// plane ui
import { Loader } from "@plane/ui";
export const StickiesLoader = () => (
<div className="overflow-scroll pb-2 grid grid-cols-4 gap-4">
{Array.from({ length: 4 }).map((_, index) => (
<Loader key={index} className="space-y-5 border border-custom-border-200 p-3 rounded">
<div className="space-y-2">
<Loader.Item height="20px" />
<Loader.Item height="15px" width="75%" />
</div>
<div className="space-y-2">
<div className="flex items-center gap-2">
<Loader.Item height="15px" width="15px" className="flex-shrink-0" />
<Loader.Item height="15px" width="100%" />
</div>
<div className="flex items-center gap-2">
<Loader.Item height="15px" width="15px" className="flex-shrink-0" />
<Loader.Item height="15px" width="75%" />
</div>
<div className="flex items-center gap-2">
<Loader.Item height="15px" width="15px" className="flex-shrink-0" />
<Loader.Item height="15px" width="90%" />
</div>
<div className="flex items-center gap-2">
<Loader.Item height="15px" width="15px" className="flex-shrink-0" />
<Loader.Item height="15px" width="60%" />
</div>
<div className="flex items-center gap-2">
<Loader.Item height="15px" width="15px" className="flex-shrink-0" />
<Loader.Item height="15px" width="50%" />
</div>
</div>
<div className="flex items-center justify-between gap-2">
<div className="flex items-center gap-2">
<Loader.Item height="25px" width="25px" />
<Loader.Item height="25px" width="25px" />
<Loader.Item height="25px" width="25px" />
</div>
<Loader.Item height="25px" width="25px" className="flex-shrink-0" />
</div>
</Loader>
))}
</div>
);

View File

@@ -0,0 +1,52 @@
import { observer } from "mobx-react";
import Link from "next/link";
import { useParams } from "next/navigation";
import useSWR from "swr";
// plane utils
import { useTranslation } from "@plane/i18n";
import { cn } from "@plane/utils";
// hooks
import { useSticky } from "@/hooks/use-stickies";
// components
import { ContentOverflowWrapper } from "../../core/content-overflow-HOC";
import { StickiesLayout } from "./stickies-list";
type StickiesTruncatedProps = {
handleClose?: () => void;
};
export const StickiesTruncated = observer((props: StickiesTruncatedProps) => {
const { handleClose = () => {} } = props;
// navigation
const { workspaceSlug } = useParams();
// store hooks
const { fetchWorkspaceStickies } = useSticky();
const { t } = useTranslation();
useSWR(
workspaceSlug ? `WORKSPACE_STICKIES_${workspaceSlug}` : null,
workspaceSlug ? () => fetchWorkspaceStickies(workspaceSlug.toString()) : null,
{ revalidateIfStale: false, revalidateOnFocus: false }
);
return (
<ContentOverflowWrapper
maxHeight={620}
containerClassName="pb-2 box-border"
fallback={null}
customButton={
<Link
href={`/${workspaceSlug}/stickies`}
className={cn(
"gap-1 w-full text-custom-primary-100 text-sm font-medium transition-opacity duration-300 bg-custom-background-90/20"
)}
onClick={handleClose}
>
{t("show_all")}
</Link>
}
>
<StickiesLayout workspaceSlug={workspaceSlug?.toString()} />
</ContentOverflowWrapper>
);
});

View File

@@ -0,0 +1,137 @@
import { useEffect, useRef, useState } from "react";
import { combine } from "@atlaskit/pragmatic-drag-and-drop/combine";
import type {
DropTargetRecord,
DragLocationHistory,
} from "@atlaskit/pragmatic-drag-and-drop/dist/types/internal-types";
import type { ElementDragPayload } from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
import { draggable, dropTargetForElements } from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
import { pointerOutsideOfPreview } from "@atlaskit/pragmatic-drag-and-drop/element/pointer-outside-of-preview";
import { setCustomNativeDragPreview } from "@atlaskit/pragmatic-drag-and-drop/element/set-custom-native-drag-preview";
import { attachInstruction } from "@atlaskit/pragmatic-drag-and-drop-hitbox/tree-item";
import { observer } from "mobx-react";
import { usePathname } from "next/navigation";
import { createRoot } from "react-dom/client";
// plane types
import type { InstructionType } from "@plane/types";
// plane ui
import { DropIndicator } from "@plane/ui";
// components
import { StickyNote } from "../sticky";
// helpers
import { getInstructionFromPayload } from "./sticky.helpers";
type Props = {
stickyId: string;
workspaceSlug: string;
itemWidth: string;
isLastChild: boolean;
isInFirstRow: boolean;
isInLastRow: boolean;
handleDrop: (self: DropTargetRecord, source: ElementDragPayload, location: DragLocationHistory) => void;
handleLayout: () => void;
};
export const StickyDNDWrapper = observer((props: Props) => {
const { stickyId, workspaceSlug, itemWidth, isLastChild, isInFirstRow, isInLastRow, handleDrop, handleLayout } =
props;
// states
const [isDragging, setIsDragging] = useState(false);
const [instruction, setInstruction] = useState<InstructionType | undefined>(undefined);
// refs
const elementRef = useRef<HTMLDivElement>(null);
// navigation
const pathname = usePathname();
useEffect(() => {
const element = elementRef.current;
if (!element) return;
const initialData = { id: stickyId, type: "sticky" };
if (pathname.includes("stickies"))
return combine(
draggable({
element,
dragHandle: element,
getInitialData: () => initialData,
onDragStart: () => {
setIsDragging(true);
},
onDrop: () => {
setIsDragging(false);
},
onGenerateDragPreview: ({ nativeSetDragImage }) => {
setCustomNativeDragPreview({
getOffset: pointerOutsideOfPreview({ x: "-200px", y: "0px" }),
render: ({ container }) => {
const root = createRoot(container);
root.render(
<div className="scale-50">
<div className="-m-2 max-h-[150px]">
<StickyNote
className={"w-[290px]"}
workspaceSlug={workspaceSlug.toString()}
stickyId={stickyId}
showToolbar={false}
/>
</div>
</div>
);
return () => root.unmount();
},
nativeSetDragImage,
});
},
}),
dropTargetForElements({
element,
canDrop: ({ source }) => source.data?.type === "sticky",
getData: ({ input, element }) => {
const blockedStates: InstructionType[] = ["make-child"];
if (!isLastChild) {
blockedStates.push("reorder-below");
}
return attachInstruction(initialData, {
input,
element,
currentLevel: 1,
indentPerLevel: 0,
mode: isLastChild ? "last-in-group" : "standard",
block: blockedStates,
});
},
onDrag: ({ self, source, location }) => {
const instruction = getInstructionFromPayload(self, source, location);
setInstruction(instruction);
},
onDragLeave: () => {
setInstruction(undefined);
},
onDrop: ({ self, source, location }) => {
setInstruction(undefined);
handleDrop(self, source, location);
},
})
);
}, [handleDrop, isDragging, isLastChild, pathname, stickyId, workspaceSlug]);
return (
<div
className="flex flex-col box-border p-[8px]"
style={{
width: itemWidth,
}}
>
{/* {!isInFirstRow && <DropIndicator isVisible={instruction === "reorder-above"} />} */}
<StickyNote
key={stickyId || "new"}
workspaceSlug={workspaceSlug}
stickyId={stickyId}
handleLayout={handleLayout}
/>
{/* {!isInLastRow && <DropIndicator isVisible={instruction === "reorder-below"} />} */}
</div>
);
});

View File

@@ -0,0 +1,45 @@
import { extractInstruction } from "@atlaskit/pragmatic-drag-and-drop-hitbox/tree-item";
import type { InstructionType, IPragmaticPayloadLocation, TDropTarget } from "@plane/types";
export type TargetData = {
id: string;
parentId: string | null;
isGroup: boolean;
isChild: boolean;
};
/**
* extracts the Payload and translates the instruction for the current dropTarget based on drag and drop payload
* @param dropTarget dropTarget for which the instruction is required
* @param source the dragging sticky data that is being dragged on the dropTarget
* @param location location includes the data of all the dropTargets the source is being dragged on
* @returns Instruction for dropTarget
*/
export const getInstructionFromPayload = (
dropTarget: TDropTarget,
source: TDropTarget,
location: IPragmaticPayloadLocation
): InstructionType | undefined => {
const dropTargetData = dropTarget?.data as TargetData;
const sourceData = source?.data as TargetData;
const allDropTargets = location?.current?.dropTargets;
// if all the dropTargets are greater than 1 meaning the source is being dragged on a group and its child at the same time
// and also if the dropTarget in question is also a group then, it should be a child of the current Droptarget
if (allDropTargets?.length > 1 && dropTargetData?.isGroup) return "make-child";
if (!dropTargetData || !sourceData) return undefined;
let instruction = extractInstruction(dropTargetData)?.type;
// If the instruction is blocked then set an instruction based on if dropTarget it is a child or not
if (instruction === "instruction-blocked") {
instruction = dropTargetData.isChild ? "reorder-above" : "make-child";
}
// if source that is being dragged is a group. A group cannon be a child of any other sticky,
// hence if current instruction is to be a child of dropTarget then reorder-above instead
if (instruction === "make-child" && sourceData.isGroup) instruction = "reorder-above";
return instruction;
};