|
| 1 | +import { useContext, useState } from "react"; |
| 2 | +import { useDebounce, useLocalStorage } from "react-use"; |
| 3 | +import { TextInput } from "@common/elements/TextInput"; |
| 4 | +import { LogList } from "@common/features/logs/components/LogList"; |
| 5 | +import { LogToolbar } from "@common/features/logs/components/LogToolbar"; |
| 6 | +import { filterLogs } from "@common/features/logs/lib/filterLogs"; |
| 7 | +import { displayNameToIdentifier } from "@common/lib/functions/FunctionsProvider"; |
| 8 | +import { functionIdentifierValue } from "@common/lib/functions/generateFileTree"; |
| 9 | +import { UdfLog, useLogs } from "@common/lib/useLogs"; |
| 10 | +import { ModuleFunction } from "@common/lib/functions/types"; |
| 11 | +import { Nent } from "@common/lib/useNents"; |
| 12 | +import { Button } from "@common/elements/Button"; |
| 13 | +import { ExternalLinkIcon } from "@radix-ui/react-icons"; |
| 14 | +import { useRouter } from "next/router"; |
| 15 | +import { DeploymentInfoContext } from "@common/lib/deploymentContext"; |
| 16 | + |
| 17 | +type LogLevel = "success" | "failure" | "DEBUG" | "INFO" | "WARN" | "ERROR"; |
| 18 | + |
| 19 | +const DEFAULT_LOG_LEVELS: LogLevel[] = [ |
| 20 | + "success", |
| 21 | + "failure", |
| 22 | + "DEBUG", |
| 23 | + "INFO", |
| 24 | + "WARN", |
| 25 | + "ERROR", |
| 26 | +]; |
| 27 | + |
| 28 | +interface FunctionLogsProps { |
| 29 | + currentOpenFunction: ModuleFunction; |
| 30 | + selectedNent?: Nent; |
| 31 | +} |
| 32 | + |
| 33 | +export function FunctionLogs({ |
| 34 | + currentOpenFunction, |
| 35 | + selectedNent, |
| 36 | +}: FunctionLogsProps) { |
| 37 | + const functionId = functionIdentifierValue( |
| 38 | + displayNameToIdentifier(currentOpenFunction.displayName), |
| 39 | + selectedNent?.path, |
| 40 | + ); |
| 41 | + |
| 42 | + const [logs, setLogs] = useState<UdfLog[]>([]); |
| 43 | + const [paused, setPaused] = useState<number>(0); |
| 44 | + const [manuallyPaused, setManuallyPaused] = useState(false); |
| 45 | + |
| 46 | + // Store filter and selected levels in local storage, scoped to the function |
| 47 | + const [filter, setFilter] = useLocalStorage<string>( |
| 48 | + `function-logs/${functionId}/filter`, |
| 49 | + "", |
| 50 | + ); |
| 51 | + const [innerFilter, setInnerFilter] = useState(filter ?? ""); |
| 52 | + const [selectedLevels, setSelectedLevels] = useLocalStorage<LogLevel[]>( |
| 53 | + `function-logs/${functionId}/selected-levels`, |
| 54 | + DEFAULT_LOG_LEVELS, |
| 55 | + ); |
| 56 | + |
| 57 | + useDebounce( |
| 58 | + () => { |
| 59 | + setFilter(innerFilter); |
| 60 | + }, |
| 61 | + 200, |
| 62 | + [innerFilter], |
| 63 | + ); |
| 64 | + |
| 65 | + const onPause = (p: boolean) => { |
| 66 | + const now = new Date().getTime(); |
| 67 | + setPaused(p ? now : 0); |
| 68 | + }; |
| 69 | + |
| 70 | + const logsConnectivityCallbacks = { |
| 71 | + onReconnected: () => {}, |
| 72 | + onDisconnected: () => {}, |
| 73 | + }; |
| 74 | + |
| 75 | + const receiveLogs = (entries: UdfLog[]) => { |
| 76 | + setLogs((prev) => { |
| 77 | + const newLogs = filterLogs( |
| 78 | + { |
| 79 | + logTypes: DEFAULT_LOG_LEVELS, |
| 80 | + functions: [functionId], |
| 81 | + selectedFunctions: [functionId], |
| 82 | + filter: "", |
| 83 | + }, |
| 84 | + entries, |
| 85 | + ); |
| 86 | + if (!newLogs) { |
| 87 | + return prev; |
| 88 | + } |
| 89 | + return [...prev, ...newLogs].slice( |
| 90 | + Math.max(prev.length + entries.length - 10000, 0), |
| 91 | + prev.length + entries.length, |
| 92 | + ); |
| 93 | + }); |
| 94 | + }; |
| 95 | + |
| 96 | + useLogs(logsConnectivityCallbacks, receiveLogs, paused > 0 || manuallyPaused); |
| 97 | + |
| 98 | + const router = useRouter(); |
| 99 | + const { deploymentsURI } = useContext(DeploymentInfoContext); |
| 100 | + |
| 101 | + return ( |
| 102 | + <div className="flex h-full w-full min-w-[48rem] grow flex-col gap-2"> |
| 103 | + <LogToolbar |
| 104 | + functions={[functionId]} |
| 105 | + selectedFunctions={[functionId]} |
| 106 | + setSelectedFunctions={(_functions) => {}} |
| 107 | + selectedLevels={selectedLevels ?? DEFAULT_LOG_LEVELS} |
| 108 | + setSelectedLevels={(levels) => setSelectedLevels(levels as LogLevel[])} |
| 109 | + selectedNents={selectedNent ? [selectedNent.path] : []} |
| 110 | + setSelectedNents={() => {}} |
| 111 | + hideFunctionFilter |
| 112 | + firstItem={ |
| 113 | + <div className="flex grow gap-2"> |
| 114 | + <Button |
| 115 | + variant="neutral" |
| 116 | + size="sm" |
| 117 | + icon={<ExternalLinkIcon />} |
| 118 | + href={`${deploymentsURI}/logs${router.query.component ? `?component=${router.query.component}` : ""}`} |
| 119 | + > |
| 120 | + View all Logs |
| 121 | + </Button> |
| 122 | + <TextInput |
| 123 | + id="Search logs" |
| 124 | + placeholder="Filter logs..." |
| 125 | + value={innerFilter} |
| 126 | + onChange={(e) => setInnerFilter(e.target.value)} |
| 127 | + type="search" |
| 128 | + /> |
| 129 | + </div> |
| 130 | + } |
| 131 | + /> |
| 132 | + <LogList |
| 133 | + nents={selectedNent ? [selectedNent] : []} |
| 134 | + logs={logs} |
| 135 | + filteredLogs={filterLogs( |
| 136 | + { |
| 137 | + logTypes: selectedLevels ?? DEFAULT_LOG_LEVELS, |
| 138 | + functions: [functionId], |
| 139 | + selectedFunctions: [functionId], |
| 140 | + filter: filter ?? "", |
| 141 | + }, |
| 142 | + logs, |
| 143 | + )} |
| 144 | + deploymentAuditLogs={[]} |
| 145 | + filter={filter ?? ""} |
| 146 | + clearedLogs={[]} |
| 147 | + setClearedLogs={() => {}} |
| 148 | + paused={paused > 0 || manuallyPaused} |
| 149 | + setPaused={onPause} |
| 150 | + setManuallyPaused={(p) => { |
| 151 | + onPause(p); |
| 152 | + setManuallyPaused(p); |
| 153 | + }} |
| 154 | + /> |
| 155 | + </div> |
| 156 | + ); |
| 157 | +} |
0 commit comments