Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

feat: get customer/supplier avatar based on website

Brad Barbin5fa03a5f67dd
diff --git a/apps/erp/app/components/Avatar.tsx b/apps/erp/app/components/Avatar.tsx
index 2ebc4a9b7b..e6affe1a13 100644
--- a/apps/erp/app/components/Avatar.tsx
+++ b/apps/erp/app/components/Avatar.tsx
@@ -6,13 +6,18 @@ import { getStoragePath } from "~/utils/path";
type AvatarProps = AvatarBaseProps & {
path?: string | null;
bucket?: string;
+ imageUrl?: string | null;
};
const Avatar = forwardRef<HTMLSpanElement, AvatarProps>(
({ name, path, bucket = "avatars", ...props }, ref) => {
- const imagePath = path ? getStoragePath(bucket, path) : undefined;
+ const imageUrl = props.imageUrl
+ ? props.imageUrl
+ : path
+ ? getStoragePath(bucket, path)
+ : undefined;
- return <AvatarBase src={imagePath} name={name} ref={ref} {...props} />;
+ return <AvatarBase src={imageUrl} name={name} ref={ref} {...props} />;
}
);
Avatar.displayName = "Avatar";
diff --git a/apps/erp/app/components/Chat/Favicon.tsx b/apps/erp/app/components/Chat/Favicon.tsx
index c94854ce69..47420a5f88 100644
--- a/apps/erp/app/components/Chat/Favicon.tsx
+++ b/apps/erp/app/components/Chat/Favicon.tsx
@@ -1,3 +1,4 @@
+import { getFaviconUrl } from "@carbon/utils";
import { AnimatePresence, motion } from "framer-motion";
import { LuBook } from "react-icons/lu";
@@ -10,18 +11,6 @@ interface FaviconStackProps {
sources: SourceItem[];
}
-/**
- * Get favicon URL for a given website URL
- */
-function getFaviconUrl(url: string): string {
- try {
- const domain = new URL(url).hostname;
- return `https://www.google.com/s2/favicons?domain=${domain}&sz=32`;
- } catch {
- return "";
- }
-}
-
export function FaviconStack({ sources }: FaviconStackProps) {
if (sources.length === 0) return null;
diff --git a/apps/erp/app/components/CustomerAvatar.tsx b/apps/erp/app/components/CustomerAvatar.tsx
index bafed9bd62..187af5a464 100644
--- a/apps/erp/app/components/CustomerAvatar.tsx
+++ b/apps/erp/app/components/CustomerAvatar.tsx
@@ -1,5 +1,6 @@
import type { AvatarProps } from "@carbon/react";
import { HStack } from "@carbon/react";
+import { getFaviconUrl, isUrl } from "@carbon/utils";
import { useCustomers } from "~/stores";
import Avatar from "./Avatar";
@@ -20,12 +21,23 @@ const CustomerAvatar = ({
const customer = customers.find((s) => s.id === customerId) ?? {
name: "",
- id: ""
+ id: "",
+ website: null
};
+ const imageUrl =
+ customer.website && isUrl(customer.website)
+ ? getFaviconUrl(customer.website)
+ : undefined;
+
return (
<HStack className="truncate no-underline hover:no-underline">
- <Avatar size={size ?? "xs"} {...props} name={customer?.name ?? ""} />
+ <Avatar
+ size={size ?? "xs"}
+ {...props}
+ name={customer?.name ?? ""}
+ imageUrl={imageUrl}
+ />
<span className={className}>{customer.name}</span>
</HStack>
);
diff --git a/apps/erp/app/components/RealtimeDataProvider.tsx b/apps/erp/app/components/RealtimeDataProvider.tsx
index 9f2b0fe2ef..0974c28536 100644
--- a/apps/erp/app/components/RealtimeDataProvider.tsx
+++ b/apps/erp/app/components/RealtimeDataProvider.tsx
@@ -73,13 +73,15 @@ const RealtimeDataProvider = ({ children }: { children: React.ReactNode }) => {
fetchAllFromTable<{
id: string;
name: string;
- }>(carbon, "supplier", "id, name", (query) =>
+ website: string;
+ }>(carbon, "supplier", "id, name, website", (query) =>
query.eq("companyId", companyId).order("name")
),
fetchAllFromTable<{
id: string;
name: string;
- }>(carbon, "customer", "id, name", (query) =>
+ website: string;
+ }>(carbon, "customer", "id, name, website", (query) =>
query.eq("companyId", companyId).order("name")
),
fetchAllFromTable<{
@@ -229,7 +231,8 @@ const RealtimeDataProvider = ({ children }: { children: React.ReactNode }) => {
...customers,
{
id: inserted.id,
- name: inserted.name
+ name: inserted.name,
+ website: inserted.website
}
].sort((a, b) => a.name.localeCompare(b.name))
);
@@ -242,7 +245,8 @@ const RealtimeDataProvider = ({ children }: { children: React.ReactNode }) => {
if (p.id === updated.id) {
return {
...p,
- name: updated.name
+ name: updated.name,
+ website: updated.website
};
}
return p;
@@ -283,7 +287,8 @@ const RealtimeDataProvider = ({ children }: { children: React.ReactNode }) => {
...suppliers,
{
id: inserted.id,
- name: inserted.name
+ name: inserted.name,
+ website: inserted.website
}
].sort((a, b) => a.name.localeCompare(b.name))
);
@@ -296,7 +301,8 @@ const RealtimeDataProvider = ({ children }: { children: React.ReactNode }) => {
if (p.id === updated.id) {
return {
...p,
- name: updated.name
+ name: updated.name,
+ website: updated.website
};
}
return p;
diff --git a/apps/erp/app/components/SupplierAvatar.tsx b/apps/erp/app/components/SupplierAvatar.tsx
index 2e9d2b5a87..65cd28ef02 100644
--- a/apps/erp/app/components/SupplierAvatar.tsx
+++ b/apps/erp/app/components/SupplierAvatar.tsx
@@ -1,5 +1,6 @@
import type { AvatarProps } from "@carbon/react";
import { HStack } from "@carbon/react";
+import { getFaviconUrl } from "@carbon/utils";
import { useSuppliers } from "~/stores";
import Avatar from "./Avatar";
@@ -20,12 +21,22 @@ const SupplierAvatar = ({
const supplier = suppliers.find((s) => s.id === supplierId) ?? {
name: "",
- id: ""
+ id: "",
+ website: null
};
+ const imageUrl = supplier.website
+ ? getFaviconUrl(supplier.website)
+ : undefined;
+
return (
<HStack className="truncate ">
- <Avatar size={size ?? "xs"} {...props} name={supplier?.name ?? ""} />
+ <Avatar
+ size={size ?? "xs"}
+ {...props}
+ name={supplier?.name ?? ""}
+ imageUrl={imageUrl}
+ />
<span className={className}>{supplier.name}</span>
</HStack>
);
diff --git a/apps/erp/app/modules/purchasing/purchasing.models.ts b/apps/erp/app/modules/purchasing/purchasing.models.ts
index 836a9a0fe3..dbe956cb63 100644
--- a/apps/erp/app/modules/purchasing/purchasing.models.ts
+++ b/apps/erp/app/modules/purchasing/purchasing.models.ts
@@ -267,7 +267,7 @@ export const supplierValidator = z.object({
currencyCode: zfd.text(z.string().optional()),
purchasingContactId: zfd.text(z.string().optional()),
invoicingContactId: zfd.text(z.string().optional()),
- website: zfd.text(z.string().optional())
+ website: zfd.text(z.string().url().optional())
});
export const supplierContactValidator = z.object({
diff --git a/apps/erp/app/modules/sales/sales.models.ts b/apps/erp/app/modules/sales/sales.models.ts
index a65823b925..d8815b4f51 100644
--- a/apps/erp/app/modules/sales/sales.models.ts
+++ b/apps/erp/app/modules/sales/sales.models.ts
@@ -74,7 +74,7 @@ export const customerValidator = z.object({
),
salesContactId: zfd.text(z.string().optional()),
invoicingContactId: zfd.text(z.string().optional()),
- website: zfd.text(z.string().optional())
+ website: zfd.text(z.string().url().optional())
});
export const customerPaymentValidator = z.object({
diff --git a/apps/erp/app/stores/customers.ts b/apps/erp/app/stores/customers.ts
index 3cba9f97c3..38d80bbe0b 100644
--- a/apps/erp/app/stores/customers.ts
+++ b/apps/erp/app/stores/customers.ts
@@ -2,5 +2,5 @@ import { atom } from "nanostores";
import { useNanoStore } from "~/hooks";
import type { ListItem } from "~/types";
-const $customersStore = atom<ListItem[]>([]);
+const $customersStore = atom<(ListItem & { website?: string | null })[]>([]);
export const useCustomers = () => useNanoStore($customersStore, "customers");
diff --git a/apps/erp/app/stores/suppliers.ts b/apps/erp/app/stores/suppliers.ts
index f25c1ad281..3f8d3b3544 100644
--- a/apps/erp/app/stores/suppliers.ts
+++ b/apps/erp/app/stores/suppliers.ts
@@ -2,5 +2,5 @@ import { atom } from "nanostores";
import { useNanoStore } from "~/hooks";
import type { ListItem } from "~/types";
-const $suppliersStore = atom<ListItem[]>([]);
+const $suppliersStore = atom<(ListItem & { website?: string | null })[]>([]);
export const useSuppliers = () => useNanoStore($suppliersStore, "suppliers");
diff --git a/packages/utils/src/url.ts b/packages/utils/src/url.ts
index c6fd283f1d..a43a9d265c 100644
--- a/packages/utils/src/url.ts
+++ b/packages/utils/src/url.ts
@@ -9,3 +9,15 @@ export const isUrl = (str: string) => {
return url.protocol === "http:" || url.protocol === "https:";
};
+
+/**
+ * Get favicon URL for a given website URL
+ */
+export function getFaviconUrl(url: string): string {
+ try {
+ const domain = new URL(url).hostname;
+ return `https://www.google.com/s2/favicons?domain=${domain}&sz=32`;
+ } catch {
+ return "";
+ }
+}