Skip to content

Commit

Permalink
Extension: Disable share buttons when domain is blackisted
Browse files Browse the repository at this point in the history
  • Loading branch information
PopDaph committed Jan 2, 2025
1 parent 2bb57dc commit 91be050
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 9 deletions.
45 changes: 36 additions & 9 deletions extension/app/src/components/conversation/AttachFragment.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import type { LightWorkspaceType } from "@dust-tt/client";
import { Button, CameraIcon, DocumentPlusIcon } from "@dust-tt/sparkle";
import { InputBarContext } from "@extension/components/input_bar/InputBarContext";
import { useCurrentDomain } from "@extension/hooks/useCurrentDomain";
import type { FileUploaderService } from "@extension/hooks/useFileUploaderService";
import { useContext, useEffect } from "react";

type AttachFragmentProps = {
owner: LightWorkspaceType;
fileUploaderService: FileUploaderService;
isLoading: boolean;
};

export const AttachFragment = ({
owner,
fileUploaderService,
isLoading,
}: AttachFragmentProps) => {
// Blinking animation.
const { attachPageBlinking, setAttachPageBlinking } =
useContext(InputBarContext);

useEffect(() => {
let timer: NodeJS.Timeout;
if (attachPageBlinking) {
Expand All @@ -25,12 +29,23 @@ export const AttachFragment = ({
return () => clearTimeout(timer);
}, [attachPageBlinking]);

// Blacklisting logic to disable share buttons.
const currentDomain = useCurrentDomain();
const blacklistedDomains: string[] = owner.blacklistedDomains ?? [];
const isBlacklisted = blacklistedDomains.some((d) =>
currentDomain.endsWith(d)
);

return (
<>
<div className="block sm:hidden">
<Button
icon={DocumentPlusIcon}
tooltip="Attach text from page"
tooltip={
!isBlacklisted
? "Attach text from page"
: "Attachment disabled on this website"
}
variant="outline"
size="sm"
className={attachPageBlinking ? "animate-[bgblink_200ms_3]" : ""}
Expand All @@ -40,13 +55,17 @@ export const AttachFragment = ({
includeCapture: false,
})
}
disabled={isLoading}
disabled={isLoading || isBlacklisted}
/>
</div>
<div className="block sm:hidden">
<Button
icon={CameraIcon}
tooltip="Attach page screenshot"
tooltip={
!isBlacklisted
? "Attach page screenshot"
: "Attachment disabled on this website"
}
variant="outline"
size="sm"
onClick={() =>
Expand All @@ -55,14 +74,18 @@ export const AttachFragment = ({
includeCapture: true,
})
}
disabled={isLoading}
disabled={isLoading || isBlacklisted}
/>
</div>
<div className="hidden sm:block">
<Button
icon={DocumentPlusIcon}
label="Add page text"
tooltip="Attach text from page"
tooltip={
!isBlacklisted
? "Attach text from page"
: "Attachment disabled on this website"
}
variant="outline"
size="sm"
className={attachPageBlinking ? "animate-[bgblink_200ms_3]" : ""}
Expand All @@ -72,14 +95,18 @@ export const AttachFragment = ({
includeCapture: false,
})
}
disabled={isLoading}
disabled={isLoading || isBlacklisted}
/>
</div>
<div className="hidden sm:block">
<Button
icon={CameraIcon}
label="Add page screenshot"
tooltip="Attach page screenshot"
tooltip={
!isBlacklisted
? "Attach page screenshot"
: "Attachment disabled on this website"
}
variant="outline"
size="sm"
onClick={() =>
Expand All @@ -88,7 +115,7 @@ export const AttachFragment = ({
includeCapture: true,
})
}
disabled={isLoading}
disabled={isLoading || isBlacklisted}
/>
</div>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export const InputBarContainer = ({

<div className="flex items-center justify-end space-x-2 mt-2">
<AttachFragment
owner={owner}
fileUploaderService={fileUploaderService}
isLoading={isSubmitting}
/>
Expand Down
63 changes: 63 additions & 0 deletions extension/app/src/hooks/useCurrentDomain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { useEffect, useState } from "react";

export const useCurrentDomain = () => {
const [currentDomain, setCurrentDomain] = useState<string>("");

useEffect(() => {
// Function to update domain from tab.
const updateDomainFromTab = (tab: chrome.tabs.Tab) => {
if (tab?.url) {
try {
const url = new URL(tab.url);
if (url.protocol.startsWith("http")) {
setCurrentDomain(url.hostname);
}
} catch (e) {
console.error("Invalid URL:", e);
setCurrentDomain("");
}
}
};

// Update domain when active tab changes.
const handleTabActivated = (activeInfo: chrome.tabs.TabActiveInfo) => {
chrome.tabs.get(activeInfo.tabId, (tab) => {
updateDomainFromTab(tab);
});
};

// Update domain when tab URL changes.
const handleTabUpdated = (
tabId: number,
changeInfo: chrome.tabs.TabChangeInfo,
tab: chrome.tabs.Tab
) => {
if (changeInfo.status === "complete") {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]?.id === tabId) {
updateDomainFromTab(tab);
}
});
}
};

// Get initial domain.
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
updateDomainFromTab(tabs[0]);
}
});

// Add listeners.
chrome.tabs.onActivated.addListener(handleTabActivated);
chrome.tabs.onUpdated.addListener(handleTabUpdated);

// Cleanup listeners.
return () => {
chrome.tabs.onActivated.removeListener(handleTabActivated);
chrome.tabs.onUpdated.removeListener(handleTabUpdated);
};
}, []);

return currentDomain;
};

0 comments on commit 91be050

Please sign in to comment.