Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

Add useUrlHash hook

Dave Earleye008811cc8c8
diff --git a/frontend/src/components/common/OrdersTable/index.tsx b/frontend/src/components/common/OrdersTable/index.tsx
index bb9d868f8..6aaef168e 100644
--- a/frontend/src/components/common/OrdersTable/index.tsx
+++ b/frontend/src/components/common/OrdersTable/index.tsx
@@ -14,7 +14,7 @@ import {
import {prettyDate, relativeDate} from "../../../utilites/dates.ts";
import {ViewOrderModal} from "../../modals/ViewOrderModal";
import {useDisclosure} from "@mantine/hooks";
-import {useEffect, useState} from "react";
+import {useState} from "react";
import {CancelOrderModal} from "../../modals/CancelOrderModal";
import {SendMessageModal} from "../../modals/SendMessageModal";
import {notifications} from "@mantine/notifications";
@@ -28,6 +28,7 @@ import {ShowForDesktop, ShowForMobile} from "../Responsive/ShowHideComponents.ts
import {useResendOrderConfirmation} from "../../../mutations/useResendOrderConfirmation.ts";
import {OrderStatusBadge} from "../OrderStatusBadge";
import {formatNumber} from "../../../utilites/helpers.ts";
+import {useUrlHash} from "../../../hooks/useUrlHash.ts";
interface OrdersTableProps {
event: Event,
@@ -42,17 +43,11 @@ export const OrdersTable = ({orders, event}: OrdersTableProps) => {
const [orderId, setOrderId] = useState<IdParam>();
const resendConfirmationMutation = useResendOrderConfirmation();
- useEffect(() => {
- if (window?.location.hash) {
- const match = window?.location.hash.match(/^#order-(\d+)$/);
-
- if (match && match[1]) {
- const orderId = match[1];
- setOrderId(orderId);
- viewModal.open();
- }
- }
- }, []);
+ useUrlHash(/^#order-(\d+)$/, (matches => {
+ const orderId = matches![1];
+ setOrderId(orderId);
+ viewModal.open();
+ }));
if (orders.length === 0) {
return <NoResultsSplash
diff --git a/frontend/src/components/layouts/Event/Event.module.scss b/frontend/src/components/layouts/Event/Event.module.scss
index 861bde1e5..7c3668359 100644
--- a/frontend/src/components/layouts/Event/Event.module.scss
+++ b/frontend/src/components/layouts/Event/Event.module.scss
@@ -41,7 +41,7 @@
transition: box-shadow 0.3s ease;
position: sticky;
top: 0;
- box-shadow: 0 5px 5px 0 #00000010;
+ box-shadow: 0 5px 5px 0 #00000005;
z-index: 1;
.breadcrumbs {
diff --git a/frontend/src/components/routes/event/GettingStarted/index.tsx b/frontend/src/components/routes/event/GettingStarted/index.tsx
index c63fa3145..adb538f6a 100644
--- a/frontend/src/components/routes/event/GettingStarted/index.tsx
+++ b/frontend/src/components/routes/event/GettingStarted/index.tsx
@@ -69,7 +69,7 @@ const GettingStarted = () => {
{t`Create tickets for your event, set prices, and manage available quantity.`}
</p>
- <Button variant={'light'} component={'a'} href={'/manage/event/' + eventId + '/tickets'}>
+ <Button variant={'light'} component={'a'} href={'/manage/event/' + eventId + '/tickets#create-ticket'}>
{hasTickets ? t`Add More tickets` : t`Add tickets`}
</Button>
diff --git a/frontend/src/components/routes/event/tickets.tsx b/frontend/src/components/routes/event/tickets.tsx
index b414e09cf..7767111ec 100644
--- a/frontend/src/components/routes/event/tickets.tsx
+++ b/frontend/src/components/routes/event/tickets.tsx
@@ -14,6 +14,7 @@ import {useGetTickets} from "../../../queries/useGetTickets.ts";
import {TableSkeleton} from "../../common/TableSkeleton";
import {Pagination} from "../../common/Pagination";
import {t} from "@lingui/macro";
+import {useUrlHash} from "../../../hooks/useUrlHash.ts";
export const Tickets = () => {
const [searchParams, setSearchParams] = useFilterQueryParamSync();
@@ -23,12 +24,14 @@ export const Tickets = () => {
const pagination = ticketsQuery?.data?.meta;
const tickets = ticketsQuery?.data?.data;
const enableSorting =
- (Object.keys(searchParams).length === 0) || // searchParams is an empty object
+ (Object.keys(searchParams).length === 0) ||
(
- (searchParams.sortBy === 'order' || searchParams.sortBy === undefined) && // orderBy is 'order' or undefined
- (searchParams.query === '' || searchParams.query === undefined) // query is an empty string or undefined
+ (searchParams.sortBy === 'order' || searchParams.sortBy === undefined) &&
+ (searchParams.query === '' || searchParams.query === undefined)
);
+ useUrlHash('create-ticket', () => openCreateModal());
+
return (
<PageBody>
<PageTitle>{t`Tickets`}</PageTitle>
diff --git a/frontend/src/hooks/useUrlHash.ts b/frontend/src/hooks/useUrlHash.ts
new file mode 100644
index 000000000..480282035
--- /dev/null
+++ b/frontend/src/hooks/useUrlHash.ts
@@ -0,0 +1,31 @@
+import { useEffect } from 'react';
+
+type CallbackFunction = (matches?: RegExpMatchArray) => void;
+
+export const useUrlHash = (patternOrString: RegExp | string, callback: CallbackFunction): void => {
+ useEffect(() => {
+ const checkHash = (): void => {
+ const { hash, pathname, search } = window.location;
+
+ if (hash) {
+ if (typeof patternOrString === 'string' && hash === `#${patternOrString}`) {
+ callback();
+ window.history.replaceState(null, '', pathname + search);
+ } else if (patternOrString instanceof RegExp) {
+ const match = hash.match(patternOrString);
+ if (match) {
+ callback(match);
+ window.history.replaceState(null, '', pathname + search);
+ }
+ }
+ }
+ };
+
+ checkHash();
+ window.addEventListener('hashchange', checkHash);
+
+ return () => {
+ window.removeEventListener('hashchange', checkHash);
+ };
+ }, [patternOrString, callback]);
+};