Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

feat(frontend): adopt resource picker for targets

Juan Denis652d1ab40914
diff --git a/frontend/src/components/ResourcePicker.jsx b/frontend/src/components/ResourcePicker.jsx
new file mode 100644
index 00000000..80fa7934
--- /dev/null
+++ b/frontend/src/components/ResourcePicker.jsx
@@ -0,0 +1,139 @@
+import { useMemo, useState } from 'react';
+import { Check, ChevronDown, Server } from 'lucide-react';
+import { useTranslation } from 'react-i18next';
+
+import { Button } from './ui/button';
+import {
+ Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList,
+} from './ui/command';
+import { Popover, PopoverContent, PopoverTrigger } from './ui/popover';
+import { useResourceOptions } from '../hooks/useResourceOptions';
+import { normalizeResourceRef, resourceKey } from '../utils/resourceRefs';
+
+const normalizeList = (options) => options.map(normalizeResourceRef).filter(Boolean);
+
+export default function ResourcePicker({
+ value,
+ onChange,
+ types,
+ scope,
+ capabilities,
+ staticOptions = [],
+ filterOption = () => true,
+ disabled = false,
+ label,
+ placeholder,
+ searchPlaceholder,
+ className = '',
+}) {
+ const { t } = useTranslation();
+ const [open, setOpen] = useState(false);
+ const [query, setQuery] = useState('');
+ const search = useResourceOptions({
+ types,
+ scope,
+ capabilities,
+ query,
+ enabled: open,
+ });
+ const normalizedStatic = useMemo(() => normalizeList(staticOptions), [staticOptions]);
+ const normalizedValue = useMemo(() => normalizeResourceRef(value), [value]);
+ const normalizedQuery = query.trim().toLowerCase();
+ const visibleStatic = normalizedStatic.filter((option) => (
+ filterOption(option)
+ && (!normalizedQuery || `${option.label} ${option.sublabel}`.toLowerCase().includes(normalizedQuery))
+ ));
+ const visibleGroups = search.groups
+ .map((group) => ({
+ ...group,
+ options: group.options.filter(filterOption),
+ }))
+ .filter((group) => group.options.length);
+ const hasOptions = visibleStatic.length > 0
+ || visibleGroups.some((group) => group.options.length > 0);
+
+ const select = (option) => {
+ if ((types || []).includes(option.type)) search.recordSelection(option);
+ onChange(option);
+ setOpen(false);
+ setQuery('');
+ };
+
+ const renderOption = (option) => {
+ const selected = normalizedValue
+ && resourceKey(normalizedValue) === resourceKey(option);
+ return (
+ <CommandItem
+ key={resourceKey(option)}
+ value={resourceKey(option)}
+ onSelect={() => select(option)}
+ >
+ <Server className="sk-resource-picker__option-icon" aria-hidden="true" />
+ <span className="sk-resource-picker__option-copy">
+ <span>{option.label}</span>
+ {option.sublabel && <small>{option.sublabel}</small>}
+ </span>
+ {option.status && (
+ <span className={`sk-state sk-state--${option.status}`}>{option.status}</span>
+ )}
+ {selected && <Check className="sk-resource-picker__check" aria-hidden="true" />}
+ </CommandItem>
+ );
+ };
+
+ const triggerLabel = normalizedValue?.label || placeholder;
+ const triggerClassName = ['sk-resource-picker__trigger', className]
+ .filter(Boolean).join(' ');
+
+ return (
+ <Popover open={open} onOpenChange={(nextOpen) => {
+ setOpen(nextOpen);
+ if (!nextOpen) setQuery('');
+ }}>
+ <PopoverTrigger asChild>
+ <Button
+ type="button"
+ variant="outline"
+ className={triggerClassName}
+ disabled={disabled}
+ aria-label={label}
+ aria-expanded={open}
+ >
+ <Server aria-hidden="true" />
+ <span>{triggerLabel}</span>
+ <ChevronDown className="sk-resource-picker__chevron" aria-hidden="true" />
+ </Button>
+ </PopoverTrigger>
+ <PopoverContent className="sk-resource-picker__popover" align="start">
+ <Command shouldFilter={false} label={label}>
+ <CommandInput
+ value={query}
+ onValueChange={setQuery}
+ placeholder={searchPlaceholder}
+ />
+ <CommandList>
+ {visibleStatic.length > 0 && (
+ <CommandGroup>{visibleStatic.map(renderOption)}</CommandGroup>
+ )}
+ {visibleGroups.map((group) => (
+ <CommandGroup key={group.id}>{group.options.map(renderOption)}</CommandGroup>
+ ))}
+ {search.isLoading && (
+ <div className="sk-resource-picker__message" role="status">
+ {t('common.state.loading', 'Loading')}
+ </div>
+ )}
+ {search.isError && (
+ <div className="sk-resource-picker__message sk-resource-picker__message--error" role="alert">
+ {t('common.error.failedToLoad', 'Failed to load')}
+ </div>
+ )}
+ {!search.isLoading && !search.isError && !hasOptions && (
+ <CommandEmpty>{t('common.state.noResults', 'No results found')}</CommandEmpty>
+ )}
+ </CommandList>
+ </Command>
+ </PopoverContent>
+ </Popover>
+ );
+}
diff --git a/frontend/src/components/TargetPicker.jsx b/frontend/src/components/TargetPicker.jsx
index 37a70064..34c53522 100644
--- a/frontend/src/components/TargetPicker.jsx
+++ b/frontend/src/components/TargetPicker.jsx
@@ -1,100 +1,118 @@
-import { useState, useEffect, useMemo } from 'react';
-import { Server } from 'lucide-react';
-import { api } from '../services/api';
+import { useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
-// TargetPicker — a small reusable selector for "local panel host vs.
-// connected agent." Used by the Cron, Cloudflared, and File Manager
-// pages to switch which server an action runs against.
-//
-// `feature` gates the agent list: only agents that report
-// capabilities[feature] === true are shown. Agents that haven't
-// reported capabilities yet (older builds) are filtered out — same
-// pattern as the cron picker.
-//
-// onChange receives `{ kind: 'local' } | { kind: 'agent', server_id, name,
-// allowedPaths, os_type, agentInstallDir, agentConfigDir }` (os_type:
-// linux/windows/darwin, and the agent's self-reported footprint dirs — all
-// null for agents that predate sysinfo reporting), or `{ kind: <extra.value> }`
-// for any caller-supplied `extraOptions` (e.g. an "S3 bucket" target in the
-// File Manager).
-export default function TargetPicker({ feature, value, onChange, includeLocal = true, extraOptions = [] }) {
+import ResourcePicker from './ResourcePicker';
+import { api } from '../services/api';
+
+const enabledCapabilities = (server) => Object.entries(server?.capabilities || {})
+ .filter(([, enabled]) => enabled === true)
+ .map(([name]) => name);
+
+const onlineServersOnly = (resource) => (
+ resource.type !== 'server' || resource.status === 'online'
+);
+
+// Compatibility adapter around ResourcePicker. Callers keep receiving the
+// operational target shape while selection itself now uses scoped ResourceRef
+// search, capability filters, keyboard navigation, and recent resources.
+export default function TargetPicker({
+ feature,
+ value,
+ onChange,
+ includeLocal = true,
+ extraOptions = [],
+}) {
const { t } = useTranslation();
const [servers, setServers] = useState([]);
useEffect(() => {
let cancelled = false;
api.getAvailableServers()
- .then(data => { if (!cancelled) setServers(Array.isArray(data) ? data : []); })
- .catch(() => { if (!cancelled) setServers([]); });
+ .then((data) => {
+ if (!cancelled) setServers(Array.isArray(data) ? data : []);
+ })
+ .catch(() => {
+ if (!cancelled) setServers([]);
+ });
return () => { cancelled = true; };
}, []);
- const eligible = useMemo(() => {
- return servers.filter(s => {
- if (s.id === 'local') return false; // handled separately
- if (s.status !== 'online') return false;
- if (!feature) return true;
- return s.capabilities && s.capabilities[feature];
- });
- }, [servers, feature]);
+ const localOption = useMemo(() => ({
+ type: 'target',
+ id: 'local',
+ label: t('app.targetPicker.localThisServer', 'Local (this server)'),
+ sublabel: '',
+ path: '/servers',
+ scope: {},
+ status: 'online',
+ capabilities: [],
+ }), [t]);
- function handleChange(e) {
- const id = e.target.value;
- if (id === 'local') {
- onChange({ kind: 'local' });
- return;
+ const staticOptions = useMemo(() => [
+ ...(includeLocal ? [localOption] : []),
+ ...extraOptions.map((option) => ({
+ type: 'target',
+ id: String(option.value),
+ label: option.label,
+ sublabel: '',
+ path: '/files',
+ scope: {},
+ status: null,
+ capabilities: [],
+ })),
+ ], [extraOptions, includeLocal, localOption]);
+
+ const selectedResource = useMemo(() => {
+ if (value?.kind === 'agent') {
+ const server = servers.find((candidate) => String(candidate.id) === String(value.server_id));
+ return {
+ type: 'server',
+ id: String(value.server_id),
+ label: value.name || server?.name || server?.hostname || String(value.server_id),
+ sublabel: server?.ip_address || server?.hostname || '',
+ path: `/servers/${value.server_id}`,
+ scope: {},
+ status: server?.status || null,
+ capabilities: enabledCapabilities(server),
+ };
+ }
+ if (value?.kind && value.kind !== 'local') {
+ return staticOptions.find((option) => option.id === String(value.kind)) || null;
}
- if (extraOptions.some(o => o.value === id)) {
- onChange({ kind: id });
+ return localOption;
+ }, [localOption, servers, staticOptions, value]);
+
+ const handleChange = (resource) => {
+ if (resource.type === 'target') {
+ onChange(resource.id === 'local' ? { kind: 'local' } : { kind: resource.id });
return;
}
- const s = eligible.find(x => x.id === id);
- if (!s) return;
+ const server = servers.find((candidate) => String(candidate.id) === resource.id);
onChange({
kind: 'agent',
- server_id: s.id,
- name: s.name || s.hostname || s.id,
- allowedPaths: s.allowed_paths || [],
- os_type: s.os_type || null,
- agentInstallDir: s.agent_install_dir || null,
- agentConfigDir: s.agent_config_dir || null,
+ server_id: resource.id,
+ name: resource.label,
+ allowedPaths: server?.allowed_paths || [],
+ os_type: server?.os_type || null,
+ agentInstallDir: server?.agent_install_dir || null,
+ agentConfigDir: server?.agent_config_dir || null,
});
- }
-
- const selectValue = value?.kind === 'agent'
- ? value.server_id
- : (value?.kind && value.kind !== 'local' ? value.kind : 'local');
-
- const optionCount = (includeLocal ? 1 : 0) + eligible.length + extraOptions.length;
- const singleLabel = useMemo(() => {
- if (selectValue !== 'local') {
- const agent = eligible.find(s => s.id === selectValue);
- if (agent) return agent.name || agent.hostname || agent.id;
- const extra = extraOptions.find(o => o.value === selectValue);
- if (extra) return extra.label;
- }
- return 'Local (this server)';
- }, [selectValue, eligible, extraOptions]);
+ };
return (
<div className="target-picker">
- <Server size={14} className="target-picker__icon" />
- {optionCount <= 1 ? (
- <span className="target-picker__single">{singleLabel}</span>
- ) : (
- <select value={selectValue} onChange={handleChange} className="target-picker__select">
- {includeLocal && <option value="local">{t('app.targetPicker.localThisServer', 'Local (this server)')}</option>}
- {eligible.map(s => (
- <option key={s.id} value={s.id}>
- {s.name || s.hostname || s.id}
- </option>
- ))}
- {extraOptions.map(o => (
- <option key={o.value} value={o.value}>{o.label}</option>
- ))}
- </select>
- )}
+ <ResourcePicker
+ value={selectedResource}
+ onChange={handleChange}
+ types={['server']}
+ capabilities={feature ? [feature] : []}
+ staticOptions={staticOptions}
+ filterOption={onlineServersOnly}
+ label={t('app.serverPicker.filterServers', 'Filter servers')}
+ placeholder={t('app.targetPicker.localThisServer', 'Local (this server)')}
+ searchPlaceholder={t('app.serverPicker.findAServer', 'Find a server…')}
+ className="target-picker__resource"
+ />
</div>
);
}
diff --git a/frontend/src/styles/components/_resource-picker.scss b/frontend/src/styles/components/_resource-picker.scss
new file mode 100644
index 00000000..dbd20ed9
--- /dev/null
+++ b/frontend/src/styles/components/_resource-picker.scss
@@ -0,0 +1,76 @@
+.sk-resource-picker {
+ &__trigger {
+ justify-content: flex-start;
+ min-width: 190px;
+ max-width: min(340px, 80vw);
+
+ > span {
+ overflow: hidden;
+ flex: 1;
+ text-align: start;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ }
+ }
+
+ &__chevron {
+ flex: 0 0 auto;
+ color: $text-tertiary;
+ }
+
+ &__popover {
+ width: min(360px, calc(100vw - #{$space-6}));
+ padding: 0;
+ overflow: hidden;
+ }
+
+ &__option-icon,
+ &__check {
+ width: 16px;
+ height: 16px;
+ flex: 0 0 auto;
+ }
+
+ &__option-icon {
+ color: $text-tertiary;
+ }
+
+ &__option-copy {
+ display: grid;
+ min-width: 0;
+ flex: 1;
+
+ > span,
+ small {
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ }
+
+ small {
+ color: $text-tertiary;
+ font-size: $font-size-xs;
+ }
+ }
+
+ &__check {
+ color: $primary-color;
+ }
+
+ &__message {
+ padding: $space-6 $space-3;
+ color: $text-tertiary;
+ font-size: $font-size-sm;
+ text-align: center;
+
+ &--error {
+ color: $danger;
+ }
+ }
+}
+
+.target-picker {
+ display: inline-flex;
+ align-items: center;
+ min-width: 0;
+}
diff --git a/frontend/src/styles/main.scss b/frontend/src/styles/main.scss
index 400accac..ddc6eb88 100644
--- a/frontend/src/styles/main.scss
+++ b/frontend/src/styles/main.scss
@@ -74,6 +74,7 @@
@import 'components/_design-system'; // redesign shared primitives (sk-* namespace)
@import 'components/_change-bar'; // local draft history + save/discard controls
@import 'components/_shortcut-sheet'; // registered keyboard commands
+@import 'components/_resource-picker'; // scoped ResourceRef combobox
@import 'components/_datagrid'; // DataGrid list surface (sk-grid*, ds/grid/)
@import 'components/_dashboard-widgets'; // configurable dashboard grid (skw-* namespace)
@import 'components/_setup-health'; // dashboard setup-health card + account nudge
diff --git a/frontend/src/styles/pages/_file-manager.scss b/frontend/src/styles/pages/_file-manager.scss
index 6b50281f..9cc98104 100644
--- a/frontend/src/styles/pages/_file-manager.scss
+++ b/frontend/src/styles/pages/_file-manager.scss
@@ -323,7 +323,7 @@ $ft-default: #646b7a;
.file-manager-toolbar .target-picker {
flex-shrink: 0;
- .target-picker__select {
+ .target-picker__resource {
max-width: 170px;
}
}
@@ -1756,27 +1756,3 @@ $drawer-width: 520px;
border: 1px solid fade($accent-primary-raw, 25%);
flex-shrink: 0;
}
-
-.target-picker {
- display: inline-flex;
- align-items: center;
- gap: $space-1;
- padding: $space-1 $space-2;
- background: var(--surface-2);
- border: 1px solid var(--border);
- border-radius: 8px;
- .target-picker__select,
- .target-picker__single {
- background: transparent;
- border: none;
- color: var(--text-primary);
- font-size: $font-size-sm;
- outline: none;
- }
- .target-picker__select { cursor: pointer; }
- .target-picker__single {
- padding-right: $space-1;
- white-space: nowrap;
- }
- .target-picker__icon { color: var(--text-secondary); }
-}