Skip to content

Commit

Permalink
discord id lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
harryob committed Jun 7, 2024
1 parent b4637e1 commit 9b4d907
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
7 changes: 6 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ export default function App(): React.ReactElement {
<div className="text-3xl underline text-center">[cmdb]</div>
{user && <div>{user.preferredUsername}</div>}
</div>

<div className="flex flex-col md:flex-row gap-3">
<LookupOption type="lookup">Lookup User</LookupOption>
<LookupOption type="ip">Lookup IP</LookupOption>
<LookupOption type="cid">Lookup CID</LookupOption>
<LookupOption type="discordId">Lookup Discord</LookupOption>
</div>

<div
Expand Down Expand Up @@ -88,7 +90,7 @@ export default function App(): React.ReactElement {
}

interface LookupProps extends PropsWithChildren {
type: "lookup" | "ip" | "cid";
type: "lookup" | "ip" | "cid" | "discordId";
}

const LookupOption = (props: LookupProps) => {
Expand Down Expand Up @@ -153,6 +155,9 @@ const LookupOption = (props: LookupProps) => {
className="md:w-11/12 md:h-11/12"
>
{type == "lookup" && <LookupMenu value={value} close={close} />}
{type == "discordId" && (
<LookupMenu discordId={value} close={close} />
)}
{type == "cid" && <CidLookup initialCid={value} close={close} />}
{type == "ip" && <IpLookup initialIp={value} close={close} />}
</Dialog>
Expand Down
36 changes: 27 additions & 9 deletions src/components/userLookup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ import { StickybanMatch } from "./stickybanMatch";
import { callApi } from "../helpers/api";

type ActiveLookupType = {
updateUser: (string?: string) => void;
updateUser: (_args: UpdateUserArguments) => void;
};

const ActiveLookupContext = createContext<ActiveLookupType | null>(null);

interface LookupMenuProps extends PropsWithChildren {
value?: string;
discordId?: string;
close?: () => void;
}

type UpdateUserArguments = {
userCkey?: string;
userDiscordId?: string;
};

export const LookupMenu: React.FC<LookupMenuProps> = (
props: LookupMenuProps
) => {
Expand All @@ -37,12 +43,16 @@ export const LookupMenu: React.FC<LookupMenuProps> = (
const [loading, setLoading] = useState(false);
const global = useContext(GlobalContext);

const { value, close } = props;
const { value, discordId, close } = props;

const updateUser = useCallback(
(override?: string) => {
(args: UpdateUserArguments) => {
const { userCkey, userDiscordId } = args;

setLoading(true);
callApi(`/User?ckey=${override}`).then((value) => {
callApi(
userCkey ? `/User?ckey=${userCkey}` : `/User?discordId=${userDiscordId}`
).then((value) => {
setLoading(false);
if (value.status == 404) {
global?.updateAndShowToast("Failed to find user.");
Expand All @@ -61,10 +71,15 @@ export const LookupMenu: React.FC<LookupMenuProps> = (
);

useEffect(() => {
if (discordId && !userData) {
updateUser({ userDiscordId: discordId });
return;
}
if (value && !userData) {
updateUser(value);
updateUser({ userCkey: value });
return;
}
}, [value, userData, updateUser]);
}, [value, userData, discordId, updateUser]);

return (
<ActiveLookupContext.Provider value={{ updateUser: updateUser }}>
Expand All @@ -73,7 +88,7 @@ export const LookupMenu: React.FC<LookupMenuProps> = (
className="flex flex-row justify-center gap-3"
onSubmit={(event) => {
event.preventDefault();
updateUser();
updateUser({});
}}
>
<label htmlFor="ckey">User: </label>
Expand Down Expand Up @@ -538,7 +553,7 @@ const AddNote = (props: { player: Player }) => {
}),
}).then((response) => {
response.text().then((response) => {
refetch?.updateUser(ckey);
refetch?.updateUser({ userCkey: ckey });
setAdding(false);
if (response) {
global?.updateAndShowToast(`Added note to ${ckey}.`);
Expand Down Expand Up @@ -716,7 +731,10 @@ const RichUser = (props: RichUserProps) => {
const lookup = useContext(ActiveLookupContext);

return (
<Link onClick={() => lookup?.updateUser(props.name)} className="px-1">
<Link
onClick={() => lookup?.updateUser({ userCkey: props.name })}
className="px-1"
>
{props.name}
</Link>
);
Expand Down

0 comments on commit 9b4d907

Please sign in to comment.