1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/components/dock/log-resource-selector.tsx
Alex Andreev a157eb03e6
Generic logs view with Pod selector (#1984)
* Adding LogTabStore

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Adding Pod selector in logs tab

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Refresh containers on pod change

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Adding <LogResourceSelector /> tests

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Adding LogTabStore tests

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Clearn getPodsByOwnerId method

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Extracting dummy pods into mock file

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Eliminating containers and initContainers from store

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Refreshing tab pods if pod amount is changed in store

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* A bit of cleaning up, fixing tests

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Fix lint newline errors

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Return getPodsByOwner() method

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Rename log tab when pod changes

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
2021-01-27 17:20:02 +03:00

96 lines
2.6 KiB
TypeScript

import "./log-resource-selector.scss";
import React, { useEffect } from "react";
import { observer } from "mobx-react";
import { Pod } from "../../api/endpoints";
import { Badge } from "../badge";
import { Select, SelectOption } from "../select";
import { LogTabData, logTabStore } from "./log-tab.store";
import { podsStore } from "../+workloads-pods/pods.store";
import { TabId } from "./dock.store";
interface Props {
tabId: TabId
tabData: LogTabData
save: (data: Partial<LogTabData>) => void
reload: () => void
}
export const LogResourceSelector = observer((props: Props) => {
const { tabData, save, reload, tabId } = props;
const { selectedPod, selectedContainer, pods } = tabData;
const pod = new Pod(selectedPod);
const containers = pod.getContainers();
const initContainers = pod.getInitContainers();
const onContainerChange = (option: SelectOption) => {
save({
selectedContainer: containers
.concat(initContainers)
.find(container => container.name === option.value)
});
reload();
};
const onPodChange = (option: SelectOption) => {
const selectedPod = podsStore.getByName(option.value, pod.getNs());
save({ selectedPod });
logTabStore.renameTab(tabId);
};
const getSelectOptions = (items: string[]) => {
return items.map(item => {
return {
value: item,
label: item
};
});
};
const containerSelectOptions = [
{
label: `Containers`,
options: getSelectOptions(containers.map(container => container.name))
},
{
label: `Init Containers`,
options: getSelectOptions(initContainers.map(container => container.name)),
}
];
const podSelectOptions = [
{
label: pod.getOwnerRefs()[0]?.name,
options: getSelectOptions(pods.map(pod => pod.metadata.name))
}
];
useEffect(() => {
reload();
}, [selectedPod]);
return (
<div className="LogResourceSelector flex gaps align-center">
<span>Namespace</span> <Badge data-testid="namespace-badge" label={pod.getNs()}/>
<span>Pod</span>
<Select
options={podSelectOptions}
value={{ label: pod.getName(), value: pod.getName() }}
onChange={onPodChange}
autoConvertOptions={false}
className="pod-selector"
/>
<span>Container</span>
<Select
options={containerSelectOptions}
value={{ label: selectedContainer.name, value: selectedContainer.name }}
onChange={onContainerChange}
autoConvertOptions={false}
className="container-selector"
/>
</div>
);
});