From 7804d8318158b9d797072dda0f038a2679bb5e3f Mon Sep 17 00:00:00 2001 From: Aarnav Tale Date: Sun, 7 Jul 2024 14:20:39 -0400 Subject: [PATCH] feat(TALE-4): make acl tags editable from the menu --- app/routes/_data.machines._index/action.tsx | 17 ++ .../_data.machines._index/dialogs/tags.tsx | 157 ++++++++++++++++++ app/routes/_data.machines._index/menu.tsx | 10 +- 3 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 app/routes/_data.machines._index/dialogs/tags.tsx diff --git a/app/routes/_data.machines._index/action.tsx b/app/routes/_data.machines._index/action.tsx index 31f35c0..ae68781 100644 --- a/app/routes/_data.machines._index/action.tsx +++ b/app/routes/_data.machines._index/action.tsx @@ -80,6 +80,23 @@ export async function menuAction(request: ActionFunctionArgs['request']) { } } + case 'tags': { + const tags = data.get('tags')?.toString() + .split(',') ?? [] + + try { + await post(`v1/node/${id}/tags`, session.get('hsApiKey')!, { + tags, + }) + + return json({ message: 'Tags updated' }) + } catch { + return json({ message: 'Failed to update tags' }, { + status: 500, + }) + } + } + default: { return json({ message: 'Invalid method' }, { status: 400, diff --git a/app/routes/_data.machines._index/dialogs/tags.tsx b/app/routes/_data.machines._index/dialogs/tags.tsx new file mode 100644 index 0000000..c94f435 --- /dev/null +++ b/app/routes/_data.machines._index/dialogs/tags.tsx @@ -0,0 +1,157 @@ +import { PlusIcon, XIcon } from '@primer/octicons-react' +import { Form, useSubmit } from '@remix-run/react' +import { type Dispatch, type SetStateAction, useState } from 'react' +import { Button, Input } from 'react-aria-components' + +import Dialog from '~/components/Dialog' +import Link from '~/components/Link' +import { type Machine } from '~/types' +import { cn } from '~/utils/cn' + +interface TagsProps { + readonly machine: Machine + readonly state: [boolean, Dispatch>] +} + +export default function Tags({ machine, state }: TagsProps) { + const [tags, setTags] = useState(machine.forcedTags) + const [tag, setTag] = useState('') + const submit = useSubmit() + + return ( + + + {close => ( + <> + + Edit ACL tags for + {' '} + {machine.givenName} + + + ACL tags can be used to reference machines in your ACL policies. + See the + {' '} + + + Tailscale documentation + + {' '} + for more information. + +
{ + submit(e.currentTarget) + }} + > + + + +
+
+ {tags.length === 0 + ? ( +
+

+ No tags are set on this machine. +

+
+ ) + : tags.map(item => ( +
+ {item} + +
+ ))} +
+
0 && !tag.startsWith('tag:') + && 'outline outline-red-500', + )} + > + { + setTag(e.currentTarget.value) + }} + /> + +
+
+
+ + Cancel + + + Save + +
+
+ + )} +
+
+ ) +} diff --git a/app/routes/_data.machines._index/menu.tsx b/app/routes/_data.machines._index/menu.tsx index c6818f3..eb0bffe 100644 --- a/app/routes/_data.machines._index/menu.tsx +++ b/app/routes/_data.machines._index/menu.tsx @@ -10,6 +10,7 @@ import Expire from './dialogs/expire' import Move from './dialogs/move' import Rename from './dialogs/rename' import Routes from './dialogs/routes' +import Tags from './dialogs/tags' interface MenuProps { machine: Machine @@ -24,6 +25,7 @@ export default function Menu({ machine, routes, magic, users }: MenuProps) { const removeState = useState(false) const routesState = useState(false) const moveState = useState(false) + const tagsState = useState(false) const expired = machine.expiry === '0001-01-01 00:00:00' || machine.expiry === '0001-01-01T00:00:00Z' @@ -54,6 +56,10 @@ export default function Menu({ machine, routes, magic, users }: MenuProps) { routes={routes} state={routesState} /> + Edit route settings - + Edit ACL tags - + Change owner