-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add apperance settings with time format
- Loading branch information
1 parent
7dad659
commit c6338f8
Showing
17 changed files
with
201 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useContextSelector } from "use-context-selector"; | ||
|
||
import { useFormatDate, useTheme, useTranslation } from "@/shared/lib"; | ||
import { TimeFormatSettingsContext } from "@/shared/lib/providers/TimeFormatProvider"; | ||
import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue, Separator } from "@/shared/ui"; | ||
|
||
function AppearanceSettings() { | ||
const t = useTranslation(); | ||
const { theme, setTheme } = useTheme(); | ||
const { timeFormat, setTimeFormat } = useContextSelector(TimeFormatSettingsContext, (c) => c); | ||
const { formatFullDate } = useFormatDate(); | ||
const exampleTime: Date = new Date(2025, 11, 19, 13, 27); | ||
|
||
function handleThemeChange(value: string) { | ||
if (value == "dark" || value == "light") setTheme(value); | ||
} | ||
|
||
function handleTimeFormatChange(value: string) { | ||
if (value == "12-hour" || value == "24-hour") setTimeFormat(value); | ||
} | ||
|
||
return ( | ||
<div className="flex flex-col gap-3.5"> | ||
<span className="text-3xl font-semibold">{t("APPEARANCE")}</span> | ||
<Separator /> | ||
<div className="flex flex-row gap-4 max-w-[48rem]"> | ||
<div className="w-full flex flex-col gap-1.5"> | ||
<span className="text-sm font-medium">{t("THEME")}</span> | ||
<Select value={theme} onValueChange={handleThemeChange}> | ||
<SelectTrigger> | ||
<SelectValue placeholder={t("DEFAULT")} /> | ||
</SelectTrigger> | ||
<SelectContent> | ||
<SelectGroup> | ||
<SelectLabel>{t("THEME")}</SelectLabel> | ||
<SelectItem value="light">{t("LIGHT")}</SelectItem> | ||
<SelectItem value="dark">{t("DARK")}</SelectItem> | ||
</SelectGroup> | ||
</SelectContent> | ||
</Select> | ||
</div> | ||
<div className="w-full flex flex-col gap-1.5"> | ||
<span className="text-sm font-medium">{t("TIME_FORMAT")}</span> | ||
<Select value={timeFormat} onValueChange={handleTimeFormatChange}> | ||
<SelectTrigger> | ||
<SelectValue placeholder={t("DEFAULT")} /> | ||
</SelectTrigger> | ||
<SelectContent> | ||
<SelectGroup> | ||
<SelectLabel>{t("TIME_FORMAT")}</SelectLabel> | ||
<SelectItem value="12-hour">{t("12_HOUR")}</SelectItem> | ||
<SelectItem value="24-hour">{t("24_HOUR")}</SelectItem> | ||
</SelectGroup> | ||
</SelectContent> | ||
</Select> | ||
<span className="text-slate-500 text-sm"> | ||
<span className="font-semibold">{t("EXAMPLE")}</span>: {formatFullDate(exampleTime)} | ||
</span> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default AppearanceSettings; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import "moment/min/locales"; | ||
|
||
import Moment from "moment/min/moment-with-locales"; | ||
import { useContextSelector } from "use-context-selector"; | ||
|
||
import { TimeFormatSettingsContext } from "../providers/TimeFormatProvider"; | ||
|
||
export const useFormatDate = (): { | ||
formatDateShortened: (date: Date) => string; | ||
formatFullDate: (date: Date) => string; | ||
} => { | ||
const { timeFormat } = useContextSelector(TimeFormatSettingsContext, (c) => c); | ||
|
||
const formatDateShortened = (date: Date): string => { | ||
const now = Moment(); | ||
const givenDate = Moment(date); | ||
const format = timeFormat === "12-hour" ? "h:mm A" : "HH:mm"; | ||
|
||
if (givenDate.isSame(now, "day")) { | ||
return givenDate.format(format); | ||
} | ||
|
||
if (givenDate.isSame(now.clone().add(1, "day"), "day")) { | ||
return givenDate.format(`MMM D, ${format}`); | ||
} | ||
|
||
return givenDate.format(`MMM D, ${format}`); | ||
}; | ||
|
||
const formatFullDate = (date: Date): string => { | ||
const givenDate = Moment(date); | ||
const format = timeFormat === "12-hour" ? "h:mm A" : "HH:mm"; | ||
return givenDate.format(`dddd, MMMM D, YYYY ${format}`); | ||
}; | ||
|
||
return { formatDateShortened, formatFullDate }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useEffect, useState } from "react"; | ||
import { createContext } from "use-context-selector"; | ||
|
||
import { useLocalStorage } from "../hooks/useLocalStorage"; | ||
|
||
type TimeFormat = "12-hour" | "24-hour"; | ||
|
||
export const TimeFormatSettingsContext = createContext<{ | ||
timeFormat: TimeFormat; | ||
setTimeFormat: (format: TimeFormat) => void; | ||
}>({ | ||
timeFormat: "12-hour", | ||
setTimeFormat: () => {} | ||
}); | ||
|
||
export const TimeFormatSettingsProvider = ({ children }) => { | ||
const { getFromLocalStorage, setToLocalStorage } = useLocalStorage(); | ||
const [timeFormat, setTimeFormat] = useState<TimeFormat>(getFromLocalStorage("timeFormat") ?? "12-hour"); | ||
|
||
function saveAllToLocalStorage() { | ||
setToLocalStorage("timeFormat", timeFormat); | ||
} | ||
|
||
useEffect(() => { | ||
saveAllToLocalStorage(); | ||
}, [timeFormat]); | ||
|
||
return ( | ||
<TimeFormatSettingsContext.Provider | ||
value={{ | ||
timeFormat: timeFormat, | ||
setTimeFormat: setTimeFormat | ||
}} | ||
> | ||
{children} | ||
</TimeFormatSettingsContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.