-
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: implement feature to rename items
- Loading branch information
Showing
6 changed files
with
248 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,68 @@ | ||
import { FaRandom } from "react-icons/fa" | ||
import { Button } from "@/components/ui/button" | ||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog" | ||
import { Input } from "@/components/ui/input" | ||
import { Label } from "@/components/ui/label" | ||
import { ReactNode, useState } from "react" | ||
export function useRenameDialog( | ||
callback: (newValue: string) => void, | ||
): [ReactNode, (defaultValue: string) => void, boolean] { | ||
import { ReactNode, useRef, useState } from "react" | ||
import generateRandomName from "@/lib/names" | ||
export function useRenameDialog(): [ReactNode, (defaultValue: string) => Promise<string | undefined>] { | ||
const [renameDialogOpen, setRenameDialogOpen] = useState(false) | ||
const [originTabName, setOriginTabName] = useState("") | ||
const [targetTabName, setTargetName] = useState("") | ||
function showDialog(defaultValue: string) { | ||
setOriginTabName(defaultValue) | ||
const [currentValue, setCurrentValue] = useState("") | ||
const callback = useRef<(value: string | undefined) => void>() | ||
|
||
function showDialog(defaultValue: string): Promise<string | undefined> { | ||
setCurrentValue(defaultValue) | ||
setRenameDialogOpen(true) | ||
return new Promise((resolve) => { | ||
callback.current = resolve | ||
}) | ||
} | ||
|
||
function cancel() { | ||
setRenameDialogOpen(false) | ||
if (callback.current) { | ||
callback.current(undefined) | ||
callback.current = undefined | ||
} | ||
} | ||
function confirm() { | ||
setRenameDialogOpen(false) | ||
if (callback.current) { | ||
callback.current(currentValue) | ||
callback.current = undefined | ||
} | ||
} | ||
function randomName() { | ||
setCurrentValue(generateRandomName("'s code")) | ||
} | ||
|
||
let element = ( | ||
<Dialog open={renameDialogOpen} onOpenChange={setRenameDialogOpen}> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Rename {originTabName}</DialogTitle> | ||
<DialogTitle>Rename File</DialogTitle> | ||
</DialogHeader> | ||
<div className="grid flex-1 gap-2"> | ||
<Label htmlFor="name" className="sr-only"> | ||
New Name | ||
</Label> | ||
<Input defaultValue={originTabName} onChange={(e) => setTargetName(e.target.value)} /> | ||
<div className="flex gap-1"> | ||
<Input className="flex-1" value={currentValue} onChange={(e) => setCurrentValue(e.target.value)} /> | ||
<Button size="icon" variant="ghost" onClick={randomName}> | ||
<FaRandom /> | ||
</Button> | ||
</div> | ||
<span className="text-end"> | ||
<Button className="m-2" variant="outline" onClick={() => setRenameDialogOpen(false)}> | ||
<Button className="m-2" variant="outline" onClick={cancel}> | ||
Cancel | ||
</Button> | ||
<Button | ||
className="m-2" | ||
onClick={() => { | ||
callback(targetTabName) | ||
setRenameDialogOpen(false) | ||
}} | ||
> | ||
<Button className="m-2" onClick={confirm}> | ||
Rename | ||
</Button> | ||
</span> | ||
</div> | ||
</DialogContent> | ||
</Dialog> | ||
) | ||
return [element, showDialog, renameDialogOpen] | ||
return [element, showDialog] | ||
} |
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,146 @@ | ||
import { capitalize, random } from "lodash/fp" | ||
|
||
export default function generateRandomName(suffix?: string){ | ||
const adj = random(0, adjectives.length) | ||
const n = random(0, nouns.length) | ||
return capitalize(`${adjectives[adj]} ${nouns[n]}${suffix}`.trim()) | ||
} | ||
|
||
|
||
// Cop from https://github.com/zellij-org/zellij/blob/65a7fcf426e6131fe68b300fb746271419a08865/src/sessions.rs#L426-L560 | ||
// Thanks for the organization's fantastic work | ||
const adjectives = [ | ||
"adamant", | ||
"adept", | ||
"adventurous", | ||
"arcadian", | ||
"auspicious", | ||
"awesome", | ||
"blossoming", | ||
"brave", | ||
"charming", | ||
"chatty", | ||
"circular", | ||
"considerate", | ||
"cubic", | ||
"curious", | ||
"delighted", | ||
"didactic", | ||
"diligent", | ||
"effulgent", | ||
"erudite", | ||
"excellent", | ||
"exquisite", | ||
"fabulous", | ||
"fascinating", | ||
"friendly", | ||
"glowing", | ||
"gracious", | ||
"gregarious", | ||
"hopeful", | ||
"implacable", | ||
"inventive", | ||
"joyous", | ||
"judicious", | ||
"jumping", | ||
"kind", | ||
"likable", | ||
"loyal", | ||
"lucky", | ||
"marvellous", | ||
"mellifluous", | ||
"nautical", | ||
"oblong", | ||
"outstanding", | ||
"polished", | ||
"polite", | ||
"profound", | ||
"quadratic", | ||
"quiet", | ||
"rectangular", | ||
"remarkable", | ||
"rusty", | ||
"sensible", | ||
"sincere", | ||
"sparkling", | ||
"splendid", | ||
"stellar", | ||
"tenacious", | ||
"tremendous", | ||
"triangular", | ||
"undulating", | ||
"unflappable", | ||
"unique", | ||
"verdant", | ||
"vitreous", | ||
"wise", | ||
"zippy", | ||
] | ||
|
||
const nouns = [ | ||
"aardvark", | ||
"accordion", | ||
"apple", | ||
"apricot", | ||
"bee", | ||
"brachiosaur", | ||
"cactus", | ||
"capsicum", | ||
"clarinet", | ||
"cowbell", | ||
"crab", | ||
"cuckoo", | ||
"cymbal", | ||
"diplodocus", | ||
"donkey", | ||
"drum", | ||
"duck", | ||
"echidna", | ||
"elephant", | ||
"foxglove", | ||
"galaxy", | ||
"glockenspiel", | ||
"goose", | ||
"hill", | ||
"horse", | ||
"iguanadon", | ||
"jellyfish", | ||
"kangaroo", | ||
"lake", | ||
"lemon", | ||
"lemur", | ||
"magpie", | ||
"megalodon", | ||
"mountain", | ||
"mouse", | ||
"muskrat", | ||
"newt", | ||
"oboe", | ||
"ocelot", | ||
"orange", | ||
"panda", | ||
"peach", | ||
"pepper", | ||
"petunia", | ||
"pheasant", | ||
"piano", | ||
"pigeon", | ||
"platypus", | ||
"quasar", | ||
"rhinoceros", | ||
"river", | ||
"rustacean", | ||
"salamander", | ||
"sitar", | ||
"stegosaurus", | ||
"tambourine", | ||
"tiger", | ||
"tomato", | ||
"triceratops", | ||
"ukulele", | ||
"viola", | ||
"weasel", | ||
"xylophone", | ||
"yak", | ||
"zebra", | ||
] |
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