Skip to content

Commit f6d66f3

Browse files
committed
refactor: streamline imports and enhance shared utility functions
- Updated import paths for shared modules across various components to improve maintainability and consistency. - Introduced new utility functions for downloading work media and sharing work, enhancing functionality in the shared package. - Cleaned up existing imports and removed deprecated code to simplify the codebase and improve clarity.
1 parent 20ee6a8 commit f6d66f3

File tree

8 files changed

+78
-84
lines changed

8 files changed

+78
-84
lines changed

packages/admin/src/views/Contracts/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { SUPPORTED_CHAINS } from "@green-goods/shared/config/chains";
1+
import { SUPPORTED_CHAINS } from "@green-goods/shared/config";
22
import { useAdminStore } from "@green-goods/shared/stores";
3-
import { getNetworkContracts } from "@green-goods/shared/utils/contracts";
3+
import { getNetworkContracts } from "@green-goods/shared";
44
import { RiRefreshLine, RiSettings3Line, RiUploadLine } from "@remixicon/react";
55
import { useState } from "react";
66

packages/admin/src/views/Deployment/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { getChain } from "@green-goods/shared/config";
12
import { useDeploymentRegistry } from "@green-goods/shared/hooks";
23
import { useAdminStore } from "@green-goods/shared/stores";
3-
import { getChain } from "@green-goods/shared/utils/contracts";
44
import {
55
RiCodeBoxLine,
66
RiErrorWarningLine,

packages/client/src/components/Features/Garden/Assessments.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import { getTag } from "@green-goods/shared/utils";
22
import {
33
RiCalendarLine,
44
RiExternalLinkLine,
5-
RiPriceTag3Line,
6-
RiStackLine,
75
RiFileTextLine,
86
RiInformationLine,
7+
RiPriceTag3Line,
8+
RiStackLine,
99
} from "@remixicon/react";
1010
import { forwardRef, memo } from "react";
1111
import { useIntl } from "react-intl";
1212
import { Link } from "react-router-dom";
13-
import { Badge } from "../Communication";
14-
import { Card } from "../Cards";
15-
import { Carousel, CarouselContent, CarouselItem } from "../Display";
13+
import { Card } from "@/components/Cards";
14+
import { Badge } from "@/components/Communication";
15+
import { Carousel, CarouselContent, CarouselItem } from "@/components/Display";
1616

1717
interface GardenAssessmentsProps {
1818
assessments: GardenAssessment[];

packages/client/src/components/Features/Profile/Profile.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
RiWallet3Fill,
88
RiWhatsappLine,
99
} from "@remixicon/react";
10-
import { Badge } from "../Communication";
10+
import { Badge } from "@/components/Communication";
1111

1212
const ProfileDataTypes = {
1313
location: RiUserLocationFill,

packages/client/src/views/Home/Garden/Work.tsx

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { toastService } from "@green-goods/shared";
22
import { DEFAULT_CHAIN_ID } from "@green-goods/shared/config/blockchain";
33
import {
4+
queryKeys,
45
useActions,
56
useGardens,
67
useNavigateToTop,
@@ -24,9 +25,7 @@ import {
2425
type WorkData,
2526
} from "@green-goods/shared/utils/work/workActions";
2627
import {
27-
RiCheckFill,
2828
RiCheckLine,
29-
RiCloseFill,
3029
RiCloseLine,
3130
RiErrorWarningLine,
3231
RiLoader4Line,
@@ -39,7 +38,6 @@ import { useLocation, useOutletContext, useParams } from "react-router-dom";
3938
import { Button } from "@/components/Actions";
4039
import { WorkViewSkeleton } from "@/components/Features/Work";
4140
import { TopNav } from "@/components/Navigation";
42-
import { WorkCompleted } from "../../Garden/Completed";
4341
import WorkViewSection from "./WorkViewSection";
4442

4543
type GardenWorkProps = {};
@@ -265,6 +263,8 @@ export const GardenWork: React.FC<GardenWorkProps> = () => {
265263
context: "approval submission",
266264
suppressLogging: true,
267265
});
266+
// Navigate back after successful approval
267+
setTimeout(() => navigateToTop(`/home/${garden?.id ?? ""}`), 500);
268268
}
269269
if (type === "job:failed") {
270270
const failureMessage = intl.formatMessage({
@@ -653,75 +653,6 @@ export const GardenWork: React.FC<GardenWorkProps> = () => {
653653
</div>
654654
</div>
655655
)}
656-
{!workApprovalMutation.isIdle && (
657-
<div className="padded">
658-
<WorkCompleted
659-
garden={garden}
660-
status={workApprovalMutation.status}
661-
mutationData={workApprovalMutation.data}
662-
messages={{
663-
success: {
664-
header: intl.formatMessage(
665-
{
666-
id: "app.home.workApproval.header",
667-
defaultMessage: "You've {status} the work!",
668-
},
669-
{
670-
status: workApprovalMutation.variables?.draft.approved
671-
? intl
672-
.formatMessage({
673-
id: "app.home.workApproval.approved",
674-
defaultMessage: "Approved",
675-
})
676-
.toLocaleLowerCase()
677-
: intl
678-
.formatMessage({
679-
id: "app.home.workApproval.rejected",
680-
defaultMessage: "Rejected",
681-
})
682-
.toLocaleLowerCase(),
683-
}
684-
),
685-
variant: "success",
686-
title: `${
687-
workApprovalMutation.variables?.draft.approved
688-
? intl.formatMessage({
689-
id: "app.home.workApproval.approved",
690-
defaultMessage: "Approved",
691-
})
692-
: intl.formatMessage({
693-
id: "app.home.workApproval.rejected",
694-
defaultMessage: "Rejected",
695-
})
696-
}!`,
697-
body: intl.formatMessage(
698-
{
699-
id: "app.home.workApproval.body",
700-
defaultMessage: "You've {status} the work!<br/><br/>Excellent work!",
701-
},
702-
{
703-
status: workApprovalMutation.variables?.draft.approved
704-
? intl
705-
.formatMessage({
706-
id: "app.home.workApproval.approved",
707-
defaultMessage: "Approved",
708-
})
709-
.toLocaleLowerCase()
710-
: intl
711-
.formatMessage({
712-
id: "app.home.workApproval.rejected",
713-
defaultMessage: "Rejected",
714-
})
715-
.toLocaleLowerCase(),
716-
}
717-
),
718-
icon: workApprovalMutation.variables?.draft.approved ? RiCheckFill : RiCloseFill,
719-
spinner: false,
720-
},
721-
}}
722-
/>
723-
</div>
724-
)}
725656
</article>
726657
);
727658
};

packages/shared/src/utils/blockchain/contracts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import { type Abi, createPublicClient, http } from "viem";
99
import { arbitrum, baseSepolia, celo } from "viem/chains";
1010
import type { NetworkContracts } from "../../types/contracts";
11+
import { getChain as getChainFromConfig } from "../../config/chains";
1112

1213
// Re-export chain utilities from config
1314
export { getChain } from "../../config/chains";
@@ -100,8 +101,7 @@ export function getNetworkContracts(chainId: number): NetworkContracts {
100101
}
101102

102103
export function createClients(chainId: number) {
103-
const { getChain } = require("../config/chains");
104-
const chain = getChain(chainId);
104+
const chain = getChainFromConfig(chainId);
105105
const alchemyKey = import.meta.env.VITE_ALCHEMY_API_KEY || "demo";
106106

107107
let rpcUrl = "";

packages/shared/src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,4 @@ export {
162162
} from "./work/image-compression";
163163
export { convertJobsToWorks, fetchOfflineWorks } from "./work/offline";
164164
export type { WorkData } from "./work/workActions";
165-
export { downloadWorkData } from "./work/workActions";
165+
export { downloadWorkData, downloadWorkMedia, shareWork } from "./work/workActions";

packages/shared/src/utils/work/workActions.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,66 @@ export function downloadWorkData(work: WorkData): void {
4343
document.body.removeChild(link);
4444
URL.revokeObjectURL(url);
4545
}
46+
47+
/**
48+
* Downloads work media files as a zip or individually
49+
*/
50+
export async function downloadWorkMedia(work: WorkData): Promise<void> {
51+
if (!work.media || work.media.length === 0) return;
52+
53+
// For single file, download directly
54+
if (work.media.length === 1) {
55+
const url = work.media[0];
56+
const link = document.createElement("a");
57+
link.href = url;
58+
link.download = `work-${work.id}-media`;
59+
link.target = "_blank";
60+
document.body.appendChild(link);
61+
link.click();
62+
document.body.removeChild(link);
63+
return;
64+
}
65+
66+
// For multiple files, download each one
67+
for (let i = 0; i < work.media.length; i++) {
68+
const url = work.media[i];
69+
const link = document.createElement("a");
70+
link.href = url;
71+
link.download = `work-${work.id}-media-${i + 1}`;
72+
link.target = "_blank";
73+
document.body.appendChild(link);
74+
link.click();
75+
document.body.removeChild(link);
76+
// Small delay between downloads
77+
await new Promise((resolve) => setTimeout(resolve, 500));
78+
}
79+
}
80+
81+
/**
82+
* Shares work using the Web Share API or copies to clipboard
83+
*/
84+
export async function shareWork(work: WorkData): Promise<void> {
85+
const shareData = {
86+
title: work.title || `Work ${work.id}`,
87+
text: work.description || work.feedback || `Check out this work from garden ${work.gardenId}`,
88+
url: typeof window !== "undefined" ? window.location.href : "",
89+
};
90+
91+
// Try Web Share API first
92+
if (typeof navigator !== "undefined" && navigator.share) {
93+
try {
94+
await navigator.share(shareData);
95+
return;
96+
} catch (err) {
97+
// User cancelled or share failed, fall back to clipboard
98+
if ((err as Error).name === "AbortError") {
99+
return; // User cancelled
100+
}
101+
}
102+
}
103+
104+
// Fall back to copying URL to clipboard
105+
if (typeof navigator !== "undefined" && navigator.clipboard) {
106+
await navigator.clipboard.writeText(shareData.url);
107+
}
108+
}

0 commit comments

Comments
 (0)