Files
plane/apps/web/core/hooks/use-timezone.tsx
Prateek Shourya 9cfde896b3 [WEB-5134] refactor: update web ESLint configuration and refactor imports to use type imports (#7957)
* [WEB-5134] refactor: update `web` ESLint configuration and refactor imports to use type imports

- Enhanced ESLint configuration by adding new rules for import consistency and type imports.
- Refactored multiple files to replace regular imports with type imports for better clarity and performance.
- Ensured consistent use of type imports across the application to align with TypeScript best practices.

* refactor: standardize type imports across components

- Updated multiple files to replace regular imports with type imports for improved clarity and consistency.
- Ensured adherence to TypeScript best practices in the rich filters and issue layouts components.
2025-10-14 16:45:07 +05:30

81 lines
2.2 KiB
TypeScript

import useSWR from "swr";
import type { TTimezoneObject } from "@plane/types";
// services
import timezoneService from "@/services/timezone.service";
// group timezones by value
const groupTimezones = (timezones: TTimezoneObject[]): TTimezoneObject[] => {
const groupedMap = timezones.reduce((acc, timezone: TTimezoneObject) => {
const key = timezone.value;
if (!acc.has(key)) {
acc.set(key, {
utc_offset: timezone.utc_offset,
gmt_offset: timezone.gmt_offset,
value: timezone.value,
label: timezone.label,
});
} else {
const existing = acc.get(key);
existing.label = `${existing.label}, ${timezone.label}`;
}
return acc;
}, new Map());
return Array.from(groupedMap.values());
};
const useTimezone = () => {
// fetching the timezone from the server
const {
data: timezones,
isLoading: timezoneIsLoading,
error: timezonesError,
} = useSWR("TIMEZONES_LIST", () => timezoneService.fetch(), {
refreshInterval: 0,
});
// derived values
const isDisabled = timezoneIsLoading || timezonesError || !timezones;
const getTimeZoneLabel = (timezone: TTimezoneObject | undefined) => {
if (!timezone) return undefined;
return (
<div className="flex gap-1.5">
<span className="text-custom-text-400">{timezone.utc_offset}</span>
<span className="text-custom-text-200">{timezone.label}</span>
</div>
);
};
const options = [
...groupTimezones(timezones?.timezones || [])?.map((timezone) => ({
value: timezone.value,
query: `${timezone.value} ${timezone.label}, ${timezone.gmt_offset}, ${timezone.utc_offset}`,
content: getTimeZoneLabel(timezone),
})),
{
value: "UTC",
query: "utc, coordinated universal time",
content: "UTC",
},
{
value: "Universal",
query: "universal, coordinated universal time",
content: "Universal",
},
];
const selectedTimezone = (value: string | undefined) => options.find((option) => option.value === value)?.content;
return {
timezones: options,
isLoading: timezoneIsLoading,
error: timezonesError,
disabled: isDisabled,
selectedValue: selectedTimezone,
};
};
export default useTimezone;