Skip to content

[1.x] [extensibility] feat: use common component for ip address display #4042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
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
Loading