Skip to content

Commit

Permalink
view archived year command
Browse files Browse the repository at this point in the history
  • Loading branch information
Herobread committed Nov 4, 2024
1 parent 3957ceb commit 6fb5564
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/features/command/ViewArchiveCommand.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import NiceModal from "@ebay/nice-modal-react"
import {
convertUrlSegmentsToUrl,
extractUrlSegments,
} from "@src/features/files"
import { getSegmentType } from "@src/features/router/getSegmentType"
import useSmoothRouter from "@src/features/router/useSmoothRouter"
import CommandsDialog from "@src/features/shared/dialogs/CommandsDialog"
import ViewArchiveDialog from "@src/features/shared/dialogs/ViewArchiveDialog"
import { CommandItem } from "../../components/ui/command"

const COMMAND_KEYWORDS = [
"time",
"travel",
"archive",
"back",
"previous",
"go",
"history",
]

export default function ViewArchiveCommand() {
const currentUrl = window.location.toString()
const currentUrlSegments = extractUrlSegments(currentUrl)

if (
currentUrlSegments[0] &&
getSegmentType(currentUrlSegments[0]) === "archive"
) {
return <GoToPresentCommand />
}

return <GoToArchiveCommand />
}

function GoToPresentCommand() {
const { navigateToPage } = useSmoothRouter()

const currentUrl = window.location.toString()
const currentUrlSegments = extractUrlSegments(currentUrl)

const handleGoToPresent = () => {
currentUrlSegments.splice(0, 1)
navigateToPage(convertUrlSegmentsToUrl(currentUrlSegments))

NiceModal.hide(CommandsDialog)
}

return (
<CommandItem onSelect={handleGoToPresent} keywords={COMMAND_KEYWORDS}>
🕰️ View current folder in the present
</CommandItem>
)
}

function GoToArchiveCommand() {
const handleOpenArchive = () => {
NiceModal.hide(CommandsDialog)
NiceModal.show(ViewArchiveDialog)
}

return (
<CommandItem onSelect={handleOpenArchive} keywords={COMMAND_KEYWORDS}>
🕰️ View current folder in the past
</CommandItem>
)
}
2 changes: 2 additions & 0 deletions src/features/shared/dialogs/CommandsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ClearVersionTrackingDataCommand from "@src/features/command/ClearVersionT
import SaveQuickLinkCommand from "@src/features/command/SaveQuickLinkCommand"
import { ToggleEnhancePageCommand } from "@src/features/command/ToggleEnhancePageCommand"
import ToggleThemeCommand from "@src/features/command/ToggleThemeCommand"
import ViewArchiveCommand from "@src/features/command/ViewArchiveCommand"
import { getModuleEmoji } from "@src/features/contentEnhancers/emoji/modules"
import {
BASE_URL,
Expand Down Expand Up @@ -112,6 +113,7 @@ export default NiceModal.create(() => {
<ToggleEnhancePageCommand />
<ClearVersionTrackingDataCommand />
<ClearBlackListCommand />
<ViewArchiveCommand />
</CommandGroup>
<CommandGroup heading="Visited paths">
{commandsData &&
Expand Down
78 changes: 78 additions & 0 deletions src/features/shared/dialogs/ViewArchiveDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import NiceModal, { useModal } from "@ebay/nice-modal-react"
import { VisuallyHidden } from "@radix-ui/react-visually-hidden"
import {
CommandDialog,
CommandEmpty,
CommandInput,
CommandItem,
CommandList,
} from "@src/components/ui/command"
import { DialogDescription, DialogTitle } from "@src/components/ui/dialog"
import {
convertUrlSegmentsToUrl,
extractUrlSegments,
} from "@src/features/files"
import useSmoothRouter from "@src/features/router/useSmoothRouter"

function generateArchivedYears() {
const now = new Date()

const STUDRES_ARCHIVE_YEAR_START = 2009
const STUDRES_ARCHIVE_YEAR_END = now.getFullYear()

const result = []

for (
let current = STUDRES_ARCHIVE_YEAR_START;
current < STUDRES_ARCHIVE_YEAR_END;
current++
) {
result.push(current + "_" + (current + 1))
}

return result.reverse()
}

export default NiceModal.create(() => {
const { navigateToPage } = useSmoothRouter()
const modalHandler = useModal()

const archivedYears = generateArchivedYears()

const handleSelection = (archiveYear: string) => {
const currentUrl = window.location.toString()
const currentUrlSegments = extractUrlSegments(currentUrl)

currentUrlSegments.unshift(archiveYear)

navigateToPage(convertUrlSegmentsToUrl(currentUrlSegments))
modalHandler.hide()
}

return (
<CommandDialog handler={modalHandler}>
<VisuallyHidden>
<DialogTitle>
<DialogDescription>Command dialog</DialogDescription>
</DialogTitle>
</VisuallyHidden>
<CommandInput placeholder="Search archive year..." />
<CommandList>
<CommandEmpty>No results found.</CommandEmpty>
{archivedYears.map((year) => {
return (
<CommandItem
onSelect={() => {
handleSelection(year)
}}
key={year}
value={year}
>
{year}
</CommandItem>
)
})}
</CommandList>
</CommandDialog>
)
})

0 comments on commit 6fb5564

Please sign in to comment.