Skip to content

Commit

Permalink
ResourceLinks
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmadigan committed Jun 28, 2024
1 parent b523c21 commit e630876
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions src/components/ExamplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { useParams, useHistory, useLocation } from 'react-router-dom';
import { useParams, useHistory } from 'react-router-dom';
import Helmet from 'react-helmet';
import { useTranslation } from 'react-i18next';
import {
Expand All @@ -15,7 +15,7 @@ import {
Td,
Tbody,
} from '@patternfly/react-table';
import { useK8sWatchResource, K8sResourceCommon, NamespaceBar } from '@openshift-console/dynamic-plugin-sdk';
import { useK8sWatchResource, K8sResourceCommon, NamespaceBar, ResourceLink } from '@openshift-console/dynamic-plugin-sdk';
import './example.css';

interface Resource {
Expand Down Expand Up @@ -48,30 +48,19 @@ const ExamplePage: React.FC = () => {
const { t } = useTranslation('plugin__console-plugin-template');
const { ns } = useParams<{ ns: string }>();
const history = useHistory();
const location = useLocation();
const [namespace, setNamespace] = React.useState<string>(ns || '');

// Inline function to extract namespace from URL
const useNamespaceFromURL = (): string => {
const match = location.pathname.match(/\/k8s\/ns\/([^/]+)/);
return match ? match[1] : '';
};

React.useEffect(() => {
const extractedNamespace = useNamespaceFromURL();
if (extractedNamespace && extractedNamespace !== namespace) {
setNamespace(extractedNamespace);
}
console.log(`Initial namespace: ${namespace}`);
}, [location.pathname]);

const handleNamespaceChange = (newNamespace: string) => {
console.log(`Namespace changed to: ${newNamespace}`);
setNamespace(newNamespace);
const url = newNamespace === '#ALL_NS#' ? '/k8s/all-namespaces/example' : `/k8s/ns/${newNamespace}/example`;
history.push(url);
};

React.useEffect(() => {
console.log(`Initial namespace: ${namespace}`);
}, []);

React.useEffect(() => {
console.log(`Namespace updated: ${namespace}`);
}, [namespace]);
Expand Down Expand Up @@ -100,8 +89,12 @@ const ExamplePage: React.FC = () => {
<Tbody>
{data.map((item) => (
<Tr key={item.metadata.uid}>
<Td dataLabel={t('Name')}>{item.metadata.name}</Td>
<Td dataLabel={t('Namespace')}>{item.metadata.namespace}</Td>
<Td dataLabel={t('Name')}>
<ResourceLink groupVersionKind={resource.gvk} name={item.metadata.name} title={item.metadata.uid} />
</Td>
<Td dataLabel={t('Namespace')}>
<ResourceLink groupVersionKind={{group: resource.gvk.group, version: resource.gvk.version, kind: "Namespace"}} name={item.metadata.namespace} title={item.metadata.namespace} />
</Td>
<Td dataLabel={t('Age')}>{formatTimestamp(item.metadata.creationTimestamp)}</Td>
{resource.name === 'Gateways' && (
<Td dataLabel={t('Address')}>
Expand Down

0 comments on commit e630876

Please sign in to comment.