160 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-11-16 23:50:02 +07:00
import { Button, GetProps, useMedia } from "tamagui";
import React, { useMemo, useState } from "react";
2024-11-08 18:53:30 +00:00
import Drawer from "expo-router/drawer";
2024-11-12 19:15:13 +07:00
import HostList from "./components/host-list";
2024-11-09 10:33:07 +00:00
import HostForm, { hostFormModal } from "./components/form";
import Icons from "@/components/ui/icons";
import { initialValues } from "./schema/form";
2024-11-09 18:57:36 +00:00
import KeyForm from "../keychains/components/form";
2024-11-14 17:58:19 +07:00
import { useUser } from "@/hooks/useUser";
import { useTeamId } from "@/stores/auth";
2024-11-16 23:50:02 +07:00
import { useMoveHost } from "./hooks/query";
import { useQueryParams } from "@/hooks/useQueryParams";
import { BackButton } from "@/components/ui/button";
type Params = {
parentId?: string | null;
};
2024-11-08 18:53:30 +00:00
export default function HostsPage() {
2024-11-14 17:58:19 +07:00
const teamId = useTeamId();
const user = useUser();
2024-11-16 23:50:02 +07:00
const [selected, setSelected] = useState<string[]>([]);
const queryParams = useQueryParams<Params>();
const parentId = queryParams.params?.parentId;
const media = useMedia();
const setParentId = (id: string | null) => {
queryParams.push({ parentId: id || "" });
};
const onGoBack = () => {
if (!queryParams.goBack()) {
queryParams.replace({ parentId: "" });
}
};
const actions = useMemo(() => {
if (selected?.length > 0) {
return (
<HostsActions
selected={selected}
parentId={parentId}
onClear={() => setSelected([])}
/>
);
}
if (!teamId || user?.teamCanWrite(teamId)) {
return (
<AddButton
onPress={() => hostFormModal.onOpen({ ...initialValues, parentId })}
/>
);
}
return null;
}, [teamId, user, parentId]);
2024-11-14 17:58:19 +07:00
2024-11-08 18:53:30 +00:00
return (
<>
<Drawer.Screen
options={{
2024-11-16 23:50:02 +07:00
headerLeft: parentId
? () => <BackButton onPress={onGoBack} />
: media.gtXs
? () => null
: undefined,
headerTitle:
selected.length > 0 ? `Selected ${selected.length} hosts` : "Hosts",
headerRight: () => actions,
2024-11-08 18:53:30 +00:00
}}
/>
2024-11-16 23:50:02 +07:00
<HostList
parentId={parentId}
onParentIdChange={setParentId}
selected={selected}
onSelectedChange={setSelected}
/>
2024-11-09 10:33:07 +00:00
<HostForm />
2024-11-09 18:57:36 +00:00
<KeyForm />
2024-11-08 18:53:30 +00:00
</>
);
}
2024-11-14 17:58:19 +07:00
2024-11-16 23:50:02 +07:00
const AddButton = (props: GetProps<typeof Button>) => (
2024-11-14 17:58:19 +07:00
<Button
bg="$colorTransparent"
icon={<Icons name="plus" size={24} />}
2024-11-16 23:50:02 +07:00
$gtSm={{ mr: "$2" }}
{...props}
2024-11-14 17:58:19 +07:00
>
New
</Button>
);
2024-11-16 23:50:02 +07:00
type HostsActionsProps = {
selected: string[];
parentId?: string | null;
onClear: () => void;
};
const actionMode = {
CUT: 1,
};
const HostsActions = ({
selected,
parentId = null,
onClear,
}: HostsActionsProps) => {
const [curMode, setCurMode] = useState(0);
const move = useMoveHost();
const onReset = () => {
setCurMode(0);
onClear();
};
const onMoveAction = () => {
move.mutate({ parentId, hostId: selected }, { onSuccess: onReset });
};
return (
<>
{curMode === actionMode.CUT ? (
<Button
key="paste"
circular
icon={<Icons name="content-paste" size={20} />}
bg="$colorTransparent"
onPress={onMoveAction}
/>
) : (
<Button
key="cut"
circular
icon={<Icons name="content-cut" size={20} />}
bg="$colorTransparent"
onPress={() => setCurMode(actionMode.CUT)}
/>
)}
{/* <Button
circular
icon={<Icons name="trash-can" size={24} />}
bg="$colorTransparent"
/> */}
<Button
key="close"
circular
icon={<Icons name="close" size={24} />}
bg="$colorTransparent"
onPress={onReset}
mr="$2"
/>
</>
);
};