Skip to content

Commit

Permalink
[IP Geolocation] Update Copy IP command
Browse files Browse the repository at this point in the history
  • Loading branch information
koinzhang committed May 27, 2024
1 parent c66dbdd commit eba23db
Show file tree
Hide file tree
Showing 24 changed files with 743 additions and 802 deletions.
5 changes: 5 additions & 0 deletions extensions/ip-geolocation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# IP Geolocation Changelog

## [Copy IP] - {PR_MERGE_DATE}

- Copy IP command allows you to choose whether to copy local/public IPv4/IPv6
- Optimise the style of extension icons

## [Fix] - 2023-11-22

- Fixing the IPv4 match
Expand Down
Binary file added extensions/ip-geolocation/assets/copy-ip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file modified extensions/ip-geolocation/assets/extension-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file modified extensions/ip-geolocation/assets/my-ip-geolocation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file modified extensions/ip-geolocation/assets/query-ip-geolocation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file modified extensions/ip-geolocation/metadata/ip-geolocation-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified extensions/ip-geolocation/metadata/ip-geolocation-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified extensions/ip-geolocation/metadata/ip-geolocation-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,298 changes: 570 additions & 728 deletions extensions/ip-geolocation/package-lock.json

Large diffs are not rendered by default.

76 changes: 53 additions & 23 deletions extensions/ip-geolocation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"Developer Tools",
"Web"
],
"keywords": [
"ip",
"my ip"
],
"license": "MIT",
"commands": [
{
Expand Down Expand Up @@ -41,18 +45,44 @@
"type": "checkbox",
"required": false,
"label": "Show IPv6 Address",
"description": "Query and display my IPv6 address. Can cause information loading speed to drop.",
"description": "Query and display IPv6 address. Can cause information loading speed to drop.",
"default": false
}
]
},
{
"name": "copy-local-ip-address",
"title": "Copy Local IP Address",
"icon": "copy-local-ip-address.png",
"description": "Copy local IPv4 address.",
"name": "copy-ip",
"title": "Copy IP",
"icon": "copy-ip.png",
"description": "Copy local/public IPv4/IPv6 address.",
"mode": "no-view",
"interval": "1h"
"interval": "1h",
"arguments": [
{
"name": "ipType",
"type": "dropdown",
"required": false,
"placeholder": "Local",
"data": [
{
"title": "Public",
"value": "Public"
}
]
},
{
"name": "ipVersion",
"type": "dropdown",
"required": false,
"placeholder": "IPv4",
"data": [
{
"title": "IPv6",
"value": "IPv6"
}
]
}
]
}
],
"preferences": [
Expand All @@ -63,7 +93,7 @@
"type": "dropdown",
"data": [
{
"title": "English (default)",
"title": "English",
"value": "en"
},
{
Expand Down Expand Up @@ -104,33 +134,33 @@
"description": "Coordinate format of location information display.",
"type": "dropdown",
"data": [
{
"title": "Latitude, Longitude (default)",
"value": "latLon"
},
{
"title": "Longitude, Latitude",
"value": "lonLat"
},
{
"title": "Latitude, Longitude",
"value": "latLon"
}
],
"required": false,
"default": "latLon"
"default": "lonLat"
}
],
"dependencies": {
"@raycast/api": "^1.57.1",
"axios": "^1.4.0",
"public-ip": "^6.0.1"
"@raycast/api": "^1.75.1",
"axios": "^1.7.2",
"public-ip": "^6.0.2"
},
"devDependencies": {
"@types/node": "~16.10.0",
"@types/react": "^17.0.28",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"prettier": "^2.5.1",
"typescript": "^4.4.3"
"@types/node": "~20.12.12",
"@types/react": "^18.3.3",
"@typescript-eslint/eslint-plugin": "^7.10.0",
"@typescript-eslint/parser": "^7.10.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"prettier": "^3.2.5",
"typescript": "^5.4.5"
},
"scripts": {
"build": "ray build -e dist",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Action, ActionPanel, Icon, openCommandPreferences } from "@raycast/api";
import { Action, Icon, openCommandPreferences } from "@raycast/api";

export function ActionOpenCommandPreferences() {
return (
<ActionPanel.Section>
<Action
icon={Icon.Gear}
title="Open Extension Preferences"
shortcut={{ modifiers: ["shift", "cmd"], key: "," }}
onAction={openCommandPreferences}
/>
</ActionPanel.Section>
<Action
icon={Icon.Gear}
title="Configure Command"
shortcut={{ modifiers: ["shift", "cmd"], key: "," }}
onAction={openCommandPreferences}
/>
);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Action, ActionPanel, Icon, openExtensionPreferences } from "@raycast/api";
import { Action, Icon, openExtensionPreferences } from "@raycast/api";

export function ActionOpenExtensionPreferences() {
return (
<ActionPanel.Section>
<Action
icon={Icon.Gear}
title="Open Extension Preferences"
shortcut={{ modifiers: ["shift", "cmd"], key: "," }}
onAction={openExtensionPreferences}
/>
</ActionPanel.Section>
<Action
icon={Icon.Gear}
title="Configure Extension"
shortcut={{ modifiers: ["shift", "cmd"], key: "." }}
onAction={openExtensionPreferences}
/>
);
}
6 changes: 5 additions & 1 deletion extensions/ip-geolocation/src/components/ip-empty-view.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ActionPanel, Icon, List } from "@raycast/api";
import React from "react";
import { ActionOpenExtensionPreferences } from "./action-open-extension-preferences";
import { ActionOpenCommandPreferences } from "./action-open-command-preferences";

export function IpEmptyView(props: { title: string }) {
const { title } = props;
Expand All @@ -10,7 +11,10 @@ export function IpEmptyView(props: { title: string }) {
icon={Icon.AirplaneTakeoff}
actions={
<ActionPanel>
<ActionOpenExtensionPreferences />
<ActionPanel.Section>
<ActionOpenCommandPreferences />
<ActionOpenExtensionPreferences />
</ActionPanel.Section>
</ActionPanel>
}
/>
Expand Down
62 changes: 62 additions & 0 deletions extensions/ip-geolocation/src/copy-ip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { getIPV4Address, getIPV6Address, isEmpty } from "./utils/common-utils";
import {
captureException,
Clipboard,
closeMainWindow,
environment,
LaunchProps,
LaunchType,
showHUD,
updateCommandMetadata,
} from "@raycast/api";
import { publicIpv4, publicIpv6 } from "public-ip";
import { CopyIpArguments, IpType, IpVersion } from "./types/preferences";

export default async (props: LaunchProps<{ arguments: CopyIpArguments }>) => {
const { ipType, ipVersion } = props.arguments;
const ipTypeString = ipType === IpType.PUBLIC ? IpType.PUBLIC : IpType.LOCAL;
const ipVersionString = ipVersion === IpVersion.IPV6 ? IpVersion.IPV6 : IpVersion.IPV4;
try {
await closeMainWindow();
let ip = "";
if (ipTypeString === IpType.LOCAL) {
let ip_;
if (ipVersionString === IpVersion.IPV4) {
ip_ = getIPV4Address();
} else {
ip_ = getIPV6Address();
}
ip = ip_ ? ip_ : "";
} else {
let ip_;
if (ipVersionString === IpVersion.IPV4) {
ip_ = await publicIpv4({ onlyHttps: true })
.then((ip) => ip)
.catch(() => "");
} else {
ip_ = await publicIpv6({ onlyHttps: true })
.then((ip) => ip)
.catch(() => "");
}
ip = ip_ ? ip_ : "";
}

if (environment.launchType === LaunchType.UserInitiated) {
if (!isEmpty(ip)) {
await Clipboard.copy(`${ip}`);
await showHUD(`🅿️ ${ipTypeString} ${ipVersionString} ${ip}`);
} else {
await showHUD(`🚨 Failed to get ${ipTypeString} ${ipVersionString} `);
}
}

if (ip) {
await updateCommandMetadata({ subtitle: ` ${ipTypeString} ${ipVersionString} ${ip}` });
} else {
await updateCommandMetadata({ subtitle: `IP Geolocation` });
}
} catch (e) {
captureException(e);
console.error(e);
}
};
17 changes: 0 additions & 17 deletions extensions/ip-geolocation/src/copy-local-ip-address.tsx

This file was deleted.

8 changes: 2 additions & 6 deletions extensions/ip-geolocation/src/hooks/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import { WORLD_TIME_API } from "../utils/constants";
import Style = Toast.Style;
import net from "net";

export const searchIpGeolocation = (
language: string,
searchContent: string,
coordinatesFormat: "latLon" | "lonLat"
) => {
export const useIpGeolocation = (language: string, searchContent: string, coordinatesFormat: "latLon" | "lonLat") => {
const [ipGeolocation, setIpGeolocation] = useState<[string, string][]>([]);
const [loading, setLoading] = useState<boolean>(false);

Expand Down Expand Up @@ -90,7 +86,7 @@ export const searchIpGeolocation = (
return { ipGeolocation: ipGeolocation, loading: loading };
};

export const searchMyIpGeolocation = (language: string, showIPv6: boolean, coordinatesFormat: "latLon" | "lonLat") => {
export const useMyIpGeolocation = (language: string, showIPv6: boolean, coordinatesFormat: "latLon" | "lonLat") => {
const [ipGeolocation, setIpGeolocation] = useState<[string, string][]>([]);
const [loading, setLoading] = useState<boolean>(true);

Expand Down
12 changes: 8 additions & 4 deletions extensions/ip-geolocation/src/my-ip-geolocation.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Action, ActionPanel, getPreferenceValues, List } from "@raycast/api";
import { ActionOpenCommandPreferences } from "./components/action-open-command-preferences";
import { IpEmptyView } from "./components/ip-empty-view";
import { searchMyIpGeolocation } from "./hooks/hooks";
import { useMyIpGeolocation } from "./hooks/hooks";
import { Preferences } from "./types/preferences";
import { isEmpty } from "./utils/common-utils";
import { myIpListIcons } from "./utils/constants";
import { ActionOpenExtensionPreferences } from "./components/action-open-extension-preferences";

export default function SearchIpGeolocation() {
const { language, showIPv6, coordinatesFormat } = getPreferenceValues<Preferences>();
const { ipGeolocation, loading } = searchMyIpGeolocation(language, showIPv6, coordinatesFormat);
const { ipGeolocation, loading } = useMyIpGeolocation(language, showIPv6, coordinatesFormat);

return (
<List isLoading={loading} searchBarPlaceholder={"My IP Geolocation"}>
Expand All @@ -28,11 +29,14 @@ export default function SearchIpGeolocation() {
title={`Copy All Info`}
content={JSON.stringify(Object.fromEntries(ipGeolocation), null, 2)}
/>
<ActionOpenCommandPreferences />
<ActionPanel.Section>
<ActionOpenCommandPreferences />
<ActionOpenExtensionPreferences />
</ActionPanel.Section>
</ActionPanel>
}
/>
)
),
)}
</List>
);
Expand Down
14 changes: 9 additions & 5 deletions extensions/ip-geolocation/src/query-ip-geolocation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Action, ActionPanel, getPreferenceValues, List } from "@raycast/api";
import { useState } from "react";
import { ActionOpenExtensionPreferences } from "./components/action-open-extension-preferences";
import { IpEmptyView } from "./components/ip-empty-view";
import { searchIpGeolocation } from "./hooks/hooks";
import { useIpGeolocation } from "./hooks/hooks";
import { Preferences } from "./types/preferences";
import { isEmpty } from "./utils/common-utils";
import { listIcons } from "./utils/constants";
Expand All @@ -14,8 +14,8 @@ interface IpArgument {
export default function QueryIpGeolocation(props: { arguments: IpArgument }) {
const { ipAddress } = props.arguments;
const { language, coordinatesFormat } = getPreferenceValues<Preferences>();
const [searchContent, setSearchContent] = useState<string>(ipAddress);
const { ipGeolocation, loading } = searchIpGeolocation(language, searchContent.trim(), coordinatesFormat);
const [searchContent, setSearchContent] = useState<string>(ipAddress ? ipAddress.trim() : "");
const { ipGeolocation, loading } = useIpGeolocation(language, searchContent, coordinatesFormat);

const emptyViewTitle = () => {
if (loading) {
Expand All @@ -32,7 +32,9 @@ export default function QueryIpGeolocation(props: { arguments: IpArgument }) {
isLoading={loading}
searchBarPlaceholder={"Query geolocation of IP or domain"}
searchText={searchContent}
onSearchTextChange={setSearchContent}
onSearchTextChange={(text) => {
setSearchContent(text.trim());
}}
throttle={true}
>
<IpEmptyView title={emptyViewTitle()} />
Expand All @@ -49,7 +51,9 @@ export default function QueryIpGeolocation(props: { arguments: IpArgument }) {
title={`Copy All Info`}
content={JSON.stringify(Object.fromEntries(ipGeolocation), null, 2)}
/>
<ActionOpenExtensionPreferences />
<ActionPanel.Section>
<ActionOpenExtensionPreferences />
</ActionPanel.Section>
</ActionPanel>
}
/>
Expand Down
15 changes: 15 additions & 0 deletions extensions/ip-geolocation/src/types/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,18 @@ export interface Preferences {
showIPv6: boolean;
coordinatesFormat: "latLon" | "lonLat";
}

export interface CopyIpArguments {
ipType: string;
ipVersion: string;
}

export enum IpType {
LOCAL = "Local",
PUBLIC = "Public",
}

export enum IpVersion {
IPV4 = "IPv4",
IPV6 = "IPv6",
}

0 comments on commit eba23db

Please sign in to comment.