Skip to content

Commit

Permalink
feat: use common component for ip address display
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Sep 29, 2024
1 parent de36551 commit 2a1d749
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions framework/core/js/src/common/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import AlertManagerState from './states/AlertManagerState';
import ModalManagerState from './states/ModalManagerState';
import PageState from './states/PageState';
import LabelValue from './components/LabelValue';
import IPAddress from './components/IPAddress';

export default {
extenders,
Expand Down Expand Up @@ -169,6 +170,7 @@ export default {
'components/Tooltip': Tooltip,
'components/EditUserModal': EditUserModal,
'components/LabelValue': LabelValue,
'components/IPAddress': IPAddress,
Model: Model,
Application: Application,
'helpers/fullTime': fullTime,
Expand Down
38 changes: 38 additions & 0 deletions framework/core/js/src/common/components/IPAddress.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Component, { ComponentAttrs } from '../Component';
import type Mithril from 'mithril';
import ItemList from '../utils/ItemList';

export interface IIPAddressAttrs extends ComponentAttrs {
ip: string | undefined | null;
}

/**
* A component to wrap an IP address for display.
* Designed to be customizable for different use cases.
*
* @example
* <IPAddress ip="127.0.0.1" />
* @example
* <IPAddress ip={post.data.attributes.ipAddress} />
*/
export default class IPAddress<CustomAttrs extends IIPAddressAttrs = IIPAddressAttrs> extends Component<CustomAttrs> {
ip!: string;

oninit(vnode: Mithril.Vnode<CustomAttrs, this>) {
super.oninit(vnode);

this.ip = this.attrs.ip || '';
}

view() {
return <span className="IPAddress">{this.viewItems().toArray()}</span>;
}

viewItems(): ItemList<Mithril.Children> {
const items = new ItemList<Mithril.Children>();

items.add('ip', <span>{this.ip}</span>, 100);

return items;
}
}
8 changes: 7 additions & 1 deletion framework/core/js/src/forum/components/AccessTokensList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Tooltip from '../../common/components/Tooltip';
import type Mithril from 'mithril';
import type AccessToken from '../../common/models/AccessToken';
import { NestedStringArray } from '@askvortsov/rich-icu-message-formatter';
import IPAddress from '../../common/components/IPAddress';

export interface IAccessTokensListAttrs extends ComponentAttrs {
tokens: AccessToken[];
Expand Down Expand Up @@ -100,7 +101,12 @@ export default class AccessTokensList<CustomAttrs extends IAccessTokensListAttrs
token.lastActivityAt() ? (
<>
{humanTime(token.lastActivityAt())}
{token.lastIpAddress() && ` — ${token.lastIpAddress()}`}
{token.lastIpAddress() && (
<span>
{' '}
<IPAddress ip={token.lastIpAddress()} />
</span>
)}
{this.attrs.type === 'developer_token' && token.device() && (
<>
{' '}
Expand Down
9 changes: 8 additions & 1 deletion framework/core/js/src/forum/components/PostMeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Component from '../../common/Component';
import humanTime from '../../common/helpers/humanTime';
import fullTime from '../../common/helpers/fullTime';
import ItemList from '../../common/utils/ItemList';
import IPAddress from '../../common/components/IPAddress';

/**
* The `PostMeta` component displays the time of a post, and when clicked, shows
Expand Down Expand Up @@ -78,7 +79,13 @@ export default class PostMeta extends Component {

items.add('post-time', <span className="PostMeta-time">{fullTime(time)}</span>, 90);

items.add('post-ip', <span className="PostMeta-ip">{post.data.attributes.ipAddress}</span>, 80);
items.add(
'post-ip',
<span className="PostMeta-ip">
<IPAddress ip={post.data.attributes.ipAddress} />
</span>,
80
);

items.add(
'permalink',
Expand Down

0 comments on commit 2a1d749

Please sign in to comment.