Skip to content

Commit

Permalink
telemetry: generate and preserve anonymous user ID if no token is pre…
Browse files Browse the repository at this point in the history
…sent
  • Loading branch information
bobheadxi committed Dec 6, 2024
1 parent 6b2fe8c commit 39b165d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 13 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,14 @@
"@raycast/api": "^1.86.1",
"@raycast/utils": "^1.18.1",
"@sourcegraph/telemetry": "^0.18.0",
"@types/uuid": "^10.0.0",
"cross-fetch": "^4.0.0",
"eventsource": "^2.0.2",
"graphql": "^16.9.0",
"luxon": "^3.5.0",
"nanoid": "^5.0.9",
"node-fetch": "^3.3.2"
"node-fetch": "^3.3.2",
"uuid": "^11.0.3"
},
"devDependencies": {
"@graphql-codegen/cli": "^5.0.3",
Expand Down Expand Up @@ -219,4 +221,4 @@
"gql": "graphql-codegen --config graphql-codegen.yml && npm run fmt",
"postinstall": "patch-package"
}
}
}
30 changes: 26 additions & 4 deletions src/components/DotComCommand.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LaunchProps } from "@raycast/api";
import { useEffect } from "react";
import { Detail, LaunchProps } from "@raycast/api";
import { useEffect, useState } from "react";

import checkAuthEffect from "../hooks/checkAuthEffect";
import { Sourcegraph, sourcegraphDotCom } from "../sourcegraph";
Expand All @@ -14,9 +14,31 @@ export default function DotComCommand({
Command: React.FunctionComponent<{ src: Sourcegraph; props?: LaunchProps }>;
props?: LaunchProps;
}) {
const src = sourcegraphDotCom();
const [src, setSrc] = useState<Sourcegraph>();
useEffect(() => {
async function loadSrc() {
setSrc(await sourcegraphDotCom());
}
loadSrc();
}, []);

useEffect(checkAuthEffect(src), []);
if (!src) {
return <Detail isLoading={true} />;
}
return <CheckAuthCommand src={src} props={props} Command={Command} />;
}

// Inner wrapper for the command to deal with using checkAuthEffect while src
// requires async to initialize.
function CheckAuthCommand({
src,
Command,
props,
}: {
src: Sourcegraph;
props?: LaunchProps;
Command: React.FunctionComponent<{ src: Sourcegraph; props?: LaunchProps }>;
}) {
useEffect(checkAuthEffect(src), []);
return <Command src={src} props={props} />;
}
2 changes: 1 addition & 1 deletion src/findNotebooksInstance.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import FindNotebooksCommand from "./components/FindNotebooksCommand";
import InstanceCommand from "./components/InstanceCommand";

export default function FindNotebooksSelfHosted() {
export default async function FindNotebooksSelfHosted() {
return <InstanceCommand Command={FindNotebooksCommand} />;
}
18 changes: 14 additions & 4 deletions src/sourcegraph/gql/apollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ import { setContext } from "@apollo/client/link/context";
import operations from "./operations";
import { getProxiedFetch } from "./fetchProxy";

export function newApolloClient(connect: { instance: string; token?: string; proxy?: string }) {
export function newApolloClient(connect: {
instance: string;
token?: string;
proxy?: string;
anonymousUserID?: string;
}) {
const headers: Record<string, string> = {
"X-Requested-With": "Raycast-Sourcegraph",
};
if (connect.anonymousUserID) {
headers["X-Sourcegraph-Actor-Anonymous-UID"] = connect.anonymousUserID;
}

const httpLink = createHttpLink({
uri: `${connect.instance}/.api/graphql`,
headers: {
"X-Requested-With": "Raycast-Sourcegraph",
},
headers,
fetch: getProxiedFetch(connect.proxy) as unknown as WindowOrWorkerGlobalScope["fetch"],
});

Expand Down
17 changes: 15 additions & 2 deletions src/sourcegraph/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getPreferenceValues, LocalStorage } from "@raycast/api";
import { ApolloClient, NormalizedCacheObject } from "@apollo/client";
import { getPreferenceValues } from "@raycast/api";
import { v4 as uuidv4 } from "uuid";
import { newApolloClient } from "./gql/apollo";

export interface Sourcegraph {
Expand Down Expand Up @@ -50,12 +51,24 @@ export function instanceName(src: Sourcegraph) {
/**
* sourcegraphDotCom returns the user's configuration for connecting to Sourcegraph.com.
*/
export function sourcegraphDotCom(): Sourcegraph {
export async function sourcegraphDotCom(): Promise<Sourcegraph> {
const prefs = getPreferenceValues<Preferences>();
const searchPrefs = getPreferenceValues<Preferences.SearchDotCom>();

// If there is no token, generate a persisted anonymous identifier for the user.
let anonymousUserID = "";
if (!prefs.cloudToken) {
anonymousUserID = (await LocalStorage.getItem("anonymous-user-id")) as string;
if (!anonymousUserID) {
anonymousUserID = uuidv4();
await LocalStorage.setItem("anonymous-user-id", anonymousUserID);
}
}

const connect = {
instance: dotComURL,
token: prefs.cloudToken,
anonymousUserID,
};
return {
...connect,
Expand Down

0 comments on commit 39b165d

Please sign in to comment.