"use client";
import { Anchor, Box, Card, Divider, Flex, Group, Stack, Text, Title, UnstyledButton } from "@mantine/core";
import type { RouterOutputs } from "@homarr/api";
import { clientApi } from "@homarr/api/client";
import { parseAppHrefWithVariablesClient } from "@homarr/common/client";
import { useRegisterSpotlightContextResults } from "@homarr/spotlight";
import type { WidgetComponentProps } from "../definition";
import classes from "./bookmark.module.css";
export default function BookmarksWidget({ options, width, height, itemId }: WidgetComponentProps<"bookmarks">) {
const [data] = clientApi.app.byIds.useSuspenseQuery(options.items, {
select(data) {
return data.sort((appA, appB) => options.items.indexOf(appA.id) - options.items.indexOf(appB.id));
},
});
useRegisterSpotlightContextResults(
`bookmark-${itemId}`,
data.map((app) => ({
id: app.id,
name: app.name,
icon: app.iconUrl,
interaction() {
return {
type: "link",
href: parseAppHrefWithVariablesClient(app.href ?? ""),
newTab: false,
};
},
})),
[data],
);
return (
{options.title}
{options.layout === "grid" && }
{options.layout !== "grid" && }
);
}
interface FlexLayoutProps {
data: RouterOutputs["app"]["byIds"];
direction: "row" | "column";
}
const FlexLayout = ({ data, direction }: FlexLayoutProps) => {
return (
{data.map((app, index) => (
{direction === "row" ? : }
))}
);
};
interface GridLayoutProps {
data: RouterOutputs["app"]["byIds"];
width: number;
height: number;
}
const GridLayout = ({ data, width, height }: GridLayoutProps) => {
// Calculates the perfect number of columns for the grid layout based on the width and height in pixels and the number of items
const columns = Math.ceil(Math.sqrt(data.length * (width / height)));
return (
{data.map((app) => (
))}
);
};
const VerticalItem = ({ app }: { app: RouterOutputs["app"]["byIds"][number] }) => {
return (
{app.name}
{app.href ? new URL(app.href).hostname : undefined}
);
};
const HorizontalItem = ({ app }: { app: RouterOutputs["app"]["byIds"][number] }) => {
return (
{app.name}
{app.href ? new URL(app.href).hostname : undefined}
);
};