import { useCallback, useEffect, useRef, useState } from 'react'
import {
applyProfile,
deleteProfile,
exportProfiles,
getProfile,
importProfiles,
listProfiles,
previewProfile,
snapshotProfile,
type ProfileImportPlan,
type ProfileSummary,
type ProfileTargetPreview,
type ProfileView,
} from '../../api/hub-profiles'
import type { RunOutcome } from '../../api/apps'
import { Alert } from '../../ui/Alert'
import { Button } from '../../ui/Button'
import { Check } from '../../ui/Check'
import { Confirm } from '../../ui/Confirm'
import { Dialog } from '../../ui/Dialog'
import { Empty, Failure } from '../../ui/Empty'
import { LabeledInput, LabeledTextarea } from '../../ui/Field'
import { Tag } from '../../ui/Tag'
import { Skeleton } from '@/components/ui/skeleton'
import { DiffView } from './ConfigDiffView'
import { RunFlow, pickerServers, type RunRequest } from './RunFlow'
/*
Behind a hub, for an admin: profiles, named bundles of store apps (with the hub's baseline config of
each), the shared settings and the Blocked and Allowed lists. One is made from the hub's current
baselines, can be exported to another hub, and is applied to one or several servers through the
same picker, review and results as every other change here. A server's steps run in order (apps,
app configs, settings, lists) and a failed one stops the rest for that server; "Retry failed" starts
again at the failed step. Nothing here shows a secret or an app's config text.
*/
const sessionEnded = (kind: string, message?: string): string => (kind === 'error' ? (message ?? 'Failed.') : 'The session ended.')
const when = (iso: string): string => (iso === '' ? '' : new Date(iso).toLocaleString())
const APP_STATE = {
install: ['warn', 'Will be installed'],
present: ['ok', 'Already installed'],
'version-differs': ['info', 'Other version installed'],
'not-in-store': ['dan', 'Not in this store'],
} as const
const CONFIG_STATE = {
same: ['ok', 'Same'],
differs: ['warn', 'Will change'],
'after-install': ['info', 'Set after install'],
unreadable: ['dan', 'Cannot read'],
} as const
/* ---- Apply: the review of what each server would get ---- */
function PreviewBody({ targets, opts }: { targets: ProfileTargetPreview[]; opts: { remove: boolean } }) {
const [remove, setRemove] = useState(opts.remove)
const extras = targets.reduce((n, t) => n + (t.lists?.blocked.extra ?? 0) + (t.lists?.allowed.extra ?? 0), 0)
return (
3. Settings
{t.settings.error !== undefined ? Cannot read : t.settings.unchanged ? Already in sync : {`${t.settings.fields.length} field(s) will change`}}
{t.settings.error !== undefined && {t.settings.error}}
{t.settings.secretFields.length > 0 && {t.settings.secretFields.join(', ')}: the secret values are sent but never shown.}
{t.settings.error === undefined && !t.settings.unchanged && (
)}
)}
{t.lists !== null && (
4. Blocked and Allowed lists
{t.lists.error !== undefined ? Cannot read : t.lists.status === 'same' ? Already in sync : Will change}
{t.lists.error !== undefined ? (
{t.lists.error}
) : (
Blocked: {t.lists.blocked.missing} to add, {t.lists.blocked.extra} extra. Allowed: {t.lists.allowed.missing} to add, {t.lists.allowed.extra} extra.
)}
{t.lists.tooMany !== undefined && {t.lists.tooMany}}
)}
)}
))}
{/* Only when a server has list domains the profile does not: otherwise there is nothing to remove. */}
{extras > 0 && (
{
opts.remove = on
setRemove(on)
}}
/>
)}