Skip to content
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
33 changes: 2 additions & 31 deletions src/components/terminal/TerminalPrompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ interface State {
}

export default class TerminalPrompt extends Component<Props, State> {
// Constants for IPFS host display formatting
private static readonly MAX_HOST_LENGTH = 25;
private static readonly CID_MIN_LENGTH = 16;
private static readonly CID_TRUNCATION_LENGTH = 8;

state: State = {
inputText: '',
inputRef: createRef<HTMLInputElement>(),
Expand All @@ -49,33 +44,9 @@ export default class TerminalPrompt extends Component<Props, State> {
private getDisplayHost(): string {
const fullHost = window.location.host.split(':')[0];

// Check if it's an IPFS host (contains ipns or ipfs and is long)
if (fullHost.includes('ipns') || fullHost.includes('ipfs')) {
if (fullHost.length > TerminalPrompt.MAX_HOST_LENGTH) {
// For IPFS, show first CID_TRUNCATION_LENGTH chars + ... + last CID_TRUNCATION_LENGTH chars of the CID + domain
const parts = fullHost.split('.');
const cidPart = parts[0]; // The long hash part

// Validate that we have domain parts before constructing domainPart
let domainPart = '';
if (parts.length > 1) {
domainPart = parts.slice(1).join('.'); // Everything after the CID
} else {
// Fallback to fullHost if no '.' is present (edge case)
return fullHost;
}

if (cidPart.length > TerminalPrompt.CID_MIN_LENGTH) {
const prefix = cidPart.substring(
0,
TerminalPrompt.CID_TRUNCATION_LENGTH,
);
const suffix = cidPart.substring(
cidPart.length - TerminalPrompt.CID_TRUNCATION_LENGTH,
);
return `${prefix}...${suffix}.${domainPart}`;
}
}
const parts = fullHost.split('.');
return parts.length > 1 ? parts.slice(1).join('.') : fullHost;
}

return fullHost;
Expand Down
68 changes: 43 additions & 25 deletions src/components/terminal/__tests__/TerminalPrompt.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -355,19 +355,19 @@ describe('TerminalPrompt', () => {
).toBeInTheDocument();
});

it('returns full host for short IPFS hosts', () => {
it('removes the first subdomain for IPFS hosts with two parts', () => {
Object.defineProperty(window, 'location', {
value: { host: 'short.ipfs.dweb.link' },
value: { host: 'cid.ipfs.dweb.link' },
writable: true,
});

render(<TerminalPrompt {...defaultProps} />);
expect(
screen.getByText('translated_visitor@short.ipfs.dweb.link:~$'),
screen.getByText('translated_visitor@ipfs.dweb.link:~$'),
).toBeInTheDocument();
});

it('shortens long IPNS hosts correctly', () => {
it('removes the first subdomain for IPNS hosts with multiple parts', () => {
Object.defineProperty(window, 'location', {
value: {
host: 'k2k4r8ng8uzrtqb5ham8kao889m8qezu96z4w3lpinyqghum43veb6n3.ipns.dweb.link',
Expand All @@ -376,17 +376,12 @@ describe('TerminalPrompt', () => {
});

render(<TerminalPrompt {...defaultProps} />);

const promptSpan = screen.getByText((content, element) => {
return (
element?.tagName.toLowerCase() === 'span' &&
content.includes('k2k4r8ng...43veb6n3.ipns.dweb.link')
);
});
expect(promptSpan).toBeInTheDocument();
expect(
screen.getByText('translated_visitor@ipns.dweb.link:~$'),
).toBeInTheDocument();
});

it('shortens long IPFS hosts correctly', () => {
it('removes the first subdomain for IPFS hosts with multiple parts', () => {
Object.defineProperty(window, 'location', {
value: {
host: 'QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o.ipfs.dweb.link',
Expand All @@ -395,17 +390,12 @@ describe('TerminalPrompt', () => {
});

render(<TerminalPrompt {...defaultProps} />);

const promptSpan = screen.getByText((content, element) => {
return (
element?.tagName.toLowerCase() === 'span' &&
content.includes('QmYjtig7...iFofrE7o.ipfs.dweb.link')
);
});
expect(promptSpan).toBeInTheDocument();
expect(
screen.getByText('translated_visitor@ipfs.dweb.link:~$'),
).toBeInTheDocument();
});

it('handles hosts with port numbers', () => {
it('handles hosts with port numbers by removing the port', () => {
Object.defineProperty(window, 'location', {
value: { host: 'localhost:3000' },
writable: true,
Expand All @@ -417,15 +407,43 @@ describe('TerminalPrompt', () => {
).toBeInTheDocument();
});

it('handles CID shorter than 16 characters', () => {
it('removes the first subdomain for IPNS hosts with two parts', () => {
Object.defineProperty(window, 'location', {
value: { host: 'cid.ipns.dweb.link' },
writable: true,
});

render(<TerminalPrompt {...defaultProps} />);
expect(
screen.getByText('translated_visitor@ipns.dweb.link:~$'),
).toBeInTheDocument();
});

it('handles a complex subdomain for IPFS', () => {
Object.defineProperty(window, 'location', {
value: { host: 'shortcid.ipfs.dweb.link' },
value: {
host: 'bafybeiemxf5abkkw7dne2qz6jxqj4v6q6jqtjzg6c44yvxs5kzquu3zqy.ipfs.dweb.link',
},
writable: true,
});

render(<TerminalPrompt {...defaultProps} />);
expect(
screen.getByText('translated_visitor@ipfs.dweb.link:~$'),
).toBeInTheDocument();
});

it('handles a complex subdomain for IPNS', () => {
Object.defineProperty(window, 'location', {
value: {
host: 'k51qzi5uqu5djdc6tg7g3k1e7d7j8jx7l6d5k4z3x2y1w0v9u8i7o6p.ipns.dweb.link',
},
writable: true,
});

render(<TerminalPrompt {...defaultProps} />);
expect(
screen.getByText('translated_visitor@shortcid.ipfs.dweb.link:~$'),
screen.getByText('translated_visitor@ipns.dweb.link:~$'),
).toBeInTheDocument();
});
});
Expand Down